From e7dd0ea1f6ceebce8854adad655ba9d9f8aebafb Mon Sep 17 00:00:00 2001 From: Matthias Date: Sun, 24 Mar 2024 16:30:26 +0100 Subject: [PATCH] Added and integrated bmp280 temperature sensor --- code/main/CMakeLists.txt | 2 +- code/main/application/inc/ota.h | 7 +- code/main/application/src/ota.cpp | 74 ++++++++++++++++-- code/main/main.cpp | 27 ++++++- code/main/{old => platform}/inc/bmp280.h | 50 +++++++----- code/main/{old => platform}/src/bmp280.cpp | 89 +++++++++++----------- code/sdkconfig | 19 +++-- 7 files changed, 188 insertions(+), 80 deletions(-) rename code/main/{old => platform}/inc/bmp280.h (76%) rename code/main/{old => platform}/src/bmp280.cpp (63%) diff --git a/code/main/CMakeLists.txt b/code/main/CMakeLists.txt index 0900cda..4867d1d 100644 --- a/code/main/CMakeLists.txt +++ b/code/main/CMakeLists.txt @@ -4,12 +4,12 @@ idf_component_register( SRCS # list the source files of this component "main.cpp" -# "old/src/bmp280.cpp" "hal/src/esplog.cpp" "hal/src/i2c.cpp" "hal/src/gpio.cpp" "hal/src/uart.cpp" + "platform/src/bmp280.cpp" "platform/src/isl29125.cpp" "platform/src/logger.cpp" "platform/src/prgm_ledstrip.cpp" diff --git a/code/main/application/inc/ota.h b/code/main/application/inc/ota.h index d05b407..93b919c 100644 --- a/code/main/application/inc/ota.h +++ b/code/main/application/inc/ota.h @@ -40,6 +40,7 @@ // ProjectIncludes // All include files that are provided by the project #include "esp_event.h" +#include "esp_http_client.h" // -------------------------------------------------------------------------------------------------------------------- // Constant and macro definitions @@ -65,6 +66,9 @@ class ota static const uint32_t checkInterval_ms = 10000; + typedef void (*updateStatusCallback)(int); + static updateStatusCallback usCallback; + // Class Constructor ota(); @@ -79,7 +83,8 @@ class ota // Private Section // ----------------------------------------------------------------------------------------------------------------- private: - int imageSize; + static bool updateActive; + static int imageSize; static void eventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data); }; diff --git a/code/main/application/src/ota.cpp b/code/main/application/src/ota.cpp index b363970..38e4d84 100644 --- a/code/main/application/src/ota.cpp +++ b/code/main/application/src/ota.cpp @@ -21,6 +21,7 @@ #include "esp_crt_bundle.h" #include "esp_https_ota.h" +#include "esp_http_client.h" #include "logger.h" @@ -50,10 +51,12 @@ // Function definitions // -------------------------------------------------------------------------------------------------------------------- - +ota::updateStatusCallback ota::usCallback = NULL; +int ota::imageSize = 0; +bool ota::updateActive = false; ota::ota() { - + ESP_ERROR_CHECK(esp_event_handler_register(ESP_HTTPS_OTA_EVENT, ESP_EVENT_ANY_ID, &eventHandler, NULL)); } @@ -104,6 +107,10 @@ void ota::eventHandler(void* arg, esp_event_base_t event_base, int32_t event_id, LOGGER_INFO("OTA aborted"); break; } + if (usCallback != NULL) + { + usCallback(5); + } } } @@ -118,30 +125,33 @@ void ota::task(void) config.timeout_ms = 60000; config.keep_alive_enable = true; config.crt_bundle_attach = esp_crt_bundle_attach; +// config.event_handler = eventHandler; esp_https_ota_config_t ota_config = { .http_config = &config, .http_client_init_cb = NULL, .bulk_flash_erase = true, .partial_http_download = false, - .max_http_request_size = 4096, + .max_http_request_size = 0, }; LOGGER_INFO("OTA task executing"); // First try to download specific firmware - config.url = "http://10.10.1.32:8989/code.bin"; + config.url = "https://esp.vbchaos.nl/code.bin"; LOGGER_INFO("Trying URL %s", config.url); err = esp_https_ota_begin(&ota_config, &https_ota_handle); if(err != ESP_OK) + + updateActive = false; { LOGGER_ERROR("OTA connection failed"); esp_https_ota_abort(https_ota_handle); // No specific firmware found, try generic firmware - config.url = "http://10.10.1.32:8989/code.bin"; + config.url = "https://esp.vbchaos.nl/code.bin"; LOGGER_INFO("Trying URL %s", config.url); err = esp_https_ota_begin(&ota_config, &https_ota_handle); @@ -165,6 +175,18 @@ void ota::task(void) } } + if (err == ESP_OK) + { + LOGGER_INFO("App description: Magicword: %i", app_desc.magic_word); + LOGGER_INFO("App description: SecureVer: %i", app_desc.secure_version); + LOGGER_INFO("App description: version: %s", app_desc.version); + LOGGER_INFO("App description: PrjctName: %s", app_desc.project_name); + LOGGER_INFO("App description: Time: %s", app_desc.time); + LOGGER_INFO("App description: Date: %s", app_desc.date); + LOGGER_INFO("App description: IDF Ver: %s", app_desc.idf_ver); + LOGGER_INFO("App description: SHA256 %s", app_desc.app_elf_sha256); + } + if(err == ESP_OK) @@ -180,4 +202,46 @@ void ota::task(void) } } + if(err == ESP_OK) + { + esp_err_t otaStatus; + + do + { + // esp_https_ota_perform returns after every read operation which gives user the ability to + // monitor the status of OTA upgrade by calling esp_https_ota_get_image_len_read, which gives length of image + // data read so far. + //Logger_log("Image bytes read: %d", esp_https_ota_get_image_len_read(https_ota_handle)); + otaStatus = esp_https_ota_perform(https_ota_handle); + } while(otaStatus == ESP_ERR_HTTPS_OTA_IN_PROGRESS); + + if(!esp_https_ota_is_complete_data_received(https_ota_handle)) + { + // the OTA image was not completely received and user can customise the response to this situation. + LOGGER_ERROR("Complete data was not received."); + err = ESP_FAIL; + esp_https_ota_abort(https_ota_handle); + + } + } + + if(err == ESP_OK) + { + err = esp_https_ota_finish(https_ota_handle); + + if(err != ESP_OK) + { + LOGGER_ERROR("Image validation failed, image is corrupted"); + } + } + + if(err == ESP_OK) + { + LOGGER_SUCCESS("OTA upgrade successful"); + + LOGGER_INFO("Rebooting"); + vTaskDelay(100); + esp_restart(); + } + } diff --git a/code/main/main.cpp b/code/main/main.cpp index c3c658e..56fd7fe 100644 --- a/code/main/main.cpp +++ b/code/main/main.cpp @@ -34,6 +34,7 @@ #include "uart.h" // Platform includes +#include "bmp280.h" #include "isl29125.h" #include "logger.h" #include "ledmatrix.h" @@ -80,6 +81,7 @@ static TaskHandle_t loggerTaskHandle; static TaskHandle_t otaTaskHandle; + // -------------------------------------------------------------------------------------------------------------------- // Function declarations // -------------------------------------------------------------------------------------------------------------------- @@ -87,6 +89,8 @@ static TaskHandle_t otaTaskHandle; void loggerTask(void* parameters); void otaTask(void* parameters); +static void otaCallback(int value); + // -------------------------------------------------------------------------------------------------------------------- // Function definitions // -------------------------------------------------------------------------------------------------------------------- @@ -142,7 +146,7 @@ extern "C" void app_main(void) // // logger debugLogger = logger(16, uartDebug); - logger debugLogger = logger(16, esplogger); + logger debugLogger = logger(20, esplogger); // Call the logger executable within a dedicated task and forget about it afterwards xTaskCreate(loggerTask, (const char*)"loggerTask", 3000, &debugLogger, 3, &loggerTaskHandle); @@ -193,6 +197,21 @@ extern "C" void app_main(void) // struct isl29125::rgb_t rgbValue; // rgbSensor.getRGB(&rgbValue); +// // ----------------------------------------------------------------------------------------------------------------- +// // I2C RGB Sensor on I2C MasterBus for sensors +// // +// bmp280 tempSensor = bmp280(0x76, i2cSensor); +// // Reset the sensor +// tempSensor.resetSensor(); +// // Make sure to apply a wait cycle between reset and continuous use - 2ms is advised as minimum +// vTaskDelay(10); +// // Initialize the BMP280 +// tempSensor.initialize(); +// // Set the temperature Oversampling +// tempSensor.setSensorTemperatureOversampling(bmp280::BMP280_Oversampling_t::X1); +// // Set the sensor to NORMAL mode +// tempSensor.setSensorMode(bmp280::BMP280_Mode_t::NORMAL); + // ----------------------------------------------------------------------------------------------------------------- // Wifi create and connect // @@ -231,6 +250,7 @@ extern "C" void app_main(void) // OTA handler // ota otaUpdater = ota(); + otaUpdater.usCallback = otaCallback; // Call the OTA executable within a dedicated task and forget about it afterwards xTaskCreate(otaTask, (const char*)"OTATask", 4000, &otaUpdater, 3, &otaTaskHandle); @@ -276,3 +296,8 @@ void otaTask(void* parameters) vTaskDelay(otaHandler->checkInterval_ms); } } + +void otaCallback(int value) +{ + LOGGER_WARNING("Current OTA value is %i", value); +} diff --git a/code/main/old/inc/bmp280.h b/code/main/platform/inc/bmp280.h similarity index 76% rename from code/main/old/inc/bmp280.h rename to code/main/platform/inc/bmp280.h index 4d8866b..851f30f 100644 --- a/code/main/old/inc/bmp280.h +++ b/code/main/platform/inc/bmp280.h @@ -1,10 +1,10 @@ // -------------------------------------------------------------------------------------------------------------------- -/// \file bme280.h +/// \file bmp280.h /// \brief File description // -------------------------------------------------------------------------------------------------------------------- // // vbchaos software design -// +// // -------------------------------------------------------------------------------------------------------------------- /// $Revision: $ /// $Author: $ @@ -13,12 +13,12 @@ // -------------------------------------------------------------------------------------------------------------------- -#ifndef MAIN_INC_BMP280_H_ -#define MAIN_INC_BMP280_H_ +#ifndef MAIN_APPLICATION_INC_BMP280_H_ +#define MAIN_APPLICATION_INC_BMP280_H_ /** - * bme280 implementation - * \defgroup bme280 + * bmp280 implementation + * \defgroup bmp280 * \brief {group_description} * \addtogroup {Layer} * @@ -29,39 +29,44 @@ // -------------------------------------------------------------------------------------------------------------------- -// Include files +// Include files // -------------------------------------------------------------------------------------------------------------------- // CompilerIncludes // All include files that are provided by the compiler directly - +#include // ProjectIncludes // All include files that are provided by the project -#include "i2c.h" +#include "ISerialBus.h" + // -------------------------------------------------------------------------------------------------------------------- -// Constant and macro definitions +// Constant and macro definitions // -------------------------------------------------------------------------------------------------------------------- #define BMP280_DEVICE_ID ((uint8_t)0x58) #define BMP280_RESET_VALUE ((uint8_t)0xB6) // -------------------------------------------------------------------------------------------------------------------- -// Type definitions. +// Type definitions. // -------------------------------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------------------------------- -// Function declarations +// Function declarations // -------------------------------------------------------------------------------------------------------------------- -class BMP280 +class bmp280 { + // ----------------------------------------------------------------------------------------------------------------- + // Public Section + // ----------------------------------------------------------------------------------------------------------------- public: - BMP280(I2C* bus, uint8_t slaveAddress); + // Class Constructor + bmp280(uint8_t slaveAddress, ISerialBus& serialPort); typedef enum { @@ -86,8 +91,16 @@ class BMP280 int getTemperature(void); - private: + // ----------------------------------------------------------------------------------------------------------------- + // Protected Section + // ----------------------------------------------------------------------------------------------------------------- + protected: + + // ----------------------------------------------------------------------------------------------------------------- + // Private Section + // ----------------------------------------------------------------------------------------------------------------- + private: struct CompensationParameters { // Temperature compensation parameters @@ -141,8 +154,9 @@ class BMP280 int t_fine; int temperature; - I2C* bus; uint8_t slaveAddress; + ISerialBus& bus; + bool initialized; BMP280_Mode_t mode; void resetDriver(void); @@ -162,7 +176,7 @@ class BMP280 }; - /** @} */ -#endif /* MAIN_INC_BMP280_H_ */ + +#endif /* MAIN_APPLICATION_INC_OTA_H_ */ diff --git a/code/main/old/src/bmp280.cpp b/code/main/platform/src/bmp280.cpp similarity index 63% rename from code/main/old/src/bmp280.cpp rename to code/main/platform/src/bmp280.cpp index fe9ad01..cc14101 100644 --- a/code/main/old/src/bmp280.cpp +++ b/code/main/platform/src/bmp280.cpp @@ -2,7 +2,7 @@ /// \file bme280.cpp /// \brief Description // -------------------------------------------------------------------------------------------------------------------- -// +// // vbchaos software design // // -------------------------------------------------------------------------------------------------------------------- @@ -26,19 +26,19 @@ // -------------------------------------------------------------------------------------------------------------------- // List of registers -#define ADDRESS_COMP_PARAMETERS ((uint32_t)0x88) +#define ADDRESS_COMP_PARAMETERS ((uint8_t)0x88) -#define ADDRESS_REG_ID ((uint32_t)0xD0) -#define ADDRESS_REG_RESET ((uint32_t)0xE0) -#define ADDRESS_REG_STATUS ((uint32_t)0xF3) -#define ADDRESS_REG_CTRL_MEAS ((uint32_t)0xF4) -#define ADDRESS_REG_CONFIG ((uint32_t)0xF5) -#define ADDRESS_REG_PRESSURE_MSB ((uint32_t)0xF7) -#define ADDRESS_REG_PRESSURE_LSB ((uint32_t)0xF8) -#define ADDRESS_REG_PRESSURE_XLSB ((uint32_t)0xF9) -#define ADDRESS_REG_TEMPERATURE_MSB ((uint32_t)0xFA) -#define ADDRESS_REG_TEMPERATURE_LSB ((uint32_t)0xFB) -#define ADDRESS_REG_TEMPERATURE_XLSB ((uint32_t)0xFC) +#define ADDRESS_REG_ID ((uint8_t)0xD0) +#define ADDRESS_REG_RESET ((uint8_t)0xE0) +#define ADDRESS_REG_STATUS ((uint8_t)0xF3) +#define ADDRESS_REG_CTRL_MEAS ((uint8_t)0xF4) +#define ADDRESS_REG_CONFIG ((uint8_t)0xF5) +#define ADDRESS_REG_PRESSURE_MSB ((uint8_t)0xF7) +#define ADDRESS_REG_PRESSURE_LSB ((uint8_t)0xF8) +#define ADDRESS_REG_PRESSURE_XLSB ((uint8_t)0xF9) +#define ADDRESS_REG_TEMPERATURE_MSB ((uint8_t)0xFA) +#define ADDRESS_REG_TEMPERATURE_LSB ((uint8_t)0xFB) +#define ADDRESS_REG_TEMPERATURE_XLSB ((uint8_t)0xFC) // -------------------------------------------------------------------------------------------------------------------- // Type definitions @@ -62,23 +62,20 @@ // -------------------------------------------------------------------------------------------------------------------- -BMP280::BMP280(I2C* bus, uint8_t slaveAddress) +bmp280::bmp280(uint8_t slaveAddress, ISerialBus& serialPort) : slaveAddress {slaveAddress}, bus {serialPort} { - // Take over the bus - BMP280::bus = bus; - // Take over the device slave address - BMP280::slaveAddress = slaveAddress; + initialized = false; // Reset the driver itself resetDriver(); // Reset the device } -void BMP280::resetSensor(void) +void bmp280::resetSensor(void) { resetDevice(); } -bool BMP280::initialize(void) +bool bmp280::initialize(void) { bool returnValue = true; @@ -98,7 +95,7 @@ bool BMP280::initialize(void) return returnValue; } -bool BMP280::setSensorMode(BMP280_Mode_t mode) +bool bmp280::setSensorMode(BMP280_Mode_t mode) { bool returnValue = true; memorymap.ctrl_meas.mode = mode; @@ -106,7 +103,7 @@ bool BMP280::setSensorMode(BMP280_Mode_t mode) return returnValue; } -bool BMP280::setSensorTemperatureOversampling(BMP280_Oversampling_t oversampling) +bool bmp280::setSensorTemperatureOversampling(BMP280_Oversampling_t oversampling) { bool returnValue = true; memorymap.ctrl_meas.oversampling_temp = oversampling; @@ -114,68 +111,72 @@ bool BMP280::setSensorTemperatureOversampling(BMP280_Oversampling_t oversampling return returnValue; } -int BMP280::getTemperature(void) +int bmp280::getTemperature(void) { // Get latest raw values from device - BMP280::getTemperatureValues(); + getTemperatureValues(); // Calculate temperature - BMP280::compensateTemperature(); + compensateTemperature(); // return the value return temperature; } -void BMP280::resetDriver(void) +void bmp280::resetDriver(void) { // Reset the parameters - BMP280::compensationParameters = {}; + compensationParameters = {}; // Reset the device memory map - BMP280::memorymap = {}; + memorymap = {}; // Reset the mode - BMP280::mode = STANDBY; + mode = STANDBY; // Reset calculation values t_fine = 0; temperature = 0; } -void BMP280::getDeviceID(void) +void bmp280::getDeviceID(void) { - bus->read_register(slaveAddress, ADDRESS_REG_ID, &memorymap.id, 1); + uint32_t actualLength; + bus.read(slaveAddress, ADDRESS_REG_ID, &memorymap.id, 1, &actualLength); } -void BMP280::resetDevice(void) +void bmp280::resetDevice(void) { uint8_t resetValue = BMP280_RESET_VALUE; - bus->write_register(slaveAddress, ADDRESS_REG_RESET, &resetValue, 1); + bus.write(slaveAddress, ADDRESS_REG_RESET, &resetValue, 1); } -void BMP280::setSensorControlMeasurement(void) +void bmp280::setSensorControlMeasurement(void) { - bus->write_register(slaveAddress, ADDRESS_REG_CTRL_MEAS, (uint8_t*)&memorymap.ctrl_meas, 1); + bus.write(slaveAddress, ADDRESS_REG_CTRL_MEAS, (uint8_t*)&memorymap.ctrl_meas, 1); } -void BMP280::setSensorConfiguration(void) +void bmp280::setSensorConfiguration(void) { - bus->write_register(slaveAddress, ADDRESS_REG_CONFIG, (uint8_t*)&memorymap.config, 1); + bus.write(slaveAddress, ADDRESS_REG_CONFIG, (uint8_t*)&memorymap.config, 1); } -void BMP280::getCompensationValues(void) +void bmp280::getCompensationValues(void) { - bus->read_register(slaveAddress, ADDRESS_COMP_PARAMETERS, (uint8_t*)&compensationParameters, sizeof(compensationParameters)); + uint32_t actualLength; + bus.read(slaveAddress, ADDRESS_COMP_PARAMETERS, (uint8_t*)&compensationParameters, sizeof(compensationParameters), &actualLength); LOGGER_DEBUG("Got compensation values: %04X %04X %04X", compensationParameters.dig_T1, compensationParameters.dig_T2, compensationParameters.dig_T3); } -void BMP280::getPreasureValues(void) +void bmp280::getPreasureValues(void) { - bus->read_register(slaveAddress, ADDRESS_REG_PRESSURE_LSB, (uint8_t*)&memorymap.pressure_raw, sizeof(memorymap.pressure_raw)); + uint32_t actualLength; + bus.read(slaveAddress, ADDRESS_REG_PRESSURE_LSB, (uint8_t*)&memorymap.pressure_raw, sizeof(memorymap.pressure_raw), &actualLength); } -void BMP280::getTemperatureValues(void) +void bmp280::getTemperatureValues(void) { - bus->read_register(slaveAddress, ADDRESS_REG_TEMPERATURE_MSB, (uint8_t*)&memorymap.temperature_raw, sizeof(memorymap.temperature_raw)); + uint32_t actualLength; + bus.read(slaveAddress, ADDRESS_REG_TEMPERATURE_MSB, (uint8_t*)&memorymap.temperature_raw, sizeof(memorymap.temperature_raw), &actualLength); } -void BMP280::compensateTemperature(void) +void bmp280::compensateTemperature(void) { int adc_T = 0; // Create a single temperature value from the individual memory entries diff --git a/code/sdkconfig b/code/sdkconfig index 9a10b0e..86845d2 100644 --- a/code/sdkconfig +++ b/code/sdkconfig @@ -379,12 +379,12 @@ CONFIG_ESPTOOLPY_MONITOR_BAUD=115200 # # Partition Table # -CONFIG_PARTITION_TABLE_SINGLE_APP=y +# CONFIG_PARTITION_TABLE_SINGLE_APP is not set # CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE is not set -# CONFIG_PARTITION_TABLE_TWO_OTA is not set +CONFIG_PARTITION_TABLE_TWO_OTA=y # CONFIG_PARTITION_TABLE_CUSTOM is not set CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv" -CONFIG_PARTITION_TABLE_FILENAME="partitions_singleapp.csv" +CONFIG_PARTITION_TABLE_FILENAME="partitions_two_ota.csv" CONFIG_PARTITION_TABLE_OFFSET=0x8000 CONFIG_PARTITION_TABLE_MD5=y # end of Partition Table @@ -399,8 +399,8 @@ CONFIG_ESP_WIFI_PASSWORD="mypassword" # # Compiler options # -CONFIG_COMPILER_OPTIMIZATION_DEFAULT=y -# CONFIG_COMPILER_OPTIMIZATION_SIZE is not set +# CONFIG_COMPILER_OPTIMIZATION_DEFAULT is not set +CONFIG_COMPILER_OPTIMIZATION_SIZE=y # CONFIG_COMPILER_OPTIMIZATION_PERF is not set # CONFIG_COMPILER_OPTIMIZATION_NONE is not set CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_ENABLE=y @@ -1011,7 +1011,6 @@ CONFIG_FREERTOS_TASK_NOTIFICATION_ARRAY_ENTRIES=1 # # Port # -CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER=y # CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set CONFIG_FREERTOS_TLSP_DELETION_CALLBACKS=y # CONFIG_FREERTOS_ENABLE_STATIC_TASK_CLEAN_UP is not set @@ -1627,10 +1626,10 @@ CONFIG_LOG_BOOTLOADER_LEVEL=3 CONFIG_FLASHMODE_DIO=y # CONFIG_FLASHMODE_DOUT is not set CONFIG_MONITOR_BAUD=115200 -CONFIG_OPTIMIZATION_LEVEL_DEBUG=y -CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG=y -# CONFIG_OPTIMIZATION_LEVEL_RELEASE is not set -# CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE is not set +# CONFIG_OPTIMIZATION_LEVEL_DEBUG is not set +# CONFIG_COMPILER_OPTIMIZATION_LEVEL_DEBUG is not set +CONFIG_OPTIMIZATION_LEVEL_RELEASE=y +CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE=y CONFIG_OPTIMIZATION_ASSERTIONS_ENABLED=y # CONFIG_OPTIMIZATION_ASSERTIONS_SILENT is not set # CONFIG_OPTIMIZATION_ASSERTIONS_DISABLED is not set