From 90d4343c153585b995289f4207f75afd9b5323db Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 28 Nov 2025 15:39:45 +0100 Subject: [PATCH 1/5] esp32: fix min macro --- src/BoschSensorClass.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BoschSensorClass.h b/src/BoschSensorClass.h index 0af91de..d7fa604 100644 --- a/src/BoschSensorClass.h +++ b/src/BoschSensorClass.h @@ -79,7 +79,7 @@ class ContinuousMode { if (ret != 0) { return 0; } - _available = min(status, sizeof(fifoData)) / (6 + 6); // 6 bytes per accel sample + _available = min((size_t)status, sizeof(fifoData)) / (6 + 6); // 6 bytes per accel sample _availableG = _available; _availableA = _available; ret = bmi2_extract_accel(accel_data, &_available, &fifoFrame, bmi2); From e8b6b1bb939803a1121049d4999fa7f054dc01b6 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 28 Nov 2025 15:40:01 +0100 Subject: [PATCH 2/5] nesso_n1: enable INT pin --- src/BMI270.cpp | 10 ++++++++++ src/BoschSensorClass.h | 11 +++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/BMI270.cpp b/src/BMI270.cpp index 6f869b1..9dc4eb1 100644 --- a/src/BMI270.cpp +++ b/src/BMI270.cpp @@ -39,6 +39,16 @@ void BoschSensorClass::onInterrupt(mbed::Callback cb) irq.rise(mbed::callback(this, &BoschSensorClass::interrupt_handler)); } #endif +#ifdef ARDUINO_ARCH_ESP32 +void BoschSensorClass::onInterrupt(void (*cb)(void)) +{ + if (BMI270_INT1 == -1) { + return; + } + pinMode(BMI270_INT1, INPUT_PULLUP); + attachInterrupt(BMI270_INT1, cb, FALLING); +} +#endif int BoschSensorClass::begin(CfgBoshSensor_t cfg) { _wire->begin(); diff --git a/src/BoschSensorClass.h b/src/BoschSensorClass.h index d7fa604..4290d7c 100644 --- a/src/BoschSensorClass.h +++ b/src/BoschSensorClass.h @@ -150,6 +150,17 @@ class BoschSensorClass { } PinName BMI270_INT1 = NC; #endif + #ifdef ARDUINO_ARCH_ESP32 + void onInterrupt(void (*)(void)); + void setInterruptPin(int irq_pin) { + BMI270_INT1 = irq_pin; + } + #if defined(ARDUINO_ARDUINO_NESSO_N1) + int BMI270_INT1 = 3; + #else + int BMI270_INT1 = -1; + #endif + #endif // Accelerometer virtual int readAcceleration(float& x, float& y, float& z); // Results are in G (earth gravity). virtual int accelerationAvailable(); // Number of samples in the FIFO. From ff237b5230bf6712f9610e4b993295d68c720ede Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Fri, 28 Nov 2025 15:48:55 +0100 Subject: [PATCH 3/5] nesso_n1: fix interrupt example --- .../Interrupts_subclassing.ino | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/examples/Interrupts_subclassing/Interrupts_subclassing.ino b/examples/Interrupts_subclassing/Interrupts_subclassing.ino index c88bff0..988e6c8 100644 --- a/examples/Interrupts_subclassing/Interrupts_subclassing.ino +++ b/examples/Interrupts_subclassing/Interrupts_subclassing.ino @@ -14,7 +14,11 @@ class MyBoschSensor: public BoschSensorClass { struct bmi2_int_pin_config int_pin_cfg; int_pin_cfg.pin_type = BMI2_INT1; int_pin_cfg.int_latch = BMI2_INT_NON_LATCH; + #if defined(ARDUINO_ARDUINO_NESSO_N1) + int_pin_cfg.pin_cfg[0].lvl = BMI2_INT_ACTIVE_LOW; + #else int_pin_cfg.pin_cfg[0].lvl = BMI2_INT_ACTIVE_HIGH; + #endif int_pin_cfg.pin_cfg[0].od = BMI2_INT_PUSH_PULL; int_pin_cfg.pin_cfg[0].output_en = BMI2_INT_OUTPUT_ENABLE; int_pin_cfg.pin_cfg[0].input_en = BMI2_INT_INPUT_DISABLE; @@ -52,7 +56,11 @@ class MyBoschSensor: public BoschSensorClass { } }; +#if defined(ARDUINO_ARDUINO_NESSO_N1) +MyBoschSensor myIMU(Wire); +#else MyBoschSensor myIMU(Wire1); +#endif void print_data() { // we can also read accelerometer / gyro data here! @@ -65,7 +73,11 @@ void setup() { while (!Serial); myIMU.debug(Serial); myIMU.onInterrupt(print_data); - myIMU.begin(); +#if defined(ARDUINO_ARDUINO_NESSO_N1) + myIMU.begin(BOSCH_ACCEL_ONLY); +#else + myIMU.begin(BOSCH_ACCEL_AND_GYRO); +#endif Serial.print("Accelerometer sample rate = "); Serial.println(myIMU.accelerationSampleRate()); From 718f183eb313a6e851a36c7b0b3a9acd619bbc53 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 15 Dec 2025 11:32:46 +0100 Subject: [PATCH 4/5] nesso_n1: fix compilation problems --- examples/Interrupts_subclassing/Interrupts_subclassing.ino | 4 ++-- src/BMI270.cpp | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/Interrupts_subclassing/Interrupts_subclassing.ino b/examples/Interrupts_subclassing/Interrupts_subclassing.ino index 988e6c8..6c997b8 100644 --- a/examples/Interrupts_subclassing/Interrupts_subclassing.ino +++ b/examples/Interrupts_subclassing/Interrupts_subclassing.ino @@ -74,9 +74,9 @@ void setup() { myIMU.debug(Serial); myIMU.onInterrupt(print_data); #if defined(ARDUINO_ARDUINO_NESSO_N1) - myIMU.begin(BOSCH_ACCEL_ONLY); + myIMU.begin(BOSCH_ACCELEROMETER_ONLY); #else - myIMU.begin(BOSCH_ACCEL_AND_GYRO); + myIMU.begin(); #endif Serial.print("Accelerometer sample rate = "); diff --git a/src/BMI270.cpp b/src/BMI270.cpp index 9dc4eb1..310f704 100644 --- a/src/BMI270.cpp +++ b/src/BMI270.cpp @@ -374,6 +374,7 @@ static void panic_led_trap(void) static int const LED_BUILTIN = 2; #endif +#if !defined(ARDUINO_ARDUINO_NESSO_N1) pinMode(LED_BUILTIN, OUTPUT); while (1) { @@ -382,6 +383,7 @@ static void panic_led_trap(void) digitalWrite(LED_BUILTIN, HIGH); delay(100); } +#endif } void BoschSensorClass::print_rslt(int8_t rslt) From 7d4f75dd4eebaf98a0994855764a588a657e0b81 Mon Sep 17 00:00:00 2001 From: Martino Facchin Date: Mon, 15 Dec 2025 11:34:08 +0100 Subject: [PATCH 5/5] esp32: wrap interrupt in event thread --- src/BMI270.cpp | 35 ++++++++++++++++++++++++++++++++++- src/BoschSensorClass.h | 5 +++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/BMI270.cpp b/src/BMI270.cpp index 310f704..e5b6525 100644 --- a/src/BMI270.cpp +++ b/src/BMI270.cpp @@ -10,6 +10,10 @@ static events::EventQueue queue(10 * EVENTS_EVENT_SIZE); #endif +#ifdef ARDUINO_ARCH_ESP32 +#include "FunctionalInterrupt.h" +#endif + #if defined(ARDUINO_NANO33BLE) #define TARGET_ARDUINO_NANO33BLE #endif @@ -39,14 +43,43 @@ void BoschSensorClass::onInterrupt(mbed::Callback cb) irq.rise(mbed::callback(this, &BoschSensorClass::interrupt_handler)); } #endif + #ifdef ARDUINO_ARCH_ESP32 +static EventGroupHandle_t xHandle = NULL; +struct bmi_task_data { + BoschSensorClass* imu; + struct bmi2_dev* bmi2; +}; + +void irq_thread(void *pvParameters) +{ + bmi_task_data* instance_ptr = static_cast(pvParameters); + uint16_t status; + while (1) { + xEventGroupWaitBits(xHandle, 1, pdTRUE, pdFALSE, portMAX_DELAY); + if (instance_ptr->imu && instance_ptr->imu->_cb) { + bmi2_get_int_status(&status, instance_ptr->bmi2); + instance_ptr->imu->_cb(); + } + } +} + +void BoschSensorClass::cb_wrapper() +{ + xEventGroupSetBits(xHandle, 0xFF); +} + void BoschSensorClass::onInterrupt(void (*cb)(void)) { if (BMI270_INT1 == -1) { return; } + xHandle = xEventGroupCreate(); + _cb = cb; + static struct bmi_task_data instance = { this, &bmi2 }; pinMode(BMI270_INT1, INPUT_PULLUP); - attachInterrupt(BMI270_INT1, cb, FALLING); + xTaskCreate(irq_thread, "bmi_irq_thread", 4096, &instance, 1, NULL); + attachInterrupt(BMI270_INT1, std::bind(&BoschSensorClass::cb_wrapper, this), FALLING); } #endif int BoschSensorClass::begin(CfgBoshSensor_t cfg) { diff --git a/src/BoschSensorClass.h b/src/BoschSensorClass.h index 4290d7c..8d55620 100644 --- a/src/BoschSensorClass.h +++ b/src/BoschSensorClass.h @@ -193,6 +193,11 @@ class BoschSensorClass { Stream* _debug = nullptr; #ifdef __MBED__ mbed::Callback _cb; + #else + public: + void (*_cb)(void) = nullptr; + private: + void cb_wrapper(); #endif bool _initialized = false; int _interrupts = 0;