Working BME680 task
This commit is contained in:
parent
68ca0c551a
commit
76a062dfeb
17 changed files with 401 additions and 496 deletions
src
164
src/bmeSPI.cxx
Normal file
164
src/bmeSPI.cxx
Normal file
|
@ -0,0 +1,164 @@
|
|||
#include "FreeRTOS.h"
|
||||
#include "gpio.h"
|
||||
#include "main.h"
|
||||
#include "spi.h"
|
||||
#include "task.h"
|
||||
#include <cstring>
|
||||
|
||||
#include "BME68x-Sensor-API/bme68x.h"
|
||||
|
||||
constexpr auto SPI_DEVICE = &hspi2;
|
||||
uint8_t txBuffer[512];
|
||||
|
||||
struct bme68x_dev bmeSensor;
|
||||
volatile int8_t res;
|
||||
struct bme68x_conf bmeConf;
|
||||
struct bme68x_heatr_conf bmeHeaterConf;
|
||||
struct bme68x_data bmeData[3];
|
||||
|
||||
uint16_t del_period;
|
||||
uint32_t time_ms = 0;
|
||||
uint8_t n_fields;
|
||||
uint16_t sampleCount = 1;
|
||||
|
||||
/* Heater temperature in degree Celsius */
|
||||
uint16_t temp_prof[10] = {200, 240, 280, 320, 360, 360, 320, 280, 240, 200};
|
||||
|
||||
/* Heating duration in milliseconds */
|
||||
uint16_t dur_prof[10] = {100, 100, 100, 100, 100, 100, 100, 100, 100, 100};
|
||||
|
||||
void setChipSelect(bool state)
|
||||
{
|
||||
HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, state ? GPIO_PIN_RESET : GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
void waitForSpiFinished()
|
||||
{
|
||||
ulTaskNotifyTake(pdTRUE, portMAX_DELAY);
|
||||
}
|
||||
|
||||
// SPI read function map
|
||||
BME68X_INTF_RET_TYPE bme68x_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *)
|
||||
{
|
||||
setChipSelect(true);
|
||||
HAL_SPI_Transmit_DMA(SPI_DEVICE, ®_addr, 1);
|
||||
waitForSpiFinished();
|
||||
HAL_SPI_Receive_DMA(SPI_DEVICE, reg_data, len);
|
||||
waitForSpiFinished();
|
||||
setChipSelect(false);
|
||||
|
||||
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 *)
|
||||
{
|
||||
if (len > 512)
|
||||
return 1;
|
||||
|
||||
txBuffer[0] = reg_addr;
|
||||
std::memcpy(&txBuffer[1], reg_data, len);
|
||||
|
||||
setChipSelect(true);
|
||||
HAL_SPI_Transmit_DMA(SPI_DEVICE, const_cast<uint8_t *>(txBuffer), len + 1);
|
||||
waitForSpiFinished();
|
||||
setChipSelect(false);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Delay function maps
|
||||
void bme68x_delay_us(uint32_t period, void *)
|
||||
{
|
||||
HAL_Delay(period / 1000);
|
||||
}
|
||||
|
||||
int8_t bme68x_spi_init(struct bme68x_dev *bme)
|
||||
{
|
||||
int8_t rslt = BME68X_OK;
|
||||
|
||||
if (bme != NULL)
|
||||
{
|
||||
// printf("SPI Interface\n");
|
||||
bme->read = bme68x_spi_read;
|
||||
bme->write = bme68x_spi_write;
|
||||
bme->intf = BME68X_SPI_INTF;
|
||||
|
||||
bme->delay_us = bme68x_delay_us;
|
||||
bme->intf_ptr = SPI_DEVICE;
|
||||
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;
|
||||
}
|
||||
|
||||
void bme68x_check_rslt(const char *, int8_t)
|
||||
{
|
||||
}
|
||||
|
||||
void bmeRun()
|
||||
{
|
||||
res = bme68x_spi_init(&bmeSensor);
|
||||
bme68x_check_rslt("bme68x_interface_init", res);
|
||||
|
||||
res = bme68x_init(&bmeSensor);
|
||||
bme68x_check_rslt("bme68x_init", res);
|
||||
|
||||
/* Check if res == BME68X_OK, report or handle if otherwise */
|
||||
res = bme68x_get_conf(&bmeConf, &bmeSensor);
|
||||
bme68x_check_rslt("bme68x_get_conf", res);
|
||||
|
||||
/* Check if res == BME68X_OK, report or handle if otherwise */
|
||||
bmeConf.filter = BME68X_FILTER_OFF;
|
||||
bmeConf.odr =
|
||||
BME68X_ODR_NONE; /* This parameter defines the sleep duration after each profile */
|
||||
bmeConf.os_hum = BME68X_OS_16X;
|
||||
bmeConf.os_pres = BME68X_OS_1X;
|
||||
bmeConf.os_temp = BME68X_OS_2X;
|
||||
res = bme68x_set_conf(&bmeConf, &bmeSensor);
|
||||
bme68x_check_rslt("bme68x_set_conf", res);
|
||||
|
||||
/* Check if res == BME68X_OK, report or handle if otherwise */
|
||||
bmeHeaterConf.enable = BME68X_ENABLE;
|
||||
bmeHeaterConf.heatr_temp_prof = temp_prof;
|
||||
bmeHeaterConf.heatr_dur_prof = dur_prof;
|
||||
bmeHeaterConf.profile_len = 10;
|
||||
res = bme68x_set_heatr_conf(BME68X_SEQUENTIAL_MODE, &bmeHeaterConf, &bmeSensor);
|
||||
bme68x_check_rslt("bme68x_set_heatr_conf", res);
|
||||
|
||||
/* Check if res == BME68X_OK, report or handle if otherwise */
|
||||
res = bme68x_set_op_mode(BME68X_SEQUENTIAL_MODE, &bmeSensor);
|
||||
bme68x_check_rslt("bme68x_set_op_mode", res);
|
||||
|
||||
/* Check if res == BME68X_OK, report or handle if otherwise */
|
||||
// printf("Sample, TimeStamp(ms), Temperature(deg C), Pressure(Pa), Humidity(%%), Gas "
|
||||
// "resistance(ohm), Status, Profile index, Measurement index\n");
|
||||
while (1)
|
||||
{
|
||||
del_period =
|
||||
bme68x_get_meas_dur(BME68X_SEQUENTIAL_MODE, &bmeConf) + bmeHeaterConf.heatr_dur_prof[0];
|
||||
vTaskDelay(del_period);
|
||||
|
||||
// time_ms = HAL_GetTick();
|
||||
|
||||
res = bme68x_get_data(BME68X_SEQUENTIAL_MODE, bmeData, &n_fields, &bmeSensor);
|
||||
bme68x_check_rslt("bme68x_get_data", res);
|
||||
|
||||
/* Check if res == BME68X_OK, report or handle if otherwise */
|
||||
for (uint8_t i = 0; i < n_fields; i++)
|
||||
{
|
||||
// printf("%u, %u, %d, %u, %u, %u, 0x%x, %d, %d\n", sampleCount,
|
||||
// time_ms,(bmeData[i].temperature / 100), bmeData[i].pressure, (bmeData[i].humidity /
|
||||
// 1000),bmeData[i].gas_resistance,
|
||||
// bmeData[i].status,data[i].gas_index,data[i].meas_index);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue