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
+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}}});