Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mhaberler/ef7c434ec06115391bfa568e12d5c225 to your computer and use it in GitHub Desktop.
Save mhaberler/ef7c434ec06115391bfa568e12d5c225 to your computer and use it in GitHub Desktop.
Arduino DPS368 example - IRQ driven, esp32
// example for IRQ handling on ESP32 such that the IRQ handler
// can do I/O operations
#include <M5Unified.h>
#include <Dps3xx.h>
typedef struct {
uint32_t timestamp;
int16_t pin;
} irqmsg_t;
QueueHandle_t irq_queue;
Dps3xx Dps3xxPressureSensor = Dps3xx();
// first level interrupt handler
// only notify handler task and pass parameters
static void IRAM_ATTR button_irq(void *param) {
int16_t *pin = (int16_t *)param;
static uint32_t counter;
irqmsg_t msg;
msg.pin = *pin;
msg.timestamp = micros();
xQueueSendFromISR(irq_queue, (const void*) &msg, NULL);
}
// 2nd level interrupt handler
// in user context - can do Wire I/O etc
void soft_irq(void* arg) {
irqmsg_t msg;
uint8_t pressureCount = 20;
float pressure[pressureCount];
uint8_t temperatureCount = 20;
float temperature[temperatureCount];
for (;;) {
if (xQueueReceive(irq_queue, &msg, portMAX_DELAY)) {
int16_t ff = Dps3xxPressureSensor.getIntStatusFifoFull();
int16_t pr = Dps3xxPressureSensor.getIntStatusPrsReady();
int16_t tr = Dps3xxPressureSensor.getIntStatusTempReady();
log_i("got msg pin=%d timestamp=%u fifofulll=%d pr=%d tr=%d", msg.pin, msg.timestamp, ff, pr, tr);
int16_t ret = Dps3xxPressureSensor.getContResults(temperature, temperatureCount, pressure, pressureCount);
if (ret != 0) {
Serial.println();
Serial.println();
Serial.print("FAIL! ret = ");
Serial.println(ret);
} else {
Serial.println();
Serial.println();
Serial.print(temperatureCount);
Serial.println(" temperature values found: ");
for (int16_t i = 0; i < temperatureCount; i++) {
Serial.print(temperature[i]);
Serial.println(" degrees of Celsius");
}
Serial.println();
Serial.print(pressureCount);
Serial.println(" pressure values found: ");
for (int16_t i = 0; i < pressureCount; i++) {
Serial.print(pressure[i]);
Serial.println(" Pascal");
}
}
}
}
}
int16_t pin17 = 17;
int16_t pin18 = 18;
void setup() {
delay(3000);
M5.begin();
Serial.begin(115200);
Wire.begin();
Wire.setClock(100000);
irq_queue = xQueueCreate(10, sizeof(irqmsg_t));
xTaskCreate(soft_irq, "irq_stage2", 2048, NULL, 10, NULL);
pinMode(pin17, INPUT_PULLUP);
attachInterruptArg(digitalPinToInterrupt(pin17), button_irq,(void *)&pin17,FALLING);
pinMode(pin18, INPUT_PULLUP);
attachInterruptArg(digitalPinToInterrupt(pin18), button_irq, (void *)&pin18, FALLING);
Dps3xxPressureSensor.begin(Wire, 0x76);
int16_t val = Dps3xxPressureSensor.setInterruptSources(DPS3xx_FIFO_FULL_INTR, 0);
// int16_t val = Dps3xxPressureSensor.setInterruptSources(DPS3xx_BOTH_INTR, 0);
// clear interrupt flag by reading
int16_t ff = Dps3xxPressureSensor.getIntStatusFifoFull();
int16_t pr = Dps3xxPressureSensor.getIntStatusPrsReady();
int16_t tr = Dps3xxPressureSensor.getIntStatusTempReady();
log_i("setup: fifofulll=%d pr=%d tr=%d", ff, pr, tr);
/*
* temperature measure rate (value from 0 to 7)
* 2^temp_mr temperature measurement results per second
*/
int16_t temp_mr = 5;
/*
* temperature oversampling rate (value from 0 to 7)
* 2^temp_osr internal temperature measurements per result
* A higher value increases precision
*/
int16_t temp_osr = 1;
/*
* pressure measure rate (value from 0 to 7)
* 2^prs_mr pressure measurement results per second
*/
int16_t prs_mr = 5;
/*
* pressure oversampling rate (value from 0 to 7)
* 2^prs_osr internal pressure measurements per result
* A higher value increases precision
*/
int16_t prs_osr = 1;
/*
* startMeasureBothCont enables background mode
* temperature and pressure are measured automatically
* High precision and high measure rates at the same time are not available.
* Consult Datasheet (or trial and error) for more information
*/
// int16_t ret = Dps3xxPressureSensor.startMeasurePressureCont(prs_mr, prs_osr);
int16_t ret = Dps3xxPressureSensor.startMeasureBothCont(temp_mr, temp_osr, prs_mr, prs_osr);
if (ret != 0) {
Serial.print("Init FAILED! ret = ");
Serial.println(ret);
} else {
Serial.println("Init complete!");
}
}
void loop() {
delay(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment