I am using the following git repositry and trying to get data from accelerometer using accelerometer_remapping_example.
I have connected the board with raspberry Pi Pico and ported the library as specified in the doc folder of the repositry.
After doing the above step I am successfully able to initialize BHI160 using function
But when i run the below code I don't get any accelerometer data.
#define FIFO_SIZE 69
#define GESTURE_SAMPLE_RATE 1
#define OUT_BUFFER_SIZE 80
#define MAX_PACKET_LENGTH 18
#define TICKS_IN_ONE_SECOND 32000.0F
#define SENSOR_ID_MASK 0x1F
/* system timestamp */
uint32_t bhy_timestamp = 0;
uint8_t fifo[FIFO_SIZE];
float time_stamp = 0;
uint8_t sensor_type = 0;
int16_t x_raw = 0;
int16_t y_raw = 0;
int16_t z_raw = 0;
float x_data = 0;
float y_data = 0;
float z_data = 0;
int newdata = 0;
int intrtoggled = 0;
/********************************************************************************/
/* FUNCTIONS */
/********************************************************************************/
/*!
* @brief This function is time stamp callback function that process data in fifo.
*
* @param[in] new_timestamp
*/
static void timestamp_callback(bhy_data_scalar_u16_t *new_timestamp)
{
/* updates the system timestamp */
bhy_update_system_timestamp(new_timestamp, &bhy_timestamp);
}
/*!
* @brief This function is callback function for acquring sensor datas
*
* @param[in] sensor_data
* @param[in] sensor_id
*/
static void sensors_callback_acc(bhy_data_generic_t * sensor_data, bhy_virtual_sensor_t sensor_id)
{
/* Since a timestamp is always sent before every new data, and that the callbacks */
/* are called while the parsing is done, then the system timestamp is always equal */
/* to the sample timestamp. (in callback mode only) */
time_stamp = (float)(bhy_timestamp) / TICKS_IN_ONE_SECOND;
printf("sensor_id = %d\n", sensor_id);
switch(sensor_id)
{
case VS_ID_ACCELEROMETER_WAKEUP:
case VS_ID_ACCELEROMETER:
x_raw = sensor_data->data_vector.x;
y_raw = sensor_data->data_vector.y;
z_raw = sensor_data->data_vector.z;
/* The resolution is 15bit ,the default range is 4g, actual acceleration equals: raw_data/(exp(2,15) == 32768) */
x_data = (float)x_raw / 32768.0f * 4.0f;
y_data = (float)y_raw / 32768.0f * 4.0f;
z_data = (float)z_raw / 32768.0f * 4.0f;
printf("Time:%6.3fs acc %f %f %f\n", time_stamp, x_data, y_data, z_data);
break;
default:
printf("unknown id = %d\n", sensor_id);
break;
}
newdata = 1;
}
int main(void)
{
//Code for checking BHI160B
stdio_init_all();
i2c_init(i2c_default,400*1000);
gpio_set_function(PICO_DEFAULT_I2C_SDA_PIN,GPIO_FUNC_I2C);
gpio_set_function(PICO_DEFAULT_I2C_SCL_PIN,GPIO_FUNC_I2C);
gpio_pull_up(PICO_DEFAULT_I2C_SDA_PIN);
gpio_pull_up(PICO_DEFAULT_I2C_SCL_PIN);
gpio_init(10);
gpio_set_dir(10,0);
int8_t ret;
/* BHY Variable*/
uint8_t *fifoptr = NULL;
uint8_t bytes_left_in_fifo = 0;
uint16_t bytes_remaining = 0;
uint16_t bytes_read = 0;
bhy_data_generic_t fifo_packet;
bhy_data_type_t packet_type;
BHY_RETURN_FUNCTION_TYPE result;
int8_t bhy_mapping_matrix_init[3*3] = {0};
int8_t bhy_mapping_matrix_config[3*3] = {0,1,0,-1,0,0,0,0,1};
/* To get the customized version number in firmware, it is necessary to read Parameter Page 2, index 125 */
/* to get this information. This feature is only supported for customized firmware. To get this customized */
/* firmware, you need to contact your local FAE of Bosch Sensortec. */
//struct cus_version_t bhy_cus_version;
//gpio_set_irq_enabled_with_callback(10,GPIO_IRQ_EDGE_RISE,true,&int_callback);
sleep_ms(20000);
/* init the bhy chip */
if(bhy_driver_init(bhy1_fw)){
printf("Fail to init bhy\n");
}
//intrtoggled = 0;
//waitForBhyInterrupt();
/* while(1){
printf("HELLO \n");
//int aval = gpio_get(10);
//printf("INT PIN STATE = %d \n",aval);
//bhy_driver_init(&bhy1_fw);
} */
/* wait for the bhy trigger the interrupt pin go down and up again */
/* while (gpio_get(10)){
printf("i am here \n");
}//;
while (!gpio_get(10)){
printf("i am there \n");
}//; */
sleep_ms(10000);
bhy_mapping_matrix_get(PHYSICAL_SENSOR_INDEX_ACC, bhy_mapping_matrix_init);
bhy_mapping_matrix_set(PHYSICAL_SENSOR_INDEX_ACC, bhy_mapping_matrix_config);
bhy_mapping_matrix_get(PHYSICAL_SENSOR_INDEX_ACC, bhy_mapping_matrix_init);
/* install time stamp callback */
bhy_install_timestamp_callback(VS_WAKEUP, timestamp_callback);
bhy_install_timestamp_callback(VS_NON_WAKEUP, timestamp_callback);
if(bhy_install_sensor_callback(VS_TYPE_ACCELEROMETER, VS_WAKEUP, sensors_callback_acc))
{
printf("Fail to install sensor callback\n");
}
else{printf("calbck initialized \n");}
sleep_ms(100);
if(bhy_enable_virtual_sensor(VS_TYPE_ACCELEROMETER, VS_WAKEUP, 10, 0, VS_FLUSH_NONE, 0, 0))
{
printf("Fail to enable sensor id=%d\n", VS_TYPE_ACCELEROMETER);
}
else{printf("sens enabled \n");}
/* enables the virtual sensor */
sleep_ms(10000);
/* continuously read and parse the fifo */
/* while(1){
sensors_callback_acc(&fifo_packet,VS_TYPE_ACCELEROMETER);
} */
while(1)
{
/* wait until the interrupt fires */
/* unless we already know there are bytes remaining in the fifo */
//printf("HELLO \n");
//if(intrtoggled){
//intrtoggled = 0;
//while (!gpio_get(10) && !bytes_remaining);
//printf("HELLO \n");
result = bhy_read_fifo(fifo + bytes_left_in_fifo, FIFO_SIZE - bytes_left_in_fifo, &bytes_read, &bytes_remaining);
bytes_read += bytes_left_in_fifo;
fifoptr = fifo;
packet_type = BHY_DATA_TYPE_PADDING;
//packet_type = BHY_DATA_TYPE_VECTOR;
//printf("Bytes read = %d \n",bytes_read);
do
{
/* this function will call callbacks that are registered */
result = bhy_parse_next_fifo_packet(&fifoptr, &bytes_read, &fifo_packet, &packet_type);
//sensors_callback_acc(&fifo_packet,VS_TYPE_ACCELEROMETER);
/* prints all the debug packets */
//if (packet_type == BHY_DATA_TYPE_DEBUG)
if (packet_type == BHY_DATA_TYPE_DEBUG)
{
//bhy_print_vector_packet(&fifo_packet.data_vector);
bhy_print_debug_packet(&fifo_packet.data_debug, bhy_printf);
}
//if(newdata){
//printf("Time:%6.3fs acc %f %f %f\n", time_stamp, x_data, y_data, z_data);
//}
/* the logic here is that if doing a partial parsing of the fifo, then we should not parse */
/* the last 18 bytes (max length of a packet) so that we don't try to parse an incomplete */
/* packet */
} while ((result == BHY_SUCCESS) && (bytes_read > (bytes_remaining ? MAX_PACKET_LENGTH : 0)));
bytes_left_in_fifo = 0;
if (bytes_remaining)
{
/* shifts the remaining bytes to the beginning of the buffer */
while (bytes_left_in_fifo < bytes_read)
{
fifo[bytes_left_in_fifo++] = *(fifoptr++);
}
}
//}
}
}
Please help ASAP to solve this issue