123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- #include "nrf_drv_twi.h"
- #include <nrf_delay.h>
- #include <hal/nrf_gpio.h>
- #ifdef TWIM_PRESENT
- #define INSTANCE_COUNT TWIM_COUNT
- #else
- #define INSTANCE_COUNT TWI_COUNT
- #endif
- #define SCL_PIN_INIT_CONF \
- ( (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
- | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) \
- | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) \
- | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) \
- | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos))
- #define SDA_PIN_INIT_CONF SCL_PIN_INIT_CONF
- #define SDA_PIN_UNINIT_CONF \
- ( (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
- | (GPIO_PIN_CNF_DRIVE_H0H1 << GPIO_PIN_CNF_DRIVE_Pos) \
- | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) \
- | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) \
- | (GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos))
- #define SCL_PIN_UNINIT_CONF SDA_PIN_UNINIT_CONF
- #define SCL_PIN_INIT_CONF_CLR \
- ( (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) \
- | (GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) \
- | (GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) \
- | (GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) \
- | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos))
- #define SDA_PIN_INIT_CONF_CLR SCL_PIN_INIT_CONF_CLR
- static nrf_drv_twi_evt_handler_t m_handlers[INSTANCE_COUNT];
- static void * m_contexts[INSTANCE_COUNT];
- static void twi_clear_bus(nrf_drv_twi_config_t const * p_config)
- {
- NRF_GPIO->PIN_CNF[p_config->scl] = SCL_PIN_INIT_CONF;
- NRF_GPIO->PIN_CNF[p_config->sda] = SDA_PIN_INIT_CONF;
- nrf_gpio_pin_set(p_config->scl);
- nrf_gpio_pin_set(p_config->sda);
- NRF_GPIO->PIN_CNF[p_config->scl] = SCL_PIN_INIT_CONF_CLR;
- NRF_GPIO->PIN_CNF[p_config->sda] = SDA_PIN_INIT_CONF_CLR;
- nrf_delay_us(4);
- for (int i = 0; i < 9; i++)
- {
- if (nrf_gpio_pin_read(p_config->sda))
- {
- if (i == 0)
- {
- return;
- }
- else
- {
- break;
- }
- }
- nrf_gpio_pin_clear(p_config->scl);
- nrf_delay_us(4);
- nrf_gpio_pin_set(p_config->scl);
- nrf_delay_us(4);
- }
- nrf_gpio_pin_clear(p_config->sda);
- nrf_delay_us(4);
- nrf_gpio_pin_set(p_config->sda);
- }
- #ifdef TWIM_PRESENT
- static void twim_evt_handler(nrfx_twim_evt_t const * p_event,
- void * p_context)
- {
- uint32_t inst_idx = (uint32_t)p_context;
- nrf_drv_twi_evt_t const event =
- {
- .type = (nrf_drv_twi_evt_type_t)p_event->type,
- .xfer_desc =
- {
- .type = (nrf_drv_twi_xfer_type_t)p_event->xfer_desc.type,
- .address = p_event->xfer_desc.address,
- .primary_length = p_event->xfer_desc.primary_length,
- .secondary_length = p_event->xfer_desc.secondary_length,
- .p_primary_buf = p_event->xfer_desc.p_primary_buf,
- .p_secondary_buf = p_event->xfer_desc.p_secondary_buf,
- }
- };
- m_handlers[inst_idx](&event, m_contexts[inst_idx]);
- }
- #endif
- #ifdef TWI_PRESENT
- static void twi_evt_handler(nrfx_twi_evt_t const * p_event,
- void * p_context)
- {
- uint32_t inst_idx = (uint32_t)p_context;
- nrf_drv_twi_evt_t const event =
- {
- .type = (nrf_drv_twi_evt_type_t)p_event->type,
- .xfer_desc =
- {
- .type = (nrf_drv_twi_xfer_type_t)p_event->xfer_desc.type,
- .address = p_event->xfer_desc.address,
- .primary_length = p_event->xfer_desc.primary_length,
- .secondary_length = p_event->xfer_desc.secondary_length,
- .p_primary_buf = p_event->xfer_desc.p_primary_buf,
- .p_secondary_buf = p_event->xfer_desc.p_secondary_buf,
- }
- };
- m_handlers[inst_idx](&event, m_contexts[inst_idx]);
- }
- #endif
- ret_code_t nrf_drv_twi_init(nrf_drv_twi_t const * p_instance,
- nrf_drv_twi_config_t const * p_config,
- nrf_drv_twi_evt_handler_t event_handler,
- void * p_context)
- {
- uint32_t inst_idx = p_instance->inst_idx;
- m_handlers[inst_idx] = event_handler;
- m_contexts[inst_idx] = p_context;
- if(p_config->clear_bus_init)
- {
-
- twi_clear_bus(p_config);
- }
- ret_code_t result = 0;
- if (NRF_DRV_TWI_USE_TWIM)
- {
- result = nrfx_twim_init(&p_instance->u.twim,
- (nrfx_twim_config_t const *)p_config,
- event_handler ? twim_evt_handler : NULL,
- (void *)inst_idx);
- }
- else if (NRF_DRV_TWI_USE_TWI)
- {
- result = nrfx_twi_init(&p_instance->u.twi,
- (nrfx_twi_config_t const *)p_config,
- event_handler ? twi_evt_handler : NULL,
- (void *)inst_idx);
- }
- return result;
- }
|