Skip to content

Commit 7d4f75d

Browse files
committed
esp32: wrap interrupt in event thread
1 parent 718f183 commit 7d4f75d

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/BMI270.cpp

Lines changed: 34 additions & 1 deletion
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,14 +43,43 @@ void BoschSensorClass::onInterrupt(mbed::Callback<void(void)> cb)
3943
irq.rise(mbed::callback(this, &BoschSensorClass::interrupt_handler));
4044
}
4145
#endif
46+
4247
#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+
4372
void BoschSensorClass::onInterrupt(void (*cb)(void))
4473
{
4574
if (BMI270_INT1 == -1) {
4675
return;
4776
}
77+
xHandle = xEventGroupCreate();
78+
_cb = cb;
79+
static struct bmi_task_data instance = { this, &bmi2 };
4880
pinMode(BMI270_INT1, INPUT_PULLUP);
49-
attachInterrupt(BMI270_INT1, cb, FALLING);
81+
xTaskCreate(irq_thread, "bmi_irq_thread", 4096, &instance, 1, NULL);
82+
attachInterrupt(BMI270_INT1, std::bind(&BoschSensorClass::cb_wrapper, this), FALLING);
5083
}
5184
#endif
5285
int BoschSensorClass::begin(CfgBoshSensor_t cfg) {

src/BoschSensorClass.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ class BoschSensorClass {
193193
Stream* _debug = nullptr;
194194
#ifdef __MBED__
195195
mbed::Callback<void(void)> _cb;
196+
#else
197+
public:
198+
void (*_cb)(void) = nullptr;
199+
private:
200+
void cb_wrapper();
196201
#endif
197202
bool _initialized = false;
198203
int _interrupts = 0;

0 commit comments

Comments
 (0)