123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- // Functions that directly control the hardware
- #include "CM_LIB.h"
- #include "CM_I2C.h"
- #include "CM_I2C_L.h"
- #include "hal_interface_api.h"
- #include <unistd.h>
- // Delay
- void cm_Delay(uint8_t ucDelay)
- {
- uint32_t ucTimer;
- uint8_t a,b = 0;
- while(ucDelay) {
- ucTimer = 2000;
- while(ucTimer)
- {
- ucTimer--;
- a = b;
- }
- ucDelay--;
- }
- }
- // 1/2 Clock Cycle transition to HIGH
- //
- void cm_Clockhigh(void)
- {
- cm_Delay(1);
- CM_CLK_HI;
- cm_Delay(1);
- }
- // 1/2 Clock Cycle transition to LOW
- //
- void cm_Clocklow(void)
- {
- cm_Delay(1);
- CM_CLK_LO;
- cm_Delay(1);
- }
- // Do one full clock cycle
- //
- // Changed 1/19/05 to eliminate one level of return stack requirements
- //
- void cm_ClockCycle(void)
- {
- cm_Clocklow();
- cm_Clockhigh();
- }
- // Do a number of clock cycles
- //
- void cm_ClockCycles(uint8_t ucCount)
- {
- uint8_t i;
-
- for (i = 0; i < ucCount; ++i) cm_ClockCycle();
- }
- // Send a start sequence
- //
- // Modified 7-21-04 to correctly set SDA to be an output
- //
- void cm_Start(void)
- {
- cm_Delay(50);//by jgw for delay to avoid i2c dead because of too fast
- CM_DATA_OUT; // Data line must be an output to send a start sequence
- //cm_Clocklow();
- CM_DATA_HI;
- cm_Delay(4);
- cm_Clockhigh();
- cm_Delay(4);
- CM_DATA_LO;
- cm_Delay(8);
- cm_Clocklow();
- cm_Delay(8);
- }
- // Send a stop sequence
- //
- // Modified 7-21-04 to correctly set SDA to be an output
- //
- void cm_Stop(void)
- {
- cm_Delay(10);//by jgw for delay to avoid i2c dead because of too fast
- CM_DATA_OUT; // Data line must be an output to send a stop sequence
- cm_Clocklow();
- CM_DATA_LO;
- cm_Delay(4);
- cm_Clockhigh();
- cm_Delay(8);
- CM_DATA_HI;
- cm_Delay(4);
- }
- // Write a byte
- //
- // Returns 0 if write successed, 1 if write fails failure
- //
- // Modified 7-21-04 to correctly control SDA
- //
- uint8_t cm_Write(uint8_t ucData)
- {
- uint8_t i;
- cm_Delay(10);//by jgw for delay to avoid i2c dead because of too fast
- CM_DATA_OUT; // Set data line to be an output
- for(i=0; i<8; i++) { // Send 8 bits of data
- cm_Clocklow();
- if (ucData&0x80) CM_DATA_HI;
- else CM_DATA_LO;
- cm_Clockhigh();
- ucData = ucData<<1;
- }
- cm_Clocklow();
- // wait for the ack
- CM_DATA_IN; // Set data line to be an input
- cm_Delay(8);
- cm_Clockhigh();
- while(i>1) { // loop waiting for ack (loop above left i == 8)
- cm_Delay(2);
- if (CM_DATA_RD) i--; // if SDA is high level decrement retry counter
- else i = 0;
- }
- cm_Clocklow();
- CM_DATA_OUT; // Set data line to be an output
- return i;
- }
- // Send a ACK or NAK or to the device
- void cm_AckNak(uint8_t ucAck)
- {
- cm_Delay(10);//by jgw for delay to avoid i2c dead because of too fast
- CM_DATA_OUT; // Data line must be an output to send an ACK
- cm_Clocklow();
- if (ucAck) CM_DATA_LO; // Low on data line indicates an ACK
- else CM_DATA_HI; // High on data line indicates an NACK
- cm_Delay(2);
- cm_Clockhigh();
- cm_Delay(8);
- cm_Clocklow();
- }
- // Read a byte from device, MSB
- //
- // Modified 7-21-04 to correctly control SDA
- //
- uint8_t cm_Read(void)
- {
- uint8_t i;
- uint8_t rByte = 0;
- CM_DATA_IN; // Set data line to be an input
- CM_DATA_HI;
- for(i=0x80; i; i=i>>1)
- {
- cm_ClockCycle();
- if (CM_DATA_RD) rByte |= i;
- cm_Clocklow();
- }
- CM_DATA_OUT; // Set data line to be an output
- return rByte;
- }
- void cm_WaitClock(uint8_t loop)
- {
- uint8_t i, j;
-
- CM_DATA_LO;
- for(j=0; j<loop; j++) {
- cm_Start();
- for(i = 0; i<15; i++) cm_ClockCycle();
- cm_Stop();
- }
- }
- // Send a command
- //
- uint8_t cm_SendCommand(uint8_t * pucInsBuff)
- {
- uint8_t i, ucCmd;
-
- i = CM_START_TRIES;
- ucCmd = (pucInsBuff[0]&0x0F)|CM_PORT_CFG.ucChipSelect;
- while (i) {
- cm_Start();
- if (cm_Write(ucCmd) == 0) break;
- if (--i == 0) return FAIL_CMDSTART;
- }
-
- for(i = 1; i< 4; i++) {
- if (cm_Write(pucInsBuff[i]) != 0) return FAIL_CMDSEND;
- }
- return SUCCESS;
- }
- uint8_t cm_ReceiveData(uint8_t * pucRecBuf, uint8_t ucLen)
- {
- int i;
- for(i = 0; i < (ucLen-1); i++) {
- pucRecBuf[i] = cm_Read();
- cm_AckNak(TRUE);
- }
- pucRecBuf[i] = cm_Read();
- cm_AckNak(FALSE);
- cm_Stop();
- return SUCCESS;
- }
- uint8_t cm_SendData(uint8_t * pucSendBuf, uint8_t ucLen)
- {
- int i;
- for(i = 0; i< ucLen; i++) {
- if (cm_Write(pucSendBuf[i])==1) return FAIL_WRDATA;
- }
- cm_Stop();
-
- return SUCCESS;
- }
- // Send a command byte
- //
- uint8_t cm_SendCmdByte(uint8_t ucCommand)
- {
- uint8_t i, ucCmd;
-
- i = CM_START_TRIES;
- ucCmd = (ucCommand&0x0F)|CM_PORT_CFG.ucChipSelect;
- while (i) {
- cm_Start();
- if (cm_Write(ucCmd) == 0) break;
- if (--i == 0) return FAIL_CMDSTART;
- }
- return SUCCESS;
- }
|