pal_i2c.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "boards.h"
  39. #include <stdbool.h>
  40. /// @cond hidden
  41. /**********************************************************************************************************************
  42. * MACROS
  43. *********************************************************************************************************************/
  44. /** @brief Max bitrate for i2c master */
  45. #define PAL_I2C_MASTER_MAX_BITRATE (400)
  46. /** @brief PIN for I2C SCL to Infineon OPTIGA Trust X device */
  47. #define OPTIGA_PIN_I2C_SCL (ARDUINO_SCL_PIN)
  48. /** @brief PIN for I2C SDA to Infineon OPTIGA Trust X device */
  49. #define OPTIGA_PIN_I2C_SDA (ARDUINO_SDA_PIN)
  50. /** @brief I2C driver instance */
  51. #define TWI_INSTANCE_ID 0
  52. /** @brief Maximal number of pending I2C transactions */
  53. #define MAX_PENDING_TRANSACTIONS 5
  54. /*********************************************************************************************************************
  55. * LOCAL DATA
  56. *********************************************************************************************************************/
  57. /* Pointer to the current pal i2c context */
  58. static pal_i2c_t * gp_pal_i2c_current_ctx;
  59. /** @brief Definition of TWI manager instance */
  60. #ifndef IFX_2GO_SUPPORT
  61. NRF_TWI_MNGR_DEF(m_app_twi, MAX_PENDING_TRANSACTIONS, TWI_INSTANCE_ID);
  62. #else
  63. nrf_twi_mngr_t m_app_twi;
  64. #endif
  65. /** @brief Definition of TWI manager transfer instance */
  66. static nrf_twi_mngr_transfer_t m_transfer;
  67. /** @brief Definition of TWI manager transaction instance */
  68. static nrf_twi_mngr_transaction_t m_transaction;
  69. static bool initialized = false;
  70. /**********************************************************************************************************************
  71. * LOCAL ROUTINES
  72. *********************************************************************************************************************/
  73. /**
  74. * Pal I2C event handler function to invoke the registered upper layer callback<br>
  75. *
  76. *<b>API Details:</b>
  77. * - This function implements the platform specific i2c event handling mechanism<br>
  78. * - It calls the registered upper layer function after completion of the I2C read/write operations<br>
  79. * - The respective event status are explained below.
  80. * - #PAL_I2C_EVENT_ERROR when I2C fails due to low level failures(NACK/I2C protocol errors)
  81. * - #PAL_I2C_EVENT_SUCCESS when operation is successfully completed
  82. *
  83. * \param[in] p_pal_i2c_ctx Pointer to the pal i2c context #pal_i2c_t
  84. * \param[in] event Status of the event reported after read/write completion or due to I2C errors
  85. *
  86. */
  87. static void app_twi_callback(ret_code_t result, void * p_user_data)
  88. {
  89. app_event_handler_t upper_layer_handler;
  90. //lint --e{611} suppress "void* function pointer is type casted to app_event_handler_t type"
  91. upper_layer_handler = (app_event_handler_t)gp_pal_i2c_current_ctx->upper_layer_event_handler;
  92. if (result == NRF_SUCCESS)
  93. {
  94. upper_layer_handler(gp_pal_i2c_current_ctx->upper_layer_ctx, PAL_I2C_EVENT_SUCCESS);
  95. }
  96. else
  97. {
  98. upper_layer_handler(gp_pal_i2c_current_ctx->upper_layer_ctx, PAL_I2C_EVENT_ERROR);
  99. }
  100. }
  101. /// @endcond
  102. /**********************************************************************************************************************
  103. * API IMPLEMENTATION
  104. *********************************************************************************************************************/
  105. pal_status_t pal_i2c_init(const pal_i2c_t* p_i2c_context)
  106. {
  107. #ifndef IFX_2GO_SUPPORT
  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_FREQ_400K,
  112. .interrupt_priority = APP_IRQ_PRIORITY_LOWEST,
  113. .clear_bus_init = false
  114. };
  115. #else
  116. #include "ifx_2go_common.h"
  117. nrf_drv_twi_config_t const config = {
  118. .scl = ifx_2go_pin_config()->scl,
  119. .sda = ifx_2go_pin_config()->sda,
  120. .frequency = NRF_TWI_FREQ_400K,
  121. .interrupt_priority = APP_IRQ_PRIORITY_LOWEST,
  122. .clear_bus_init = false
  123. };
  124. #endif
  125. if(initialized)
  126. {
  127. nrf_twi_mngr_uninit(&m_app_twi);
  128. }
  129. // Initialize I2C driver
  130. if (nrf_twi_mngr_init(&m_app_twi, &config) != NRF_SUCCESS)
  131. {
  132. return PAL_STATUS_FAILURE;
  133. }
  134. initialized = true;
  135. return PAL_STATUS_SUCCESS;
  136. }
  137. pal_status_t pal_i2c_deinit(const pal_i2c_t* p_i2c_context)
  138. {
  139. if(initialized) {
  140. nrf_twi_mngr_uninit(&m_app_twi);
  141. }
  142. initialized = false;
  143. return PAL_STATUS_SUCCESS;
  144. }
  145. pal_status_t pal_i2c_write(pal_i2c_t* p_i2c_context,uint8_t* p_data , uint16_t length)
  146. {
  147. gp_pal_i2c_current_ctx = p_i2c_context;
  148. m_transfer.p_data = p_data;
  149. m_transfer.length = length;
  150. m_transfer.operation = NRF_TWI_MNGR_WRITE_OP(IFX_I2C_BASE_ADDR);
  151. m_transfer.flags = 0;
  152. m_transaction.callback = app_twi_callback;
  153. m_transaction.number_of_transfers = 1;
  154. m_transaction.p_required_twi_cfg = NULL;
  155. m_transaction.p_transfers = &m_transfer;
  156. m_transaction.p_user_data = (void*) PAL_STATUS_SUCCESS;
  157. if (nrf_twi_mngr_schedule(&m_app_twi, &m_transaction) != NRF_SUCCESS)
  158. {
  159. app_twi_callback(NRF_ERROR_BUSY, 0);
  160. }
  161. return PAL_STATUS_SUCCESS;
  162. }
  163. pal_status_t pal_i2c_read(pal_i2c_t* p_i2c_context , uint8_t* p_data , uint16_t length)
  164. {
  165. gp_pal_i2c_current_ctx = p_i2c_context;
  166. m_transfer.p_data = p_data;
  167. m_transfer.length = length;
  168. m_transfer.operation = NRF_TWI_MNGR_READ_OP(IFX_I2C_BASE_ADDR);
  169. m_transfer.flags = 0;
  170. m_transaction.callback = app_twi_callback;
  171. m_transaction.number_of_transfers = 1;
  172. m_transaction.p_required_twi_cfg = 0;
  173. m_transaction.p_transfers = &m_transfer;
  174. m_transaction.p_user_data = (void*) PAL_STATUS_SUCCESS;
  175. if (nrf_twi_mngr_schedule(&m_app_twi, &m_transaction) != NRF_SUCCESS)
  176. {
  177. app_twi_callback(NRF_ERROR_BUSY, 0);
  178. }
  179. return PAL_STATUS_SUCCESS;
  180. }
  181. pal_status_t pal_i2c_set_bitrate(const pal_i2c_t* p_i2c_context , uint16_t bitrate)
  182. {
  183. // Bitrate is fixed to the maximum frequency on this platform (400K)
  184. return PAL_STATUS_SUCCESS;
  185. }
  186. #ifdef IFX_2GO_SUPPORT
  187. pal_status_t pal_i2c_set_instance(nrf_twi_mngr_t* twi_inst)
  188. {
  189. m_app_twi = *twi_inst;
  190. }
  191. #endif/*IFX_2GO_SUPPORT*/
  192. /**
  193. * @}
  194. */