Skip to content

Commit fd7295f

Browse files
authored
Merge pull request #62 from arduino-libraries/esp32_fixes
Fixes for ESP32 boards
2 parents 5fc0618 + 7d4f75d commit fd7295f

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

examples/Interrupts_subclassing/Interrupts_subclassing.ino

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ class MyBoschSensor: public BoschSensorClass {
1414
struct bmi2_int_pin_config int_pin_cfg;
1515
int_pin_cfg.pin_type = BMI2_INT1;
1616
int_pin_cfg.int_latch = BMI2_INT_NON_LATCH;
17+
#if defined(ARDUINO_ARDUINO_NESSO_N1)
18+
int_pin_cfg.pin_cfg[0].lvl = BMI2_INT_ACTIVE_LOW;
19+
#else
1720
int_pin_cfg.pin_cfg[0].lvl = BMI2_INT_ACTIVE_HIGH;
21+
#endif
1822
int_pin_cfg.pin_cfg[0].od = BMI2_INT_PUSH_PULL;
1923
int_pin_cfg.pin_cfg[0].output_en = BMI2_INT_OUTPUT_ENABLE;
2024
int_pin_cfg.pin_cfg[0].input_en = BMI2_INT_INPUT_DISABLE;
@@ -52,7 +56,11 @@ class MyBoschSensor: public BoschSensorClass {
5256
}
5357
};
5458

59+
#if defined(ARDUINO_ARDUINO_NESSO_N1)
60+
MyBoschSensor myIMU(Wire);
61+
#else
5562
MyBoschSensor myIMU(Wire1);
63+
#endif
5664

5765
void print_data() {
5866
// we can also read accelerometer / gyro data here!
@@ -65,7 +73,11 @@ void setup() {
6573
while (!Serial);
6674
myIMU.debug(Serial);
6775
myIMU.onInterrupt(print_data);
76+
#if defined(ARDUINO_ARDUINO_NESSO_N1)
77+
myIMU.begin(BOSCH_ACCELEROMETER_ONLY);
78+
#else
6879
myIMU.begin();
80+
#endif
6981

7082
Serial.print("Accelerometer sample rate = ");
7183
Serial.println(myIMU.accelerationSampleRate());

src/BMI270.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
static events::EventQueue queue(10 * EVENTS_EVENT_SIZE);
1111
#endif
1212

13+
#ifdef ARDUINO_ARCH_ESP32
14+
#include "FunctionalInterrupt.h"
15+
#endif
16+
1317
#if defined(ARDUINO_NANO33BLE)
1418
#define TARGET_ARDUINO_NANO33BLE
1519
#endif
@@ -39,6 +43,45 @@ void BoschSensorClass::onInterrupt(mbed::Callback<void(void)> cb)
3943
irq.rise(mbed::callback(this, &BoschSensorClass::interrupt_handler));
4044
}
4145
#endif
46+
47+
#ifdef ARDUINO_ARCH_ESP32
48+
static EventGroupHandle_t xHandle = NULL;
49+
struct bmi_task_data {
50+
BoschSensorClass* imu;
51+
struct bmi2_dev* bmi2;
52+
};
53+
54+
void irq_thread(void *pvParameters)
55+
{
56+
bmi_task_data* instance_ptr = static_cast<bmi_task_data*>(pvParameters);
57+
uint16_t status;
58+
while (1) {
59+
xEventGroupWaitBits(xHandle, 1, pdTRUE, pdFALSE, portMAX_DELAY);
60+
if (instance_ptr->imu && instance_ptr->imu->_cb) {
61+
bmi2_get_int_status(&status, instance_ptr->bmi2);
62+
instance_ptr->imu->_cb();
63+
}
64+
}
65+
}
66+
67+
void BoschSensorClass::cb_wrapper()
68+
{
69+
xEventGroupSetBits(xHandle, 0xFF);
70+
}
71+
72+
void BoschSensorClass::onInterrupt(void (*cb)(void))
73+
{
74+
if (BMI270_INT1 == -1) {
75+
return;
76+
}
77+
xHandle = xEventGroupCreate();
78+
_cb = cb;
79+
static struct bmi_task_data instance = { this, &bmi2 };
80+
pinMode(BMI270_INT1, INPUT_PULLUP);
81+
xTaskCreate(irq_thread, "bmi_irq_thread", 4096, &instance, 1, NULL);
82+
attachInterrupt(BMI270_INT1, std::bind(&BoschSensorClass::cb_wrapper, this), FALLING);
83+
}
84+
#endif
4285
int BoschSensorClass::begin(CfgBoshSensor_t cfg) {
4386

4487
_wire->begin();
@@ -364,6 +407,7 @@ static void panic_led_trap(void)
364407
static int const LED_BUILTIN = 2;
365408
#endif
366409

410+
#if !defined(ARDUINO_ARDUINO_NESSO_N1)
367411
pinMode(LED_BUILTIN, OUTPUT);
368412
while (1)
369413
{
@@ -372,6 +416,7 @@ static void panic_led_trap(void)
372416
digitalWrite(LED_BUILTIN, HIGH);
373417
delay(100);
374418
}
419+
#endif
375420
}
376421

377422
void BoschSensorClass::print_rslt(int8_t rslt)

src/BoschSensorClass.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class ContinuousMode {
7979
if (ret != 0) {
8080
return 0;
8181
}
82-
_available = min(status, sizeof(fifoData)) / (6 + 6); // 6 bytes per accel sample
82+
_available = min((size_t)status, sizeof(fifoData)) / (6 + 6); // 6 bytes per accel sample
8383
_availableG = _available;
8484
_availableA = _available;
8585
ret = bmi2_extract_accel(accel_data, &_available, &fifoFrame, bmi2);
@@ -150,6 +150,17 @@ class BoschSensorClass {
150150
}
151151
PinName BMI270_INT1 = NC;
152152
#endif
153+
#ifdef ARDUINO_ARCH_ESP32
154+
void onInterrupt(void (*)(void));
155+
void setInterruptPin(int irq_pin) {
156+
BMI270_INT1 = irq_pin;
157+
}
158+
#if defined(ARDUINO_ARDUINO_NESSO_N1)
159+
int BMI270_INT1 = 3;
160+
#else
161+
int BMI270_INT1 = -1;
162+
#endif
163+
#endif
153164
// Accelerometer
154165
virtual int readAcceleration(float& x, float& y, float& z); // Results are in G (earth gravity).
155166
virtual int accelerationAvailable(); // Number of samples in the FIFO.
@@ -182,6 +193,11 @@ class BoschSensorClass {
182193
Stream* _debug = nullptr;
183194
#ifdef __MBED__
184195
mbed::Callback<void(void)> _cb;
196+
#else
197+
public:
198+
void (*_cb)(void) = nullptr;
199+
private:
200+
void cb_wrapper();
185201
#endif
186202
bool _initialized = false;
187203
int _interrupts = 0;

0 commit comments

Comments
 (0)