Hi, I'm moving form using the driver twi_master (hardware) to nrf_twi/nrf_drv_twi. I can't find any sample code? Does anyone have a nrf_twi example code? Thanks
Hi, I'm moving form using the driver twi_master (hardware) to nrf_twi/nrf_drv_twi. I can't find any sample code? Does anyone have a nrf_twi example code? Thanks
Hi,
I'm afraid there aren't any examples out there yet and the only documentation available is this. Sorry for the inconvenience.
Are there any available examples of how to handle RX event of TWI with this version of TWI driver?
Simplest to start would be to use non-blocking mode (initializing driver with NULL event handler). Code snippet below would first transfer 2 bytes and then continue transaction (xfer_pending=true) expecting to receive 4 bytes. Both calls (rx&tx) are blocking and return only after requested data is transferred. To achieve better performance you can try non-blocking mode where tx&rx functions are returning once transfer is setup. Application is notified about transfer completion by event handler.
uint32_t err_code;
uint8_t tx_data[] = {'a', 'b'};
uint8_t rx_data[4];
const nrf_drv_twi_t twi = NRF_DRV_TWI_INSTANCE(0);
err_code = nrf_drv_twi_init(&twi, NULL, NULL);
APP_ERROR_CHECK(err_code);
nrf_drv_twi_enable(&twi);
err_code = nrf_drv_twi_tx(&twi, SLAVE_ADDRESS, tx_data, sizeof(tx_data), true);
APP_ERROR_CHECK(err_code);
err_code = nrf_drv_twi_rx(&twi, SLAVE_ADDRESS, rx_data, sizeof(rx_data), false);
APP_ERROR_CHECK(err_code);
Non-blocking mode is used if event_handler function is provided during initialization.
void event_handler(nrf_drv_twi_evt_t * p_event)
{
if (p_event->type == NRF_DRV_TWI_RX_DONE){...}
}
Then function nrf_drv_twi_tx and nrf_drv_twi_rx returns immediately and event handler is called in the context of TWI interrupt. Event contains also pointer to the buffer and amount of data transmitted or received.
Thanks for answer Panie Krzysztofie :) But what about non-blocking mode? How to use received data event handler?
Sorry. I miss comment. and I don't know how to delete my comment.