BME task removed / do i2c masking via interrupt

This commit is contained in:
Verkehrsrot 2019-07-29 20:23:27 +02:00
parent 11cfa27c76
commit 85aa9655c6
8 changed files with 58 additions and 65 deletions

View File

@ -11,14 +11,16 @@
#include <Adafruit_BME280.h> #include <Adafruit_BME280.h>
#endif #endif
extern Ticker bmecycler;
extern bmeStatus_t extern bmeStatus_t
bme_status; // Make struct for storing gps data globally available bme_status; // Make struct for storing gps data globally available
extern TaskHandle_t BmeTask;
// --- Bosch BSEC v1.4.7.4 library configuration --- // --- Bosch BSEC v1.4.7.4 library configuration ---
// 3,3V supply voltage; 3s max time between sensor_control calls; 4 days // 3,3V supply voltage; 3s max time between sensor_control calls; 4 days
// calibration. Change this const if not applicable for your application (see // calibration. Change this const if not applicable for your application (see
// BME680 datasheet) // BME680 datasheet)
// Note: 3s max time not exceed BMECYCLE frequency set in paxcounter.conf!
const uint8_t bsec_config_iaq[454] = { const uint8_t bsec_config_iaq[454] = {
4, 7, 4, 1, 61, 0, 0, 0, 0, 0, 0, 0, 174, 1, 0, 4, 7, 4, 1, 61, 0, 0, 0, 0, 0, 0, 0, 174, 1, 0,
0, 48, 0, 1, 0, 0, 192, 168, 71, 64, 49, 119, 76, 0, 0, 0, 48, 0, 1, 0, 0, 192, 168, 71, 64, 49, 119, 76, 0, 0,
@ -54,7 +56,8 @@ const uint8_t bsec_config_iaq[454] = {
// Helper functions declarations // Helper functions declarations
int bme_init(); int bme_init();
void bme_loop(void *pvParameters); void bmecycle(void);
void bme_storedata(bmeStatus_t *bme_store);
int checkIaqSensorStatus(void); int checkIaqSensorStatus(void);
void loadState(void); void loadState(void);
void updateState(void); void updateState(void);

View File

@ -8,12 +8,14 @@
#define TIMESYNC_IRQ 0x010 #define TIMESYNC_IRQ 0x010
#define MASK_IRQ 0x020 #define MASK_IRQ 0x020
#define UNMASK_IRQ 0x040 #define UNMASK_IRQ 0x040
#define BME_IRQ 0x080
#define MATRIX_DISPLAY_IRQ 0x100 #define MATRIX_DISPLAY_IRQ 0x100
#include "globals.h" #include "globals.h"
#include "cyclic.h" #include "cyclic.h"
#include "senddata.h" #include "senddata.h"
#include "timekeeper.h" #include "timekeeper.h"
#include "bmesensor.h"
void irqHandler(void *pvParameters); void irqHandler(void *pvParameters);
void mask_user_IRQ(); void mask_user_IRQ();

View File

@ -5,8 +5,9 @@
// Local logging tag // Local logging tag
static const char TAG[] = __FILE__; static const char TAG[] = __FILE__;
bmeStatus_t bme_status; bmeStatus_t bme_status = {0};
TaskHandle_t BmeTask;
Ticker bmecycler;
#define SEALEVELPRESSURE_HPA (1013.25) #define SEALEVELPRESSURE_HPA (1013.25)
@ -37,6 +38,8 @@ Adafruit_BME280 bme; // I2C
#endif #endif
void bmecycle() { xTaskNotify(irqHandlerTask, BME_IRQ, eSetBits); }
// initialize BME680 sensor // initialize BME680 sensor
int bme_init(void) { int bme_init(void) {
@ -136,47 +139,37 @@ int checkIaqSensorStatus(void) {
} // checkIaqSensorStatus() } // checkIaqSensorStatus()
#endif #endif
// loop function which reads and processes data based on sensor settings // store current BME sensor data in struct
void bme_loop(void *pvParameters) { void bme_storedata(bmeStatus_t *bme_store) {
if (I2C_MUTEX_LOCK()) { // block i2c bus access
configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check
#ifdef HAS_BME680 #ifdef HAS_BME680
while (1) { if (iaqSensor.run()) { // if new data is available
// block i2c bus access bme_store->raw_temperature =
if (I2C_MUTEX_LOCK()) { iaqSensor.rawTemperature; // temperature in degree celsius
if (iaqSensor.run()) { // If new data is available bme_store->raw_humidity = iaqSensor.rawHumidity;
bme_status.raw_temperature = bme_store->temperature = iaqSensor.temperature;
iaqSensor.rawTemperature; // Temperature in degree celsius bme_store->humidity =
bme_status.raw_humidity = iaqSensor.rawHumidity; iaqSensor.humidity; // humidity in % relative humidity x1000
bme_status.temperature = iaqSensor.temperature; bme_store->pressure = // pressure in Pascal
bme_status.humidity = (iaqSensor.pressure / 100.0); // conversion Pa -> hPa
iaqSensor.humidity; // Humidity in % relative humidity x1000 bme_store->iaq = iaqSensor.iaqEstimate;
bme_status.pressure = // Pressure in Pascal bme_store->iaq_accuracy = iaqSensor.iaqAccuracy;
(iaqSensor.pressure / 100.0); // conversion Pa -> hPa bme_store->gas = iaqSensor.gasResistance; // gas resistance in ohms
bme_status.iaq = iaqSensor.iaqEstimate; updateState();
bme_status.iaq_accuracy = iaqSensor.iaqAccuracy;
bme_status.gas = iaqSensor.gasResistance; // Gas resistance in Ohms
updateState();
}
I2C_MUTEX_UNLOCK();
} }
}
#elif defined HAS_BME280 #elif defined HAS_BME280
while (1) { bme_store->temperature = bme.readTemperature();
if (I2C_MUTEX_LOCK()) { bme_store->pressure = (bme.readPressure() / 100.0); // conversion Pa -> hPa
bme_status.temperature = bme.readTemperature(); // bme.readAltitude(SEALEVELPRESSURE_HPA);
bme_status.pressure = bme_store->humidity = bme.readHumidity();
(bme.readPressure() / 100.0); // conversion Pa -> hPa bme_store->iaq = 0; // IAQ feature not present with BME280
// bme.readAltitude(SEALEVELPRESSURE_HPA);
bme_status.humidity = bme.readHumidity();
bme_status.iaq = 0; // IAQ feature not present with BME280
I2C_MUTEX_UNLOCK();
}
}
#endif #endif
} // bme_loop() I2C_MUTEX_UNLOCK(); // release i2c bus access
}
} // bme_storedata()
#ifdef HAS_BME680 #ifdef HAS_BME680
void loadState(void) { void loadState(void) {

View File

@ -38,10 +38,6 @@ void doHousekeeping() {
ESP_LOGD(TAG, "Gpsloop %d bytes left | Taskstate = %d", ESP_LOGD(TAG, "Gpsloop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(GpsTask), eTaskGetState(GpsTask)); uxTaskGetStackHighWaterMark(GpsTask), eTaskGetState(GpsTask));
#endif #endif
#if (HAS_BME)
ESP_LOGD(TAG, "Bmeloop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(BmeTask), eTaskGetState(BmeTask));
#endif
#if (defined HAS_DCF77 || defined HAS_IF482) #if (defined HAS_DCF77 || defined HAS_IF482)
ESP_LOGD(TAG, "Clockloop %d bytes left | Taskstate = %d", ESP_LOGD(TAG, "Clockloop %d bytes left | Taskstate = %d",
uxTaskGetStackHighWaterMark(ClockTask), eTaskGetState(ClockTask)); uxTaskGetStackHighWaterMark(ClockTask), eTaskGetState(ClockTask));
@ -59,16 +55,15 @@ void doHousekeeping() {
ESP_LOGI(TAG, "Voltage: %dmV", batt_voltage); ESP_LOGI(TAG, "Voltage: %dmV", batt_voltage);
#endif #endif
// display BME sensor data // display BME680/280 sensor data
#if (HAS_BME)
#ifdef HAS_BME680 #ifdef HAS_BME680
ESP_LOGI(TAG, "BME680 Temp: %.2f°C | IAQ: %.2f | IAQacc: %d", ESP_LOGI(TAG, "BME680 Temp: %.2f°C | IAQ: %.2f | IAQacc: %d",
bme_status.temperature, bme_status.iaq, bme_status.iaq_accuracy); bme_status.temperature, bme_status.iaq, bme_status.iaq_accuracy);
#endif #elif defined HAS_BME280
// display BME280 sensor data
#ifdef HAS_BME280
ESP_LOGI(TAG, "BME280 Temp: %.2f°C | Humidity: %.2f | Pressure: %.0f", ESP_LOGI(TAG, "BME280 Temp: %.2f°C | Humidity: %.2f | Pressure: %.0f",
bme_status.temperature, bme_status.humidity, bme_status.pressure); bme_status.temperature, bme_status.humidity, bme_status.pressure);
#endif
#endif #endif
// check free heap memory // check free heap memory

View File

@ -45,6 +45,12 @@ void irqHandler(void *pvParameters) {
refreshTheMatrixDisplay(); refreshTheMatrixDisplay();
#endif #endif
// BME sensor data to be read?
#if (HAS_BME)
if (InterruptStatus & BME_IRQ)
bme_storedata(&bme_status);
#endif
// are cyclic tasks due? // are cyclic tasks due?
if (InterruptStatus & CYCLIC_IRQ) if (InterruptStatus & CYCLIC_IRQ)
doHousekeeping(); doHousekeeping();

View File

@ -35,7 +35,6 @@ clockloop 1 4 generates realtime telegrams for external clock
timesync_req 1 3 processes realtime time sync requests timesync_req 1 3 processes realtime time sync requests
irqhandler 1 2 display, timesync, gps, etc. triggered by timers irqhandler 1 2 display, timesync, gps, etc. triggered by timers
gpsloop 1 2 reads data from GPS via serial or i2c gpsloop 1 2 reads data from GPS via serial or i2c
bmeloop 1 2 reads data from BME sensor via i2c
looptask 1 1 runs the LMIC LoRa stack (arduino loop) looptask 1 1 runs the LMIC LoRa stack (arduino loop)
IDLE 1 0 ESP32 arduino scheduler -> runs wifi channel rotator IDLE 1 0 ESP32 arduino scheduler -> runs wifi channel rotator
@ -63,6 +62,7 @@ fired by software (Ticker.h)
TIMESYNC_IRQ -> timeSync() -> irqHandlerTask (Core 1) TIMESYNC_IRQ -> timeSync() -> irqHandlerTask (Core 1)
CYLCIC_IRQ -> housekeeping() -> irqHandlerTask (Core 1) CYLCIC_IRQ -> housekeeping() -> irqHandlerTask (Core 1)
SENDCYCLE_IRQ -> sendcycle() -> irqHandlerTask (Core 1) SENDCYCLE_IRQ -> sendcycle() -> irqHandlerTask (Core 1)
BME_IRQ -> bmecycle() -> irqHandlerTask (Core 1)
// External RTC timer (if present) // External RTC timer (if present)
@ -368,16 +368,8 @@ void setup() {
#elif defined HAS_BME280 #elif defined HAS_BME280
strcat_P(features, " BME280"); strcat_P(features, " BME280");
#endif #endif
if (bme_init()) { if (bme_init())
ESP_LOGI(TAG, "Starting BME sensor..."); ESP_LOGI(TAG, "Starting BME sensor...");
xTaskCreatePinnedToCore(bme_loop, // task function
"bmeloop", // name of task
2048, // stack size of task
(void *)1, // parameter of the task
2, // priority of the task
&BmeTask, // task handle
1); // CPU core
}
#endif #endif
// starting timers and interrupts // starting timers and interrupts
@ -418,6 +410,7 @@ void setup() {
// cyclic function interrupts // cyclic function interrupts
sendcycler.attach(SENDCYCLE * 2, sendcycle); sendcycler.attach(SENDCYCLE * 2, sendcycle);
housekeeper.attach(HOMECYCLE, housekeeping); housekeeper.attach(HOMECYCLE, housekeeping);
bmecycler.attach(BMECYCLE, bmecycle);
#if (TIME_SYNC_INTERVAL) #if (TIME_SYNC_INTERVAL)

View File

@ -48,7 +48,7 @@
#define PAYLOAD_BUFFER_SIZE 51 // maximum size of payload block per transmit #define PAYLOAD_BUFFER_SIZE 51 // maximum size of payload block per transmit
#define LORASFDEFAULT 9 // 7 ... 12 SF, according to LoRaWAN specs #define LORASFDEFAULT 9 // 7 ... 12 SF, according to LoRaWAN specs
#define MAXLORARETRY 500 // maximum count of TX retries if LoRa busy #define MAXLORARETRY 500 // maximum count of TX retries if LoRa busy
#define SEND_QUEUE_SIZE 10 // maximum number of messages in payload send queue [1 = no queue] #define SEND_QUEUE_SIZE 10 // maximum number of messages in payload send queue [1 = no queue]
// Hardware settings // Hardware settings
#define RGBLUMINOSITY 30 // RGB LED luminosity [default = 30%] #define RGBLUMINOSITY 30 // RGB LED luminosity [default = 30%]
@ -58,6 +58,7 @@
// Settings for BME680 environmental sensor // Settings for BME680 environmental sensor
#define BME_TEMP_OFFSET 5.0f // Offset sensor on chip temp <-> ambient temp [default = 5°C] #define BME_TEMP_OFFSET 5.0f // Offset sensor on chip temp <-> ambient temp [default = 5°C]
#define STATE_SAVE_PERIOD UINT32_C(360 * 60 * 1000) // update every 360 minutes = 4 times a day #define STATE_SAVE_PERIOD UINT32_C(360 * 60 * 1000) // update every 360 minutes = 4 times a day
#define BMECYCLE 1 // bme sensor read cycle in seconds [default = 1 secs]
// OTA settings // OTA settings
#define USE_OTA 1 // set to 0 to disable OTA update #define USE_OTA 1 // set to 0 to disable OTA update

View File

@ -8,7 +8,7 @@ static const char TAG[] = __FILE__;
// helper function // helper function
void do_reset() { void do_reset() {
ESP_LOGI(TAG, "Remote command: restart device"); ESP_LOGI(TAG, "Remote command: restart device");
#if(HAS_LORA) #if (HAS_LORA)
LMIC_shutdown(); LMIC_shutdown();
#endif #endif
delay(3000); delay(3000);
@ -132,7 +132,7 @@ void set_gps(uint8_t val[]) {
} }
void set_sensor(uint8_t val[]) { void set_sensor(uint8_t val[]) {
#if(HAS_SENSORS) #if (HAS_SENSORS)
switch (val[0]) { // check if valid sensor number 1...4 switch (val[0]) { // check if valid sensor number 1...4
case 1: case 1:
case 2: case 2:
@ -170,7 +170,7 @@ void set_monitor(uint8_t val[]) {
} }
void set_lorasf(uint8_t val[]) { void set_lorasf(uint8_t val[]) {
#if(HAS_LORA) #if (HAS_LORA)
ESP_LOGI(TAG, "Remote command: set LoRa SF to %d", val[0]); ESP_LOGI(TAG, "Remote command: set LoRa SF to %d", val[0]);
switch_lora(val[0], cfg.txpower); switch_lora(val[0], cfg.txpower);
#else #else
@ -179,7 +179,7 @@ void set_lorasf(uint8_t val[]) {
} }
void set_loraadr(uint8_t val[]) { void set_loraadr(uint8_t val[]) {
#if(HAS_LORA) #if (HAS_LORA)
ESP_LOGI(TAG, "Remote command: set LoRa ADR mode to %s", ESP_LOGI(TAG, "Remote command: set LoRa ADR mode to %s",
val[0] ? "on" : "off"); val[0] ? "on" : "off");
cfg.adrmode = val[0] ? 1 : 0; cfg.adrmode = val[0] ? 1 : 0;
@ -222,7 +222,7 @@ void set_rgblum(uint8_t val[]) {
}; };
void set_lorapower(uint8_t val[]) { void set_lorapower(uint8_t val[]) {
#if(HAS_LORA) #if (HAS_LORA)
ESP_LOGI(TAG, "Remote command: set LoRa TXPOWER to %d", val[0]); ESP_LOGI(TAG, "Remote command: set LoRa TXPOWER to %d", val[0]);
switch_lora(cfg.lorasf, val[0]); switch_lora(cfg.lorasf, val[0]);
#else #else
@ -252,7 +252,7 @@ void get_status(uint8_t val[]) {
void get_gps(uint8_t val[]) { void get_gps(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: get gps status"); ESP_LOGI(TAG, "Remote command: get gps status");
#if(HAS_GPS) #if (HAS_GPS)
gps_storelocation(&gps_status); gps_storelocation(&gps_status);
payload.reset(); payload.reset();
payload.addGPS(gps_status); payload.addGPS(gps_status);