|
| 1 | +/** |
| 2 | + * An example to use SDP3x sensor interrupts. |
| 3 | + * |
| 4 | + * Software setup: |
| 5 | + * 1) install the arduino-esp library |
| 6 | + * 2) select your ESP board in "Tools" -> "Board" |
| 7 | + * 3) select the right port in "Tools" -> "Port" |
| 8 | + * 4) turn on the logs by setting "Tools" -> "Core Debug Level" -> "Info" |
| 9 | + * Hardware setup: |
| 10 | + * 1) connect GPIO SDA to pin 19 |
| 11 | + * 2) connect GPIO SCL to pin 23 |
| 12 | + * 3) connect GPIO IRQ to pin 4 (!) |
| 13 | + * or change the code appropriately. |
| 14 | + */ |
| 15 | + |
| 16 | +#include <Wire.h> |
| 17 | + |
| 18 | +#include "freertos/FreeRTOS.h" |
| 19 | +#include "freertos/task.h" |
| 20 | + |
| 21 | +#include "sdpsensor.h" |
| 22 | + |
| 23 | +#define GPIO_IRQ_PIN 4 // SDP3x IRQ pin connected to your board |
| 24 | + |
| 25 | +static TaskHandle_t read_sensor_task_handle; |
| 26 | + |
| 27 | +SDPSensor sdp(0x21); |
| 28 | + |
| 29 | + |
| 30 | +static void IRAM_ATTR sdpsensor_irq_handler() { |
| 31 | + BaseType_t xHigherPriorityTaskWoken = pdFALSE; |
| 32 | + vTaskNotifyGiveFromISR(read_sensor_task_handle, &xHigherPriorityTaskWoken); |
| 33 | + |
| 34 | + /* If xHigherPriorityTaskWoken is now set to pdTRUE then a context switch |
| 35 | + should be performed to ensure the interrupt returns directly to the highest |
| 36 | + priority task. The macro used for this purpose is dependent on the port in |
| 37 | + use and may be called portEND_SWITCHING_ISR(). */ |
| 38 | + portYIELD_FROM_ISR(xHigherPriorityTaskWoken); |
| 39 | +} |
| 40 | + |
| 41 | + |
| 42 | +static void sdptask_read_sensor() { |
| 43 | + esp_err_t err; |
| 44 | + int16_t diff_pressure; |
| 45 | + |
| 46 | + while (1) { |
| 47 | + // wait for an interrupt |
| 48 | + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); |
| 49 | + |
| 50 | + err = sdp.readDiffPressure(&diff_pressure); |
| 51 | + |
| 52 | + if (err == ESP_OK) { |
| 53 | + // send to a receiver here |
| 54 | + } |
| 55 | + |
| 56 | + // Note that you should never print here any messages because |
| 57 | + // printing to serial is VERY slow. It's done for demonstration |
| 58 | + // purposes here. |
| 59 | + Serial.print("Raw diff pressure: "); |
| 60 | + Serial.println(diff_pressure); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +void setup() { |
| 66 | + Serial.begin(115200); |
| 67 | + delay(1000); // let serial console settle |
| 68 | + Wire.begin(19, 23); |
| 69 | + |
| 70 | + // you should be already familiar with this |
| 71 | + while (sdp.stopContinuous() != ESP_OK); |
| 72 | + while (sdp.begin() != ESP_OK); |
| 73 | + while (sdp.startContinuous() != ESP_OK); |
| 74 | + |
| 75 | + uint32_t modelNumber; |
| 76 | + sdp.getInfo(&modelNumber, NULL, NULL, NULL); |
| 77 | + if (!(modelNumber == 31 || modelNumber == 32)) { |
| 78 | + Serial.println("WARNING! Your SDP sensor does NOT support interrupts!"); |
| 79 | + } |
| 80 | + |
| 81 | + if (sdp.attachIRQHandler(GPIO_IRQ_PIN, sdpsensor_irq_handler) != ESP_OK) { |
| 82 | + Serial.println("Failed to hook an interrupt handler. Check the wiring."); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // start a task on the second core with the priority '1' |
| 87 | + xTaskCreatePinnedToCore((TaskFunction_t) sdptask_read_sensor, "sdp_read", 4096, NULL, 1, &read_sensor_task_handle, APP_CPU_NUM); |
| 88 | + |
| 89 | + // allow the sensor to run the first measurement |
| 90 | + xTaskNotifyGive(read_sensor_task_handle); |
| 91 | +} |
| 92 | + |
| 93 | + |
| 94 | +void loop() { |
| 95 | + // sdptask_read_sensor task is running |
| 96 | +} |
0 commit comments