CM_I2C.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 <unistd.h>
  6. #include <stdio.h>
  7. #include "linux/fcntl.h"
  8. #include "driver.h"
  9. #include <string.h>
  10. // Delay
  11. void cm_Delay(uint8_t ucDelay)
  12. {
  13. // uint32_t ucTimer;
  14. // uint8_t a = 1,b = 2;
  15. // while(ucDelay) {
  16. // ucTimer = 3000; //2000;
  17. // while(ucTimer)
  18. // {
  19. // ucTimer--;
  20. // a = a*b;
  21. // }
  22. // ucDelay--;
  23. // }
  24. usleep(ucDelay);
  25. }
  26. #if 0
  27. // 1/2 Clock Cycle transition to HIGH
  28. //
  29. void cm_Clockhigh(void)
  30. {
  31. cm_Delay(1);
  32. CM_CLK_HI;
  33. cm_Delay(1);
  34. }
  35. // 1/2 Clock Cycle transition to LOW
  36. //
  37. void cm_Clocklow(void)
  38. {
  39. cm_Delay(1);
  40. CM_CLK_LO;
  41. cm_Delay(1);
  42. }
  43. // Do one full clock cycle
  44. //
  45. // Changed 1/19/05 to eliminate one level of return stack requirements
  46. //
  47. void cm_ClockCycle(void)
  48. {
  49. cm_Clocklow();
  50. cm_Clockhigh();
  51. }
  52. // Do a number of clock cycles
  53. //
  54. void cm_ClockCycles(uint8_t ucCount)
  55. {
  56. uint8_t i;
  57. for (i = 0; i < ucCount; ++i) cm_ClockCycle();
  58. }
  59. // Send a start sequence
  60. //
  61. // Modified 7-21-04 to correctly set SDA to be an output
  62. //
  63. void cm_Start(void)
  64. {
  65. cm_Delay(50);//by jgw for delay to avoid i2c dead because of too fast
  66. CM_DATA_OUT; // Data line must be an output to send a start sequence
  67. //cm_Clocklow();
  68. CM_DATA_HI;
  69. cm_Delay(4);
  70. cm_Clockhigh();
  71. cm_Delay(40); //4
  72. CM_DATA_LO;
  73. cm_Delay(8);
  74. cm_Clocklow();
  75. cm_Delay(8);
  76. }
  77. // Send a stop sequence
  78. //
  79. // Modified 7-21-04 to correctly set SDA to be an output
  80. //
  81. void cm_Stop(void)
  82. {
  83. cm_Delay(10);//by jgw for delay to avoid i2c dead because of too fast
  84. cm_Clocklow();
  85. CM_DATA_OUT; // Data line must be an output to send a stop sequence
  86. CM_DATA_LO;
  87. cm_Delay(4);
  88. cm_Clockhigh();
  89. cm_Delay(80); //8
  90. CM_DATA_HI;
  91. cm_Delay(4);
  92. }
  93. // Write a byte
  94. //
  95. // Returns 0 if write successed, 1 if write fails failure
  96. //
  97. // Modified 7-21-04 to correctly control SDA
  98. //
  99. uint8_t cm_Write(uint8_t ucData)
  100. {
  101. uint8_t i;
  102. cm_Delay(10);//by jgw for delay to avoid i2c dead because of too fast
  103. CM_DATA_OUT; // Set data line to be an output
  104. for(i=0; i<8; i++) { // Send 8 bits of data
  105. cm_Clocklow();
  106. if (ucData&0x80) CM_DATA_HI;
  107. else CM_DATA_LO;
  108. cm_Clockhigh();
  109. ucData = ucData<<1;
  110. }
  111. cm_Clocklow();
  112. // wait for the ack
  113. CM_DATA_IN; // Set data line to be an input
  114. cm_Delay(8);
  115. cm_Clockhigh();
  116. while(i>1) { // loop waiting for ack (loop above left i == 8)
  117. cm_Delay(2);
  118. if (CM_DATA_RD) i--; // if SDA is high level decrement retry counter
  119. else i = 0;
  120. }
  121. cm_Clocklow();
  122. CM_DATA_OUT; // Set data line to be an output
  123. return i;
  124. }
  125. // Send a ACK or NAK or to the device
  126. void cm_AckNak(uint8_t ucAck)
  127. {
  128. cm_Delay(10);//by jgw for delay to avoid i2c dead because of too fast
  129. CM_DATA_OUT; // Data line must be an output to send an ACK
  130. cm_Clocklow();
  131. if (ucAck) CM_DATA_LO; // Low on data line indicates an ACK
  132. else CM_DATA_HI; // High on data line indicates an NACK
  133. cm_Delay(2);
  134. cm_Clockhigh();
  135. cm_Delay(8);
  136. cm_Clocklow();
  137. }
  138. // Read a byte from device, MSB
  139. //
  140. // Modified 7-21-04 to correctly control SDA
  141. //
  142. uint8_t cm_Read(void)
  143. {
  144. uint8_t i;
  145. uint8_t rByte = 0;
  146. CM_DATA_IN; // Set data line to be an input
  147. CM_DATA_HI;
  148. for(i=0x80; i; i=i>>1)
  149. {
  150. cm_ClockCycle();
  151. if (CM_DATA_RD) rByte |= i;
  152. cm_Clocklow();
  153. }
  154. CM_DATA_OUT; // Set data line to be an output
  155. return rByte;
  156. }
  157. #endif
  158. void cm_WaitClock(uint8_t loop)
  159. {
  160. int ret;
  161. crypto_t crypto_arg;
  162. crypto_arg.loop = loop;
  163. int fd = open("/dev/crypto", O_RDWR);
  164. if(fd <= 0)
  165. {
  166. printf("Open /dev/crypto error!\n");
  167. return;
  168. }
  169. ret = ioctl(fd, CRYPTO_WAIT_CLOCK, &crypto_arg);
  170. if(ret != 0)
  171. {
  172. printf("cm_WaitClock error!\n");
  173. }
  174. close(fd);
  175. // CM_DATA_LO;
  176. // for(j=0; j<loop; j++) {
  177. // cm_Start();
  178. // for(i = 0; i<15; i++) cm_ClockCycle();
  179. // cm_Stop();
  180. // }
  181. }
  182. // Send a command
  183. //
  184. uint8_t cm_SendCommand(uint8_t * pucInsBuff)
  185. {
  186. //uint8_t i, ucCmd;
  187. //i = CM_START_TRIES;
  188. //ucCmd = (pucInsBuff[0]&0x0F)|CM_PORT_CFG.ucChipSelect;
  189. // while (i) {
  190. // cm_Start();
  191. // printf("cm_SendCommand ucCmd=%#x\n", ucCmd);
  192. // if (cm_Write(ucCmd) == 0) break;
  193. // if (--i == 0) {printf("---> cm_SendCommand log1\n"); return FAIL_CMDSTART;}
  194. // }
  195. // for(i = 1; i< 4; i++)
  196. // {
  197. // cm_Delay(1);
  198. // printf("cm_SendCommand i=%d, data=%#x\n", i, pucInsBuff[i]);
  199. // if (cm_Write(pucInsBuff[i]) != 0)
  200. // {
  201. // printf("---> cm_SendCommand log2, i = %d\n", i);
  202. // return FAIL_CMDSEND;
  203. // }
  204. // }
  205. int ret;
  206. crypto_t crypto_arg;
  207. int fd = open("/dev/crypto", O_RDWR);
  208. if(fd <= 0)
  209. {
  210. printf("Open /dev/crypto error!\n");
  211. return FAIL_CMDSEND;
  212. }
  213. crypto_arg.data[0] = (pucInsBuff[0]&0x0F)|CM_PORT_CFG.ucChipSelect;
  214. crypto_arg.data[1] = pucInsBuff[1];
  215. crypto_arg.data[2] = pucInsBuff[2];
  216. crypto_arg.data[3] = pucInsBuff[3];
  217. crypto_arg.size = 4;
  218. ret = ioctl(fd, CRYPTO_SEND_COMMAND, &crypto_arg);
  219. if(ret != 0)
  220. {
  221. printf("cm_SendCommand failed!\n");
  222. close(fd);
  223. return FAIL_CMDSEND;
  224. }
  225. close(fd);
  226. return SUCCESS;
  227. }
  228. uint8_t cm_ReceiveData(uint8_t * pucRecBuf, uint8_t ucLen)
  229. {
  230. // int i;
  231. // for(i = 0; i < (ucLen-1); i++) {
  232. // pucRecBuf[i] = cm_Read();
  233. // cm_AckNak(TRUE);
  234. // }
  235. // pucRecBuf[i] = cm_Read();
  236. // cm_AckNak(FALSE);
  237. // cm_Stop();
  238. int ret;
  239. crypto_t crypto_arg;
  240. int fd = open("/dev/crypto", O_RDWR);
  241. if(fd <= 0)
  242. {
  243. printf("Open /dev/crypto error!\n");
  244. return -1;
  245. }
  246. crypto_arg.size = ucLen;
  247. ret = ioctl(fd, CRYPTO_RECEIVE_DATA, &crypto_arg);
  248. if(ret != 0)
  249. {
  250. printf("cm_ReceiveData failed!\n");
  251. close(fd);
  252. return -1;
  253. }
  254. close(fd);
  255. memcpy(pucRecBuf, crypto_arg.data, ucLen);
  256. return SUCCESS;
  257. }
  258. uint8_t cm_SendData(uint8_t * pucSendBuf, uint8_t ucLen)
  259. {
  260. // int i;
  261. // for(i = 0; i< ucLen; i++) {
  262. // if (cm_Write(pucSendBuf[i])==1) {printf("---> cm_SendData log1, i=%d\n", i); return FAIL_WRDATA;}
  263. // }
  264. // cm_Stop();
  265. int ret;
  266. crypto_t crypto_arg;
  267. int fd = open("/dev/crypto", O_RDWR);
  268. if(fd <= 0)
  269. {
  270. printf("Open /dev/crypto error!\n");
  271. return FAIL_WRDATA;
  272. }
  273. memcpy(crypto_arg.data, pucSendBuf, ucLen);
  274. crypto_arg.size = ucLen;
  275. ret = ioctl(fd, CRYPTO_SEND_DATA, &crypto_arg);
  276. if(ret != 0)
  277. {
  278. printf("cm_SendData failed!\n");
  279. close(fd);
  280. return FAIL_WRDATA;
  281. }
  282. close(fd);
  283. return SUCCESS;
  284. }
  285. // Send a command byte
  286. //
  287. uint8_t cm_SendCmdByte(uint8_t ucCommand)
  288. {
  289. // uint8_t i, ucCmd;
  290. // i = CM_START_TRIES;
  291. // ucCmd = (ucCommand&0x0F)|CM_PORT_CFG.ucChipSelect;
  292. // while (i) {
  293. // cm_Start();
  294. // if (cm_Write(ucCmd) == 0) break;
  295. // if (--i == 0) return FAIL_CMDSTART;
  296. // }
  297. int ret;
  298. crypto_t crypto_arg;
  299. int fd = open("/dev/crypto", O_RDWR);
  300. if(fd <= 0)
  301. {
  302. printf("Open /dev/crypto error!\n");
  303. return FAIL_CMDSTART;
  304. }
  305. crypto_arg.data[0] = (ucCommand&0x0F)|CM_PORT_CFG.ucChipSelect;
  306. ret = ioctl(fd, CRYPTO_SEND_CMD_BYTE, &crypto_arg);
  307. if(ret != 0)
  308. {
  309. printf("cm_SendCmdByte failed!\n");
  310. close(fd);
  311. return FAIL_CMDSTART;
  312. }
  313. close(fd);
  314. return SUCCESS;
  315. }