Some clean up

This commit is contained in:
Maximilian Grau 2021-11-02 01:16:45 +01:00
parent 9405b3d8b8
commit 4f58326d4c
4 changed files with 59 additions and 123 deletions

View file

@ -9,10 +9,7 @@
constexpr size_t OledWidth = 128; constexpr size_t OledWidth = 128;
constexpr size_t OledPages = 4; constexpr size_t OledPages = 4;
namespace constexpr auto UsedSpiPeripherie = &hspi2;
{
constexpr auto DisplaySpiPeripherie = &hspi2;
} // namespace
extern QueueHandle_t spiMutex; extern QueueHandle_t spiMutex;
extern void waitForSpiFinished(); extern void waitForSpiFinished();
@ -28,7 +25,7 @@ public:
setCommandPin(); setCommandPin();
setChipSelect(true); setChipSelect(true);
HAL_SPI_Transmit_DMA(DisplaySpiPeripherie, &cmd, 1); HAL_SPI_Transmit_DMA(UsedSpiPeripherie, &cmd, 1);
waitForSpiFinished(); waitForSpiFinished();
setChipSelect(false); setChipSelect(false);
@ -42,7 +39,7 @@ public:
setDataPin(); setDataPin();
setChipSelect(true); setChipSelect(true);
HAL_SPI_Transmit_DMA(DisplaySpiPeripherie, &data, 1); HAL_SPI_Transmit_DMA(UsedSpiPeripherie, &data, 1);
waitForSpiFinished(); waitForSpiFinished();
setChipSelect(false); setChipSelect(false);
@ -59,7 +56,7 @@ public:
setDataPin(); setDataPin();
setChipSelect(true); setChipSelect(true);
HAL_SPI_Transmit_DMA(DisplaySpiPeripherie, const_cast<uint8_t *>(data), length); HAL_SPI_Transmit_DMA(UsedSpiPeripherie, const_cast<uint8_t *>(data), length);
waitForSpiFinished(); waitForSpiFinished();
setChipSelect(false); setChipSelect(false);

View file

