int bhi360_bmm350_init(void) { uint8_t product_id = 0; uint16_t version = 0; int8_t rslt; uint8_t work_buffer[WORK_BUFFER_SIZE]; uint8_t hintr_ctrl, hif_ctrl, boot_status; uint8_t accuracy; /* Accuracy is reported as a meta event. It is being printed alongside the data */ #ifdef BHY2_USE_I2C intf = BHY2_I2C_INTERFACE; #else intf = BHY2_SPI_INTERFACE; #endif setup_interfaces(true, intf); /* Perform a power on reset */ #ifdef BHY2_USE_I2C rslt = bhy2_init(BHY2_I2C_INTERFACE, bhy2_i2c_read, bhy2_i2c_write, bhy2_delay_us, BHY2_RD_WR_LEN, NULL, &bhy2); #else rslt = bhy2_init(BHY2_SPI_INTERFACE, bhy2_spi_read, bhy2_spi_write, bhy2_delay_us, BHY2_RD_WR_LEN, NULL, &bhy2); #endif print_api_error(rslt, &bhy2); rslt = bhy2_soft_reset(&bhy2); print_api_error(rslt, &bhy2); rslt = bhy2_soft_reset(&bhy2); print_api_error(rslt, &bhy2); //usr_cfg_delay_us(200); rslt = bhy2_get_product_id(&product_id, &bhy2); print_api_error(rslt, &bhy2); if (product_id != BHY2_PRODUCT_ID) { co_printf("Product ID read %X. Expected %X\r\n", product_id, BHY2_PRODUCT_ID); } else { co_printf("BHI360 found. Product ID read %X\r\n", product_id); } /* Check for a valid product ID */ if (product_id != BHY2_PRODUCT_ID) { co_printf("Product ID read %X. Expected %X\r\n", product_id, BHY2_PRODUCT_ID); } else { co_printf("BHI360 found. Product ID read %X\r\n", product_id); } /* Check the interrupt pin and FIFO configurations. Disable status and debug */ hintr_ctrl = BHY2_ICTL_DISABLE_STATUS_FIFO | BHY2_ICTL_DISABLE_DEBUG; rslt = bhy2_set_host_interrupt_ctrl(hintr_ctrl, &bhy2); print_api_error(rslt, &bhy2); rslt = bhy2_get_host_interrupt_ctrl(&hintr_ctrl, &bhy2); print_api_error(rslt, &bhy2); co_printf("Host interrupt control\r\n"); co_printf(" Wake up FIFO %s.\r\n", (hintr_ctrl & BHY2_ICTL_DISABLE_FIFO_W) ? "disabled" : "enabled"); co_printf(" Non wake up FIFO %s.\r\n", (hintr_ctrl & BHY2_ICTL_DISABLE_FIFO_NW) ? "disabled" : "enabled"); co_printf(" Status FIFO %s.\r\n", (hintr_ctrl & BHY2_ICTL_DISABLE_STATUS_FIFO) ? "disabled" : "enabled"); co_printf(" Debugging %s.\r\n", (hintr_ctrl & BHY2_ICTL_DISABLE_DEBUG) ? "disabled" : "enabled"); co_printf(" Fault %s.\r\n", (hintr_ctrl & BHY2_ICTL_DISABLE_FAULT) ? "disabled" : "enabled"); co_printf(" Interrupt is %s.\r\n", (hintr_ctrl & BHY2_ICTL_ACTIVE_LOW) ? "active low" : "active high"); co_printf(" Interrupt is %s triggered.\r\n", (hintr_ctrl & BHY2_ICTL_EDGE) ? "pulse" : "level"); co_printf(" Interrupt pin drive is %s.\r\n", (hintr_ctrl & BHY2_ICTL_OPEN_DRAIN) ? "open drain" : "push-pull"); /* Configure the host interface */ hif_ctrl = 0; rslt = bhy2_set_host_intf_ctrl(hif_ctrl, &bhy2); print_api_error(rslt, &bhy2); /* Check if the sensor is ready to load firmware */ rslt = bhy2_get_boot_status(&boot_status, &bhy2); print_api_error(rslt, &bhy2); if (boot_status & BHY2_BST_HOST_INTERFACE_READY) { upload_firmware(boot_status, &bhy2); rslt = bhy2_get_kernel_version(&version, &bhy2); print_api_error(rslt, &bhy2); if ((rslt == BHY2_OK) && (version != 0)) { co_printf("Boot successful. Kernel version %u.\r\n", version); } rslt = bhy2_register_fifo_parse_callback(BHY2_SYS_ID_META_EVENT, parse_meta_event, (void*)&accuracy, &bhy2); print_api_error(rslt, &bhy2); rslt = bhy2_register_fifo_parse_callback(BHY2_SYS_ID_META_EVENT_WU, parse_meta_event, (void*)&accuracy, &bhy2); print_api_error(rslt, &bhy2); rslt = bhy2_register_fifo_parse_callback(EULER_SENSOR_ID, parse_euler, (void*)&accuracy, &bhy2); print_api_error(rslt, &bhy2); rslt = bhy2_get_and_process_fifo(work_buffer, WORK_BUFFER_SIZE, &bhy2); print_api_error(rslt, &bhy2); } else { co_printf("Host interface not ready. Exiting\r\n"); close_interfaces(intf); return 0; } /* Update the callback table to enable parsing of sensor data */ rslt = bhy2_update_virtual_sensor_list(&bhy2); print_api_error(rslt, &bhy2); float sample_rate = 100.0; /* Read out data measured at 100Hz */ uint32_t report_latency_ms = 0; /* Report immediately */ #ifdef extra_setting //bhy2_set_calibration_profile(BHY2_PHYS_SENSOR_ID_ACCELEROMETER, acc_calib_prof, acc_prof_len, &bhy2dev); rslt = bhy2_set_virt_sensor_cfg(BHY2_SENSOR_ID_ACC_WU, sample_rate, report_latency_ms, &bhy2); print_api_error(rslt, &bhy2); //PDEBUG("Enable %s at %.2fHz.\r\n", get_sensor_name(BHY2_SENSOR_ID_ACC_WU), sample_rate); struct bhy2_virt_sensor_conf virt_sensor_conf; bhy2_get_virt_sensor_cfg(BHY2_SENSOR_ID_ACC_WU, &virt_sensor_conf, &bhy2); //PDEBUG("BHY2_SENSOR_ID_ACC_WU:range=%u, sample_rate=%f\r\n", virt_sensor_conf.range, virt_sensor_conf.sample_rate); //bhy2_set_calibration_profile(BHY2_PHYS_SENSOR_ID_GYROSCOPE, gyro_calib_prof, gyro_prof_len, &bhy2dev); rslt = bhy2_set_virt_sensor_cfg(BHY2_SENSOR_ID_GYRO_WU, sample_rate, report_latency_ms, &bhy2); print_api_error(rslt, &bhy2); //PDEBUG("Enable %s at %.2fHz.\r\n", get_sensor_name(BHY2_SENSOR_ID_GYRO_WU), sample_rate); bhy2_get_virt_sensor_cfg(BHY2_SENSOR_ID_GYRO_WU, &virt_sensor_conf, &bhy2); //PDEBUG("BHY2_SENSOR_ID_GYRO_WU:range=%u, sample_rate=%f\r\n", virt_sensor_conf.range, virt_sensor_conf.sample_rate); rslt = bhy2_set_virt_sensor_cfg(BHY2_SENSOR_ID_MAG_WU, sample_rate, report_latency_ms, &bhy2); print_api_error(rslt, &bhy2); //PDEBUG("Enable %s at %.2fHz.\r\n", get_sensor_name(BHY2_SENSOR_ID_MAG_WU), sample_rate); rslt = bhy2_set_virt_sensor_cfg(BHY2_SENSOR_ID_RV_WU, sample_rate, report_latency_ms, &bhy2); print_api_error(rslt, &bhy2); //PDEBUG("Enable %s at %.2fHz.\r\n", get_sensor_name(BHY2_SENSOR_ID_RV_WU), sample_rate); #endif rslt = bhy2_set_virt_sensor_cfg(EULER_SENSOR_ID, sample_rate, report_latency_ms, &bhy2); print_api_error(rslt, &bhy2); co_printf("Enable %s at %.2fHz.\r\n", get_sensor_name(EULER_SENSOR_ID), sample_rate); while (rslt == BHY2_OK) { usr_cfg_delay_us(200); /* Data from the FIFO is read and the relevant callbacks if registered are called */ rslt = bhy2_get_and_process_fifo(work_buffer, WORK_BUFFER_SIZE, &bhy2); print_api_error(rslt, &bhy2); } // close_interfaces(intf); return 1; }