Hi,
I have been trying to read the register values, from BNO_080, using the functions nrf_drv_twi_tx to write and nrf_drv_twi_rx to read. But for some reason, i have been getting a constant value as the output, when i try to see them on the serial monitor. I have attached my code below. I have put a flag after every function, but for some reason, it does not go into the flag , the one after the read_sensor_data() in the int main().
Any information why?
#define SHTP_REPORT_COMMAND_RESPONSE 0xF1
#define SHTP_REPORT_COMMAND_REQUEST 0xF2
#define SHTP_REPORT_FRS_READ_RESPONSE 0xF3
#define SHTP_REPORT_FRS_READ_REQUEST 0xF4
#define SHTP_REPORT_PRODUCT_ID_RESPONSE 0xF8
#define SHTP_REPORT_PRODUCT_ID_REQUEST 0xF9
#define SHTP_REPORT_BASE_TIMESTAMP 0xFB
#define SHTP_REPORT_SET_FEATURE_COMMAND 0xFD
/* TWI instance ID. */
#define TWI_INSTANCE_ID 0
#define TRANSACTION_QUEUE_SIZE 5
#define BNO_080 0x4B //the BNO_080 address, which gets toggled when we call the tx or rx functions to either 1 or a 0//
#define SENSOR_REPORTID_GAME_ROTATION_VECTOR 0x08 //this is the register data, which we are trying to send out//
#define SENSOR_REPORTID_ACCELEROMETER 0x01
#define SENSOR_REPORTID_GYROSCOPE 0x02
#define SENSOR_REPORTID_MAGNETIC_FIELD 0x03
#define SENSOR_REPORTID_LINEAR_ACCELERATION 0x04
#define SENSOR_REPORTID_ROTATION_VECTOR 0x05
#define SENSOR_REPORTID_GRAVITY 0x06
#define SENSOR_REPORTID_GEOMAGNETIC_ROTATION_VECTOR 0x09
#define SENSOR_REPORTID_TAP_DETECTOR 0x10
#define SENSOR_REPORTID_STEP_COUNTER 0x11
#define SENSOR_REPORTID_STABILITY_CLASSIFIER 0x13
#define SENSOR_REPORTID_PERSONAL_ACTIVITY_CLASSIFIER 0x1E
/* Indicates if operation on TWI has ended. */
static volatile bool m_xfer_done = false;
/* TWI instance. */
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
uint8_t address = SENSOR_REPORTID_STEP_COUNTER;
void BNO_080_set_mode(void)
{
ret_code_t err_code;
uint8_t reg[2] = {SHTP_REPORT_PRODUCT_ID_REQUEST, 0b00000000};
err_code = nrf_drv_twi_tx(&m_twi, BNO_080, reg, sizeof(reg), false);
APP_ERROR_CHECK(err_code);
while (m_xfer_done == false);
reg[0] = SHTP_REPORT_PRODUCT_ID_RESPONSE;
m_xfer_done = false;
err_code = nrf_drv_twi_tx(&m_twi, BNO_080, reg, 1, false);
APP_ERROR_CHECK(err_code);
while (m_xfer_done == false);
}
__STATIC_INLINE void data_handler(uint8_t address)
{
NRF_LOG_INFO("Data: %d.\r\n", address);
NRF_LOG_FLUSH();
}
/**
* @brief TWI events handler.
*/
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
switch (p_event->type)
{
case NRF_DRV_TWI_EVT_DONE:
if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
{
data_handler(address);
}
m_xfer_done = true;
break;
default:
break;
}
}
/**
* @brief UART initialization.
*/
void twi_init (void)
{
ret_code_t err_code;
const nrf_drv_twi_config_t twi_bno080_config = {
.scl = 27,
.sda = 26,
.frequency = NRF_DRV_TWI_FREQ_400K,
.interrupt_priority = APP_IRQ_PRIORITY_HIGH,
.clear_bus_init = false
};
err_code = nrf_drv_twi_init(&m_twi, &twi_bno080_config, twi_handler, NULL);
APP_ERROR_CHECK(err_code);
nrf_drv_twi_enable(&m_twi);
}
/**
* @brief Function for reading data.
*/
static void read_sensor_data(void)
{
m_xfer_done = false;
ret_code_t err_code = nrf_drv_twi_rx(&m_twi, BNO_080, &address, sizeof(address));
APP_ERROR_CHECK(err_code);
// ret_code_t err_code;
// err_code = nrf_drv_twi_tx(&m_twi, BNO_080, &address, 1, true);
// if (NRF_SUCCESS == err_code)
// err_code = nrf_drv_twi_rx(&m_twi, BNO_080, dataValue, 12, false);
// APP_ERROR_CHECK(err_code);
}
int main(void)
{
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("FLAG 1:\r\n");
NRF_LOG_FLUSH();
twi_init();
NRF_LOG_INFO("Flag 2:\r\n");
NRF_LOG_FLUSH();
BNO_080_set_mode();
NRF_LOG_INFO("Flag 3: \r\n");
NRF_LOG_FLUSH();
while (true)
{
nrf_delay_ms(500);
do
{
__WFE();
}
while (m_xfer_done == false);
read_sensor_data();
NRF_LOG_INFO("Flag4:\r\n")
NRF_LOG_FLUSH();
}
}