pal_i2c.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /**
  2. * MIT License
  3. *
  4. * Copyright (c) 2018 Infineon Technologies AG
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE
  23. *
  24. *
  25. * \file
  26. *
  27. * \brief This file implements the platform abstraction layer(pal) APIs for I2C.
  28. *
  29. * \addtogroup grPAL
  30. * @{
  31. */
  32. /**********************************************************************************************************************
  33. * HEADER FILES
  34. *********************************************************************************************************************/
  35. #include "optiga/pal/pal_i2c.h"
  36. #include "optiga/ifx_i2c/ifx_i2c.h"
  37. #include "nrf_twi_mngr.h"
  38. #include "pal_pin_config.h"
  39. #include <stdbool.h>
  40. /// @cond hidden
  41. /**********************************************************************************************************************
  42. * MACROS
  43. *********************************************************************************************************************/
  44. #define PAL_I2C_MASTER_MAX_BITRATE (400)
  45. /** @brief I2C driver instance */
  46. #define TWI_INSTANCE_ID 0
  47. /** @brief Maximal number of pending I2C transactions */
  48. #define MAX_PENDING_TRANSACTIONS 5
  49. /*********************************************************************************************************************
  50. * LOCAL DATA
  51. *********************************************************************************************************************/
  52. /* Pointer to the current pal i2c context */
  53. static pal_i2c_t * gp_pal_i2c_current_ctx;
  54. /** @brief Definition of TWI manager instance */
  55. #ifndef IFX_2GO_SUPPORT
  56. NRF_TWI_MNGR_DEF(m_app_twi, MAX_PENDING_TRANSACTIONS, TWI_INSTANCE_ID);
  57. #else
  58. nrf_twi_mngr_t m_app_twi;
  59. #endif
  60. /** @brief Definition of TWI manager transfer instance */
  61. static nrf_twi_mngr_transfer_t m_transfer;
  62. /** @brief Definition of TWI manager transaction instance */
  63. static nrf_twi_mngr_transaction_t m_transaction;
  64. static bool initialized = false;
  65. /**********************************************************************************************************************
  66. * LOCAL ROUTINES
  67. *********************************************************************************************************************/
  68. /**
  69. * Pal I2C event handler function to invoke the registered upper layer callback<br>
  70. *
  71. *<b>API Details:</b>
  72. * - This function implements the platform specific i2c event handling mechanism<br>
  73. * - It calls the registered upper layer function after completion of the I2C read/write operations<br>
  74. * - The respective event status are explained below.
  75. * - #PAL_I2C_EVENT_ERROR when I2C fails due to low level failures(NACK/I2C protocol errors)
  76. * - #PAL_I2C_EVENT_SUCCESS when operation is successfully completed
  77. *
  78. * \param[in] p_pal_i2c_ctx Pointer to the pal i2c context #pal_i2c_t
  79. * \param[in] event Status of the event reported after read/write completion or due to I2C errors
  80. *
  81. */
  82. static void app_twi_callback(ret_code_t result, void * p_user_data)
  83. {
  84. app_event_handler_t upper_layer_handler;
  85. //lint --e{611} suppress "void* function pointer is type casted to app_event_handler_t type"
  86. upper_layer_handler = (app_event_handler_t)gp_pal_i2c_current_ctx->upper_layer_event_handler;
  87. if (result == NRF_SUCCESS)
  88. {
  89. upper_layer_handler(gp_pal_i2c_current_ctx->upper_layer_ctx, PAL_I2C_EVENT_SUCCESS);
  90. }
  91. else
  92. {
  93. upper_layer_handler(gp_pal_i2c_current_ctx->upper_layer_ctx, PAL_I2C_EVENT_ERROR);
  94. }
  95. }
  96. /// @endcond
  97. /**********************************************************************************************************************
  98. * API IMPLEMENTATION
  99. *********************************************************************************************************************/
  100. /**
  101. * API to initialize the i2c master with the given context.
  102. * <br>
  103. *
  104. *<b>API Details:</b>
  105. * - The platform specific initialization of I2C master has to be implemented as part of this API, if required.<br>
  106. * - If the target platform does not demand explicit initialization of i2c master
  107. * (Example: If the platform driver takes care of init after the reset), it would not be required to implement.<br>
  108. * - The implementation must take care the following scenarios depending upon the target platform selected.
  109. * - The implementation must handle the acquiring and releasing of the I2C bus before initializing the I2C master to
  110. * avoid interrupting the ongoing slave I2C transactions using the same I2C master.
  111. * - If the I2C bus is in busy state, the API must not initialize and return #PAL_STATUS_I2C_BUSY status.
  112. * - Repeated initialization must be taken care with respect to the platform requirements. (Example: Multiple users/applications
  113. * sharing the same I2C master resource)
  114. *
  115. *<b>User Input:</b><br>
  116. * - The input #pal_i2c_t p_i2c_context must not be NULL.<br>
  117. *
  118. * \param[in] p_i2c_context Pal i2c context to be initialized
  119. *
  120. * \retval #PAL_STATUS_SUCCESS Returns when the I2C master init it successfull
  121. * \retval #PAL_STATUS_FAILURE Returns when the I2C init fails.
  122. */
  123. pal_status_t pal_i2c_init(const pal_i2c_t* p_i2c_context)
  124. {
  125. #ifndef IFX_2GO_SUPPORT
  126. nrf_drv_twi_config_t const config = {
  127. .scl = OPTIGA_PIN_I2C_SCL,
  128. .sda = OPTIGA_PIN_I2C_SDA,
  129. .frequency = NRF_DRV_TWI_FREQ_400K,
  130. .interrupt_priority = APP_IRQ_PRIORITY_LOWEST,
  131. .clear_bus_init = false
  132. };
  133. #else
  134. #include "ifx_2go_common.h"
  135. nrf_drv_twi_config_t const config = {
  136. .scl = ifx_2go_pin_config()->scl,
  137. .sda = ifx_2go_pin_config()->sda,
  138. .frequency = NRF_TWI_FREQ_400K,
  139. .interrupt_priority = APP_IRQ_PRIORITY_LOWEST,
  140. .clear_bus_init = false
  141. };
  142. #endif
  143. if(initialized)
  144. {
  145. nrf_twi_mngr_uninit(&m_app_twi);
  146. }
  147. // Initialize I2C driver
  148. if (nrf_twi_mngr_init(&m_app_twi, &config) != NRF_SUCCESS)
  149. {
  150. return PAL_STATUS_FAILURE;
  151. }
  152. initialized = true;
  153. return PAL_STATUS_SUCCESS;
  154. }
  155. /**
  156. * API to de-initialize the I2C master with the specified context.
  157. * <br>
  158. *
  159. *<b>API Details:</b>
  160. * - The platform specific de-initialization of I2C master has to be implemented as part of this API, if required.<br>
  161. * - If the target platform does not demand explicit de-initialization of i2c master
  162. * (Example: If the platform driver takes care of init after the reset), it would not be required to implement.<br>
  163. * - The implementation must take care the following scenarios depending upon the target platform selected.
  164. * - The implementation must handle the acquiring and releasing of the I2C bus before de-initializing the I2C master to
  165. * avoid interrupting the ongoing slave I2C transactions using the same I2C master.
  166. * - If the I2C bus is in busy state, the API must not de-initialize and return #PAL_STATUS_I2C_BUSY status.
  167. * - This API must ensure that multiple users/applications sharing the same I2C master resource is not impacted.
  168. *
  169. *<b>User Input:</b><br>
  170. * - The input #pal_i2c_t p_i2c_context must not be NULL.<br>
  171. *
  172. * \param[in] p_i2c_context I2C context to be de-initialized
  173. *
  174. * \retval #PAL_STATUS_SUCCESS Returns when the I2C master de-init it successfull
  175. * \retval #PAL_STATUS_FAILURE Returns when the I2C de-init fails.
  176. */
  177. pal_status_t pal_i2c_deinit(const pal_i2c_t* p_i2c_context)
  178. {
  179. if(initialized) {
  180. nrf_twi_mngr_uninit(&m_app_twi);
  181. }
  182. initialized = false;
  183. return PAL_STATUS_SUCCESS;
  184. }
  185. /**
  186. * Platform abstraction layer API to write the data to I2C slave.
  187. * <br>
  188. * <br>
  189. * \image html pal_i2c_write.png "pal_i2c_write()" width=20cm
  190. *
  191. *
  192. *<b>API Details:</b>
  193. * - The API attempts to write if the I2C bus is free, else it returns busy status #PAL_STATUS_I2C_BUSY<br>
  194. * - The bus is released only after the completion of transmission or after completion of error handling.<br>
  195. * - The API invokes the upper layer handler with the respective event status as explained below.
  196. * - #PAL_I2C_EVENT_BUSY when I2C bus in busy state
  197. * - #PAL_I2C_EVENT_ERROR when API fails
  198. * - #PAL_I2C_EVENT_SUCCESS when operation is successfully completed asynchronously
  199. *<br>
  200. *
  201. *<b>User Input:</b><br>
  202. * - The input #pal_i2c_t p_i2c_context must not be NULL.<br>
  203. * - The upper_layer_event_handler must be initialized in the p_i2c_context before invoking the API.<br>
  204. *
  205. *<b>Notes:</b><br>
  206. * - Otherwise the below implementation has to be updated to handle different bitrates based on the input context.<br>
  207. * - The caller of this API must take care of the guard time based on the slave's requirement.<br>
  208. *
  209. * \param[in] p_i2c_context Pointer to the pal I2C context #pal_i2c_t
  210. * \param[in] p_data Pointer to the data to be written
  211. * \param[in] length Length of the data to be written
  212. *
  213. * \retval #PAL_STATUS_SUCCESS Returns when the I2C write is invoked successfully
  214. * \retval #PAL_STATUS_FAILURE Returns when the I2C write fails.
  215. * \retval #PAL_STATUS_I2C_BUSY Returns when the I2C bus is busy.
  216. */
  217. pal_status_t pal_i2c_write(pal_i2c_t* p_i2c_context,uint8_t* p_data , uint16_t length)
  218. {
  219. gp_pal_i2c_current_ctx = p_i2c_context;
  220. m_transfer.p_data = p_data;
  221. m_transfer.length = length;
  222. m_transfer.operation = NRF_TWI_MNGR_WRITE_OP(IFX_I2C_BASE_ADDR);
  223. m_transfer.flags = 0;
  224. m_transaction.callback = app_twi_callback;
  225. m_transaction.number_of_transfers = 1;
  226. m_transaction.p_required_twi_cfg = NULL;
  227. m_transaction.p_transfers = &m_transfer;
  228. m_transaction.p_user_data = (void*) PAL_STATUS_SUCCESS;
  229. if (nrf_twi_mngr_schedule(&m_app_twi, &m_transaction) != NRF_SUCCESS)
  230. {
  231. app_twi_callback(NRF_ERROR_BUSY, 0);
  232. }
  233. return PAL_STATUS_SUCCESS;
  234. }
  235. /**
  236. * Platform abstraction layer API to read the data from I2C slave.
  237. * <br>
  238. * <br>
  239. * \image html pal_i2c_read.png "pal_i2c_read()" width=20cm
  240. *
  241. *<b>API Details:</b>
  242. * - The API attempts to read if the I2C bus is free, else it returns busy status #PAL_STATUS_I2C_BUSY<br>
  243. * - The bus is released only after the completion of reception or after completion of error handling.<br>
  244. * - The API invokes the upper layer handler with the respective event status as explained below.
  245. * - #PAL_I2C_EVENT_BUSY when I2C bus in busy state
  246. * - #PAL_I2C_EVENT_ERROR when API fails
  247. * - #PAL_I2C_EVENT_SUCCESS when operation is successfully completed asynchronously
  248. *<br>
  249. *
  250. *<b>User Input:</b><br>
  251. * - The input #pal_i2c_t p_i2c_context must not be NULL.<br>
  252. * - The upper_layer_event_handler must be initialized in the p_i2c_context before invoking the API.<br>
  253. *
  254. *<b>Notes:</b><br>
  255. * - Otherwise the below implementation has to be updated to handle different bitrates based on the input context.<br>
  256. * - The caller of this API must take care of the guard time based on the slave's requirement.<br>
  257. *
  258. * \param[in] p_i2c_context pointer to the PAL i2c context #pal_i2c_t
  259. * \param[in] p_data Pointer to the data buffer to store the read data
  260. * \param[in] length Length of the data to be read
  261. *
  262. * \retval #PAL_STATUS_SUCCESS Returns when the I2C read is invoked successfully
  263. * \retval #PAL_STATUS_FAILURE Returns when the I2C read fails.
  264. * \retval #PAL_STATUS_I2C_BUSY Returns when the I2C bus is busy.
  265. */
  266. pal_status_t pal_i2c_read(pal_i2c_t* p_i2c_context , uint8_t* p_data , uint16_t length)
  267. {
  268. gp_pal_i2c_current_ctx = p_i2c_context;
  269. m_transfer.p_data = p_data;
  270. m_transfer.length = length;
  271. m_transfer.operation = NRF_TWI_MNGR_READ_OP(IFX_I2C_BASE_ADDR);
  272. m_transfer.flags = 0;
  273. m_transaction.callback = app_twi_callback;
  274. m_transaction.number_of_transfers = 1;
  275. m_transaction.p_required_twi_cfg = 0;
  276. m_transaction.p_transfers = &m_transfer;
  277. m_transaction.p_user_data = (void*) PAL_STATUS_SUCCESS;
  278. if (nrf_twi_mngr_schedule(&m_app_twi, &m_transaction) != NRF_SUCCESS)
  279. {
  280. app_twi_callback(NRF_ERROR_BUSY, 0);
  281. }
  282. return PAL_STATUS_SUCCESS;
  283. }
  284. /**
  285. * Platform abstraction layer API to set the bitrate/speed(KHz) of I2C master.
  286. * <br>
  287. *
  288. *<b>API Details:</b>
  289. * - Sets the bitrate of I2C master if the I2C bus is free, else it returns busy status #PAL_STATUS_I2C_BUSY<br>
  290. * - The bus is released after the setting the bitrate.<br>
  291. * - This API must take care of setting the bitrate to I2C master's maximum supported value.
  292. * - Eg. In XMC4500, the maximum supported bitrate is 400 KHz. If the supplied bitrate is greater than 400KHz, the API will
  293. * set the I2C master's bitrate to 400KHz.
  294. * - Use the #PAL_I2C_MASTER_MAX_BITRATE macro to specify the maximum supported bitrate value for the target platform.
  295. * - If upper_layer_event_handler is initialized, the upper layer handler is invoked with the respective event
  296. * status listed below.
  297. * - #PAL_I2C_EVENT_BUSY when I2C bus in busy state
  298. * - #PAL_I2C_EVENT_ERROR when API fails to set the bit rate
  299. * - #PAL_I2C_EVENT_SUCCESS when operation is successful
  300. *<br>
  301. *
  302. *<b>User Input:</b><br>
  303. * - The input #pal_i2c_t p_i2c_context must not be NULL.<br>
  304. *
  305. * \param[in] p_i2c_context Pointer to the pal i2c context
  306. * \param[in] bitrate Bitrate to be used by i2c master in KHz
  307. *
  308. * \retval #PAL_STATUS_SUCCESS Returns when the setting of bitrate is successfully completed
  309. * \retval #PAL_STATUS_FAILURE Returns when the setting of bitrate fails.
  310. * \retval #PAL_STATUS_I2C_BUSY Returns when the I2C bus is busy.
  311. */
  312. pal_status_t pal_i2c_set_bitrate(const pal_i2c_t* p_i2c_context , uint16_t bitrate)
  313. {
  314. // Bitrate is fixed to the maximum frequency on this platform (400K)
  315. return PAL_STATUS_SUCCESS;
  316. }
  317. #ifdef IFX_2GO_SUPPORT
  318. pal_status_t pal_i2c_set_instance(nrf_twi_mngr_t* twi_inst)
  319. {
  320. m_app_twi = *twi_inst;
  321. }
  322. #endif/*IFX_2GO_SUPPORT*/
  323. /**
  324. * @}
  325. */