2018年7月20日
nRF52832中使用的TWI(Two—wire Serial Interface).TWI是对iic总线接口的继承和发展,完全兼容IIC.详细区别暂时不研究,这里只是为了使用IIC接口而已
只是最简单TWI初始化,没有用SDK中提供的TWI transaction manager及TWI Sensor module.
TWI初始化比较简单,直接nrf_drv_twi_init函数初始化后用nrf_drv_twi_enable使能即可:
/* TWI instance ID. */
#define TWI_INSTANCE_ID 0
/* TWI instance. */
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
ret_code_t err_code;
nrf_drv_twi_config_t p_transfers = {
.scl=TWI_SCL,
.sda=TWI_SDA,
.frequency=NRF_DRV_TWI_FREQ_100K, //100kHz
.interrupt_priority=NRFX_TWIM_DEFAULT_CONFIG_IRQ_PRIORITY,
.clear_bus_init=true,
.hold_bus_uninit=true,
};
err_code = nrf_drv_twi_init(&m_twi, &p_transfers, twi_handler, NULL);
APP_ERROR_CHECK(err_code);
nrf_drv_twi_enable(&m_twi);
需要注意的是,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)
函数中,参数nrf_drv_twi_evt_handler_t event_handler
如果为NULL,则iic会运行为阻塞模式,读写函数完成后才会返回值,如果不为NULL,则调用读写函数后会立刻返回,在回调函数中返回执行结果
iic读写SDK直接封装好了接口函数:nrf_drv_twi_tx
与nrf_drv_twi_rx
,很简单了,不多说了.
__STATIC_INLINE ret_code_t nrf_drv_twi_rx(nrf_drv_twi_t const * p_instance,uint8_t address,uint8_t * p_data,uint8_t length)
__STATIC_INLINE ret_code_t nrf_drv_twi_tx(nrf_drv_twi_t const * p_instance,uint8_t address,uint8_t const * p_data,uint8_t length,bool no_stop)
注意sdk_config.h的配置