SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
sensor_bhi260.cpp
Go to the documentation of this file.
1
30#include "sdkconfig.h"
31#include "esp_log.h"
32#include "esp_err.h"
33#include "freertos/FreeRTOS.h"
34#include "driver/spi_master.h"
35#include "driver/gpio.h"
36#include <ImuDrv.hpp>
37
38
39#if CONFIG_BHI260
40
41
42// The firmware runs in RAM and will be lost if the power is off. The firmware will be loaded from RAM each time it is run.
43#define BOSCH_APP30_SHUTTLE_BHI260_FW
44// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150FW
45// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X
46// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390
47// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO
48// #define BOSCH_BHI260_AUX_BEM280
49// #define BOSCH_BHI260_AUX_BMM150_BEM280
50// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO
51// #define BOSCH_BHI260_AUX_BMM150_GPIO
52// #define BOSCH_BHI260_GPIO
53
54// Firmware is stored in flash and booted from flash,Depends on BHI260 hardware connected to SPI Flash
55// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150_FLASH
56// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X_FLASH
57// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390_FLASH
58// #define BOSCH_APP30_SHUTTLE_BHI260_FLASH
59// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO_FLASH
60// #define BOSCH_BHI260_AUX_BEM280_FLASH
61// #define BOSCH_BHI260_AUX_BMM150_BEM280_FLASH
62// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO_FLASH
63// #define BOSCH_BHI260_AUX_BMM150_GPIO_FLASH
64// #define BOSCH_BHI260_GPIO_FLASH
65
66#include <BoschFirmware.h>
67
68static const char *TAG = "BHI";
69
70extern uint32_t hal_callback(SensorCommCustomHal::Operation op, void *param1, void *param2);
71
73
74static volatile bool dataReady = false;
75
76static bool init_done = false;
77
78#if CONFIG_SENSOR_IRQ != -1
79static void IRAM_ATTR bhi260_set_flag(void *arg)
80{
81 dataReady = true;
82}
83#endif
84
85static void bhi260_isr_init()
86{
87#if CONFIG_SENSOR_IRQ != -1
88 gpio_config_t io_conf;
89 io_conf.intr_type = GPIO_INTR_NEGEDGE;
90 io_conf.mode = GPIO_MODE_INPUT;
91 io_conf.pin_bit_mask = (1ULL << CONFIG_SENSOR_IRQ);
92 io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
93 io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
94 gpio_config(&io_conf);
95 gpio_set_intr_type((gpio_num_t)CONFIG_SENSOR_IRQ, GPIO_INTR_POSEDGE);
96 //install gpio isr service
97 gpio_install_isr_service(0);
98 //hook isr handler for specific gpio pin
99 gpio_isr_handler_add((gpio_num_t)CONFIG_SENSOR_IRQ, bhi260_set_flag, NULL);
100#endif
101}
102
103static void accel_process_callback(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
104{
105 struct bhy2_data_xyz data;
106 float scaling_factor = BoschSensorUtils::get_sensor_default_scaling(sensor_id);
107 bhy2_parse_xyz(data_ptr, &data);
108 ESP_LOGI(TAG, "%s: x: %f, y: %f, z: %f;", bhy.getSensorName(sensor_id),
109 data.x * scaling_factor,
110 data.y * scaling_factor,
111 data.z * scaling_factor);
112}
113
114static void gyro_process_callback(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
115{
116 struct bhy2_data_xyz data;
117 float scaling_factor = BoschSensorUtils::get_sensor_default_scaling(sensor_id);
118 bhy2_parse_xyz(data_ptr, &data);
119 ESP_LOGI(TAG, "%s: x: %f, y: %f, z: %f;", bhy.getSensorName(sensor_id),
120 data.x * scaling_factor,
121 data.y * scaling_factor,
122 data.z * scaling_factor);
123}
124
125esp_err_t bhi260_init()
126{
127 ESP_LOGI(TAG, "----DRIVER BHI260AP----");
128
129 // Set the reset pin
130 bhy.setPins(CONFIG_SENSOR_RST);
131
132#if CONFIG_USE_I2C_INTERFACE
133 // BHI260AP_SLAVE_ADDRESS_L = 0x28
134 // BHI260AP_SLAVE_ADDRESS_H = 0x29
135 uint8_t address = BHI260AP_SLAVE_ADDRESS_L;
136
137 //* Implemented using read and write callback methods, applicable to other platforms
138#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
139
140 // * Provide the device address to the callback function
141 i2c_drv_device_init(address);
142
143 ESP_LOGI(TAG, "Implemented using read and write callback methods");
144 if (bhy.begin(i2c_wr_function, hal_callback, address)) {
145 ESP_LOGI(TAG, "Initialize BHI260AP using I2C interface");
146 } else {
147 ESP_LOGE(TAG, "Failed to initialize the BHI260AP !");
148 return ESP_FAIL;
149 }
150#endif
151
152 //* Use the built-in esp-idf communication method
153#if CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
154#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
155
156 ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use higher version >= 5.0 API)");
157
158 // * Using the new API of esp-idf 5.x, you need to pass the I2C BUS handle,
159 // * which is useful when the bus shares multiple devices.
160 extern i2c_master_bus_handle_t bus_handle;
161
162 if (bhy.begin(bus_handle, address)) {
163 ESP_LOGI(TAG, "Initialize BHI260AP using I2C interface");
164 } else {
165 ESP_LOGE(TAG, "Failed to initialize the BHI260AP !");
166 return ESP_FAIL;
167 }
168
169#else
170
171 ESP_LOGI(TAG, "Implemented using built-in read and write methods (Use lower version < 5.0 API)");
172 if (bhy.begin((i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM, address, CONFIG_SENSOR_SDA, CONFIG_SENSOR_SCL)) {
173 ESP_LOGI(TAG, "Initialize BHI260AP using I2C interface");
174 } else {
175 ESP_LOGE(TAG, "Failed to initialize the BHI260AP !");
176 return ESP_FAIL;
177 }
178#endif //ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)
179#endif //CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW
180
181
182#else //USE_I2C_INTERFACE
183
184 // Bus handle
185 spi_device_handle_t spi = NULL;
186
187 // Using SPI interface
188 if (bhy.begin(SPI2_HOST, spi, CONFIG_SPI_CS, CONFIG_SPI_MOSI, CONFIG_SPI_MISO, CONFIG_SPI_SCK)) {
189 ESP_LOGI(TAG, "Initialize BHI260AP using SPI interface");
190 } else {
191 ESP_LOGE(TAG, "Failed to initialize the BHI260AP !");
192 return ESP_FAIL;
193 }
194#endif //USE_SPI_INTERFACE
195
196 // Output all sensors info to Serial
197 BoschSensorInfo info = bhy.getSensorInfo();
198 info.printInfo();
199
200 float sample_rate = 1.0; /* Read out hintr_ctrl measured at 1Hz */
201 uint32_t report_latency_ms = 0; /* Report immediately */
202
203 // Enable acceleration
204 bhy.configure(BoschSensorID::ACCEL_PASSTHROUGH, sample_rate, report_latency_ms);
205 // Enable gyroscope
206 bhy.configure(BoschSensorID::GYRO_PASSTHROUGH, sample_rate, report_latency_ms);
207
208 // Set the acceleration sensor result callback function
209 bhy.onResultEvent(BoschSensorID::ACCEL_PASSTHROUGH, accel_process_callback);
210
211 // Set the gyroscope sensor result callback function
212 bhy.onResultEvent(BoschSensorID::GYRO_PASSTHROUGH, gyro_process_callback);
213
214 // Registration interruption
215 bhi260_isr_init();
216
217 init_done = true;
218
219 return ESP_OK;
220}
221
222void bhi260_loop()
223{
224 if (!init_done) {
225 return;
226 }
227#if CONFIG_SENSOR_IRQ != -1
228 if (dataReady) {
229 dataReady = false;
230#endif
231 bhy.update();
232#if CONFIG_SENSOR_IRQ != -1
233 }
234#endif
235}
236
237#endif
@license MIT License
@license MIT License
SensorBHI260AP bhy
uint32_t hal_callback(SensorCommCustomHal::Operation op, void *param1, void *param2)
bool i2c_wr_function(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len, bool writeReg, bool isWrite)