Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions examples/Interrupts_subclassing/Interrupts_subclassing.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!
Expand All @@ -65,7 +73,11 @@ void setup() {
while (!Serial);
myIMU.debug(Serial);
myIMU.onInterrupt(print_data);
#if defined(ARDUINO_ARDUINO_NESSO_N1)
myIMU.begin(BOSCH_ACCELEROMETER_ONLY);
#else
myIMU.begin();
#endif

Serial.print("Accelerometer sample rate = ");
Serial.println(myIMU.accelerationSampleRate());
Expand Down
45 changes: 45 additions & 0 deletions src/BMI270.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,6 +43,45 @@ void BoschSensorClass::onInterrupt(mbed::Callback<void(void)> 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<bmi_task_data*>(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);
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) {

_wire->begin();
Expand Down Expand Up @@ -364,6 +407,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)
{
Expand All @@ -372,6 +416,7 @@ static void panic_led_trap(void)
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
}
#endif
}

void BoschSensorClass::print_rslt(int8_t rslt)
Expand Down
18 changes: 17 additions & 1 deletion src/BoschSensorClass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -182,6 +193,11 @@ class BoschSensorClass {
Stream* _debug = nullptr;
#ifdef __MBED__
mbed::Callback<void(void)> _cb;
#else
public:
void (*_cb)(void) = nullptr;
private:
void cb_wrapper();
#endif
bool _initialized = false;
int _interrupts = 0;
Expand Down
Loading