pal_i2c.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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
  12. *
  13. * \brief This file implements the platform abstraction layer(pal) APIs for I2C.
  14. *
  15. * \addtogroup grPAL
  16. * @{
  17. */
  18. /**********************************************************************************************************************
  19. * HEADER FILES
  20. *********************************************************************************************************************/
  21. #include "pal_i2c.h"
  22. #include "ifx_i2c.h"
  23. #include "nrf_twi_mngr.h"
  24. /// @cond hidden
  25. /**********************************************************************************************************************
  26. * MACROS
  27. *********************************************************************************************************************/
  28. #define PAL_I2C_MASTER_MAX_BITRATE (400)
  29. /** @brief PIN for I2C SCL to Infineon OPTIGA Trust X device */
  30. #define OPTIGA_PIN_I2C_SCL (27)
  31. /** @brief PIN for I2C SDA to Infineon OPTIGA Trust X device */
  32. #define OPTIGA_PIN_I2C_SDA (26)
  33. /** @brief I2C driver instance */
  34. #define TWI_INSTANCE_ID 0
  35. /** @brief Maximal number of pending I2C transactions */
  36. #define MAX_PENDING_TRANSACTIONS 5
  37. /*********************************************************************************************************************
  38. * LOCAL DATA
  39. *********************************************************************************************************************/
  40. /* Pointer to the current pal i2c context */
  41. static pal_i2c_t * gp_pal_i2c_current_ctx;
  42. /** @brief Definition of TWI manager instance */
  43. NRF_TWI_MNGR_DEF(m_app_twi, MAX_PENDING_TRANSACTIONS, TWI_INSTANCE_ID);
  44. /** @brief Definition of TWI manager transfer instance */
  45. static nrf_twi_mngr_transfer_t m_transfer;
  46. /** @brief Definition of TWI manager transaction instance */
  47. static nrf_twi_mngr_transaction_t m_transaction;
  48. /**********************************************************************************************************************
  49. * LOCAL ROUTINES
  50. *********************************************************************************************************************/
  51. /**
  52. * Pal I2C event handler function to invoke the registered upper layer callback<br>
  53. *
  54. *<b>API Details:</b>
  55. * - This function implements the platform specific i2c event handling mechanism<br>
  56. * - It calls the registered upper layer function after completion of the I2C read/write operations<br>
  57. * - The respective event status are explained below.
  58. * - #PAL_I2C_EVENT_ERROR when I2C fails due to low level failures(NACK/I2C protocol errors)
  59. * - #PAL_I2C_EVENT_SUCCESS when operation is successfully completed
  60. *
  61. * \param[in] p_pal_i2c_ctx Pointer to the pal i2c context #pal_i2c_t
  62. * \param[in] event Status of the event reported after read/write completion or due to I2C errors
  63. *
  64. */
  65. static void app_twi_callback(ret_code_t result, void * p_user_data)
  66. {
  67. app_event_handler_t upper_layer_handler;
  68. //lint --e{611} suppress "void* function pointer is type casted to app_event_handler_t type"
  69. upper_layer_handler = (app_event_handler_t)gp_pal_i2c_current_ctx->upper_layer_event_handler;
  70. if (result == NRF_SUCCESS)
  71. {
  72. upper_layer_handler(gp_pal_i2c_current_ctx->upper_layer_ctx, PAL_I2C_EVENT_SUCCESS);
  73. }
  74. else
  75. {
  76. upper_layer_handler(gp_pal_i2c_current_ctx->upper_layer_ctx, PAL_I2C_EVENT_ERROR);
  77. }
  78. }
  79. /// @endcond
  80. /**********************************************************************************************************************
  81. * API IMPLEMENTATION
  82. *********************************************************************************************************************/
  83. /**
  84. * API to initialize the i2c master with the given context.
  85. * <br>
  86. *
  87. *<b>API Details:</b>
  88. * - The platform specific initialization of I2C master has to be implemented as part of this API, if required.<br>
  89. * - If the target platform does not demand explicit initialization of i2c master
  90. * (Example: If the platform driver takes care of init after the reset), it would not be required to implement.<br>
  91. * - The implementation must take care the following scenarios depending upon the target platform selected.
  92. * - The implementation must handle the acquiring and releasing of the I2C bus before initializing the I2C master to
  93. * avoid interrupting the ongoing slave I2C transactions using the same I2C master.
  94. * - If the I2C bus is in busy state, the API must not initialize and return #PAL_STATUS_I2C_BUSY status.
  95. * - Repeated initialization must be taken care with respect to the platform requirements. (Example: Multiple users/applications
  96. * sharing the same I2C master resource)
  97. *
  98. *<b>User Input:</b><br>
  99. * - The input #pal_i2c_t p_i2c_context must not be NULL.<br>
  100. *
  101. * \param[in] p_i2c_context Pal i2c context to be initialized
  102. *
  103. * \retval #PAL_STATUS_SUCCESS Returns when the I2C master init it successfull
  104. * \retval #PAL_STATUS_FAILURE Returns when the I2C init fails.
  105. */
  106. pal_status_t pal_i2c_init(const pal_i2c_t* p_i2c_context)
  107. {
  108. nrf_drv_twi_config_t const config = {
  109. .scl = OPTIGA_PIN_I2C_SCL,
  110. .sda = OPTIGA_PIN_I2C_SDA,
  111. .frequency = (nrf_drv_twi_frequency_t) NRF_TWI_FREQ_400K,
  112. .interrupt_priority = APP_IRQ_PRIORITY_LOWEST,
  113. .clear_bus_init = false
  114. };
  115. // Initialize I2C driver
  116. if (nrf_twi_mngr_init(&m_app_twi, &config) != NRF_SUCCESS)
  117. {
  118. return PAL_STATUS_FAILURE;
  119. }
  120. return PAL_STATUS_SUCCESS;
  121. }
  122. /**
  123. * API to de-initialize the I2C master with the specified context.
  124. * <br>
  125. *
  126. *<b>API Details:</b>
  127. * - The platform specific de-initialization of I2C master has to be implemented as part of this API, if required.<br>
  128. * - If the target platform does not demand explicit de-initialization of i2c master
  129. * (Example: If the platform driver takes care of init after the reset), it would not be required to implement.<br>
  130. * - The implementation must take care the following scenarios depending upon the target platform selected.
  131. * - The implementation must handle the acquiring and releasing of the I2C bus before de-initializing the I2C master to
  132. * avoid interrupting the ongoing slave I2C transactions using the same I2C master.
  133. * - If the I2C bus is in busy state, the API must not de-initialize and return #PAL_STATUS_I2C_BUSY status.
  134. * - This API must ensure that multiple users/applications sharing the same I2C master resource is not impacted.
  135. *
  136. *<b>User Input:</b><br>
  137. * - The input #pal_i2c_t p_i2c_context must not be NULL.<br>
  138. *
  139. * \param[in] p_i2c_context I2C context to be de-initialized
  140. *
  141. * \retval #PAL_STATUS_SUCCESS Returns when the I2C master de-init it successfull
  142. * \retval #PAL_STATUS_FAILURE Returns when the I2C de-init fails.
  143. */
  144. pal_status_t pal_i2c_deinit(const pal_i2c_t* p_i2c_context)
  145. {
  146. nrf_twi_mngr_uninit(&m_app_twi);
  147. return PAL_STATUS_SUCCESS;
  148. }
  149. /**
  150. * Platform abstraction layer API to write the data to I2C slave.
  151. * <br>
  152. * <br>
  153. * \image html pal_i2c_write.png "pal_i2c_write()" width=20cm
  154. *
  155. *
  156. *<b>API Details:</b>
  157. * - The API attempts to write if the I2C bus is free, else it returns busy status #PAL_STATUS_I2C_BUSY<br>
  158. * - The bus is released only after the completion of transmission or after completion of error handling.<br>
  159. * - The API invokes the upper layer handler with the respective event status as explained below.
  160. * - #PAL_I2C_EVENT_BUSY when I2C bus in busy state
  161. * - #PAL_I2C_EVENT_ERROR when API fails
  162. * - #PAL_I2C_EVENT_SUCCESS when operation is successfully completed asynchronously
  163. *<br>
  164. *
  165. *<b>User Input:</b><br>
  166. * - The input #pal_i2c_t p_i2c_context must not be NULL.<br>
  167. * - The upper_layer_event_handler must be initialized in the p_i2c_context before invoking the API.<br>
  168. *
  169. *<b>Notes:</b><br>
  170. * - Otherwise the below implementation has to be updated to handle different bitrates based on the input context.<br>
  171. * - The caller of this API must take care of the guard time based on the slave's requirement.<br>
  172. *
  173. * \param[in] p_i2c_context Pointer to the pal I2C context #pal_i2c_t
  174. * \param[in] p_data Pointer to the data to be written
  175. * \param[in] length Length of the data to be written
  176. *
  177. * \retval #PAL_STATUS_SUCCESS Returns when the I2C write is invoked successfully
  178. * \retval #PAL_STATUS_FAILURE Returns when the I2C write fails.
  179. * \retval #PAL_STATUS_I2C_BUSY Returns when the I2C bus is busy.
  180. */
  181. pal_status_t pal_i2c_write(pal_i2c_t* p_i2c_context,uint8_t* p_data , uint16_t length)
  182. {
  183. gp_pal_i2c_current_ctx = p_i2c_context;
  184. m_transfer.p_data = p_data;
  185. m_transfer.length = length;
  186. m_transfer.operation = NRF_TWI_MNGR_WRITE_OP(IFX_I2C_BASE_ADDR);
  187. m_transfer.flags = 0;
  188. m_transaction.callback = app_twi_callback;
  189. m_transaction.number_of_transfers = 1;
  190. m_transaction.p_required_twi_cfg = NULL;
  191. m_transaction.p_transfers = &m_transfer;
  192. m_transaction.p_user_data = (void*) PAL_STATUS_SUCCESS;
  193. if (nrf_twi_mngr_schedule(&m_app_twi, &m_transaction) != NRF_SUCCESS)
  194. {
  195. app_twi_callback(NRF_ERROR_BUSY, 0);
  196. }
  197. return PAL_STATUS_SUCCESS;
  198. }
  199. /**
  200. * Platform abstraction layer API to read the data from I2C slave.
  201. * <br>
  202. * <br>
  203. * \image html pal_i2c_read.png "pal_i2c_read()" width=20cm
  204. *
  205. *<b>API Details:</b>
  206. * - The API attempts to read if the I2C bus is free, else it returns busy status #PAL_STATUS_I2C_BUSY<br>
  207. * - The bus is released only after the completion of reception or after completion of error handling.<br>
  208. * - The API invokes the upper layer handler with the respective event status as explained below.
  209. * - #PAL_I2C_EVENT_BUSY when I2C bus in busy state
  210. * - #PAL_I2C_EVENT_ERROR when API fails
  211. * - #PAL_I2C_EVENT_SUCCESS when operation is successfully completed asynchronously
  212. *<br>
  213. *
  214. *<b>User Input:</b><br>
  215. * - The input #pal_i2c_t p_i2c_context must not be NULL.<br>
  216. * - The upper_layer_event_handler must be initialized in the p_i2c_context before invoking the API.<br>
  217. *
  218. *<b>Notes:</b><br>
  219. * - Otherwise the below implementation has to be updated to handle different bitrates based on the input context.<br>
  220. * - The caller of this API must take care of the guard time based on the slave's requirement.<br>
  221. *
  222. * \param[in] p_i2c_context pointer to the PAL i2c context #pal_i2c_t
  223. * \param[in] p_data Pointer to the data buffer to store the read data
  224. * \param[in] length Length of the data to be read
  225. *
  226. * \retval #PAL_STATUS_SUCCESS Returns when the I2C read is invoked successfully
  227. * \retval #PAL_STATUS_FAILURE Returns when the I2C read fails.
  228. * \retval #PAL_STATUS_I2C_BUSY Returns when the I2C bus is busy.
  229. */
  230. pal_status_t pal_i2c_read(pal_i2c_t* p_i2c_context , uint8_t* p_data , uint16_t length)
  231. {
  232. gp_pal_i2c_current_ctx = p_i2c_context;
  233. m_transfer.p_data = p_data;
  234. m_transfer.length = length;
  235. m_transfer.operation = NRF_TWI_MNGR_READ_OP(IFX_I2C_BASE_ADDR);
  236. m_transfer.flags = 0;
  237. m_transaction.callback = app_twi_callback;
  238. m_transaction.number_of_transfers = 1;
  239. m_transaction.p_required_twi_cfg = 0;
  240. m_transaction.p_transfers = &m_transfer;
  241. m_transaction.p_user_data = (void*) PAL_STATUS_SUCCESS;
  242. if (nrf_twi_mngr_schedule(&m_app_twi, &m_transaction) != NRF_SUCCESS)
  243. {
  244. app_twi_callback(NRF_ERROR_BUSY, 0);
  245. }
  246. return PAL_STATUS_SUCCESS;
  247. }
  248. /**
  249. * Platform abstraction layer API to set the bitrate/speed(KHz) of I2C master.
  250. * <br>
  251. *
  252. *<b>API Details:</b>
  253. * - Sets the bitrate of I2C master if the I2C bus is free, else it returns busy status #PAL_STATUS_I2C_BUSY<br>
  254. * - The bus is released after the setting the bitrate.<br>
  255. * - This API must take care of setting the bitrate to I2C master's maximum supported value.
  256. * - Eg. In XMC4500, the maximum supported bitrate is 400 KHz. If the supplied bitrate is greater than 400KHz, the API will
  257. * set the I2C master's bitrate to 400KHz.
  258. * - Use the #PAL_I2C_MASTER_MAX_BITRATE macro to specify the maximum supported bitrate value for the target platform.
  259. * - If upper_layer_event_handler is initialized, the upper layer handler is invoked with the respective event
  260. * status listed below.
  261. * - #PAL_I2C_EVENT_BUSY when I2C bus in busy state
  262. * - #PAL_I2C_EVENT_ERROR when API fails to set the bit rate
  263. * - #PAL_I2C_EVENT_SUCCESS when operation is successful
  264. *<br>
  265. *
  266. *<b>User Input:</b><br>
  267. * - The input #pal_i2c_t p_i2c_context must not be NULL.<br>
  268. *
  269. * \param[in] p_i2c_context Pointer to the pal i2c context
  270. * \param[in] bitrate Bitrate to be used by i2c master in KHz
  271. *
  272. * \retval #PAL_STATUS_SUCCESS Returns when the setting of bitrate is successfully completed
  273. * \retval #PAL_STATUS_FAILURE Returns when the setting of bitrate fails.
  274. * \retval #PAL_STATUS_I2C_BUSY Returns when the I2C bus is busy.
  275. */
  276. pal_status_t pal_i2c_set_bitrate(const pal_i2c_t* p_i2c_context , uint16_t bitrate)
  277. {
  278. // Bitrate is fixed to the maximum frequency on this platform (400K)
  279. return PAL_STATUS_SUCCESS;
  280. }
  281. /**
  282. * @}
  283. */