i2c_comm_controller.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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_ADC (0x68)
  98. //#define DEF_TARGET_ADDR_DAC (0x60)
  99. /*! @brief Buffer Info structure */
  100. typedef struct
  101. {
  102. /*! Buffer array */
  103. _Alignas(uint32_t) uint8_t buffer[MAX_BUFFER_SIZE];
  104. /*! Pointer */
  105. uint8_t ptr;
  106. /*! Length */
  107. uint8_t len;
  108. } BufferInfo;
  109. /*! @brief Frame Info structure*/
  110. typedef struct
  111. {
  112. /*! CRC */
  113. uint16_t crc;
  114. /*! Control Byte */
  115. uint8_t ctrl;
  116. /*! Data */
  117. uint8_t data[MAX_DATA_SIZE];
  118. } FrameInfo;
  119. /*! @brief I2C status */
  120. typedef enum
  121. {
  122. /*! I2C IDLE state */
  123. I2C_STATUS_IDLE = 0,
  124. /*! I2C Tx Started state */
  125. I2C_STATUS_TX_STARTED,
  126. /*! I2C Tx in Progress state */
  127. I2C_STATUS_TX_INPROGRESS,
  128. /*! I2C Tx Complete state */
  129. I2C_STATUS_TX_COMPLETE,
  130. /*! I2C Rx Started state */
  131. I2C_STATUS_RX_STARTED,
  132. /*! I2C Rx in Progress state */
  133. I2C_STATUS_RX_INPROGRESS,
  134. /*! I2C Rx Complete state */
  135. I2C_STATUS_RX_COMPLETE,
  136. /*! I2C Error state */
  137. I2C_STATUS_ERROR,
  138. } I2C_Status;
  139. typedef enum
  140. {
  141. /*! No error */
  142. ERROR_TYPE_NONE = 0x00,
  143. /*! Mismatch CRC error */
  144. ERROR_TYPE_MISMATCH_CRC = 0xE1,
  145. /*! Error in Address range */
  146. ERROR_TYPE_ADDR_RANGE = 0xE2,
  147. } ErrorType;
  148. /*! @brief I2C instance */
  149. typedef struct
  150. {
  151. /*! Transmit message */
  152. BufferInfo txMsg;
  153. /*! Receive message */
  154. BufferInfo rxMsg;
  155. /*! Data length */
  156. uint8_t dataLen;
  157. /*! CRC enabled */
  158. _Bool isCrc;
  159. /*! I2C status */
  160. I2C_Status status;
  161. /*! I2C error type */
  162. ErrorType error;
  163. } I2C_Instance;
  164. /*! @brief I2C Command types */
  165. typedef enum
  166. {
  167. /*! I2C Read Command */
  168. READ_COMMAND = 0x00,
  169. /*! I2C Write Command */
  170. WRITE_COMMAND = 0x80,
  171. } CommandType;
  172. /*! @brief I2C Command Info */
  173. typedef struct
  174. {
  175. /*! Target Address */
  176. uint32_t targetAddr;
  177. /*! I2C Command Type */
  178. CommandType commandType;
  179. /*! Address */
  180. uint32_t addr;
  181. /*! Pointer to array of Data */
  182. uint8_t* dataArray;
  183. /*! Data Size */
  184. uint8_t dataSize;
  185. /*! CRC Enable*/
  186. bool crcEnable;
  187. } I2C_CommandInfo;
  188. /*! @brief I2C Response Info */
  189. typedef struct
  190. {
  191. /*! Response Frame Info */
  192. FrameInfo frame;
  193. /*! Response data size */
  194. uint8_t dataSize;
  195. /*! Response error status */
  196. ErrorType status;
  197. /*! Response received */
  198. bool received;
  199. } I2C_ResponseInfo;
  200. /**
  201. * @brief Initializes I2C_Instance Handle
  202. * @param[in] I2C_handle Pointer to I2C_Instance
  203. */
  204. void I2C_init(I2C_Instance *I2C_handle);
  205. /**
  206. * @brief Prepares I2C Command frame and stores in TX Buffer to transmit
  207. * @param[in] I2C_handle Pointer to I2C_Instance
  208. * @param[in] command Pointer to I2C_CommandInfo
  209. */
  210. void I2C_sendCommand(I2C_Instance *I2C_handle,I2C_CommandInfo *command);
  211. /**
  212. * @brief Sends I2C Read command to get the response from target
  213. * @param[in] I2C_handle Pointer to I2C_Instance
  214. * @param[in] targetAddr I2C Target Address
  215. */
  216. void I2C_getResponse(I2C_Instance* I2C_handle,uint32_t targetAddr);
  217. /**
  218. * @brief Decodes the received data in Rx Buffer for response
  219. * @param[in] I2C_handle Pointer to I2C_Instance
  220. * @param[in] response Pointer to I2C_ResponseInfo
  221. */
  222. void I2C_decodeResponse(I2C_Instance *I2C_handle,I2C_ResponseInfo *response);
  223. #ifdef __cplusplus
  224. }
  225. #endif
  226. #endif /* I2C_COMM_CONTROLLER_H_ */