From 4f58326d4c2d68da95e8fe5ff39b8f185d53f25c Mon Sep 17 00:00:00 2001 From: Maximilian Grau Date: Tue, 2 Nov 2021 01:16:45 +0100 Subject: [PATCH] Some clean up --- src/SSD1306_SPI.hpp | 11 ++- src/bmeSPI.cxx | 166 ++++++++++++++------------------------------ src/handlers.cxx | 3 + src/main.cxx | 2 +- 4 files changed, 59 insertions(+), 123 deletions(-) diff --git a/src/SSD1306_SPI.hpp b/src/SSD1306_SPI.hpp index 9c2da9a..0264a4a 100644 --- a/src/SSD1306_SPI.hpp +++ b/src/SSD1306_SPI.hpp @@ -9,10 +9,7 @@ constexpr size_t OledWidth = 128; constexpr size_t OledPages = 4; -namespace -{ -constexpr auto DisplaySpiPeripherie = &hspi2; -} // namespace +constexpr auto UsedSpiPeripherie = &hspi2; extern QueueHandle_t spiMutex; extern void waitForSpiFinished(); @@ -28,7 +25,7 @@ public: setCommandPin(); setChipSelect(true); - HAL_SPI_Transmit_DMA(DisplaySpiPeripherie, &cmd, 1); + HAL_SPI_Transmit_DMA(UsedSpiPeripherie, &cmd, 1); waitForSpiFinished(); setChipSelect(false); @@ -42,7 +39,7 @@ public: setDataPin(); setChipSelect(true); - HAL_SPI_Transmit_DMA(DisplaySpiPeripherie, &data, 1); + HAL_SPI_Transmit_DMA(UsedSpiPeripherie, &data, 1); waitForSpiFinished(); setChipSelect(false); @@ -59,7 +56,7 @@ public: setDataPin(); setChipSelect(true); - HAL_SPI_Transmit_DMA(DisplaySpiPeripherie, const_cast(data), length); + HAL_SPI_Transmit_DMA(UsedSpiPeripherie, const_cast(data), length); waitForSpiFinished(); setChipSelect(false); diff --git a/src/bmeSPI.cxx b/src/bmeSPI.cxx index 1f6e422..7918e0e 100644 --- a/src/bmeSPI.cxx +++ b/src/bmeSPI.cxx @@ -16,63 +16,58 @@ extern QueueHandle_t spiMutex; extern void waitForSpiFinished(); extern Renderer renderer; - extern void initDisplay(); -constexpr auto MaximumChars = 22 * 4; -char buffer[MaximumChars]; - -constexpr auto SpiPeripherie = &hspi2; +namespace +{ uint8_t txBuffer[512 + 1]; -constexpr auto temperatureOffset = 7.0f; +constexpr auto HeaterProfileLength = 1; +constexpr auto TemperatureOffset = 7.0f; +constexpr auto MaximumChars = 22 * 4; +char buffer[MaximumChars]; struct bme68x_dev bmeSensor; struct bme68x_conf bmeConf; struct bme68x_heatr_conf bmeHeaterConf; struct bme68x_data bmeData[3]; -uint32_t delayInUs; uint8_t numberOfData; -constexpr auto ProfileLength = 1; - // Heater temperature in degree Celsius -uint16_t temperatureProfile[ProfileLength] = {320}; +uint16_t temperatureProfile[HeaterProfileLength] = {320}; // Heating duration in milliseconds -uint16_t durationProfile[ProfileLength] = {150}; +uint16_t durationProfile[HeaterProfileLength] = {150}; constexpr uint8_t numberRequestedVirtualSensors = 4; bsec_sensor_configuration_t requestedVirtualSensors[numberRequestedVirtualSensors]; -float iaq, rawTemperature, pressure, rawHumidity, gasResistance, stabStatus, runInStatus, - temperature, humidity, staticIaq, co2Equivalent, breathVocEquivalent, compGasValue, - gasPercentage; - -uint8_t iaqAccuracy, staticIaqAccuracy, co2Accuracy, breathVocAccuracy, compGasAccuracy, - gasPercentageAcccuracy; +float iaq, temperature, humidity, co2Equivalent; +uint8_t iaqAccuracy, co2Accuracy; uint8_t bsecState[BSEC_MAX_STATE_BLOB_SIZE]; uint8_t workBuffer[BSEC_MAX_WORKBUFFER_SIZE]; constexpr uintptr_t EepromAddress = FLASH_EEPROM_BASE; +//-------------------------------------------------------------------------------------------------- void setChipSelect(bool state) { HAL_GPIO_WritePin(VocSensorCS_GPIO_Port, VocSensorCS_Pin, state ? GPIO_PIN_RESET : GPIO_PIN_SET); } +//-------------------------------------------------------------------------------------------------- // SPI read function map BME68X_INTF_RET_TYPE bme68x_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *) { xSemaphoreTake(spiMutex, portMAX_DELAY); setChipSelect(true); - HAL_SPI_Transmit_DMA(SpiPeripherie, ®_addr, 1); + HAL_SPI_Transmit_DMA(UsedSpiPeripherie, ®_addr, 1); waitForSpiFinished(); - HAL_SPI_Receive_DMA(SpiPeripherie, reg_data, len); + HAL_SPI_Receive_DMA(UsedSpiPeripherie, reg_data, len); waitForSpiFinished(); setChipSelect(false); @@ -81,6 +76,7 @@ BME68X_INTF_RET_TYPE bme68x_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32 return 0; } +//-------------------------------------------------------------------------------------------------- // SPI write function map BME68X_INTF_RET_TYPE bme68x_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *) @@ -94,7 +90,7 @@ BME68X_INTF_RET_TYPE bme68x_spi_write(uint8_t reg_addr, const uint8_t *reg_data, xSemaphoreTake(spiMutex, portMAX_DELAY); setChipSelect(true); - HAL_SPI_Transmit_DMA(SpiPeripherie, const_cast(txBuffer), len + 1); + HAL_SPI_Transmit_DMA(UsedSpiPeripherie, const_cast(txBuffer), len + 1); waitForSpiFinished(); setChipSelect(false); @@ -103,34 +99,29 @@ BME68X_INTF_RET_TYPE bme68x_spi_write(uint8_t reg_addr, const uint8_t *reg_data, return 0; } +//-------------------------------------------------------------------------------------------------- // Delay function maps void bme68x_delay_us(uint32_t period, void *) { vTaskDelay(period / 1000); } -int8_t bme68x_spi_init(struct bme68x_dev *bme) +//-------------------------------------------------------------------------------------------------- +void bme68x_spi_init(struct bme68x_dev *bme) { - int8_t rslt = BME68X_OK; + if (bme == NULL) + return; - if (bme != NULL) - { - bme->read = bme68x_spi_read; - bme->write = bme68x_spi_write; - bme->intf = BME68X_SPI_INTF; + bme->read = bme68x_spi_read; + bme->write = bme68x_spi_write; + bme->intf = BME68X_SPI_INTF; - bme->delay_us = bme68x_delay_us; - bme->amb_temp = - 25; /* The ambient temperature in deg C is used for defining the heater temperature */ - } - else - { - rslt = BME68X_E_NULL_PTR; - } - - return rslt; + bme->delay_us = bme68x_delay_us; + bme->amb_temp = 25; /* The ambient temperature in deg C is used for defining the heater + temperature */ } +//-------------------------------------------------------------------------------------------------- void bmeSensorInit() { bme68x_spi_init(&bmeSensor); @@ -147,22 +138,22 @@ void bmeSensorInit() bmeHeaterConf.enable = BME68X_ENABLE; bmeHeaterConf.heatr_temp_prof = temperatureProfile; bmeHeaterConf.heatr_dur_prof = durationProfile; - bmeHeaterConf.profile_len = ProfileLength; + bmeHeaterConf.profile_len = HeaterProfileLength; bme68x_set_heatr_conf(BME68X_SEQUENTIAL_MODE, &bmeHeaterConf, &bmeSensor); bme68x_set_op_mode(BME68X_SEQUENTIAL_MODE, &bmeSensor); bsec_init(); - // Change 3 virtual sensors (switch IAQ and raw temperature -> on / pressure -> off + // create 3 virtual sensor requestedVirtualSensors[0].sensor_id = BSEC_OUTPUT_IAQ; - requestedVirtualSensors[0].sample_rate = BSEC_SAMPLE_RATE_CONTINUOUS; + requestedVirtualSensors[0].sample_rate = BSEC_SAMPLE_RATE_LP; requestedVirtualSensors[1].sensor_id = BSEC_OUTPUT_CO2_EQUIVALENT; - requestedVirtualSensors[1].sample_rate = BSEC_SAMPLE_RATE_CONTINUOUS; + requestedVirtualSensors[1].sample_rate = BSEC_SAMPLE_RATE_LP; requestedVirtualSensors[2].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE; - requestedVirtualSensors[2].sample_rate = BSEC_SAMPLE_RATE_CONTINUOUS; + requestedVirtualSensors[2].sample_rate = BSEC_SAMPLE_RATE_LP; requestedVirtualSensors[3].sensor_id = BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY; - requestedVirtualSensors[3].sample_rate = BSEC_SAMPLE_RATE_CONTINUOUS; + requestedVirtualSensors[3].sample_rate = BSEC_SAMPLE_RATE_LP; // Allocate a struct for the returned physical sensor settings bsec_sensor_configuration_t requiredSensorSettings[BSEC_MAX_PHYSICAL_SENSOR]; @@ -173,43 +164,25 @@ void bmeSensorInit() requiredSensorSettings, &numberRequiredSensorSettings); } +//-------------------------------------------------------------------------------------------------- void bmeRun() { - delayInUs = bme68x_get_meas_dur(BME68X_SEQUENTIAL_MODE, &bmeConf, &bmeSensor) + - (bmeHeaterConf.heatr_dur_prof[0] * 1000); + uint32_t delayInUs = bme68x_get_meas_dur(BME68X_SEQUENTIAL_MODE, &bmeConf, &bmeSensor) + + (bmeHeaterConf.heatr_dur_prof[0] * 1000); vTaskDelay(delayInUs / 1000); auto status = bme68x_get_data(BME68X_SEQUENTIAL_MODE, bmeData, &numberOfData, &bmeSensor); - if (status != 0) - { - __asm("bkpt"); - } } +//-------------------------------------------------------------------------------------------------- void bsecRun() { - /* - auto status = bsec_set_state(state, BSEC_MAX_STATE_BLOB_SIZE, workBuffer, sizeof(workBuffer)); - - if (status == BSEC_OK) - { - for (uint32_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE; i++) - { - bsecState[i] = state[i]; - } - validBsecState = true; - } - */ - if (!(bmeData[numberOfData - 1].status & BME68X_NEW_DATA_MSK)) - { - __asm("bkpt"); return; - } bsec_input_t inputs[BSEC_MAX_PHYSICAL_SENSOR]; - uint8_t nInputs = 0, nOutputs = 0; - int64_t currentTimeInNs = xTaskGetTickCount() * int64_t(1000) * int64_t(1000); + uint8_t nInputs = 0; + int64_t currentTimeInNs = xTaskGetTickCount() * int64_t(1'000'000); inputs[nInputs].sensor_id = BSEC_INPUT_TEMPERATURE; inputs[nInputs].signal = bmeData[numberOfData - 1].temperature / 100.0f; @@ -232,25 +205,19 @@ void bsecRun() nInputs++; inputs[nInputs].sensor_id = BSEC_INPUT_HEATSOURCE; - inputs[nInputs].signal = temperatureOffset; + inputs[nInputs].signal = TemperatureOffset; inputs[nInputs].time_stamp = currentTimeInNs; - nInputs++; + uint8_t nOutputs = 0; nOutputs = BSEC_NUMBER_OUTPUTS; bsec_output_t outputs[BSEC_NUMBER_OUTPUTS]; auto status = bsec_do_steps(inputs, nInputs, outputs, &nOutputs); if (status != BSEC_OK) - { return; - } - - // zeroOutputs(); if (nOutputs > 0) { - auto outputTimestamp = outputs[0].time_stamp / 1000000; /* Convert from ns to ms */ - for (uint8_t i = 0; i < nOutputs; i++) { switch (outputs[i].sensor_id) @@ -259,50 +226,16 @@ void bsecRun() iaq = outputs[i].signal; iaqAccuracy = outputs[i].accuracy; break; - case BSEC_OUTPUT_STATIC_IAQ: - staticIaq = outputs[i].signal; - staticIaqAccuracy = outputs[i].accuracy; - break; case BSEC_OUTPUT_CO2_EQUIVALENT: co2Equivalent = outputs[i].signal; co2Accuracy = outputs[i].accuracy; break; - case BSEC_OUTPUT_BREATH_VOC_EQUIVALENT: - breathVocEquivalent = outputs[i].signal; - breathVocAccuracy = outputs[i].accuracy; - break; - case BSEC_OUTPUT_RAW_TEMPERATURE: - rawTemperature = outputs[i].signal; - break; - case BSEC_OUTPUT_RAW_PRESSURE: - pressure = outputs[i].signal; - break; - case BSEC_OUTPUT_RAW_HUMIDITY: - rawHumidity = outputs[i].signal; - break; - case BSEC_OUTPUT_RAW_GAS: - gasResistance = outputs[i].signal; - break; - case BSEC_OUTPUT_STABILIZATION_STATUS: - stabStatus = outputs[i].signal; - break; - case BSEC_OUTPUT_RUN_IN_STATUS: - runInStatus = outputs[i].signal; - break; case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE: temperature = outputs[i].signal; break; case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY: humidity = outputs[i].signal; break; - case BSEC_OUTPUT_COMPENSATED_GAS: - compGasValue = outputs[i].signal; - compGasAccuracy = outputs[i].accuracy; - break; - case BSEC_OUTPUT_GAS_PERCENTAGE: - gasPercentage = outputs[i].signal; - gasPercentageAcccuracy = outputs[i].accuracy; - break; default: break; } @@ -310,6 +243,7 @@ void bsecRun() } } +//-------------------------------------------------------------------------------------------------- void printBmeSensorData() { renderer.clearAll(); @@ -335,9 +269,9 @@ void printBmeSensorData() renderer.render(); } +//-------------------------------------------------------------------------------------------------- void readStateFromEeprom() { - uint8_t sizeOfData = *reinterpret_cast(EepromAddress); if (sizeOfData != BSEC_MAX_STATE_BLOB_SIZE) @@ -352,13 +286,14 @@ void readStateFromEeprom() bsec_set_state(bsecState, BSEC_MAX_STATE_BLOB_SIZE, workBuffer, sizeof(workBuffer)); } +//-------------------------------------------------------------------------------------------------- void writeStateToEeprom() { + // only write calibrated state to EEPROM if (iaqAccuracy != 3) return; uint32_t numberSerializedState = BSEC_MAX_STATE_BLOB_SIZE; - auto status = bsec_get_state(0, bsecState, BSEC_MAX_STATE_BLOB_SIZE, workBuffer, BSEC_MAX_STATE_BLOB_SIZE, &numberSerializedState); @@ -367,18 +302,19 @@ void writeStateToEeprom() HAL_FLASHEx_DATAEEPROM_Unlock(); - // write size + // write state array size HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE, EepromAddress, BSEC_MAX_STATE_BLOB_SIZE); for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE; i++) { - HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE, EepromAddress + i + 1, + HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE, EepromAddress + 1 + i, bsecState[i]); } HAL_FLASHEx_DATAEEPROM_Lock(); } +} // namespace //-------------------------------------------------------------------------------------------------- extern "C" void sensorTask(void *) @@ -395,10 +331,10 @@ extern "C" void sensorTask(void *) bsecRun(); printBmeSensorData(); - if (counter++ >= 100) + if (counter++ >= 1000) { - initDisplay(); counter = 0; + initDisplay(); writeStateToEeprom(); } diff --git a/src/handlers.cxx b/src/handlers.cxx index 3439494..96eeec4 100644 --- a/src/handlers.cxx +++ b/src/handlers.cxx @@ -48,6 +48,7 @@ extern "C" void prvGetRegistersFromStack(uint32_t *pulFaultStackAddress) #endif } +//-------------------------------------------------------------------------------------------------- extern "C" void hard_fault_handler(void) { /* @@ -69,6 +70,7 @@ extern "C" void hard_fault_handler(void) ".syntax divided\n"); } +//-------------------------------------------------------------------------------------------------- extern "C" void vApplicationMallocFailedHook(void) { #ifdef DEBUG @@ -80,6 +82,7 @@ extern "C" void vApplicationMallocFailedHook(void) #endif } +//-------------------------------------------------------------------------------------------------- extern "C" void vApplicationStackOverflowHook(xTaskHandle *pxTask, signed portCHAR *pcTaskName) { (void)pxTask; diff --git a/src/main.cxx b/src/main.cxx index e734397..f1db1b7 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -11,7 +11,7 @@ // oled display SSD1306_SPI ssdSpiInterface; Display display(ssdSpiInterface); -Renderer renderer(128, 4, display); +Renderer renderer(OledWidth, OledPages, display); QueueHandle_t spiMutex = xSemaphoreCreateMutex(); QueueHandle_t spiBinary = xSemaphoreCreateBinary();