void twi_init (void) {
ret_code_t err_code;
const nrfx_twim_config_t twi_bno085_config = {
.scl = ARDUINO_SCL_PIN,
.sda = ARDUINO_SDA_PIN,
.frequency = NRF_TWIM_FREQ_100K,
.interrupt_priority = APP_IRQ_PRIORITY_HIGH,
.hold_bus_uninit = false,
};
err_code = nrfx_twim_init(&m_twi, &twi_bno085_config, NULL, NULL);
APP_ERROR_CHECK(err_code);
nrfx_twim_enable(&m_twi);
}
static struct {
uint16_t length;
uint8_t channel;
uint8_t sequence_number;
uint8_t data[512];
} bno085_shtp_packet;
bool twi_read_dma(int address, uint8_t *dst, int length) {
if(length < 4) {
NRF_LOG_ERROR("tried to read less than 4 bytes in twi_read_dma (%i)", length);
return false;
}
ret_code_t err_code = nrfx_twim_rx(&m_twi, address, dst, length);
if(err_code != NRFX_SUCCESS) {
NRF_LOG_ERROR("read error is: %s", nrf_strerror_get(err_code));
}
return err_code == NRFX_SUCCESS;
}
bool twi_write_dma(int address, uint8_t *src, int length) {
if(length < 4) {
NRF_LOG_ERROR("tried to write less than 4 bytes in twi_write_dma (%i)", length);
return false;
}
ret_code_t err_code = nrfx_twim_tx(&m_twi, address, src, length, false);
if(err_code != NRFX_SUCCESS) {
NRF_LOG_ERROR("write error is: %s", nrf_strerror_get(err_code));
}
return err_code == NRFX_SUCCESS;
}
#define BNO085_READ(X, N) twi_read_dma(BNO085_ADDR, (X), (N))
#define BNO085_WRITE(X, N) twi_write_dma(BNO085_ADDR, (X), (N))
I'm using the same buffer for read and write, eg.
BNO085_READ((void *)&bno085_shtp_packet, 4)
BNO085_WRITE((void *)&bno085_shtp_packet, length + 4)
Sometimes receive buffer length is zero, sometimes receive is the same as write which means that it didn't correctly transmit. Sometimes it receives a packet which exceeds maximum size.
I'm in the process of switching from a SAM4 to an nRF, and the only thing I've changed is the read/write implementation, so there shouldn't be a problem with the rest of the code.
Has anyone ever gotten it to work with a BNO085 or maybe BNO080?
TWI0_ENABLED 1
TWI0_USE_EASY_DMA 1