CM_I2C.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Functions that directly control the hardware
  2. #include "CM_LIB.h"
  3. #include "CM_I2C.h"
  4. #include "CM_I2C_L.h"
  5. #include "hal_interface_api.h"
  6. #include <unistd.h>
  7. // Delay
  8. void cm_Delay(uint8_t ucDelay)
  9. {
  10. uint32_t ucTimer;
  11. uint8_t a,b = 0;
  12. while(ucDelay) {
  13. ucTimer = 2000;
  14. while(ucTimer)
  15. {
  16. ucTimer--;
  17. a = b;
  18. }
  19. ucDelay--;
  20. }
  21. }
  22. // 1/2 Clock Cycle transition to HIGH
  23. //
  24. void cm_Clockhigh(void)
  25. {
  26. cm_Delay(1);
  27. CM_CLK_HI;
  28. cm_Delay(1);
  29. }
  30. // 1/2 Clock Cycle transition to LOW
  31. //
  32. void cm_Clocklow(void)
  33. {
  34. cm_Delay(1);
  35. CM_CLK_LO;
  36. cm_Delay(1);
  37. }
  38. // Do one full clock cycle
  39. //
  40. // Changed 1/19/05 to eliminate one level of return stack requirements
  41. //
  42. void cm_ClockCycle(void)
  43. {
  44. cm_Clocklow();
  45. cm_Clockhigh();
  46. }
  47. // Do a number of clock cycles
  48. //
  49. void cm_ClockCycles(uint8_t ucCount)
  50. {
  51. uint8_t i;
  52. for (i = 0; i < ucCount; ++i) cm_ClockCycle();
  53. }
  54. // Send a start sequence
  55. //
  56. // Modified 7-21-04 to correctly set SDA to be an output
  57. //
  58. void cm_Start(void)
  59. {
  60. cm_Delay(50);//by jgw for delay to avoid i2c dead because of too fast
  61. CM_DATA_OUT; // Data line must be an output to send a start sequence
  62. //cm_Clocklow();
  63. CM_DATA_HI;
  64. cm_Delay(4);
  65. cm_Clockhigh();
  66. cm_Delay(4);
  67. CM_DATA_LO;
  68. cm_Delay(8);
  69. cm_Clocklow();
  70. cm_Delay(8);
  71. }
  72. // Send a stop sequence
  73. //
  74. // Modified 7-21-04 to correctly set SDA to be an output
  75. //
  76. void cm_Stop(void)
  77. {
  78. cm_Delay(10);//by jgw for delay to avoid i2c dead because of too fast
  79. CM_DATA_OUT; // Data line must be an output to send a stop sequence
  80. cm_Clocklow();
  81. CM_DATA_LO;
  82. cm_Delay(4);
  83. cm_Clockhigh();
  84. cm_Delay(8);
  85. CM_DATA_HI;
  86. cm_Delay(4);
  87. }
  88. // Write a byte
  89. //
  90. // Returns 0 if write successed, 1 if write fails failure
  91. //
  92. // Modified 7-21-04 to correctly control SDA
  93. //
  94. uint8_t cm_Write(uint8_t ucData)
  95. {
  96. uint8_t i;
  97. cm_Delay(10);//by jgw for delay to avoid i2c dead because of too fast
  98. CM_DATA_OUT; // Set data line to be an output
  99. for(i=0; i<8; i++) { // Send 8 bits of data
  100. cm_Clocklow();
  101. if (ucData&0x80) CM_DATA_HI;
  102. else CM_DATA_LO;
  103. cm_Clockhigh();
  104. ucData = ucData<<1;
  105. }
  106. cm_Clocklow();
  107. // wait for the ack
  108. CM_DATA_IN; // Set data line to be an input
  109. cm_Delay(8);
  110. cm_Clockhigh();
  111. while(i>1) { // loop waiting for ack (loop above left i == 8)
  112. cm_Delay(2);
  113. if (CM_DATA_RD) i--; // if SDA is high level decrement retry counter
  114. else i = 0;
  115. }
  116. cm_Clocklow();
  117. CM_DATA_OUT; // Set data line to be an output
  118. return i;
  119. }
  120. // Send a ACK or NAK or to the device
  121. void cm_AckNak(uint8_t ucAck)
  122. {
  123. cm_Delay(10);//by jgw for delay to avoid i2c dead because of too fast
  124. CM_DATA_OUT; // Data line must be an output to send an ACK
  125. cm_Clocklow();
  126. if (ucAck) CM_DATA_LO; // Low on data line indicates an ACK
  127. else CM_DATA_HI; // High on data line indicates an NACK
  128. cm_Delay(2);
  129. cm_Clockhigh();
  130. cm_Delay(8);
  131. cm_Clocklow();
  132. }
  133. // Read a byte from device, MSB
  134. //
  135. // Modified 7-21-04 to correctly control SDA
  136. //
  137. uint8_t cm_Read(void)
  138. {
  139. uint8_t i;
  140. uint8_t rByte = 0;
  141. CM_DATA_IN; // Set data line to be an input
  142. CM_DATA_HI;
  143. for(i=0x80; i; i=i>>1)
  144. {
  145. cm_ClockCycle();
  146. if (CM_DATA_RD) rByte |= i;
  147. cm_Clocklow();
  148. }
  149. CM_DATA_OUT; // Set data line to be an output
  150. return rByte;
  151. }
  152. void cm_WaitClock(uint8_t loop)
  153. {
  154. uint8_t i, j;
  155. CM_DATA_LO;
  156. for(j=0; j<loop; j++) {
  157. cm_Start();
  158. for(i = 0; i<15; i++) cm_ClockCycle();
  159. cm_Stop();
  160. }
  161. }
  162. // Send a command
  163. //
  164. uint8_t cm_SendCommand(uint8_t * pucInsBuff)
  165. {
  166. uint8_t i, ucCmd;
  167. i = CM_START_TRIES;
  168. ucCmd = (pucInsBuff[0]&0x0F)|CM_PORT_CFG.ucChipSelect;
  169. while (i) {
  170. cm_Start();
  171. if (cm_Write(ucCmd) == 0) break;
  172. if (--i == 0) return FAIL_CMDSTART;
  173. }
  174. for(i = 1; i< 4; i++) {
  175. if (cm_Write(pucInsBuff[i]) != 0) return FAIL_CMDSEND;
  176. }
  177. return SUCCESS;
  178. }
  179. uint8_t cm_ReceiveData(uint8_t * pucRecBuf, uint8_t ucLen)
  180. {
  181. int i;
  182. for(i = 0; i < (ucLen-1); i++) {
  183. pucRecBuf[i] = cm_Read();
  184. cm_AckNak(TRUE);
  185. }
  186. pucRecBuf[i] = cm_Read();
  187. cm_AckNak(FALSE);
  188. cm_Stop();
  189. return SUCCESS;
  190. }
  191. uint8_t cm_SendData(uint8_t * pucSendBuf, uint8_t ucLen)
  192. {
  193. int i;
  194. for(i = 0; i< ucLen; i++) {
  195. if (cm_Write(pucSendBuf[i])==1) return FAIL_WRDATA;
  196. }
  197. cm_Stop();
  198. return SUCCESS;
  199. }
  200. // Send a command byte
  201. //
  202. uint8_t cm_SendCmdByte(uint8_t ucCommand)
  203. {
  204. uint8_t i, ucCmd;
  205. i = CM_START_TRIES;
  206. ucCmd = (ucCommand&0x0F)|CM_PORT_CFG.ucChipSelect;
  207. while (i) {
  208. cm_Start();
  209. if (cm_Write(ucCmd) == 0) break;
  210. if (--i == 0) return FAIL_CMDSTART;
  211. }
  212. return SUCCESS;
  213. }