@ -16,63 +16,58 @@
extern QueueHandle_t spiMutex; extern QueueHandle_t spiMutex;
extern void waitForSpiFinished(); extern void waitForSpiFinished();
extern Renderer renderer; extern Renderer renderer;
extern void initDisplay(); extern void initDisplay();
constexpr auto MaximumChars = 22 * 4; namespace
char buffer[MaximumChars]; {
constexpr auto SpiPeripherie = &hspi2;
uint8_t txBuffer[512 + 1]; 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_dev bmeSensor;
struct bme68x_conf bmeConf; struct bme68x_conf bmeConf;
struct bme68x_heatr_conf bmeHeaterConf; struct bme68x_heatr_conf bmeHeaterConf;
struct bme68x_data bmeData[3]; struct bme68x_data bmeData[3];
uint32_t delayInUs;
uint8_t numberOfData; uint8_t numberOfData;
constexpr auto ProfileLength = 1;
// Heater temperature in degree Celsius // Heater temperature in degree Celsius
uint16_t temperatureProfile[ProfileLength] = {320}; uint16_t temperatureProfile[HeaterProfileLength] = {320};
// Heating duration in milliseconds // Heating duration in milliseconds
uint16_t durationProfile[ProfileLength] = {150}; uint16_t durationProfile[HeaterProfileLength] = {150};
constexpr uint8_t numberRequestedVirtualSensors = 4; constexpr uint8_t numberRequestedVirtualSensors = 4;
bsec_sensor_configuration_t requestedVirtualSensors[numberRequestedVirtualSensors]; bsec_sensor_configuration_t requestedVirtualSensors[numberRequestedVirtualSensors];
float iaq, rawTemperature, pressure, rawHumidity, gasResistance, stabStatus, runInStatus, float iaq, temperature, humidity, co2Equivalent;
temperature, humidity, staticIaq, co2Equivalent, breathVocEquivalent, compGasValue, uint8_t iaqAccuracy, co2Accuracy;
gasPercentage;
uint8_t iaqAccuracy, staticIaqAccuracy, co2Accuracy, breathVocAccuracy, compGasAccuracy,
gasPercentageAcccuracy;
uint8_t bsecState[BSEC_MAX_STATE_BLOB_SIZE]; uint8_t bsecState[BSEC_MAX_STATE_BLOB_SIZE];
uint8_t workBuffer[BSEC_MAX_WORKBUFFER_SIZE]; uint8_t workBuffer[BSEC_MAX_WORKBUFFER_SIZE];
constexpr uintptr_t EepromAddress = FLASH_EEPROM_BASE; constexpr uintptr_t EepromAddress = FLASH_EEPROM_BASE;
//--------------------------------------------------------------------------------------------------
void setChipSelect(bool state) void setChipSelect(bool state)
{ {
HAL_GPIO_WritePin(VocSensorCS_GPIO_Port, VocSensorCS_Pin, HAL_GPIO_WritePin(VocSensorCS_GPIO_Port, VocSensorCS_Pin,
state ? GPIO_PIN_RESET : GPIO_PIN_SET); state ? GPIO_PIN_RESET : GPIO_PIN_SET);
} }
//--------------------------------------------------------------------------------------------------
// SPI read function map // SPI read function map
BME68X_INTF_RET_TYPE bme68x_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *) BME68X_INTF_RET_TYPE bme68x_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *)
{ {
xSemaphoreTake(spiMutex, portMAX_DELAY); xSemaphoreTake(spiMutex, portMAX_DELAY);
setChipSelect(true); setChipSelect(true);
HAL_SPI_Transmit_DMA(SpiPeripherie, &reg_addr, 1); HAL_SPI_Transmit_DMA(UsedSpiPeripherie, &reg_addr, 1);
waitForSpiFinished(); waitForSpiFinished();
HAL_SPI_Receive_DMA(SpiPeripherie, reg_data, len); HAL_SPI_Receive_DMA(UsedSpiPeripherie, reg_data, len);
waitForSpiFinished(); waitForSpiFinished();
setChipSelect(false); setChipSelect(false);
@ -81,6 +76,7 @@ BME68X_INTF_RET_TYPE bme68x_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32
return 0; return 0;
} }
//--------------------------------------------------------------------------------------------------
// SPI write function map // SPI write function map
BME68X_INTF_RET_TYPE bme68x_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, BME68X_INTF_RET_TYPE bme68x_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len,
void *) 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); xSemaphoreTake(spiMutex, portMAX_DELAY);
setChipSelect(true); setChipSelect(true);
HAL_SPI_Transmit_DMA(SpiPeripherie, const_cast<uint8_t *>(txBuffer), len + 1); HAL_SPI_Transmit_DMA(UsedSpiPeripherie, const_cast<uint8_t *>(txBuffer), len + 1);
waitForSpiFinished(); waitForSpiFinished();
setChipSelect(false); setChipSelect(false);
@ -103,34 +99,29 @@ BME68X_INTF_RET_TYPE bme68x_spi_write(uint8_t reg_addr, const uint8_t *reg_data,
return 0; return 0;
} }
//--------------------------------------------------------------------------------------------------
// Delay function maps // Delay function maps
void bme68x_delay_us(uint32_t period, void *) void bme68x_delay_us(uint32_t period, void *)
{ {
vTaskDelay(period / 1000); 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->read = bme68x_spi_read; bme->intf = BME68X_SPI_INTF;
bme->write = bme68x_spi_write;
bme->intf = BME68X_SPI_INTF;
bme->delay_us = bme68x_delay_us; bme->delay_us = bme68x_delay_us;
bme->amb_temp = bme->amb_temp = 25; /* The ambient temperature in deg C is used for defining the heater
25; /* The ambient temperature in deg C is used for defining the heater temperature */ temperature */
}
else
{
rslt = BME68X_E_NULL_PTR;
}
return rslt;
} }
//--------------------------------------------------------------------------------------------------
void bmeSensorInit() void bmeSensorInit()
{ {
bme68x_spi_init(&bmeSensor); bme68x_spi_init(&bmeSensor);
@ -147,22 +138,22 @@ void bmeSensorInit()
bmeHeaterConf.enable = BME68X_ENABLE; bmeHeaterConf.enable = BME68X_ENABLE;
bmeHeaterConf.heatr_temp_prof = temperatureProfile; bmeHeaterConf.heatr_temp_prof = temperatureProfile;
bmeHeaterConf.heatr_dur_prof = durationProfile; bmeHeaterConf.heatr_dur_prof = durationProfile;
bmeHeaterConf.profile_len = ProfileLength; bmeHeaterConf.profile_len = HeaterProfileLength;
bme68x_set_heatr_conf(BME68X_SEQUENTIAL_MODE, &bmeHeaterConf, &bmeSensor); bme68x_set_heatr_conf(BME68X_SEQUENTIAL_MODE, &bmeHeaterConf, &bmeSensor);
bme68x_set_op_mode(BME68X_SEQUENTIAL_MODE, &bmeSensor); bme68x_set_op_mode(BME68X_SEQUENTIAL_MODE, &bmeSensor);
bsec_init(); 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].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].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].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].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 // Allocate a struct for the returned physical sensor settings
bsec_sensor_configuration_t requiredSensorSettings[BSEC_MAX_PHYSICAL_SENSOR]; bsec_sensor_configuration_t requiredSensorSettings[BSEC_MAX_PHYSICAL_SENSOR];
@ -173,43 +164,25 @@ void bmeSensorInit()
requiredSensorSettings, &numberRequiredSensorSettings); requiredSensorSettings, &numberRequiredSensorSettings);
} }
//--------------------------------------------------------------------------------------------------
void bmeRun() void bmeRun()
{ {
delayInUs = bme68x_get_meas_dur(BME68X_SEQUENTIAL_MODE, &bmeConf, &bmeSensor) + uint32_t delayInUs = bme68x_get_meas_dur(BME68X_SEQUENTIAL_MODE, &bmeConf, &bmeSensor) +
(bmeHeaterConf.heatr_dur_prof[0] * 1000); (bmeHeaterConf.heatr_dur_prof[0] * 1000);
vTaskDelay(delayInUs / 1000); vTaskDelay(delayInUs / 1000);
auto status = bme68x_get_data(BME68X_SEQUENTIAL_MODE, bmeData, &numberOfData, &bmeSensor); auto status = bme68x_get_data(BME68X_SEQUENTIAL_MODE, bmeData, &numberOfData, &bmeSensor);
if (status != 0)
{
__asm("bkpt");
}
} }
//--------------------------------------------------------------------------------------------------
void bsecRun() 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)) if (!(bmeData[numberOfData - 1].status & BME68X_NEW_DATA_MSK))
{
__asm("bkpt");
return; return;
}
bsec_input_t inputs[BSEC_MAX_PHYSICAL_SENSOR]; bsec_input_t inputs[BSEC_MAX_PHYSICAL_SENSOR];
uint8_t nInputs = 0, nOutputs = 0; uint8_t nInputs = 0;
int64_t currentTimeInNs = xTaskGetTickCount() * int64_t(1000) * int64_t(1000); int64_t currentTimeInNs = xTaskGetTickCount() * int64_t(1'000'000);
inputs[nInputs].sensor_id = BSEC_INPUT_TEMPERATURE; inputs[nInputs].sensor_id = BSEC_INPUT_TEMPERATURE;
inputs[nInputs].signal = bmeData[numberOfData - 1].temperature / 100.0f; inputs[nInputs].signal = bmeData[numberOfData - 1].temperature / 100.0f;
@ -232,25 +205,19 @@ void bsecRun()
nInputs++; nInputs++;
inputs[nInputs].sensor_id = BSEC_INPUT_HEATSOURCE; inputs[nInputs].sensor_id = BSEC_INPUT_HEATSOURCE;
inputs[nInputs].signal = temperatureOffset; inputs[nInputs].signal = TemperatureOffset;
inputs[nInputs].time_stamp = currentTimeInNs; inputs[nInputs].time_stamp = currentTimeInNs;
nInputs++;
uint8_t nOutputs = 0;
nOutputs = BSEC_NUMBER_OUTPUTS; nOutputs = BSEC_NUMBER_OUTPUTS;
bsec_output_t outputs[BSEC_NUMBER_OUTPUTS]; bsec_output_t outputs[BSEC_NUMBER_OUTPUTS];
auto status = bsec_do_steps(inputs, nInputs, outputs, &nOutputs); auto status = bsec_do_steps(inputs, nInputs, outputs, &nOutputs);
if (status != BSEC_OK) if (status != BSEC_OK)
{
return; return;
}
// zeroOutputs();
if (nOutputs > 0) if (nOutputs > 0)
{ {
auto outputTimestamp = outputs[0].time_stamp / 1000000; /* Convert from ns to ms */
for (uint8_t i = 0; i < nOutputs; i++) for (uint8_t i = 0; i < nOutputs; i++)
{ {
switch (outputs[i].sensor_id) switch (outputs[i].sensor_id)
@ -259,50 +226,16 @@ void bsecRun()
iaq = outputs[i].signal; iaq = outputs[i].signal;
iaqAccuracy = outputs[i].accuracy; iaqAccuracy = outputs[i].accuracy;
break; break;
case BSEC_OUTPUT_STATIC_IAQ:
staticIaq = outputs[i].signal;
staticIaqAccuracy = outputs[i].accuracy;
break;
case BSEC_OUTPUT_CO2_EQUIVALENT: case BSEC_OUTPUT_CO2_EQUIVALENT:
co2Equivalent = outputs[i].signal; co2Equivalent = outputs[i].signal;
co2Accuracy = outputs[i].accuracy; co2Accuracy = outputs[i].accuracy;
break; 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: case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE:
temperature = outputs[i].signal; temperature = outputs[i].signal;
break; break;
case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY: case BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY:
humidity = outputs[i].signal; humidity = outputs[i].signal;
break; 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: default:
break; break;
} }
@ -310,6 +243,7 @@ void bsecRun()
} }
} }
//--------------------------------------------------------------------------------------------------
void printBmeSensorData() void printBmeSensorData()
{ {
renderer.clearAll(); renderer.clearAll();
@ -335,9 +269,9 @@ void printBmeSensorData()
renderer.render(); renderer.render();
} }
//--------------------------------------------------------------------------------------------------
void readStateFromEeprom() void readStateFromEeprom()
{ {
uint8_t sizeOfData = *reinterpret_cast<uint8_t *>(EepromAddress); uint8_t sizeOfData = *reinterpret_cast<uint8_t *>(EepromAddress);
if (sizeOfData != BSEC_MAX_STATE_BLOB_SIZE) 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)); bsec_set_state(bsecState, BSEC_MAX_STATE_BLOB_SIZE, workBuffer, sizeof(workBuffer));
} }
//--------------------------------------------------------------------------------------------------
void writeStateToEeprom() void writeStateToEeprom()
{ {
// only write calibrated state to EEPROM
if (iaqAccuracy != 3) if (iaqAccuracy != 3)
return; return;
uint32_t numberSerializedState = BSEC_MAX_STATE_BLOB_SIZE; uint32_t numberSerializedState = BSEC_MAX_STATE_BLOB_SIZE;
auto status = bsec_get_state(0, bsecState, BSEC_MAX_STATE_BLOB_SIZE, workBuffer, auto status = bsec_get_state(0, bsecState, BSEC_MAX_STATE_BLOB_SIZE, workBuffer,
BSEC_MAX_STATE_BLOB_SIZE, &numberSerializedState); BSEC_MAX_STATE_BLOB_SIZE, &numberSerializedState);
@ -367,18 +302,19 @@ void writeStateToEeprom()
HAL_FLASHEx_DATAEEPROM_Unlock(); HAL_FLASHEx_DATAEEPROM_Unlock();
// write size // write state array size
HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE, EepromAddress, HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE, EepromAddress,
BSEC_MAX_STATE_BLOB_SIZE); BSEC_MAX_STATE_BLOB_SIZE);
for (uint8_t i = 0; i < BSEC_MAX_STATE_BLOB_SIZE; i++) 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]); bsecState[i]);
} }
HAL_FLASHEx_DATAEEPROM_Lock(); HAL_FLASHEx_DATAEEPROM_Lock();
} }
} // namespace
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------
extern "C" void sensorTask(void *) extern "C" void sensorTask(void *)
@ -395,10 +331,10 @@ extern "C" void sensorTask(void *)
bsecRun(); bsecRun();
printBmeSensorData(); printBmeSensorData();
if (counter++ >= 100) if (counter++ >= 1000)
{ {
initDisplay();
counter = 0; counter = 0;
initDisplay();
writeStateToEeprom(); writeStateToEeprom();
} }

