SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
bhi260ap_update_firmware.ino
Go to the documentation of this file.
1
30#include <Arduino.h>
31#include <Wire.h>
32#include <SPI.h>
33
34#if defined(ARDUINO_ARCH_NRF52)
35
36#include <SdFat.h> //Deplib https://github.com/adafruit/SdFat.git
37#include <ImuDrv.hpp>
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_SPI_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
92#define CS_PIN 5
93
94/***************************************
95 * SD CARD
96 ***************************************/
97SdFat32 sd;
98
99// Try max SPI clock for an SD. Reduce SPI_CLOCK if errors occur.
100#define SPI_CLOCK SD_SCK_MHZ(8)
101//------------------------------------------------------------------------------
102// Store error strings in flash to save RAM.
103#define error(s) sd.errorHalt(&Serial, F(s))
104#define SD_CONFIG SdSpiConfig(CS_PIN, DEDICATED_SPI, SPI_CLOCK)
105
106#if BHI260_IRQ > 0
107#define USING_SENSOR_IRQ_METHOD
108#endif
109
110#ifdef USING_SENSOR_IRQ_METHOD
111volatile bool isInterruptTriggered = false;
112
113void dataReadyISR()
114{
116}
117#endif /*USING_SENSOR_IRQ_METHOD*/
118
119void parse_bme280_sensor_data(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
120{
121 float humidity = 0;
122 float temperature = 0;
123 float pressure = 0;
124 switch (sensor_id) {
125 case BoschSensorID::HUMIDITY:
126 bhy2_parse_humidity(data_ptr, &humidity);
127 Serial.print("humidity:"); Serial.print(humidity); Serial.println("%");
128 break;
129 case BoschSensorID::TEMPERATURE:
130 bhy2_parse_temperature_celsius(data_ptr, &temperature);
131 Serial.print("temperature:"); Serial.print(temperature); Serial.println("*C");
132 break;
133 case BoschSensorID::BAROMETER:
134 bhy2_parse_pressure(data_ptr, &pressure);
135 Serial.print("pressure:"); Serial.print(pressure); Serial.println("hPa");
136 break;
137 default:
138 Serial.println("Unkown.");
139 break;
140 }
141}
142
143void printResult(uint8_t sensor_id, float sample_rate, bool rslt)
144{
145 const char *sensorName = bhy.getSensorName(sensor_id);
146 Serial.print("Configure ");
147 Serial.print(sensorName);
148 Serial.print(" sensor ");
149 Serial.print(sample_rate, 2);
150 Serial.print(" HZ ");
151 if (rslt) {
152 Serial.print("successfully");
153 } else {
154 Serial.print("failed");
155 }
156 Serial.println();
157}
158
159void setup()
160{
161 Serial.begin(115200);
162 while (!Serial);
163
164 // In this example, BHI260 and SD Card are on the same SPI bus
165 SPI.setPins(SPI_MISO, SPI_SCK, SPI_MOSI);
166
167 SPI.begin();
168
169 /***************************************
170 * SD CARD
171 ***************************************/
172 // If multiple SPI peripherals are mounted on a single bus, first set the CS of other peripherals to HIGH
173 const uint8_t other_spi_dev_cs_pin[] = {5, 28, 40};
174 for (size_t i = 0; i < sizeof(other_spi_dev_cs_pin); ++i) {
175 pinMode(other_spi_dev_cs_pin[i], OUTPUT);
176 digitalWrite(other_spi_dev_cs_pin[i], HIGH);
177 }
178
179 // Initialize the SD card.
180 Serial.println("Initializing SD Card ...");
181 if (!sd.begin(SD_CONFIG)) {
182 sd.initErrorHalt(&Serial);
183 while (1);
184 } else {
185 Serial.println(" success");
186 }
187
188 File firmware_file = sd.open("/BHI260AP_aux_BMM150_BME280_GPIO-flash.fw", FILE_READ);
189 if (!firmware_file) {
190 Serial.println("Open firmware file failed!");
191 while (1);
192 }
193
194 size_t fw_size = firmware_file.size();
195 Serial.println("Read firmware file successfully .");
196
197 uint8_t *firmware = (uint8_t *)malloc(fw_size);
198 if (!firmware) {
199 Serial.println("malloc memory failed!");
200 while (1);
201 }
202
203 firmware_file.readBytes(firmware, fw_size);
204
205 firmware_file.close();
206
207
208 /***************************************
209 * BHI260 Initializing
210 ***************************************/
211 Serial.println("Initializing Sensors...");
212 // Set the reset pin
213 bhy.setPins(BHI260_RST);
214 // Force update of the current firmware, regardless of whether it exists.
215 // After uploading the firmware once, you can change it to false to speed up the startup time.
216 bool force_update = true;
217 // true : Write firmware to flash , false : Write to ram
218 bool write_to_flash = true;
219 // Set the firmware array address and firmware size
220 bhy.setFirmware(firmware, fw_size, write_to_flash, force_update);
221 // Set to load firmware from flash or ram
222 bhy.setBootFromFlash(write_to_flash);
223
224#ifdef USE_I2C_INTERFACE
225 // Using I2C interface
226 // BHI260AP_SLAVE_ADDRESS_L = 0x28
227 // BHI260AP_SLAVE_ADDRESS_H = 0x29
228 if (!bhy.begin(Wire, BHI260AP_SLAVE_ADDRESS_L, BHI260_SDA, BHI260_SCL)) {
229 Serial.print("Failed to initialize sensor - error code:");
230 Serial.println(bhy.getError());
231 while (1) {
232 delay(1000);
233 }
234 }
235#endif
236
237#ifdef USE_SPI_INTERFACE
238 // Using SPI interface
239 if (!bhy.begin(SPI, BHI260_CS, SPI_MOSI, SPI_MISO, SPI_SCK)) {
240 Serial.print("Failed to initialize sensor - error code:");
241 Serial.println(bhy.getError());
242 while (1) {
243 delay(1000);
244 }
245 }
246#endif
247
248 // Release the requested memory space
249 free(firmware);
250
251 Serial.println("Initializing the sensor successfully!");
252
253 // Output all sensors info to Serial
254 BoschSensorInfo info = bhy.getSensorInfo();
255
256#ifdef ARDUINO
257 ArduinoStreamPrinter printer(Serial);
258 info.printInfo(printer);
259#else
260 info.printInfo([](const char *format, ...) -> int {
261 va_list args;
262 va_start(args, format);
263 int result = vprintf(format, args);
264 va_end(args);
265 return result;
266 });
267#endif
268
269 /*
270 * Enable monitoring.
271 * sample_rate ​​can only control the rate of the pressure sensor.
272 * Temperature and humidity will only be updated when there is a change.
273 * * */
274 float sample_rate = 1.0; /* Set to 1Hz update frequency */
275 uint32_t report_latency_ms = 0; /* Report immediately */
276 bool rslt = false;
277
278 rslt = bhy.configure(BoschSensorID::TEMPERATURE, sample_rate, report_latency_ms);
279 printResult(BoschSensorID::TEMPERATURE, sample_rate, rslt);
280 rslt = bhy.configure(BoschSensorID::BAROMETER, sample_rate, report_latency_ms);
281 printResult(BoschSensorID::BAROMETER, sample_rate, rslt);
282 rslt = bhy.configure(BoschSensorID::HUMIDITY, sample_rate, report_latency_ms);
283 printResult(BoschSensorID::HUMIDITY, sample_rate, rslt);
284
285 // Register BME280 data parse callback function
286 Serial.println("Register sensor result callback function");
287 bhy.onResultEvent(BoschSensorID::TEMPERATURE, parse_bme280_sensor_data);
288 bhy.onResultEvent(BoschSensorID::HUMIDITY, parse_bme280_sensor_data);
289 bhy.onResultEvent(BoschSensorID::BAROMETER, parse_bme280_sensor_data);
290
291#ifdef USING_SENSOR_IRQ_METHOD
292 // Set the specified pin (BHI260_IRQ) ​​to an input pin.
293 // This makes the pin ready to receive external signals.
294 // If the interrupt is already connected, if BHI260_IRQ is equal to -1 then the polling method will be used
295 pinMode(BHI260_IRQ, INPUT);
296
297 // Attach an interrupt service routine (ISR) to the specified pin (BHI260_IRQ).
298 // The ISR 'dataReadyISR' will be called whenever a rising edge is detected on the pin.
299 attachInterrupt(BHI260_IRQ, dataReadyISR, RISING);
300#endif
301}
302
303void loop()
304{
305#ifdef USING_SENSOR_IRQ_METHOD
307 isInterruptTriggered = false;
308#endif /*USING_SENSOR_IRQ_METHOD*/
309
310 /* If the interrupt is connected to the sensor and BHI260_IRQ is not equal to -1,
311 * the interrupt function will be enabled, otherwise the method of polling the sensor is used
312 */
313 bhy.update();
314
315#ifdef USING_SENSOR_IRQ_METHOD
316 }
317#endif /*USING_SENSOR_IRQ_METHOD*/
318
319 delay(50);
320}
321#else
322void setup() {}
323void loop() {}
324#endif
@license MIT License
SensorBHI260AP bhy
#define BHI260_SCL
void dataReadyISR()
volatile bool isInterruptTriggered
#define BHI260_IRQ
#define BHI260_SDA
#define BHI260_RST
void printResult(uint8_t sensor_id, float sample_rate, bool rslt)
SensorTemperature temperature(bhy)
SensorPressure pressure(bhy)
SensorHumidity humidity(bhy)
void parse_bme280_sensor_data(uint8_t sensor_id, const uint8_t *data_ptr, uint32_t len, uint64_t *timestamp, void *user_data)
void setBootFromFlash(bool boot_from_flash)
setBootFromFlash
uint8_t i