i2c_comm_controller.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. #define LEFT_SHIFT_8(x) ((x) << 8)
  34. #define RIGHT_SHIFT_8(x) ((x) >> 8)
  35. /* Function Declarations */
  36. /**
  37. * @brief Calculates CRC 16 for an array of 8-bit data
  38. * @param[in] ptr Pointer to the data to compute CRC
  39. * @param[in] size size of the array of data in bytes
  40. * @return Returns the CRC value
  41. */
  42. static uint16_t CRC_calc16(uint8_t* ptr,uint8_t size);
  43. /**
  44. * @brief Prepares I2C Tx FIFO and issues I2C write command
  45. * @param[in] I2C_handle Pointer to I2C_Instance
  46. * @param[in] targetAddr I2C Target Address
  47. */
  48. static void I2C_sendBuffer(I2C_Instance *I2C_handle,uint8_t targetAddr);
  49. /* Helper Functions */
  50. /**
  51. * @brief 32 bit to 8 bit
  52. * @param[in] b pointer to unsigned 8 bit output
  53. * @param[in] u32 unsigned 32 bit input
  54. */
  55. static void u8From32(uint8_t *b, uint32_t u32)
  56. {
  57. b[0] = (uint8_t)u32;
  58. b[1] = (uint8_t)(u32 = RIGHT_SHIFT_8(u32));
  59. b[2] = (uint8_t)(u32 = RIGHT_SHIFT_8(u32));
  60. b[3] = (uint8_t)(u32 = RIGHT_SHIFT_8(u32));
  61. }
  62. /**
  63. * @brief 16 bit to 8 bit
  64. * @param[in] b pointer to unsigned 8 bit output
  65. * @param[in] u16 unsigned 16 bit input
  66. */
  67. static void u8From16(uint8_t *b, uint16_t u16)
  68. {
  69. b[0] = (uint8_t)u16;
  70. b[1] = (uint8_t)(u16 = RIGHT_SHIFT_8(u16));
  71. }
  72. /**
  73. * @brief 8 bit to 32 bit
  74. * @param[in] b pointer to unsigned 8 bit output
  75. * @return 32 bit result
  76. */
  77. static uint32_t u32(uint8_t *b)
  78. {
  79. uint32_t u;
  80. u = b[3];
  81. u = LEFT_SHIFT_8(u) + b[2];
  82. u = LEFT_SHIFT_8(u) + b[1];
  83. u = LEFT_SHIFT_8(u) + b[0];
  84. return u;
  85. }
  86. /**
  87. * @brief 8 bit to 16 bit
  88. * @param[in] b pointer to unsigned 8 bit output
  89. * @return 16 bit result
  90. */
  91. static uint16_t u16(uint8_t *b)
  92. {
  93. uint16_t u;
  94. u = b[1];
  95. u = LEFT_SHIFT_8(u) + b[0];
  96. return u;
  97. }
  98. #ifdef CONFIG_MSPM0G351X
  99. static uint16_t CRC_calc16(uint8_t* ptr,uint8_t size)
  100. {
  101. uint8_t remainder = (size & 1);
  102. uint16_t size16 = size>>1;
  103. uint16_t checkSum = DL_CRCP_calculateBlock16(CRCP0,CRCP_SEED,
  104. (uint16_t*)ptr,size16);
  105. if(remainder)
  106. {
  107. DL_CRCP_feedData8(CRCP0,ptr[size - 1]);
  108. checkSum = ((uint16_t) DL_CRCP_getResult16(CRCP0));
  109. }
  110. return checkSum;
  111. }
  112. #else
  113. static uint16_t CRC_calc16(uint8_t* ptr,uint8_t size)
  114. {
  115. uint8_t remainder = (size & 1);
  116. uint16_t size16 = size>>1;
  117. uint16_t checkSum = DL_CRC_calculateBlock16(CRC,CRC_SEED,
  118. (uint16_t*)ptr,size16);
  119. if(remainder)
  120. {
  121. DL_CRC_feedData8(CRC,ptr[size - 1]);
  122. checkSum = ((uint16_t) DL_CRC_getResult16(CRC));
  123. }
  124. return checkSum;
  125. }
  126. #endif
  127. /**
  128. * @brief Resets pointer and length of the buffer
  129. * @param[in] frame Pointer to BufferInfo (Rx,Tx)
  130. */
  131. __STATIC_INLINE void I2C_clearBuffer(BufferInfo *frame)
  132. {
  133. frame->ptr = 0;
  134. frame->len = 0;
  135. }
  136. void I2C_init(I2C_Instance *I2C_handle)
  137. {
  138. I2C_clearBuffer(&I2C_handle->rxMsg);
  139. I2C_clearBuffer(&I2C_handle->txMsg);
  140. I2C_handle->dataLen = 0;
  141. I2C_handle->isCrc = false;
  142. I2C_handle->status = I2C_STATUS_IDLE;
  143. I2C_handle->error = ERROR_TYPE_NONE;
  144. }
  145. void I2C_sendCommand(I2C_Instance *I2C_handle,I2C_CommandInfo *command)
  146. {
  147. /* Prepare TX Buffer */
  148. I2C_handle->txMsg.ptr = 0;
  149. /* Control Byte */
  150. I2C_handle->txMsg.buffer[I2C_handle->txMsg.ptr++] = (command->commandType) | (command->crcEnable ? CRC_MASK : (0x00)) | ((command->dataSize - 1) & LEN_MASK) ;
  151. u8From32(&I2C_handle->txMsg.buffer[I2C_handle->txMsg.ptr],command->addr);
  152. I2C_handle->txMsg.ptr += 4;
  153. /* For Read Command */
  154. I2C_handle->dataLen = command->dataSize;
  155. /* For Write Command */
  156. if(command->commandType == WRITE_COMMAND)
  157. {
  158. I2C_handle->dataLen = 1;
  159. for(int i = 0 ; i < command->dataSize ; i++)
  160. {
  161. I2C_handle->txMsg.buffer[I2C_handle->txMsg.ptr++] = command->dataArray[i];
  162. }
  163. }
  164. /* CRC */
  165. I2C_handle->isCrc = false;
  166. if(command->crcEnable)
  167. {
  168. I2C_handle->isCrc = true;
  169. uint16_t Crc = CRC_calc16((&I2C_handle->txMsg.buffer[0]),I2C_handle->txMsg.ptr);
  170. u8From16(&I2C_handle->txMsg.buffer[I2C_handle->txMsg.ptr],Crc);
  171. I2C_handle->txMsg.ptr += 2;
  172. }
  173. I2C_handle->txMsg.len = I2C_handle->txMsg.ptr;
  174. I2C_sendBuffer(I2C_handle, command->targetAddr);
  175. /* Wait until I2C Controller is idle */
  176. while (!(
  177. DL_I2C_getControllerStatus(I2C_INST) & DL_I2C_CONTROLLER_STATUS_IDLE))
  178. ;
  179. }
  180. static void I2C_sendBuffer(I2C_Instance *I2C_handle,uint8_t targetAddr)
  181. {
  182. I2C_handle->txMsg.ptr = 0;
  183. I2C_handle->txMsg.ptr = DL_I2C_fillControllerTXFIFO(I2C_INST, &I2C_handle->txMsg.buffer[0], I2C_handle->txMsg.len);
  184. /* Enable TXFIFO trigger interrupt if there are more bytes to send */
  185. if (I2C_handle->txMsg.ptr < I2C_handle->txMsg.len)
  186. {
  187. DL_I2C_enableInterrupt(
  188. I2C_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
  189. } else
  190. {
  191. DL_I2C_disableInterrupt(
  192. I2C_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
  193. }
  194. I2C_handle->status = I2C_STATUS_TX_STARTED;
  195. /* Wait until I2C Controller is idle */
  196. while (!(
  197. DL_I2C_getControllerStatus(I2C_INST) & DL_I2C_CONTROLLER_STATUS_IDLE))
  198. ;
  199. DL_I2C_startControllerTransfer(
  200. I2C_INST, targetAddr, DL_I2C_CONTROLLER_DIRECTION_TX, I2C_handle->txMsg.len);
  201. /* Wait until the Controller sends all bytes */
  202. while ((I2C_handle->status != I2C_STATUS_TX_COMPLETE) &&
  203. (I2C_handle->status != I2C_STATUS_ERROR)) {
  204. __WFE();
  205. }
  206. /* Wait until I2C bus is Idle */
  207. while (DL_I2C_getControllerStatus(I2C_INST) &
  208. DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
  209. ;
  210. /* Trap if there was an error */
  211. if (DL_I2C_getControllerStatus(I2C_INST) &
  212. DL_I2C_CONTROLLER_STATUS_ERROR) {
  213. __BKPT(0);
  214. }
  215. }
  216. void I2C_getResponse(I2C_Instance* I2C_handle,uint32_t targetAddr)
  217. {
  218. uint32_t expectedLen = CTRL_SIZE + I2C_handle->dataLen + (I2C_handle->isCrc ? CRC_SIZE : 0);
  219. DL_I2C_startControllerTransfer(
  220. I2C_INST, targetAddr, DL_I2C_CONTROLLER_DIRECTION_RX, expectedLen);
  221. /* Wait for all bytes to be received in interrupt */
  222. while (I2C_handle->status != I2C_STATUS_RX_COMPLETE)
  223. {
  224. /* Resend the Read Command if it is N'ACKed */
  225. if(I2C_handle->status == I2C_STATUS_ERROR)
  226. {
  227. I2C_handle->status = I2C_STATUS_RX_STARTED;
  228. delay_cycles(10000);
  229. DL_I2C_startControllerTransfer(
  230. I2C_INST, targetAddr, DL_I2C_CONTROLLER_DIRECTION_RX, expectedLen);
  231. }
  232. }
  233. /* Wait until I2C bus is Idle */
  234. while (DL_I2C_getControllerStatus(I2C_INST) &
  235. DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
  236. ;
  237. }
  238. void I2C_decodeResponse(I2C_Instance *I2C_handle,I2C_ResponseInfo *response)
  239. {
  240. response->received = true;
  241. /* Length stored in frame is 1 less than actual length */
  242. response->dataSize = ( I2C_handle->rxMsg.buffer[CTRL_IDX] & LEN_MASK ) + 1;
  243. response->status = ERROR_TYPE_NONE;
  244. if(I2C_handle->rxMsg.buffer[CTRL_IDX] & ERROR_MASK)
  245. {
  246. /* Store error code sent by target */
  247. response->status = (ErrorType) I2C_handle->rxMsg.buffer[RESP_DATA_IDX];
  248. }
  249. response->frame.ctrl = I2C_handle->rxMsg.buffer[CTRL_IDX];
  250. for(int i = 0; i < MAX_DATA_SIZE; i++)
  251. {
  252. if(i < response->dataSize)
  253. {
  254. response->frame.data[i] = I2C_handle->rxMsg.buffer[RESP_DATA_IDX + i];
  255. }
  256. /* Resetting extra space to zero*/
  257. else
  258. {
  259. response->frame.data[i] = 0;
  260. }
  261. }
  262. if(I2C_handle->isCrc)
  263. {
  264. response->frame.crc = u16(&I2C_handle->rxMsg.buffer[I2C_handle->rxMsg.len - CRC_SIZE]);
  265. }
  266. }