SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_gpio.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#include <Commander.h> //Deplib https://github.com/CreativeRobotics/Commander
36Commander cmd;
38
39// #define USE_I2C_INTERFACE true
40// #define USE_SPI_INTERFACE true
41
42#if !defined(USE_I2C_INTERFACE) && !defined(USE_SPI_INTERFACE)
43#define USE_I2C_INTERFACE
44#warning "No interface type is selected, use I2C interface"
45#endif
46
47#if defined(USE_SPI_INTERFACE)
48#ifndef SPI_MOSI
49#define SPI_MOSI 33
50#endif
51
52#ifndef SPI_MISO
53#define SPI_MISO 34
54#endif
55
56#ifndef SPI_SCK
57#define SPI_SCK 35
58#endif
59
60// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
61#ifndef BHI260_IRQ
62#define BHI260_IRQ 37
63#endif
64
65#ifndef BHI260_CS
66#define BHI260_CS 36
67#endif
68
69#else //* I2C */
70
71#ifndef BHI260_SDA
72#define BHI260_SDA 2
73#endif
74
75#ifndef BHI260_SCL
76#define BHI260_SCL 3
77#endif
78
79// If BHI260_IRQ is set to -1, sensor interrupts are not used and the sensor polling method is used instead.
80#ifndef BHI260_IRQ
81#define BHI260_IRQ 8
82#endif
83#endif /*USE_SPI_INTERFACE*/
84
85#ifndef BHI260_RST
86#define BHI260_RST -1
87#endif
88
90
91// 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.
92// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO
93// #define BOSCH_BHI260_AUX_BMM150_GPIO
94#define BOSCH_BHI260_GPIO
95
96// Firmware is stored in flash and booted from flash,Depends on BHI260 hardware connected to SPI Flash
97// #define BOSCH_BHI260_AUX_BMM150_BEM280_GPIO_FLASH
98// #define BOSCH_BHI260_AUX_BMM150_GPIO_FLASH
99// #define BOSCH_BHI260_GPIO_FLASH
100
101#include <BoschFirmware.h>
102
103// Force update of current firmware, whether it exists or not.
104// Only works when external SPI Flash is connected to BHI260.
105// After uploading firmware once, you can change this to false to speed up boot time.
107
108#if BHI260_IRQ > 0
109#define USING_SENSOR_IRQ_METHOD
110#endif
111
112#ifdef USING_SENSOR_IRQ_METHOD
113volatile bool isInterruptTriggered = false;
114
116{
118}
119#endif /*USING_SENSOR_IRQ_METHOD*/
120
121// Firmware update progress callback
122void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
123{
124 float progress = (float)transferred / total * 100;
125 Serial.print("Upload progress: ");
126 Serial.print(progress);
127 Serial.println("%");
128}
129
130void setup()
131{
132 Serial.begin(115200);
133 while (!Serial);
134
135 // Set the reset pin
136 bhy.setPins(BHI260_RST);
137
138 Serial.println("Initializing Sensors...");
139
140 // Set the firmware array address and firmware size
141 bhy.setFirmware(bosch_firmware_image, bosch_firmware_size, bosch_firmware_type, force_update_flash_firmware);
142
143 // Set the firmware update processing progress callback function
144 // bhy.setUpdateProcessCallback(progress_callback, NULL);
145
146 // Set the maximum transfer bytes of I2C/SPI,The default size is I2C 32 bytes, SPI 256 bytes.
147 // bhy.setMaxiTransferSize(256);
148
149 // Set the processing fifo data buffer size,The default size is 512 bytes.
150 // bhy.setProcessBufferSize(1024);
151
152 // Set to load firmware from flash
153 bhy.setBootFromFlash(bosch_firmware_type);
154
155 Serial.println("Initializing Sensors...");
156
157#ifdef USE_I2C_INTERFACE
158 // Using I2C interface
159 // BHI260AP_SLAVE_ADDRESS_L = 0x28
160 // BHI260AP_SLAVE_ADDRESS_H = 0x29
161 if (!bhy.begin(Wire, BHI260AP_SLAVE_ADDRESS_L, BHI260_SDA, BHI260_SCL)) {
162 Serial.print("Failed to initialize sensor - error code:");
163 Serial.println(bhy.getError());
164 while (1) {
165 delay(1000);
166 }
167 }
168#endif
169
170#ifdef USE_SPI_INTERFACE
171 // Using SPI interface
172 if (!bhy.begin(SPI, BHI260_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
173 Serial.print("Failed to initialize sensor - error code:");
174 Serial.println(bhy.getError());
175 while (1) {
176 delay(1000);
177 }
178 }
179#endif
180
181 Serial.println("Initializing the sensor successfully!");
182
183 // Output all sensors info to Serial
184 BoschSensorInfo info = bhy.getSensorInfo();
185
186#ifdef ARDUINO
187 ArduinoStreamPrinter printer(Serial);
188 info.printInfo(printer);
189#else
190 info.printInfo([](const char *format, ...) -> int {
191 va_list args;
192 va_start(args, format);
193 int result = vprintf(format, args);
194 va_end(args);
195 return result;
196 });
197#endif
198
200
201 Serial.println("Hello: Type 'help' to get help");
202
203 cmd.printCommandPrompt();
204
205#ifdef USING_SENSOR_IRQ_METHOD
206 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
207 // This makes the pin ready to receive external signals.
208 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
209 pinMode(BHI260_IRQ, INPUT);
210
211 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
212 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
213 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
214#endif
215}
216
217uint32_t check_millis = 0;
218
219void loop()
220{
221 //Call the update functions using the activeCommander pointer
222 cmd.update();
223
224#ifdef USING_SENSOR_IRQ_METHOD
226 isInterruptTriggered = false;
227#endif /*USING_SENSOR_IRQ_METHOD*/
228
229 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
230 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
231 */
232 bhy.update();
233
234#ifdef USING_SENSOR_IRQ_METHOD
235 }
236#endif /*USING_SENSOR_IRQ_METHOD*/
237}
238
239
240//All commands for 'master'
241//COMMAND ARRAY ------------------------------------------------------------------------------
242const commandList_t masterCommands[] = {
243 {"help", helpHandler, "help"},
244 {"set gpio", setGpioLevel, "set gpio level"},
245 {"get gpio", getGpioLevel, "get gpio level"},
246 {"dis gpio", disGpioMode, "disable gpio"},
247};
248
250{
251 cmd.begin(&Serial, masterCommands, sizeof(masterCommands));
252 cmd.commandPrompt(ON); //enable the command prompt
253 cmd.echo(true); //Echo incoming characters to theoutput port
254 cmd.errorMessages(ON); //error messages are enabled - it will tell us if we issue any unrecognised commands
255 //Error messaged do NOT work for quick set and get commands
256}
257
258bool helpHandler(Commander &Cmdr)
259{
260 Serial.println("Help:");
261 Serial.println("\tCustom firmware valid gpio : 1, 4, 5, 6, 14, 15, 16, 17, 18, 19, 20");
262 Serial.println("\tset gpio [gpio num] [level]");
263 Serial.println("\tget gpio [gpio num] [pullup]");
264 Serial.println("\tdis gpio [gpio num]");
265 return 0;
266}
267
268/*
269* GPIO Comparison Table
270* M1SCX = N.A ! INVALID PIN
271* M1SDX = N.A ! INVALID PIN
272* M1SDI = N.A ! INVALID PIN
273* M2SCX = 14 ! OK
274* M2SDX = 15 ! OK
275* M2SDI = 16 ! OK
276* MCSB1 = 1 ! OK
277* MCSB2 = 4 ! OK
278* M3SCL = 17 ! OK
279* M3SDA = 18 ! OK
280* MCSB3 = 5 ! OK
281* MCSB4 = 6 ! OK
282* JTAG_CLK = 19 ! OK
283* JTAG_DIO = 20 ! OK
284* RESV1 = 2 ! INVALID PIN
285* RESV2 = 3 ! INVALID PIN
286* RESV3 = N.A ! INVALID PIN
287* */
288bool setGpioLevel(Commander &Cmdr)
289{
290 int values[2] = {0, 0};
291 int items = Cmdr.countItems();
292 if (items < 2) {
293 return false;
294 }
295 for (int n = 0; n < 2; n++) {
296 Cmdr.getInt(values[n]);
297 }
298 uint8_t pin = values[0];
299 uint8_t level = values[1];
300 bhy.digitalWrite(pin, level);
301 return 0;
302}
303
304bool getGpioLevel(Commander &Cmdr)
305{
306 int values[2] = {0, 0};
307 int items = Cmdr.countItems();
308 if (items < 1) {
309 return 0;
310 }
311 if (items > 2 )items = 2;
312 for (int n = 0; n < items; n++) {
313 Cmdr.getInt(values[n]);
314 }
315 bool pullup = false;
316 uint8_t pin = values[0];
317 if (items == 2 ) {
318 pullup = values[1];
319 }
320 uint8_t level = bhy.digitalRead(pin, pullup);
321 Serial.print("Get GPIO : "); Serial.print(pin);
322 Serial.print(" level is "); Serial.println(level);
323 return 0;
324}
325
326bool disGpioMode(Commander &Cmdr)
327{
328 int values[1] = {0};
329 int items = Cmdr.countItems();
330 if (items < 1) {
331 return 0;
332 }
333 Cmdr.getInt(values[0]);
334 uint8_t pin = values[0];
335 bhy.disableGpio(pin);
336 return 0;
337}
@license MIT License
@license MIT License
uint8_t level
bool helpHandler(Commander &Cmdr)
void initialiseCommander()
SensorBHI260AP bhy
bool getGpioLevel(Commander &Cmdr)
uint32_t check_millis
#define BHI260_SCL
bool disGpioMode(Commander &Cmdr)
void setup()
void dataReadyISR()
volatile bool isInterruptTriggered
#define BHI260_IRQ
#define BHI260_SDA
void progress_callback(uint32_t total, uint32_t transferred, void *user_data)
bool force_update_flash_firmware
const commandList_t masterCommands[]
Commander cmd
bool setGpioLevel(Commander &Cmdr)
#define BHI260_RST
void loop()
void digitalWrite(uint8_t pin, uint8_t level)
digitalWrite
uint8_t digitalRead(uint8_t pin, bool pullup=false)
digitalRead
void disableGpio(uint8_t pin)
disableGpio
void setBootFromFlash(bool boot_from_flash)
setBootFromFlash