Added basic bmp280 structure - functional

temperature readout must be added and compensation must be checked
This commit is contained in:
Matthias Mitscherlich
2023-02-08 12:14:27 +01:00
parent 35c940ca5b
commit edeaedeb07
5 changed files with 220 additions and 41 deletions
+87 -1
View File
@@ -39,12 +39,14 @@
// ProjectIncludes
// All include files that are provided by the project
#include "i2c.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
#define BMP280_DEVICE_ID ((uint8_t)0x58)
#define BMP280_RESET_VALUE ((uint8_t)0xB6)
// --------------------------------------------------------------------------------------------------------------------
// Type definitions.
@@ -59,9 +61,93 @@
class BMP280
{
public:
BMP280(I2C* bus, uint8_t slaveAddress);
typedef enum
{
STANDBY = 0,
FORCED = 1,
NORMAL = 3
} BMP280_Mode_t;
typedef enum
{
SKIPPED = 0,
X1 = 1,
X2 = 2,
X4 = 3,
X8 = 4,
X16 = 5
} BMP280_Oversampling_t;
void resetSensor(void);
bool initialize(void);
bool setSensorMode(BMP280_Mode_t mode);
bool setSensorTemperatureOversampling(BMP280_Oversampling_t oversampling);
private:
struct CompensationParameters
{
// Temperature compensation parameters
uint16_t dig_T1;
int16_t dig_T2;
int16_t dig_T3;
// Pressure compensation parameters
uint16_t dig_P1;
int16_t dig_P2;
int16_t dig_P3;
int16_t dig_P4;
int16_t dig_P5;
int16_t dig_P6;
int16_t dig_P7;
int16_t dig_P8;
int16_t dig_P9;
} compensationParameters;
struct memorymap
{
struct
{
uint8_t msb;
uint8_t lsb;
uint8_t xlsb;
} temperature_raw;
struct
{
uint8_t msb;
uint8_t lsb;
uint8_t xlsb;
} pressure_raw;
struct
{
uint8_t mode : 2;
uint8_t oversampling_pressure : 3;
uint8_t oversampling_temp : 3;
} config;
uint8_t ctrl_meas;
uint8_t status;
uint8_t reset;
uint8_t id;
} memorymap;
int t_fine;
int temperature;
I2C* bus;
uint8_t slaveAddress;
BMP280_Mode_t mode;
void resetDriver(void);
// Communication with Device
void resetDevice(void);
void getDeviceID(void);
void setSensorConfiguration(void);
void getCompensationValues(void);
void compensateTemperature(void);
};
+16 -39
View File
@@ -110,27 +110,10 @@ static void devTask(void* parameters);
static void colourMapTask(void* parameters);
static int bmp280_compensate_T_int32(int adc_T);
// --------------------------------------------------------------------------------------------------------------------
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
int t_fine;
uint16_t dig_T1 = 0x6AA3;
int16_t dig_T2 = 0x6555;
int16_t dig_T3 = 0x0032;
int bmp280_compensate_T_int32(int adc_T)
{
int var1, var2, T;
var1 = ((((adc_T>>3) - ((int)dig_T1<<1))) * ((int)dig_T2)) >> 11;
var2 = (((((adc_T>>4) - ((int)dig_T1)) * ((adc_T>>4) - ((int)dig_T1))) >> 12) * ((int)dig_T3)) >> 14;
t_fine = var1 + var2;
T = (t_fine * 5 + 128) >> 8;
return T;
}
extern "C" void app_main(void)
{
esp_log_level_set("*", ESP_LOG_WARN);
@@ -209,22 +192,27 @@ extern "C" void app_main(void)
// SourceData: GPIO 9
I2C i2c0(8, 9);
uint8_t data[6];
// Read BME280 ID register
i2c0.read_register(0x76, 0xD0, data, 1);
LOGGER_DEBUG("BMP280 ID: %02X", data[0]);
// Read compensation values
i2c0.read_register(0x76, 0x88, data, 6);
// Set the oversampling to x1
uint8_t writeData = 0x27;
i2c0.write_register(0x76, 0xF4, &writeData, 1);
//--------------------------------------------
// BMP280
//
// Communicates on I2C i2c0
// Has slave address 0x76
BMP280 bmp280(&i2c0, 0x76);
// Reset the sensor
bmp280.resetSensor();
// Make sure to apply a wait cycle between reset and continuous use - 2ms is advised as minimum
vTaskDelay(10);
// Initialize the BMP280
bmp280.initialize();
// Set the temperature Oversampling
bmp280.setSensorTemperatureOversampling(BMP280::BMP280_Oversampling_t::X1);
// Set the sensor to NORMAL mode
bmp280.setSensorMode(BMP280::BMP280_Mode_t::NORMAL);
//--------------------------------------------
// LED Matrix
//
matrix.setGlobalColour(0x10, 0, 0x04);
// matrix.setGlobalColour(0x80, 0, 0x40);
@@ -286,17 +274,6 @@ extern "C" void app_main(void)
matrix.setPixelValue(10, 9, clock.getTime() % 2);
i2c0.read_register(0x76, 0xF7, data, 3);
LOGGER_DEBUG("BMP280 pressure: %02X %02X %02X", data[0], data[1], data[2]);
i2c0.read_register(0x76, 0xFA, data, 3);
int32_t value = 0;
value |= data[0] << 12;
value |= data[1] << 4;
value |= data[2] >> 4;
int valueComp = bmp280_compensate_T_int32(value);
LOGGER_DEBUG("BMP280 temperature: %02X %02X %02X -> %d", data[0], data[1], data[2], valueComp);
// Update the clock every second (1000 ms)
vTaskDelay(1000);
+115
View File
@@ -19,11 +19,26 @@
#include <bmp280.h>
#include "logger.h"
// --------------------------------------------------------------------------------------------------------------------
// Constant and macro definitions
// --------------------------------------------------------------------------------------------------------------------
// List of registers
#define ADDRESS_COMP_PARAMETERS ((uint32_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)
// --------------------------------------------------------------------------------------------------------------------
// Type definitions
@@ -47,5 +62,105 @@
// --------------------------------------------------------------------------------------------------------------------
BMP280::BMP280(I2C* bus, uint8_t slaveAddress)
{
// Take over the bus
BMP280::bus = bus;
// Take over the device slave address
BMP280::slaveAddress = slaveAddress;
// Reset the driver itself
resetDriver();
// Reset the device
}
void BMP280::resetSensor(void)
{
resetDevice();
}
bool BMP280::initialize(void)
{
bool returnValue = true;
// First read the device ID
getDeviceID();
// Device ID must match, otherwise there is a component error
if (memorymap.id != BMP280_DEVICE_ID)
{
returnValue = false;
}
if (returnValue)
{
// Get the compensation values from the device
getCompensationValues();
}
return returnValue;
}
bool BMP280::setSensorMode(BMP280_Mode_t mode)
{
bool returnValue = true;
memorymap.config.mode = mode;
setSensorConfiguration();
return returnValue;
}
bool BMP280::setSensorTemperatureOversampling(BMP280_Oversampling_t oversampling)
{
bool returnValue = true;
memorymap.config.oversampling_temp = oversampling;
setSensorConfiguration();
return returnValue;
}
void BMP280::resetDriver(void)
{
// Reset the parameters
BMP280::compensationParameters = {};
// Reset the device memory map
BMP280::memorymap = {};
// Reset the mode
BMP280::mode = STANDBY;
// Reset calculation values
t_fine = 0;
temperature = 0;
}
void BMP280::getDeviceID(void)
{
bus->read_register(slaveAddress, ADDRESS_REG_ID, &memorymap.id, 1);
}
void BMP280::resetDevice(void)
{
uint8_t resetValue = BMP280_RESET_VALUE;
bus->write_register(slaveAddress, ADDRESS_REG_RESET, &resetValue, 1);
}
void BMP280::setSensorConfiguration(void)
{
bus->write_register(slaveAddress, ADDRESS_REG_CONFIG, (uint8_t*)&memorymap.config, 1);
}
void BMP280::getCompensationValues(void)
{
bus->read_register(slaveAddress, ADDRESS_COMP_PARAMETERS, (uint8_t*)&compensationParameters, sizeof(compensationParameters));
LOGGER_DEBUG("Got compensation values: %04X %04X %04X", compensationParameters.dig_T1, compensationParameters.dig_T2, compensationParameters.dig_T3);
}
void BMP280::compensateTemperature(void)
{
int adc_T = 0;
// Create a single temperature value from the individual memory entries
adc_T |= memorymap.temperature_raw.msb << 12;
adc_T |= memorymap.temperature_raw.lsb << 4;
// XLSB is only a nibble
adc_T |= memorymap.temperature_raw.xlsb >> 4;
int var1 = ((((adc_T >> 3) - ((int)compensationParameters.dig_T1<<1))) * ((int)compensationParameters.dig_T2)) >> 11;
int var2 = (((((adc_T>>4) - ((int)compensationParameters.dig_T1)) * ((adc_T>>4) - ((int)compensationParameters.dig_T1))) >> 12) * ((int)compensationParameters.dig_T3)) >> 14;
t_fine = var1 + var2;
temperature = (t_fine * 5 + 128) >> 8;
}
+1
View File
@@ -152,6 +152,7 @@ void Clock::generateWordlist(list<string>* wordlist)
else
{
wordlist->push_back("almost");
wordlist->push_back("hours");
}
}
+1 -1
View File
@@ -79,7 +79,7 @@ void ClockWordmap::createList_NL(void)
wordlist[NL].push_back((struct word){"one", {{3,5},{4,5},{5,5}}});
wordlist[NL].push_back((struct word){"two", {{1,5},{2,5},{3,5},{4,5}}});
wordlist[NL].push_back((struct word){"three", {{6,5},{7,5},{8,5},{9,5}}});
wordlist[NL].push_back((struct word){"four", {{7,6},{8,6},{9,6},{10,5}}});
wordlist[NL].push_back((struct word){"four", {{7,6},{8,6},{9,6},{10,6}}});
wordlist[NL].push_back((struct word){"five", {{0,6},{1,6},{2,6},{3,6}}});
wordlist[NL].push_back((struct word){"six", {{4,6},{5,6},{6,6}}});
wordlist[NL].push_back((struct word){"seven", {{0,7},{1,7},{2,7},{3,7},{4,7}}});