i2c_comm_controller.h 7.1 KB

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