diff --git a/nrf5/hal/hal_twi.c b/nrf5/hal/hal_twi.c index e318d2021f..d71f29d5a8 100644 --- a/nrf5/hal/hal_twi.c +++ b/nrf5/hal/hal_twi.c @@ -35,7 +35,10 @@ static const uint32_t hal_twi_frequency_lookup[] = { TWI_FREQUENCY_FREQUENCY_K400, // 400 kbps }; -void hal_twi_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init) { +void hal_twi_master_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init) { +} + +void hal_twi_slave_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init) { } #endif // HAL_TWI_MODULE_ENABLED diff --git a/nrf5/hal/hal_twi.h b/nrf5/hal/hal_twi.h index 5e2d10c31e..4bc3fc6792 100644 --- a/nrf5/hal/hal_twi.h +++ b/nrf5/hal/hal_twi.h @@ -81,8 +81,11 @@ typedef enum { */ typedef struct { uint8_t id; /* TWI instance id */ + const pin_obj_t * scl_pin; /* TWI SCL pin */ + const pin_obj_t * sda_pin; /* TWI SDA pin */ hal_twi_role_t role; /* TWI master/slave */ hal_twi_clk_freq_t freq; /* TWI frequency */ + uint32_t dev_addr; /* TWI master device address */ } hal_twi_init_t; /** @@ -94,6 +97,9 @@ typedef struct __TWI_HandleTypeDef hal_twi_init_t init; /* TWI initialization parameters */ } TWI_HandleTypeDef; -void hal_twi_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init); +void hal_twi_master_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init); + +void hal_twi_slave_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init); + #endif // HAL_TWI_H__ diff --git a/nrf5/hal/hal_twie.c b/nrf5/hal/hal_twie.c index cc20b2c97d..404bb3efbc 100644 --- a/nrf5/hal/hal_twie.c +++ b/nrf5/hal/hal_twie.c @@ -39,7 +39,26 @@ static const uint32_t hal_twi_frequency_lookup[] = { TWIM_FREQUENCY_FREQUENCY_K400, // 400 kbps }; -void hal_twi_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init) { +void hal_twi_master_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init) { + // cast to master type + NRF_TWIM_Type * twim_instance = (NRF_TWIM_Type *)p_instance; + + twim_instance->PSEL.SCL = p_twi_init->scl_pin->pin; + twim_instance->PSEL.SDA = p_twi_init->sda_pin->pin; + +#if NRF52840_XXAA + twim_instance->PSEL.SCL |= (p_twi_init->scl_pin->port << TWIM_PSEL_SCL_PORT_Pos); + twim_instance->PSEL.SDA |= (p_twi_init->sda_pin->port << TWIM_PSEL_SDA_PORT_Pos); +#endif + twim_instance->ADDRESS = p_twi_init->dev_addr; + twim_instance->FREQUENCY = hal_twi_frequency_lookup[p_twi_init->freq]; + +} + +void hal_twi_slave_init(NRF_TWI_Type * p_instance, hal_twi_init_t const * p_twi_init) { + // cast to slave type + NRF_TWIS_Type * twis_instance = (NRF_TWIS_Type *)p_instance; + (void)twis_instance; } #endif // HAL_TWIE_MODULE_ENABLED