SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_interrupt_settings.ino
Go to the documentation of this file.
1
30#include <Wire.h>
31#include <SPI.h>
32#include <Arduino.h>
33#include <ImuDrv.hpp>
34
35
36// #define USE_I2C_INTERFACE true
37// #define USE_SPI_INTERFACE true
38
39#if !defined(USE_I2C_INTERFACE) && !defined(USE_SPI_INTERFACE)
40#define USE_I2C_INTERFACE
41#warning "No interface type is selected, use I2C interface"
42#endif
43
44#if defined(USE_SPI_INTERFACE)
45#ifndef SPI_MOSI
46#define SPI_MOSI 33
47#endif
48
49#ifndef SPI_MISO
50#define SPI_MISO 34
51#endif
52
53#ifndef SPI_SCK
54#define SPI_SCK 35
55#endif
56
57// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
58#ifndef BHI260_IRQ
59#define BHI260_IRQ 37
60#endif
61
62#ifndef BHI260_CS
63#define BHI260_CS 36
64#endif
65
66#else //* I2C */
67
68#ifndef BHI260_SDA
69#define BHI260_SDA 2
70#endif
71
72#ifndef BHI260_SCL
73#define BHI260_SCL 3
74#endif
75
76// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
77#ifndef BHI260_IRQ
78#define BHI260_IRQ 8
79#endif
80#endif /*USE_SPI_INTERFACE*/
81
82#ifndef BHI260_RST
83#define BHI260_RST -1
84#endif
85
87SensorAcceleration accel(bhy);
88SensorGyroscope gyro(bhy);
89
90// 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.
91#define BOSCH_APP30_SHUTTLE_BHI260_FW
92// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150FW
93// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X
94// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390
95// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO
96// #define BOSCH_BHI260_AUX_BEM280
97// #define BOSCH_BHI260_AUX_BMM150_BEM280
98// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO
99// #define BOSCH_BHI260_AUX_BMM150_GPIO
100// #define BOSCH_BHI260_GPIO
101
102// Firmware is stored in flash and booted from flash,Depends on BHI260 hardware connected to SPI Flash
103// #define BOSCH_APP30_SHUTTLE_BHI260_AUX_BMM150_FLASH
104// #define BOSCH_APP30_SHUTTLE_BHI260_BME68X_FLASH
105// #define BOSCH_APP30_SHUTTLE_BHI260_BMP390_FLASH
106// #define BOSCH_APP30_SHUTTLE_BHI260_FLASH
107// #define BOSCH_APP30_SHUTTLE_BHI260_TURBO_FLASH
108// #define BOSCH_BHI260_AUX_BEM280_FLASH
109// #define BOSCH_BHI260_AUX_BMM150_BEM280_FLASH
110// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO_FLASH
111// #define BOSCH_BHI260_AUX_BMM150_GPIO_FLASH
112// #define BOSCH_BHI260_GPIO_FLASH
113
114#include <BoschFirmware.h>
115
116// Force update of current firmware, whether it exists or not.
117// Only works when external SPI Flash is connected to BHI260.
118// After uploading firmware once, you can change this to false to speed up boot time.
120
121#if BHI260_IRQ > 0
122#define USING_SENSOR_IRQ_METHOD
123#endif
124
125#ifdef USING_SENSOR_IRQ_METHOD
126volatile bool isInterruptTriggered = false;
127
129{
131}
132#endif /*USING_SENSOR_IRQ_METHOD*/
133
134
135// Firmware update progress callback
136void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
137{
138 float progress = (float)transferred / total * 100;
139 Serial.print("Upload progress: ");
140 Serial.print(progress);
141 Serial.println("%");
142}
143
144
145void setup()
146{
147 Serial.begin(115200);
148 while (!Serial);
149
150 // Set the reset pin
151 bhy.setPins(BHI260_RST);
152
153 Serial.println("Initializing Sensors...");
154
155 // Set the firmware array address and firmware size
156 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size, bosch_firmware_type, force_update_flash_firmware);
157
158 // Set the firmware update processing progress callback function
159 // bhy.setUpdateProcessCallback(progress_callback, NULL);
160
161 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
162 // bhy.setMaxiTransferSize(256);
163
164 // Set the processing fifo data buffer size,The default size is 512 bytes.
165 // bhy.setProcessBufferSize(1024);
166
167 // Set to load firmware from flash
168 bhy.setBootFromFlash(bosch_firmware_type);
169
170#ifdef USE_I2C_INTERFACE
171 // Using I2C interface
172 // BHI260AP_SLAVE_ADDRESS_L = 0x28
173 // BHI260AP_SLAVE_ADDRESS_H = 0x29
174 if (!bhy.begin(Wire, BHI260AP_SLAVE_ADDRESS_L, BHI260_SDA, BHI260_SCL)) {
175 Serial.print("Failed to initialize sensor - error code:");
176 Serial.println(bhy.getError());
177 while (1) {
178 delay(1000);
179 }
180 }
181#endif
182
183#ifdef USE_SPI_INTERFACE
184 // Using SPI interface
185 if (!bhy.begin(SPI, BHI260_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
186 Serial.print("Failed to initialize sensor - error code:");
187 Serial.println(bhy.getError());
188 while (1) {
189 delay(1000);
190 }
191 }
192#endif
193
194 Serial.println("Initializing the sensor successfully!");
195
196 // Output all sensors info to Serial
197 BoschSensorInfo info = bhy.getSensorInfo();
198
199#ifdef ARDUINO
200 ArduinoStreamPrinter printer(Serial);
201 info.printInfo(printer);
202#else
203 info.printInfo([](const char *format, ...) -> int {
204 va_list args;
205 va_start(args, format);
206 int result = vprintf(format, args);
207 va_end(args);
208 return result;
209 });
210#endif
211
212
213 // Interrupt configuration
214 InterruptConfig config;
215 config.wakeUpFIFOEnabled = false;
216 config.nonWakeUpFIFOEnabled = true; // It must be enabled, otherwise FIFO cannot be updated.
217 config.faultEnabled = false;
218 config.debuggingEnabled = false;
219 config.polarity = InterruptConfig::Polarity::ACTIVE_HIGH;
220 config.triggerMode = InterruptConfig::TriggerMode::EDGE;
221 bhy.configureInterrupt(config);
222
223 uint8_t regValue = bhy.getInterruptRegisterValue();
224 String desc;
225 desc += "Register Value: 0x" + String(regValue, HEX) + "\n";
226 desc += "Bit 0 - Wake-up FIFO: " + String((regValue & (1 << 0)) ? "MASKED" : "ACTIVE") + "\n";
227 desc += "Bit 1 - Non-Wake FIFO: " + String((regValue & (1 << 1)) ? "MASKED" : "ACTIVE") + "\n";
228 desc += "Bit 2 - Status FIFO: " + String((regValue & (1 << 2)) ? "MASKED" : "ACTIVE") + "\n";
229 desc += "Bit 3 - Debug: " + String((regValue & (1 << 3)) ? "MASKED" : "ACTIVE") + "\n";
230 desc += "Bit 4 - Fault: " + String((regValue & (1 << 4)) ? "MASKED" : "ACTIVE") + "\n";
231 desc += "Bit 5 - Polarity: " + String((regValue & (1 << 5)) ? "ACTIVE_LOW" : "ACTIVE_HIGH") + "\n";
232 desc += "Bit 6 - Trigger: " + String((regValue & (1 << 6)) ? "EDGE" : "LEVEL") + "\n";
233 desc += "Bit 7 - Pin Mode: " + String((regValue & (1 << 7)) ? "OPEN_DRAIN" : "PUSH_PULL");
234
235 /*
236 Register Value: 0x59
237 Bit 0 - Wake-up FIFO: MASKED
238 Bit 1 - Non-Wake FIFO: ACTIVE
239 Bit 2 - Status FIFO: ACTIVE
240 Bit 3 - Debug: MASKED
241 Bit 4 - Fault: MASKED
242 Bit 5 - Polarity: ACTIVE_HIGH
243 Bit 6 - Trigger: EDGE
244 Bit 7 - Pin Mode: PUSH_PULL
245 */
246 Serial.println(desc);
247
248
249 float sample_rate = 1.0; /* Read out data measured at 1Hz */
250 uint32_t report_latency_ms = 0; /* Report immediately */
251
252 // Enable acceleration
253 accel.enable(sample_rate, report_latency_ms);
254 // Enable gyroscope
255 gyro.enable(sample_rate, report_latency_ms);
256
257#ifdef USING_SENSOR_IRQ_METHOD
258 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
259 // This makes the pin ready to receive external signals.
260 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
261 pinMode(BHI260_IRQ, INPUT);
262
263 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
264 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
265 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
266#endif
267}
268
269
270void loop()
271{
272#ifdef USING_SENSOR_IRQ_METHOD
274 isInterruptTriggered = false;
275#endif /*USING_SENSOR_IRQ_METHOD*/
276
277 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
278 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
279 */
280 bhy.update();
281
282#ifdef USING_SENSOR_IRQ_METHOD
283 }
284#endif /*USING_SENSOR_IRQ_METHOD*/
285
286
287 uint32_t s;
288 uint32_t ns;
289 AccelerometerData accel_data;
290 if (accel.readData(accel_data)) {
291 accel.getLastTime(s, ns);
292#ifdef PLATFORM_HAS_PRINTF
293 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] [AX:%+7.2f AY:%+7.2f AZ:%+7.2f] \n",
294 s, ns, accel_data.mps2.x, accel_data.mps2.y, accel_data.mps2.z);
295#else
296 Serial.print("[T: ");
297 Serial.print(s);
298 Serial.print(".");
299 Serial.print(ns);
300 Serial.print("] [AX:");
301 Serial.print(accel_data.mps2.x, 2);
302 Serial.print(" AY:");
303 Serial.print(accel_data.mps2.y, 2);
304 Serial.print(" AZ:");
305 Serial.print(accel_data.mps2.z, 2);
306 Serial.println("]");
307#endif
308 }
309 GyroscopeData gyro_data;
310 if (gyro.readData(gyro_data)) {
311 gyro.getLastTime(s, ns);
312#ifdef PLATFORM_HAS_PRINTF
313 Serial.printf("[T: %" PRIu32 ".%09" PRIu32 "] [GX:%+7.2f GY:%+7.2f GZ:%+7.2f] \n",
314 s, ns, gyro_data.dps.x, gyro_data.dps.y, gyro_data.dps.z);
315#else
316 Serial.print("[T: ");
317 Serial.print(s);
318 Serial.print(".");
319 Serial.print(ns);
320 Serial.print("] [GX:");
321 Serial.print(gyro_data.dps.x, 2);
322 Serial.print(" GY:");
323 Serial.print(gyro_data.dps.y, 2);
324 Serial.print(" GZ:");
325 Serial.print(gyro_data.dps.z, 2);
326 Serial.println("]");
327#endif
328 }
329 delay(50);
330}
331
332
333
@license MIT License
@license MIT License
SensorBHI260AP bhy
volatile bool isInterruptTriggered
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
bool force_update_flash_firmware
void setBootFromFlash(bool boot_from_flash)
setBootFromFlash
IMUdata gyro[buffer_size]
IMUdata accel[buffer_size]
Structure representing accelerometer data.
SensorVector mps2
Acceleration in meters per second squared (m/s²)
Structure representing gyroscope data.
SensorVector dps
Angular velocity in degrees per second (°/s)
float x
X-axis component.
float y
Y-axis component.
float z
Z-axis component.