ifx_i2c_config.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /**
  2. * \copyright
  3. * Copyright (c) 2018, Infineon Technologies AG
  4. * All rights reserved.
  5. *
  6. * This software is provided with terms and conditions as specified in OPTIGA(TM) Trust X Evaluation Kit License Agreement.
  7. * \endcopyright
  8. *
  9. * \author Infineon AG
  10. *
  11. * \file ifx_i2c_config.h
  12. *
  13. * \brief This file defines the structures and macros for the Infineon I2C Protocol.
  14. *
  15. * \addtogroup grIFXI2C
  16. * @{
  17. */
  18. #ifndef _IFX_I2C_CONFIG_H_
  19. #define _IFX_I2C_CONFIG_H_
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /***********************************************************************************************************************
  24. * HEADER FILES
  25. **********************************************************************************************************************/
  26. // Protocol Stack Includes
  27. #include <stdint.h>
  28. #include "pal_i2c.h"
  29. #include "pal_gpio.h"
  30. #include "pal_os_timer.h"
  31. /***********************************************************************************************************************
  32. * MACROS
  33. **********************************************************************************************************************/
  34. /** @brief I2C slave address of the Infineon device */
  35. #define IFX_I2C_BASE_ADDR (0x30)
  36. /** @brief Physical Layer: polling interval in microseconds */
  37. #define PL_POLLING_INVERVAL_US (1000)
  38. /** @brief Physical layer: maximal attempts */
  39. #define PL_POLLING_MAX_CNT (200)
  40. /** @brief Physical Layer: data register polling interval in microseconds */
  41. #define PL_DATA_POLLING_INVERVAL_US (5000)
  42. /** @brief Physical Layer: guard time interval in microseconds */
  43. #define PL_GUARD_TIME_INTERVAL_US (50)
  44. /** @brief Data link layer: maximum frame size */
  45. #define DL_MAX_FRAME_SIZE (300)
  46. /** @brief Data link layer: header size */
  47. #define DL_HEADER_SIZE (5)
  48. /** @brief Data link layer: maximum number of retries in case of transmission error */
  49. #define DL_TRANS_REPEAT (3)
  50. /** @brief Data link layer: Trans timeout in milliseconds*/
  51. #define PL_TRANS_TIMEOUT_MS (10)
  52. /** @brief Transport layer: Maximum exit timeout in seconds */
  53. #define TL_MAX_EXIT_TIMEOUT (6)
  54. /** @brief Reset low time for GPIO pin toggling */
  55. #define RESET_LOW_TIME_MSEC (2000)
  56. /** @brief Start up time */
  57. #define STARTUP_TIME_MSEC (12000)
  58. /** @brief Protocol Stack: Status codes for success */
  59. #define IFX_I2C_STACK_SUCCESS (0x00)
  60. /** @brief Protocol Stack: Status codes for error */
  61. #define IFX_I2C_STACK_ERROR (0x01)
  62. /** @brief Protocol Stack: Status codes busy */
  63. #define IFX_I2C_STACK_BUSY (0x02)
  64. /** @brief Protocol Stack: Memory insufficient */
  65. #define IFX_I2C_STACK_MEM_ERROR (0x03)
  66. /** @brief Protocol Stack: Fatal error. Used internal to IFX I2C Stack */
  67. #define IFX_I2C_FATAL_ERROR (0x04)
  68. /** @brief Offset of Datalink header in tx_frame_buffer */
  69. #define IFX_I2C_DL_HEADER_OFFSET (0)
  70. /** @brief Offset of Transport header in tx_frame_buffer */
  71. #define IFX_I2C_TL_HEADER_OFFSET (IFX_I2C_DL_HEADER_OFFSET+3)
  72. /** @brief Protocol Stack debug switch for physical layer (set to 0 or 1) */
  73. #define IFX_I2C_LOG_PL 0
  74. /** @brief Protocol Stack debug switch for data link layer (set to 0 or 1) */
  75. #define IFX_I2C_LOG_DL 0
  76. /** @brief Protocol Stack debug switch for transport layer (set to 0 or 1) */
  77. #define IFX_I2C_LOG_TL 0
  78. /** @brief Log ID number for physical layer */
  79. #define IFX_I2C_LOG_ID_PL 0x00
  80. /** @brief Log ID number for data link layer */
  81. #define IFX_I2C_LOG_ID_DL 0x01
  82. /** @brief Log ID number for transport layer */
  83. #define IFX_I2C_LOG_ID_TL 0x02
  84. /** @brief Log ID number for platform abstraction layer */
  85. #define IFX_I2C_LOG_ID_PAL 0x04
  86. /***********************************************************************************************************************
  87. * ENUMS
  88. ***********************************************************************************************************************/
  89. /***********************************************************************************************************************
  90. * DATA STRUCTURES
  91. ***********************************************************************************************************************/
  92. typedef struct ifx_i2c_context ifx_i2c_context_t;
  93. /** @brief Event handler function prototype */
  94. typedef void (*ifx_i2c_event_handler_t)(struct ifx_i2c_context* ctx, host_lib_status_t event, const uint8_t* data, uint16_t data_len);
  95. /** @brief Physical layer structure */
  96. typedef struct ifx_i2c_pl
  97. {
  98. // Physical Layer low level interface variables
  99. /// Physical layer buffer
  100. uint8_t buffer[DL_MAX_FRAME_SIZE+1];
  101. /// Tx length
  102. uint16_t buffer_tx_len;
  103. /// Rx length
  104. uint16_t buffer_rx_len;
  105. /// Action on register, read/write
  106. uint8_t register_action;
  107. /// i2c read/i2c write
  108. uint8_t i2c_cmd;
  109. /// Retry counter
  110. uint16_t retry_counter;
  111. // Physical Layer high level interface variables
  112. /// Action of frame. Tx/Rx
  113. uint8_t frame_action;
  114. /// Frame state
  115. uint8_t frame_state ;
  116. /// Pointer to data to be sent
  117. uint8_t * p_tx_frame;
  118. /// Length of data to be sent
  119. uint16_t tx_frame_len;
  120. // Upper layer handler
  121. ifx_i2c_event_handler_t upper_layer_event_handler;
  122. // Physical Layer negotiation/soft reset variables
  123. /// Negotiation state
  124. uint8_t negotiate_state;
  125. /// Soft reset requested
  126. uint8_t request_soft_reset;
  127. } ifx_i2c_pl_t;
  128. /** @brief Datalink layer structure */
  129. typedef struct ifx_i2c_dl
  130. {
  131. // Data Link layer internal state variables
  132. /// Datalink layer state
  133. uint8_t state;
  134. /// Tx sequence number
  135. uint8_t tx_seq_nr;
  136. // Rx sequence number
  137. uint8_t rx_seq_nr;
  138. /// Indicate only Rx required
  139. uint8_t action_rx_only;
  140. /// Retransmit counter
  141. uint8_t retransmit_counter;
  142. /// Error occured
  143. uint8_t error;
  144. /// Resynced
  145. uint8_t resynced;
  146. /// Timeout value
  147. uint32_t data_poll_timeout;
  148. /// Transmit buffer size
  149. uint16_t tx_buffer_size;
  150. /// Receive buffer size
  151. uint16_t rx_buffer_size;
  152. /// Pointer to main transmit buffers
  153. uint8_t* p_tx_frame_buffer;
  154. /// Pointer to main receive buffers
  155. uint8_t* p_rx_frame_buffer;
  156. ///Start time of sending frame
  157. uint32_t frame_start_time;
  158. // Upper layer Event handler
  159. ifx_i2c_event_handler_t upper_layer_event_handler;
  160. } ifx_i2c_dl_t;
  161. /** @brief Transport layer structure */
  162. typedef struct ifx_i2c_tl
  163. {
  164. // Transport Layer state and buffer
  165. /// Transport layer state
  166. uint8_t state;
  167. /// Pointer to packet provided by user
  168. uint8_t* p_actual_packet;
  169. /// Total received data
  170. uint16_t total_recv_length;
  171. /// Actual length of user provided packet
  172. uint16_t actual_packet_length;
  173. /// Offset till which data is sent from p_actual_packet
  174. uint16_t packet_offset;
  175. /// Maximum length of packet at transport layer
  176. uint16_t max_packet_length;
  177. /// Pointer to user provided receive buffer
  178. uint8_t* p_recv_packet_buffer;
  179. /// Length of receive buffer
  180. uint16_t* p_recv_packet_buffer_length;
  181. /// Start time of the transport layer API
  182. uint32_t api_start_time;
  183. ///Chaining error coutn from slave
  184. uint8_t chaining_error_count;
  185. ///Chaining error count for master
  186. uint8_t master_chaining_error_count;
  187. ///State to check last chaining state
  188. uint8_t previous_chaining;
  189. /// transmission done
  190. uint8_t transmission_completed;
  191. /// Error event state
  192. uint8_t error_event;
  193. /// Upper layer event handler
  194. ifx_i2c_event_handler_t upper_layer_event_handler;
  195. } ifx_i2c_tl_t;
  196. /** @brief IFX I2C context structure */
  197. struct ifx_i2c_context
  198. {
  199. /// I2C Slave address
  200. uint8_t slave_address;
  201. /// Frequency of i2c master
  202. uint16_t frequency;
  203. /// Data link layer frame size
  204. uint16_t frame_size;
  205. /// Pointer to pal gpio context for vdd
  206. pal_gpio_t* p_slave_vdd_pin;
  207. /// Pointer to pal gpio context for reset
  208. pal_gpio_t* p_slave_reset_pin;
  209. /// Pointer to pal i2c context
  210. pal_i2c_t* p_pal_i2c_ctx;
  211. /// Upper layer event handler
  212. app_event_handler_t upper_layer_event_handler;
  213. /// Upper layer context
  214. void* p_upper_layer_ctx;
  215. /// Pointer to upper layer rx buffer
  216. uint8_t* p_upper_layer_rx_buffer;
  217. /// Pointer to length of upper layer rx buffer
  218. uint16_t* p_upper_layer_rx_buffer_len;
  219. /// Protocol variables
  220. /// ifx i2c wrapper apis state
  221. uint8_t state;
  222. /// ifx i2c wrapper api status
  223. uint8_t status;
  224. /// reset states
  225. uint8_t reset_state;
  226. /// type of reset
  227. uint8_t reset_type;
  228. /// init pal
  229. uint8_t do_pal_init;
  230. /// Transport layer context
  231. ifx_i2c_tl_t tl;
  232. /// Datalink layer context
  233. ifx_i2c_dl_t dl;
  234. /// Physical layer context
  235. ifx_i2c_pl_t pl;
  236. /// IFX I2C tx frame of max length
  237. uint8_t tx_frame_buffer[DL_MAX_FRAME_SIZE];
  238. /// IFX I2C rx frame of max length
  239. uint8_t rx_frame_buffer[DL_MAX_FRAME_SIZE];
  240. };
  241. /***********************************************************************************************************************
  242. * GLOBAL
  243. ***********************************************************************************************************************/
  244. /** @brief IFX I2C Instance */
  245. extern ifx_i2c_context_t ifx_i2c_context_0;
  246. /***********************************************************************************************************************
  247. * LOCAL ROUTINES
  248. ***********************************************************************************************************************/
  249. /***********************************************************************************************************************
  250. * API PROTOTYPES
  251. **********************************************************************************************************************/
  252. /**
  253. * @}
  254. **/
  255. #ifdef __cplusplus
  256. }
  257. #endif
  258. #endif /* _IFX_I2C_CONFIG_H_ */