i2c_comm_controller.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Copyright (c) 2024, Texas Instruments Incorporated
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of Texas Instruments Incorporated nor the names of
  17. * its contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include "i2c_comm_controller.h"
  33. #include <stdio.h>
  34. #include<stdint.h>
  35. #define LEFT_SHIFT_8(x) ((x) << 8)
  36. #define RIGHT_SHIFT_8(x) ((x) >> 8)
  37. /* Function Declarations */
  38. /**
  39. * @brief Calculates CRC 16 for an array of 8-bit data
  40. * @param[in] ptr Pointer to the data to compute CRC
  41. * @param[in] size size of the array of data in bytes
  42. * @return Returns the CRC value
  43. */
  44. static uint16_t CRC_calc16(uint8_t* ptr,uint8_t size);
  45. /**
  46. * @brief Prepares I2C Tx FIFO and issues I2C write command
  47. * @param[in] I2C_handle Pointer to I2C_Instance
  48. * @param[in] targetAddr I2C Target Address
  49. */
  50. static void I2C_sendBuffer(I2C_Instance *I2C_handle,uint8_t targetAddr);
  51. /* Helper Functions */
  52. /**
  53. * @brief 32 bit to 8 bit
  54. * @param[in] b pointer to unsigned 8 bit output
  55. * @param[in] u32 unsigned 32 bit input
  56. */
  57. static void u8From32(uint8_t *b, uint32_t u32)
  58. {
  59. b[0] = (uint8_t)u32;
  60. b[1] = (uint8_t)(u32 = RIGHT_SHIFT_8(u32));
  61. b[2] = (uint8_t)(u32 = RIGHT_SHIFT_8(u32));
  62. b[3] = (uint8_t)(u32 = RIGHT_SHIFT_8(u32));
  63. }
  64. /**
  65. * @brief 16 bit to 8 bit
  66. * @param[in] b pointer to unsigned 8 bit output
  67. * @param[in] u16 unsigned 16 bit input
  68. */
  69. static void u8From16(uint8_t *b, uint16_t u16)
  70. {
  71. b[0] = (uint8_t)u16;
  72. b[1] = (uint8_t)(u16 = RIGHT_SHIFT_8(u16));
  73. }
  74. /**
  75. * @brief 8 bit to 32 bit
  76. * @param[in] b pointer to unsigned 8 bit output
  77. * @return 32 bit result
  78. */
  79. static uint32_t u32(uint8_t *b)
  80. {
  81. uint32_t u;
  82. u = b[3];
  83. u = LEFT_SHIFT_8(u) + b[2];
  84. u = LEFT_SHIFT_8(u) + b[1];
  85. u = LEFT_SHIFT_8(u) + b[0];
  86. return u;
  87. }
  88. /**
  89. * @brief 8 bit to 16 bit
  90. * @param[in] b pointer to unsigned 8 bit output
  91. * @return 16 bit result
  92. */
  93. static uint16_t u16(uint8_t *b)
  94. {
  95. uint16_t u;
  96. u = b[1];
  97. u = LEFT_SHIFT_8(u) + b[0];
  98. return u;
  99. }
  100. #ifdef CONFIG_MSPM0G351X
  101. static uint16_t CRC_calc16(uint8_t* ptr,uint8_t size)
  102. {
  103. uint8_t remainder = (size & 1);
  104. uint16_t size16 = size>>1;
  105. uint16_t checkSum = DL_CRCP_calculateBlock16(CRCP0,CRCP_SEED,
  106. (uint16_t*)ptr,size16);
  107. if(remainder)
  108. {
  109. DL_CRCP_feedData8(CRCP0,ptr[size - 1]);
  110. checkSum = ((uint16_t) DL_CRCP_getResult16(CRCP0));
  111. }
  112. return checkSum;
  113. }
  114. #else
  115. static uint16_t CRC_calc16(uint8_t* ptr,uint8_t size)
  116. {
  117. uint8_t remainder = (size & 1);
  118. uint16_t size16 = size>>1;
  119. uint16_t checkSum = DL_CRC_calculateBlock16(CRC,CRC_SEED,
  120. (uint16_t*)ptr,size16);
  121. if(remainder)
  122. {
  123. DL_CRC_feedData8(CRC,ptr[size - 1]);
  124. checkSum = ((uint16_t) DL_CRC_getResult16(CRC));
  125. }
  126. return checkSum;
  127. }
  128. #endif
  129. /**
  130. * @brief Resets pointer and length of the buffer
  131. * @param[in] frame Pointer to BufferInfo (Rx,Tx)
  132. */
  133. __STATIC_INLINE void I2C_clearBuffer(BufferInfo *frame)
  134. {
  135. frame->ptr = 0;
  136. frame->len = 0;
  137. }
  138. /*Initialize the I2C instance structure:
  139. * Clears the Receive and Tramnsmit Buffer
  140. * Resets data length to 0
  141. * Disables CRC
  142. * Sets the initial I2C status to IDLE
  143. * Resets Error Status
  144. */
  145. void I2C_init(I2C_Instance *I2C_handle)
  146. {
  147. I2C_clearBuffer(&I2C_handle->rxMsg);
  148. I2C_clearBuffer(&I2C_handle->txMsg);
  149. I2C_handle->dataLen = 0;
  150. I2C_handle->isCrc = false;
  151. I2C_handle->status = I2C_STATUS_IDLE;
  152. I2C_handle->error = ERROR_TYPE_NONE;
  153. }
  154. /*
  155. Send I2C command:
  156. * Resets the transmit buffer to 0: Prepares for new data
  157. * (command->commandType): Specifies whether the command is READ or WRITE
  158. * (command->crcEnable ? CRC_MASK : (0x00)): Adds a CRC FLag if enabled
  159. * ((command->dataSize - 1) & LEN_MASK) : encodes the data size in control byte
  160. * I2C_handle->txMsg.ptr += 4 -> moves the pointer forward 4 bytes (since address is 32 bits)
  161. * READ operation expects dataSize
  162. * Loops through the command array and stores it to the buffer.
  163. * I2C_handle->txMsg.len = I2C_handle->txMsg.ptr -> final length of the TX message
  164. */
  165. void I2C_sendCommand(I2C_Instance *I2C_handle,I2C_CommandInfo *command)
  166. {
  167. /* Prepare TX Buffer */
  168. I2C_handle->txMsg.ptr = 0;
  169. /* Control Byte */
  170. I2C_handle->txMsg.buffer[I2C_handle->txMsg.ptr++] = (command->commandType) | (command->crcEnable ? CRC_MASK : (0x00)) | ((command->dataSize - 1) & LEN_MASK) ;
  171. u8From32(&I2C_handle->txMsg.buffer[I2C_handle->txMsg.ptr],command->addr);
  172. I2C_handle->txMsg.ptr += 4;
  173. /* For Read Command */
  174. I2C_handle->dataLen = command->dataSize;
  175. /* For Write Command */
  176. if(command->commandType == WRITE_COMMAND)
  177. {
  178. I2C_handle->dataLen = 1;
  179. for(int i = 0 ; i < command->dataSize ; i++)
  180. {
  181. I2C_handle->txMsg.buffer[I2C_handle->txMsg.ptr++] = command->dataArray[i];
  182. }
  183. }
  184. /* CRC */
  185. I2C_handle->isCrc = false;
  186. if(command->crcEnable)
  187. {
  188. I2C_handle->isCrc = true;
  189. uint16_t Crc = CRC_calc16((&I2C_handle->txMsg.buffer[0]),I2C_handle->txMsg.ptr);
  190. u8From16(&I2C_handle->txMsg.buffer[I2C_handle->txMsg.ptr],Crc);
  191. I2C_handle->txMsg.ptr += 2;
  192. }
  193. I2C_handle->txMsg.len = I2C_handle->txMsg.ptr;
  194. I2C_sendBuffer(I2C_handle, command->targetAddr);
  195. /* Wait until I2C Controller is idle */
  196. while (!(
  197. DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_IDLE))
  198. ;
  199. }
  200. static void I2C_sendBuffer(I2C_Instance *I2C_handle,uint8_t targetAddr)
  201. {
  202. I2C_handle->txMsg.ptr = 0;
  203. I2C_handle->txMsg.ptr = DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &I2C_handle->txMsg.buffer[0], I2C_handle->txMsg.len);
  204. /* Enable TXFIFO trigger interrupt if there are more bytes to send */
  205. if (I2C_handle->txMsg.ptr < I2C_handle->txMsg.len)
  206. {
  207. DL_I2C_enableInterrupt(
  208. I2C_controller_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
  209. } else
  210. {
  211. DL_I2C_disableInterrupt(
  212. I2C_controller_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
  213. }
  214. I2C_handle->status = I2C_STATUS_TX_STARTED;
  215. /* Wait until I2C Controller is idle */
  216. while (!(
  217. DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_IDLE))
  218. ;
  219. DL_I2C_startControllerTransfer(
  220. I2C_controller_INST, targetAddr, DL_I2C_CONTROLLER_DIRECTION_TX, I2C_handle->txMsg.len);
  221. // added to avoid infinite wait...
  222. /* Wait until the Controller sends all bytes */
  223. int timeout = 100000;
  224. while ((I2C_handle->status != I2C_STATUS_TX_COMPLETE) &&
  225. (I2C_handle->status != I2C_STATUS_ERROR) && (--timeout)) {
  226. printf("Wait till the bus is idle.\n");
  227. __WFE();
  228. }
  229. /* Handle timeout error */
  230. if (timeout == 0) {
  231. printf("ERROR: Timeout waiting for I2C transmission to complete!\n");
  232. I2C_handle->status = I2C_STATUS_ERROR;
  233. }
  234. /* Wait until I2C bus is Idle */
  235. while (DL_I2C_getControllerStatus(I2C_controller_INST) &
  236. DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
  237. ;
  238. /* Trap if there was an error */
  239. if (DL_I2C_getControllerStatus(I2C_controller_INST) &
  240. DL_I2C_CONTROLLER_STATUS_ERROR) {
  241. //Testing for error trap:
  242. uint32_t status = DL_I2C_getControllerStatus(I2C_controller_INST);
  243. printf("I2C ERROR: Controller status = 0x%X\n", status);
  244. __BKPT(0);
  245. }
  246. }
  247. /*
  248. This function returns and receives the data from the I2C peripheral.
  249. The total expected length of the response is calculated as:
  250. * CTRL_SIZE: The control byte size
  251. * I2C_handle->dataLen: The data length that is expected in response
  252. * (I2C_handle->isCrc ? CRC_SIZE : 0) : if the CRC checking is enabled, add the CRC_Size to the expected length
  253. * Calls DL_I2C_startControllerTransfer to start an I2C read transaction.
  254. * I2C_controller_INST: Reference to the the I2C controller instance
  255. * targetAddr: I2C address of the device
  256. * DL_I2C_CONTROLLER_DIRECTION_RX: indicates this is a READ operation
  257. * expectedLen: The no. of bytes expected in the response
  258. * Waits until data reception is complete
  259. * Retries if a NACK error occurs
  260. * Wait for the bus to become idle.
  261. */
  262. void I2C_getResponse(I2C_Instance* I2C_handle,uint32_t targetAddr)
  263. {
  264. uint32_t expectedLen = CTRL_SIZE + I2C_handle->dataLen + (I2C_handle->isCrc ? CRC_SIZE : 0);
  265. DL_I2C_startControllerTransfer(
  266. I2C_controller_INST, targetAddr, DL_I2C_CONTROLLER_DIRECTION_RX, expectedLen);
  267. if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR){
  268. printf("ERROR: ADC(0x%X) not responding.\n", targetAddr);
  269. return;
  270. }else{
  271. printf("ADC(0x%X) acknowledged.\n",targetAddr);
  272. }
  273. /* Wait for all bytes to be received in interrupt */
  274. while (I2C_handle->status != I2C_STATUS_RX_COMPLETE)
  275. {
  276. /* Resend the Read Command if it is N'ACKed */
  277. if(I2C_handle->status == I2C_STATUS_ERROR)
  278. {
  279. I2C_handle->status = I2C_STATUS_RX_STARTED;
  280. delay_cycles(10000);
  281. DL_I2C_startControllerTransfer(
  282. I2C_controller_INST, targetAddr, DL_I2C_CONTROLLER_DIRECTION_RX, expectedLen);
  283. }
  284. }
  285. /* Wait until I2C bus is Idle */
  286. while (DL_I2C_getControllerStatus(I2C_controller_INST) &
  287. DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
  288. ;
  289. }
  290. void I2C_decodeResponse(I2C_Instance *I2C_handle,I2C_ResponseInfo *response)
  291. {
  292. response->received = true;
  293. /* Length stored in frame is 1 less than actual length */
  294. response->dataSize = ( I2C_handle->rxMsg.buffer[CTRL_IDX] & LEN_MASK ) + 1;
  295. response->status = ERROR_TYPE_NONE;
  296. if(I2C_handle->rxMsg.buffer[CTRL_IDX] & ERROR_MASK)
  297. {
  298. /* Store error code sent by target */
  299. response->status = (ErrorType) I2C_handle->rxMsg.buffer[RESP_DATA_IDX];
  300. }
  301. response->frame.ctrl = I2C_handle->rxMsg.buffer[CTRL_IDX];
  302. for(int i = 0; i < MAX_DATA_SIZE; i++)
  303. {
  304. if(i < response->dataSize)
  305. {
  306. response->frame.data[i] = I2C_handle->rxMsg.buffer[RESP_DATA_IDX + i];
  307. }
  308. /* Resetting extra space to zero*/
  309. else
  310. {
  311. response->frame.data[i] = 0;
  312. }
  313. }
  314. if(I2C_handle->isCrc)
  315. {
  316. response->frame.crc = u16(&I2C_handle->rxMsg.buffer[I2C_handle->rxMsg.len - CRC_SIZE]);
  317. }
  318. }