BME task removed / do i2c masking via interrupt
This commit is contained in:
		
							parent
							
								
									11cfa27c76
								
							
						
					
					
						commit
						85aa9655c6
					
				| @ -11,14 +11,16 @@ | ||||
| #include <Adafruit_BME280.h> | ||||
| #endif | ||||
| 
 | ||||
| extern Ticker bmecycler; | ||||
| 
 | ||||
| extern bmeStatus_t | ||||
|     bme_status; // Make struct for storing gps data globally available
 | ||||
| extern TaskHandle_t BmeTask; | ||||
| 
 | ||||
| // --- Bosch BSEC v1.4.7.4 library configuration ---
 | ||||
| // 3,3V supply voltage; 3s max time between sensor_control calls; 4 days
 | ||||
| // calibration. Change this const if not applicable for your application (see
 | ||||
| // BME680 datasheet)
 | ||||
| // Note: 3s max time not exceed BMECYCLE frequency set in paxcounter.conf!
 | ||||
| const uint8_t bsec_config_iaq[454] = { | ||||
|     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, | ||||
| @ -54,7 +56,8 @@ const uint8_t bsec_config_iaq[454] = { | ||||
| 
 | ||||
| // Helper functions declarations
 | ||||
| int bme_init(); | ||||
| void bme_loop(void *pvParameters); | ||||
| void bmecycle(void); | ||||
| void bme_storedata(bmeStatus_t *bme_store); | ||||
| int checkIaqSensorStatus(void); | ||||
| void loadState(void); | ||||
| void updateState(void); | ||||
|  | ||||
| @ -8,12 +8,14 @@ | ||||
| #define TIMESYNC_IRQ 0x010 | ||||
| #define MASK_IRQ 0x020 | ||||
| #define UNMASK_IRQ 0x040 | ||||
| #define BME_IRQ 0x080 | ||||
| #define MATRIX_DISPLAY_IRQ 0x100 | ||||
| 
 | ||||
| #include "globals.h" | ||||
| #include "cyclic.h" | ||||
| #include "senddata.h" | ||||
| #include "timekeeper.h" | ||||
| #include "bmesensor.h" | ||||
| 
 | ||||
| void irqHandler(void *pvParameters); | ||||
| void mask_user_IRQ(); | ||||
|  | ||||
| @ -5,8 +5,9 @@ | ||||
| // Local logging tag
 | ||||
| static const char TAG[] = __FILE__; | ||||
| 
 | ||||
| bmeStatus_t bme_status; | ||||
| TaskHandle_t BmeTask; | ||||
| bmeStatus_t bme_status = {0}; | ||||
| 
 | ||||
| Ticker bmecycler; | ||||
| 
 | ||||
| #define SEALEVELPRESSURE_HPA (1013.25) | ||||
| 
 | ||||
| @ -37,6 +38,8 @@ Adafruit_BME280 bme; // I2C | ||||
| 
 | ||||
| #endif | ||||
| 
 | ||||
| void bmecycle() { xTaskNotify(irqHandlerTask, BME_IRQ, eSetBits); } | ||||
| 
 | ||||
| // initialize BME680 sensor
 | ||||
| int bme_init(void) { | ||||
| 
 | ||||
| @ -136,47 +139,37 @@ int checkIaqSensorStatus(void) { | ||||
| } // checkIaqSensorStatus()
 | ||||
| #endif | ||||
| 
 | ||||
| // loop function which reads and processes data based on sensor settings
 | ||||
| void bme_loop(void *pvParameters) { | ||||
| 
 | ||||
|   configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check
 | ||||
| // store current BME sensor data in struct
 | ||||
| void bme_storedata(bmeStatus_t *bme_store) { | ||||
|   if (I2C_MUTEX_LOCK()) { // block i2c bus access
 | ||||
| 
 | ||||
| #ifdef HAS_BME680 | ||||
|   while (1) { | ||||
|     // block i2c bus access
 | ||||
|     if (I2C_MUTEX_LOCK()) { | ||||
|       if (iaqSensor.run()) { // If new data is available
 | ||||
|         bme_status.raw_temperature = | ||||
|             iaqSensor.rawTemperature; // Temperature in degree celsius
 | ||||
|         bme_status.raw_humidity = iaqSensor.rawHumidity; | ||||
|         bme_status.temperature = iaqSensor.temperature; | ||||
|         bme_status.humidity = | ||||
|             iaqSensor.humidity; // Humidity in % relative humidity x1000
 | ||||
|         bme_status.pressure =   // Pressure in Pascal
 | ||||
|             (iaqSensor.pressure / 100.0); // conversion Pa -> hPa
 | ||||
|         bme_status.iaq = iaqSensor.iaqEstimate; | ||||
|         bme_status.iaq_accuracy = iaqSensor.iaqAccuracy; | ||||
|         bme_status.gas = iaqSensor.gasResistance; // Gas resistance in Ohms
 | ||||
|         updateState(); | ||||
|       } | ||||
|       I2C_MUTEX_UNLOCK(); | ||||
|     if (iaqSensor.run()) { // if new data is available
 | ||||
|       bme_store->raw_temperature = | ||||
|           iaqSensor.rawTemperature; // temperature in degree celsius
 | ||||
|       bme_store->raw_humidity = iaqSensor.rawHumidity; | ||||
|       bme_store->temperature = iaqSensor.temperature; | ||||
|       bme_store->humidity = | ||||
|           iaqSensor.humidity;           // humidity in % relative humidity x1000
 | ||||
|       bme_store->pressure =             // pressure in Pascal
 | ||||
|           (iaqSensor.pressure / 100.0); // conversion Pa -> hPa
 | ||||
|       bme_store->iaq = iaqSensor.iaqEstimate; | ||||
|       bme_store->iaq_accuracy = iaqSensor.iaqAccuracy; | ||||
|       bme_store->gas = iaqSensor.gasResistance; // gas resistance in ohms
 | ||||
|       updateState(); | ||||
|     } | ||||
|   } | ||||
| #elif defined HAS_BME280 | ||||
|   while (1) { | ||||
|     if (I2C_MUTEX_LOCK()) { | ||||
|       bme_status.temperature = bme.readTemperature(); | ||||
|       bme_status.pressure = | ||||
|           (bme.readPressure() / 100.0); // conversion Pa -> hPa
 | ||||
|       // bme.readAltitude(SEALEVELPRESSURE_HPA);
 | ||||
|       bme_status.humidity = bme.readHumidity(); | ||||
|       bme_status.iaq = 0; // IAQ feature not present with BME280
 | ||||
|       I2C_MUTEX_UNLOCK(); | ||||
|     } | ||||
|   } | ||||
|     bme_store->temperature = bme.readTemperature(); | ||||
|     bme_store->pressure = (bme.readPressure() / 100.0); // conversion Pa -> hPa
 | ||||
|     // bme.readAltitude(SEALEVELPRESSURE_HPA);
 | ||||
|     bme_store->humidity = bme.readHumidity(); | ||||
|     bme_store->iaq = 0; // IAQ feature not present with BME280
 | ||||
| #endif | ||||
| 
 | ||||
| } // bme_loop()
 | ||||
|     I2C_MUTEX_UNLOCK(); // release i2c bus access
 | ||||
|   } | ||||
| 
 | ||||
| } // bme_storedata()
 | ||||
| 
 | ||||
| #ifdef HAS_BME680 | ||||
| void loadState(void) { | ||||
|  | ||||
| @ -38,10 +38,6 @@ void doHousekeeping() { | ||||
|   ESP_LOGD(TAG, "Gpsloop %d bytes left | Taskstate = %d", | ||||
|            uxTaskGetStackHighWaterMark(GpsTask), eTaskGetState(GpsTask)); | ||||
| #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) | ||||
|   ESP_LOGD(TAG, "Clockloop %d bytes left | Taskstate = %d", | ||||
|            uxTaskGetStackHighWaterMark(ClockTask), eTaskGetState(ClockTask)); | ||||
| @ -59,16 +55,15 @@ void doHousekeeping() { | ||||
|   ESP_LOGI(TAG, "Voltage: %dmV", batt_voltage); | ||||
| #endif | ||||
| 
 | ||||
| // display BME sensor data
 | ||||
| // display BME680/280 sensor data
 | ||||
| #if (HAS_BME) | ||||
| #ifdef HAS_BME680 | ||||
|   ESP_LOGI(TAG, "BME680 Temp: %.2f°C | IAQ: %.2f | IAQacc: %d", | ||||
|            bme_status.temperature, bme_status.iaq, bme_status.iaq_accuracy); | ||||
| #endif | ||||
| 
 | ||||
| // display BME280 sensor data
 | ||||
| #ifdef HAS_BME280 | ||||
| #elif defined HAS_BME280 | ||||
|   ESP_LOGI(TAG, "BME280 Temp: %.2f°C | Humidity: %.2f | Pressure: %.0f", | ||||
|            bme_status.temperature, bme_status.humidity, bme_status.pressure); | ||||
| #endif | ||||
| #endif | ||||
| 
 | ||||
|   // check free heap memory
 | ||||
|  | ||||
| @ -45,6 +45,12 @@ void irqHandler(void *pvParameters) { | ||||
|       refreshTheMatrixDisplay(); | ||||
| #endif | ||||
| 
 | ||||
| // BME sensor data to be read?
 | ||||
| #if (HAS_BME) | ||||
|     if (InterruptStatus & BME_IRQ) | ||||
|       bme_storedata(&bme_status); | ||||
| #endif | ||||
| 
 | ||||
|     // are cyclic tasks due?
 | ||||
|     if (InterruptStatus & CYCLIC_IRQ) | ||||
|       doHousekeeping(); | ||||
|  | ||||
							
								
								
									
										13
									
								
								src/main.cpp
									
									
									
									
									
								
							
							
						
						
									
										13
									
								
								src/main.cpp
									
									
									
									
									
								
							| @ -35,7 +35,6 @@ clockloop     1     4     generates realtime telegrams for external clock | ||||
| timesync_req  1     3     processes realtime time sync requests | ||||
| irqhandler    1     2     display, timesync, gps, etc. triggered by timers | ||||
| 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) | ||||
| 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) | ||||
| CYLCIC_IRQ      -> housekeeping() -> irqHandlerTask (Core 1) | ||||
| SENDCYCLE_IRQ   -> sendcycle()    -> irqHandlerTask (Core 1) | ||||
| BME_IRQ         -> bmecycle()     -> irqHandlerTask (Core 1) | ||||
| 
 | ||||
| 
 | ||||
| // External RTC timer (if present)
 | ||||
| @ -368,16 +368,8 @@ void setup() { | ||||
| #elif defined HAS_BME280 | ||||
|   strcat_P(features, " BME280"); | ||||
| #endif | ||||
|   if (bme_init()) { | ||||
|   if (bme_init()) | ||||
|     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 | ||||
| 
 | ||||
|   // starting timers and interrupts
 | ||||
| @ -418,6 +410,7 @@ void setup() { | ||||
|   // cyclic function interrupts
 | ||||
|   sendcycler.attach(SENDCYCLE * 2, sendcycle); | ||||
|   housekeeper.attach(HOMECYCLE, housekeeping); | ||||
|   bmecycler.attach(BMECYCLE, bmecycle); | ||||
| 
 | ||||
| #if (TIME_SYNC_INTERVAL) | ||||
| 
 | ||||
|  | ||||
| @ -48,7 +48,7 @@ | ||||
| #define PAYLOAD_BUFFER_SIZE             51      // maximum size of payload block per transmit | ||||
| #define LORASFDEFAULT                   9       // 7 ... 12 SF, according to LoRaWAN specs | ||||
| #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 | ||||
| #define RGBLUMINOSITY                   30      // RGB LED luminosity [default = 30%] | ||||
| @ -58,6 +58,7 @@ | ||||
| // Settings for BME680 environmental sensor | ||||
| #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 BMECYCLE                        1       // bme sensor read cycle in seconds [default = 1 secs] | ||||
| 
 | ||||
| // OTA settings | ||||
| #define USE_OTA                         1       // set to 0 to disable OTA update | ||||
|  | ||||
| @ -8,7 +8,7 @@ static const char TAG[] = __FILE__; | ||||
| // helper function
 | ||||
| void do_reset() { | ||||
|   ESP_LOGI(TAG, "Remote command: restart device"); | ||||
| #if(HAS_LORA) | ||||
| #if (HAS_LORA) | ||||
|   LMIC_shutdown(); | ||||
| #endif | ||||
|   delay(3000); | ||||
| @ -132,7 +132,7 @@ void set_gps(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
 | ||||
|   case 1: | ||||
|   case 2: | ||||
| @ -170,7 +170,7 @@ void set_monitor(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]); | ||||
|   switch_lora(val[0], cfg.txpower); | ||||
| #else | ||||
| @ -179,7 +179,7 @@ void set_lorasf(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", | ||||
|            val[0] ? "on" : "off"); | ||||
|   cfg.adrmode = val[0] ? 1 : 0; | ||||
| @ -222,7 +222,7 @@ void set_rgblum(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]); | ||||
|   switch_lora(cfg.lorasf, val[0]); | ||||
| #else | ||||
| @ -252,7 +252,7 @@ void get_status(uint8_t val[]) { | ||||
| 
 | ||||
| void get_gps(uint8_t val[]) { | ||||
|   ESP_LOGI(TAG, "Remote command: get gps status"); | ||||
| #if(HAS_GPS) | ||||
| #if (HAS_GPS) | ||||
|   gps_storelocation(&gps_status); | ||||
|   payload.reset(); | ||||
|   payload.addGPS(gps_status); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user