i2c_comm_controller.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. /*!****************************************************************************
  33. * @file i2c_comm_controller.h
  34. * @brief I2C communication Module
  35. *
  36. *
  37. * @anchor i2c_comm_controller
  38. * # Overview
  39. *
  40. * APIs for I2C communication module
  41. *
  42. * <hr>
  43. ******************************************************************************/
  44. #ifndef I2C_COMM_CONTROLLER_H_
  45. #define I2C_COMM_CONTROLLER_H_
  46. #include "ti_msp_dl_config.h"
  47. #include <stdbool.h>
  48. #ifdef __cplusplus
  49. extern "C" {
  50. #endif
  51. /* FRAME STRUCTURE */
  52. /* Control Byte */
  53. /*! @brief Control index */
  54. #define CTRL_IDX (0)
  55. /*! @brief Control size */
  56. #define CTRL_SIZE (1)
  57. /*! @brief Control mask */
  58. #define CMD_MASK (0x80)
  59. /*! @brief CRC mask */
  60. #define CRC_MASK (0x40)
  61. /*! @brief Length mask */
  62. #define LEN_MASK (0x3F)
  63. /*! @brief Write command */
  64. #define WRITE_CMD (0x80)
  65. /*! @brief Read command */
  66. #define READ_CMD (0x00)
  67. /*! @brief Error Mask */
  68. #define ERROR_MASK (0x80)
  69. /* Address */
  70. /*! @brief Address index */
  71. #define ADDR_IDX (CTRL_IDX + CTRL_SIZE)
  72. /*! @brief Address size */
  73. #define ADDR_SIZE (4)
  74. /*! @brief Address range start */
  75. #define ADDR_RANGE_START (0x20100000)
  76. /*! @brief Address range end */
  77. #define ADDR_RANGE_END (0x20307FFF)
  78. /* Data */
  79. /*! @brief Command Data index */
  80. #define DATA_IDX (ADDR_IDX + ADDR_SIZE)
  81. /*! @brief Response Data index */
  82. #define RESP_DATA_IDX (CTRL_IDX + CTRL_SIZE)
  83. /*! @brief Maximum data size */
  84. #define MAX_DATA_SIZE (64)
  85. /* OFFSET is from the end of frame */
  86. /* CRC */
  87. /*! @brief CRC offset */
  88. #define CRC_OFFSET (0)
  89. /*! @brief CRC size */
  90. #define CRC_SIZE (2)
  91. /*! @brief Maximum buffer size */
  92. #define MAX_BUFFER_SIZE (CTRL_SIZE + ADDR_SIZE + MAX_DATA_SIZE + CRC_SIZE)
  93. /*! @brief Maximum response size */
  94. #define MAX_RESP_SIZE (CTRL_SIZE + MAX_DATA_SIZE + CRC_SIZE)
  95. /*! I2C Target Addr configured in the example */
  96. /*! @brief Default Target Address */
  97. #define DEF_TARGET_ADDR (0x68)
  98. /*! @brief Buffer Info structure */
  99. typedef struct
  100. {
  101. /*! Buffer array */
  102. _Alignas(uint32_t) uint8_t buffer[MAX_BUFFER_SIZE];
  103. /*! Pointer */
  104. uint8_t ptr;
  105. /*! Length */
  106. uint8_t len;
  107. } BufferInfo;
  108. /*! @brief Frame Info structure*/
  109. typedef struct
  110. {
  111. /*! CRC */
  112. uint16_t crc;
  113. /*! Control Byte */
  114. uint8_t ctrl;
  115. /*! Data */
  116. uint8_t data[MAX_DATA_SIZE];
  117. } FrameInfo;
  118. /*! @brief I2C status */
  119. typedef enum
  120. {
  121. /*! I2C IDLE state */
  122. I2C_STATUS_IDLE = 0,
  123. /*! I2C Tx Started state */
  124. I2C_STATUS_TX_STARTED,
  125. /*! I2C Tx in Progress state */
  126. I2C_STATUS_TX_INPROGRESS,
  127. /*! I2C Tx Complete state */
  128. I2C_STATUS_TX_COMPLETE,
  129. /*! I2C Rx Started state */
  130. I2C_STATUS_RX_STARTED,
  131. /*! I2C Rx in Progress state */
  132. I2C_STATUS_RX_INPROGRESS,
  133. /*! I2C Rx Complete state */
  134. I2C_STATUS_RX_COMPLETE,
  135. /*! I2C Error state */
  136. I2C_STATUS_ERROR,
  137. } I2C_Status;
  138. typedef enum
  139. {
  140. /*! No error */
  141. ERROR_TYPE_NONE = 0x00,
  142. /*! Mismatch CRC error */
  143. ERROR_TYPE_MISMATCH_CRC = 0xE1,
  144. /*! Error in Address range */
  145. ERROR_TYPE_ADDR_RANGE = 0xE2,
  146. } ErrorType;
  147. /*! @brief I2C instance */
  148. typedef struct
  149. {
  150. /*! Transmit message */
  151. BufferInfo txMsg;
  152. /*! Receive message */
  153. BufferInfo rxMsg;
  154. /*! Data length */
  155. uint8_t dataLen;
  156. /*! CRC enabled */
  157. _Bool isCrc;
  158. /*! I2C status */
  159. I2C_Status status;
  160. /*! I2C error type */
  161. ErrorType error;
  162. } I2C_Instance;
  163. /*! @brief I2C Command types */
  164. typedef enum
  165. {
  166. /*! I2C Read Command */
  167. READ_COMMAND = 0x00,
  168. /*! I2C Write Command */
  169. WRITE_COMMAND = 0x80,
  170. } CommandType;
  171. /*! @brief I2C Command Info */
  172. typedef struct
  173. {
  174. /*! Target Address */
  175. uint32_t targetAddr;
  176. /*! I2C Command Type */
  177. CommandType commandType;
  178. /*! Address */
  179. uint32_t addr;
  180. /*! Pointer to array of Data */
  181. uint8_t* dataArray;
  182. /*! Data Size */
  183. uint8_t dataSize;
  184. /*! CRC Enable*/
  185. bool crcEnable;
  186. } I2C_CommandInfo;
  187. /*! @brief I2C Response Info */
  188. typedef struct
  189. {
  190. /*! Response Frame Info */
  191. FrameInfo frame;
  192. /*! Response data size */
  193. uint8_t dataSize;
  194. /*! Response error status */
  195. ErrorType status;
  196. /*! Response received */
  197. bool received;
  198. } I2C_ResponseInfo;
  199. /**
  200. * @brief Initializes I2C_Instance Handle
  201. * @param[in] I2C_handle Pointer to I2C_Instance
  202. */
  203. void I2C_init(I2C_Instance *I2C_handle);
  204. /**
  205. * @brief Prepares I2C Command frame and stores in TX Buffer to transmit
  206. * @param[in] I2C_handle Pointer to I2C_Instance
  207. * @param[in] command Pointer to I2C_CommandInfo
  208. */
  209. void I2C_sendCommand(I2C_Instance *I2C_handle,I2C_CommandInfo *command);
  210. /**
  211. * @brief Sends I2C Read command to get the response from target
  212. * @param[in] I2C_handle Pointer to I2C_Instance
  213. * @param[in] targetAddr I2C Target Address
  214. */
  215. void I2C_getResponse(I2C_Instance* I2C_handle,uint32_t targetAddr);
  216. /**
  217. * @brief Decodes the received data in Rx Buffer for response
  218. * @param[in] I2C_handle Pointer to I2C_Instance
  219. * @param[in] response Pointer to I2C_ResponseInfo
  220. */
  221. void I2C_decodeResponse(I2C_Instance *I2C_handle,I2C_ResponseInfo *response);
  222. #ifdef __cplusplus
  223. }
  224. #endif
  225. #endif /* I2C_COMM_CONTROLLER_H_ */