123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360 |
- #include "optiga/pal/pal_i2c.h"
- #include "optiga/ifx_i2c/ifx_i2c.h"
- #include "nrf_twi_mngr.h"
- #include "pal_pin_config.h"
- #include <stdbool.h>
- #define PAL_I2C_MASTER_MAX_BITRATE (400)
- #define TWI_INSTANCE_ID 0
- #define MAX_PENDING_TRANSACTIONS 5
- static pal_i2c_t * gp_pal_i2c_current_ctx;
- #ifndef IFX_2GO_SUPPORT
- NRF_TWI_MNGR_DEF(m_app_twi, MAX_PENDING_TRANSACTIONS, TWI_INSTANCE_ID);
- #else
- nrf_twi_mngr_t m_app_twi;
- #endif
- static nrf_twi_mngr_transfer_t m_transfer;
- static nrf_twi_mngr_transaction_t m_transaction;
- static bool initialized = false;
- static void app_twi_callback(ret_code_t result, void * p_user_data)
- {
- app_event_handler_t upper_layer_handler;
-
- upper_layer_handler = (app_event_handler_t)gp_pal_i2c_current_ctx->upper_layer_event_handler;
- if (result == NRF_SUCCESS)
- {
- upper_layer_handler(gp_pal_i2c_current_ctx->upper_layer_ctx, PAL_I2C_EVENT_SUCCESS);
- }
- else
- {
- upper_layer_handler(gp_pal_i2c_current_ctx->upper_layer_ctx, PAL_I2C_EVENT_ERROR);
- }
- }
- pal_status_t pal_i2c_init(const pal_i2c_t* p_i2c_context)
- {
- #ifndef IFX_2GO_SUPPORT
- nrf_drv_twi_config_t const config = {
- .scl = OPTIGA_PIN_I2C_SCL,
- .sda = OPTIGA_PIN_I2C_SDA,
- .frequency = NRF_DRV_TWI_FREQ_400K,
- .interrupt_priority = APP_IRQ_PRIORITY_LOWEST,
- .clear_bus_init = false
- };
- #else
- #include "ifx_2go_common.h"
- nrf_drv_twi_config_t const config = {
- .scl = ifx_2go_pin_config()->scl,
- .sda = ifx_2go_pin_config()->sda,
- .frequency = NRF_TWI_FREQ_400K,
- .interrupt_priority = APP_IRQ_PRIORITY_LOWEST,
- .clear_bus_init = false
- };
- #endif
- if(initialized)
- {
- nrf_twi_mngr_uninit(&m_app_twi);
- }
-
- if (nrf_twi_mngr_init(&m_app_twi, &config) != NRF_SUCCESS)
- {
- return PAL_STATUS_FAILURE;
- }
- initialized = true;
- return PAL_STATUS_SUCCESS;
- }
- pal_status_t pal_i2c_deinit(const pal_i2c_t* p_i2c_context)
- {
- if(initialized) {
- nrf_twi_mngr_uninit(&m_app_twi);
- }
- initialized = false;
- return PAL_STATUS_SUCCESS;
- }
- pal_status_t pal_i2c_write(pal_i2c_t* p_i2c_context,uint8_t* p_data , uint16_t length)
- {
- gp_pal_i2c_current_ctx = p_i2c_context;
- m_transfer.p_data = p_data;
- m_transfer.length = length;
- m_transfer.operation = NRF_TWI_MNGR_WRITE_OP(IFX_I2C_BASE_ADDR);
- m_transfer.flags = 0;
- m_transaction.callback = app_twi_callback;
- m_transaction.number_of_transfers = 1;
- m_transaction.p_required_twi_cfg = NULL;
- m_transaction.p_transfers = &m_transfer;
- m_transaction.p_user_data = (void*) PAL_STATUS_SUCCESS;
- if (nrf_twi_mngr_schedule(&m_app_twi, &m_transaction) != NRF_SUCCESS)
- {
- app_twi_callback(NRF_ERROR_BUSY, 0);
- }
- return PAL_STATUS_SUCCESS;
- }
- pal_status_t pal_i2c_read(pal_i2c_t* p_i2c_context , uint8_t* p_data , uint16_t length)
- {
- gp_pal_i2c_current_ctx = p_i2c_context;
- m_transfer.p_data = p_data;
- m_transfer.length = length;
- m_transfer.operation = NRF_TWI_MNGR_READ_OP(IFX_I2C_BASE_ADDR);
- m_transfer.flags = 0;
- m_transaction.callback = app_twi_callback;
- m_transaction.number_of_transfers = 1;
- m_transaction.p_required_twi_cfg = 0;
- m_transaction.p_transfers = &m_transfer;
- m_transaction.p_user_data = (void*) PAL_STATUS_SUCCESS;
- if (nrf_twi_mngr_schedule(&m_app_twi, &m_transaction) != NRF_SUCCESS)
- {
- app_twi_callback(NRF_ERROR_BUSY, 0);
- }
- return PAL_STATUS_SUCCESS;
- }
- pal_status_t pal_i2c_set_bitrate(const pal_i2c_t* p_i2c_context , uint16_t bitrate)
- {
-
- return PAL_STATUS_SUCCESS;
- }
- #ifdef IFX_2GO_SUPPORT
- pal_status_t pal_i2c_set_instance(nrf_twi_mngr_t* twi_inst)
- {
- m_app_twi = *twi_inst;
- }
- #endif
|