Hi,
yesterday, I met the same situation as here. But now, I can't even see one log, even after I annotated the code related twi and just remained some “NRF_LOG_INFO()” in main function.
I was confused about that. I've tried all the ways that I could, I think. I use the "twi_sensor " example, in SDK 15.0 and no soft devcie.
please help
Code as below.
int main(void)
{
//uint8_t id;
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_DEFAULT_BACKENDS_INIT();
NRF_LOG_INFO("\r\nTWI sensor example started.");
nrf_gpio_cfg_output(22);
nrf_gpio_pin_set(22);
// twi_init();
// ICM_init();
//// NRF_LOG_INFO("icm_init and id is%x\n",id);
//// NRF_LOG_INFO("icm_init");
// data_conversion();
while (true)
{
NRF_LOG_INFO("icm_init");
nrf_delay_ms(50);
}
}
code related twi:
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(m_sample);
NRF_LOG_INFO("received complete");
}
m_xfer_done = true;
break;
default:
break;
}
}
void twi_init (void)
{
ret_code_t err_code;
const nrf_drv_twi_config_t twi_icm_config = {
.scl = ARDUINO_SCL_PIN,
.sda = ARDUINO_SDA_PIN,
.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_icm_config, twi_handler, NULL);
APP_ERROR_CHECK(err_code);
nrf_drv_twi_enable(&m_twi);
}
code related sensor:(the sensor is likely mpu's upgraded version )
#include "icm20600.h"
static uint8_t tx_buffer[6];
static uint8_t rx_buffer[6];
uint8_t *m_tx_buf=tx_buffer;
uint8_t *m_rx_buf=rx_buffer;
uint8_t H_rx_buf[1];
uint8_t L_rx_buf[1];
float icm_acc_x=1.0;
int16_t icm_acc_x_hex=1;
int16_t x_data=1;
void ICM_write(uint8_t *tx_data,uint8_t tx_length)
{
m_xfer_done = false;
ret_code_t err_code;
err_code = nrf_drv_twi_tx(&m_twi, i2c_address, tx_data,tx_length, 0);
while (m_xfer_done == false);
APP_ERROR_CHECK(err_code);
//while (m_xfer_done == false);
}
void ICM_read(uint8_t *rx_data,uint8_t rx_length)
{
m_xfer_done = false;
ret_code_t err_code;
// err_code = nrf_drv_twi_tx(&m_twi, i2c_address,&addr,1, 1);
// APP_ERROR_CHECK(err_code);
// while (m_xfer_done == false);
err_code = nrf_drv_twi_rx(&m_twi, i2c_address, rx_data,rx_length);
while (m_xfer_done == false);
APP_ERROR_CHECK(err_code);
//while (m_xfer_done == false);
}
/**
* sample_rate set
*/
void ICM_Set_Rate(uint16_t rate)
{
uint8_t div,DLPF_CFG;
uint16_t filter=rate/2;
if(rate>1000)
{rate=1000;}
if (filter >= 176) {
DLPF_CFG = 1;
} else if (filter >= 92) {
DLPF_CFG = 2;
} else if (filter >= 41) {
DLPF_CFG = 3;
} else if (filter >= 20) {
DLPF_CFG = 4;
} else if (filter >= 10) {
DLPF_CFG = 5;
} else {
DLPF_CFG = 6;
}
m_tx_buf[0]=ICM20602_GYRO_CONFIG_REG;
m_tx_buf[1]=DLPF_CFG;
ICM_write(m_tx_buf,2);
div=(1000/rate)-1;
m_tx_buf[0]=ICM20602_SMPLRT_DIV;
m_tx_buf[1]=div;
ICM_write(m_tx_buf,2);
}
/*accelerometer_threshold*/
uint8_t set_accelerometer_threshold(uint8_t threshold)
{
uint8_t accl_val;
if(threshold > 100){
return 1;
}
uint16_t val_1= threshold * 0xFF;
accl_val = (uint8_t)(val_1 / 100);
m_tx_buf[0]=ICM20602_LACCEL_WOM_X_THR;
m_tx_buf[1]=accl_val;
m_tx_buf[2]=ICM20602_LACCEL_WOM_Y_THR;
m_tx_buf[3]=accl_val;
m_tx_buf[4]=ICM20602_LACCEL_WOM_Z_THR;
m_tx_buf[5]=accl_val;
ICM_write(m_tx_buf,6);
return 0;
}
/*****************************sensor_init****************************
*
*/
void ICM_init()
{
uint8_t ICM_ID,id;
/*reset sensor*/
m_tx_buf[0]=ICM20602_PWR_MGMT1_REG;
m_tx_buf[1]=0x80;
ICM_write(m_tx_buf,2);
nrf_delay_ms(100);
/*reset all registers*/
m_tx_buf[0]=ICM20602_USER_CTRL;
m_tx_buf[1]=0x01;
ICM_write(m_tx_buf,2);
nrf_delay_ms(100);
m_tx_buf[0]=ICM20602_USER_CTRL;
m_tx_buf[1]=0x00;
ICM_write(m_tx_buf,2);
/*device id judgment */
m_tx_buf[0]=ICM20602_WHO_AM_I;
ICM_write(m_tx_buf,1);
ICM_read(m_rx_buf,1);
ICM_ID=*m_rx_buf;
if(ICM_ID){NRF_LOG_INFO("ICM_ID is:%x\n",ICM_ID);}
else {NRF_LOG_INFO("ICM_ID false");}
//return ICM_ID;
if(ICM_ID==0x12)
{
m_tx_buf[0]=ICM20602_GYRO_CONFIG_REG;
m_tx_buf[1]=0x18;
m_tx_buf[2]=ICM20602_ACCEL_CONFIG1_REG;
m_tx_buf[3]=0;
ICM_write(m_tx_buf,4);
ICM_Set_Rate(50);
m_tx_buf[0]=ICM20602_INT_ENABLE_REG;
m_tx_buf[1]=0x00;
m_tx_buf[2]=ICM20602_FIFO_EN;
m_tx_buf[3]=0x00;
m_tx_buf[4]=ICM20602_INT_PIN_CFG;
m_tx_buf[5]=0x80;
/*set clock source*/
m_tx_buf[0]=ICM20602_PWR_MGMT1_REG;
m_tx_buf[1]=0x01; //Auto selects the best available clock source � PLL if ready, else use the Internal oscillator
ICM_write(m_tx_buf,2);
while(set_accelerometer_threshold(0x50)==1);
/*start accelerometer*/
m_tx_buf[0]=ICM20602_PWR_MGMT2_REG ;
m_tx_buf[1]=0x00;
ICM_write(m_tx_buf,2);
NRF_LOG_INFO("ICM initalize success");
}
else
{
NRF_LOG_INFO("ICM initalize failed");
}
}
/**************************Accelerometer read *****************************/
int16_t ICM_Get_X_Accelerometer()
{
uint8_t XOUT_H,XOUT_L;
m_tx_buf[0]=ICM20602_ACCEL_XOUT_H ;
ICM_write(m_tx_buf,1);
ICM_read(H_rx_buf,1);
m_tx_buf[0]=ICM20602_ACCEL_XOUT_L ;
ICM_write(m_tx_buf,1);
ICM_read(L_rx_buf,1);
x_data=(int16_t)((uint16_t)((H_rx_buf[0])<<8)|(L_rx_buf[0]));
return x_data;
}
void data_conversion()
{
icm_acc_x_hex=ICM_Get_X_Accelerometer();
icm_acc_x=(float)icm_acc_x_hex/16384;
NRF_LOG_INFO("icm_acc_x:%x\n",icm_acc_x_hex);
}