View file

@ -48,6 +48,7 @@ extern "C" void prvGetRegistersFromStack(uint32_t *pulFaultStackAddress)
#endif #endif
} }
//--------------------------------------------------------------------------------------------------
extern "C" void hard_fault_handler(void) extern "C" void hard_fault_handler(void)
{ {
/* /*
@ -69,6 +70,7 @@ extern "C" void hard_fault_handler(void)
".syntax divided\n"); ".syntax divided\n");
} }
//--------------------------------------------------------------------------------------------------
extern "C" void vApplicationMallocFailedHook(void) extern "C" void vApplicationMallocFailedHook(void)
{ {
#ifdef DEBUG #ifdef DEBUG
@ -80,6 +82,7 @@ extern "C" void vApplicationMallocFailedHook(void)
#endif #endif
} }
//--------------------------------------------------------------------------------------------------
extern "C" void vApplicationStackOverflowHook(xTaskHandle *pxTask, signed portCHAR *pcTaskName) extern "C" void vApplicationStackOverflowHook(xTaskHandle *pxTask, signed portCHAR *pcTaskName)
{ {
(void)pxTask; (void)pxTask;

View file

@ -11,7 +11,7 @@
// oled display // oled display
SSD1306_SPI ssdSpiInterface; SSD1306_SPI ssdSpiInterface;
Display display(ssdSpiInterface); Display display(ssdSpiInterface);
Renderer renderer(128, 4, display); Renderer renderer(OledWidth, OledPages, display);
QueueHandle_t spiMutex = xSemaphoreCreateMutex(); QueueHandle_t spiMutex = xSemaphoreCreateMutex();
QueueHandle_t spiBinary = xSemaphoreCreateBinary(); QueueHandle_t spiBinary = xSemaphoreCreateBinary();