More updates to the sensor API

This commit is contained in:
dario 2024-03-26 14:10:32 +01:00
parent 4c4f2658e2
commit 26f3a6fcf6
2 changed files with 289 additions and 211 deletions

View File

@ -2,8 +2,24 @@
#define STA_SENSORS_MS5607_HPP
#include <sta/bus/spi/device.hpp>
#include <sta/bus/i2c/device.hpp>
#include <sta/endian.hpp>
#include <sta/stm32/delay.hpp>
// Important: The pin CSB shall be connected to VDD or GND (do not leave unconnected!).
/**
* @brief MS56xx address when the CS pin is connected to GND.
*
*/
#define MS56XX_ADDRESS_CS_LOW 0x1110111
/**
* @brief MS56xx address when the CS pin is connected to VDD.
*
*/
#define MS56XX_ADDRESS_CS_HIGH 0x1110110
namespace sta
{
@ -16,7 +32,7 @@ namespace sta
/**
* @brief Signature for delay msec function.
*/
using DelayMsFunc = void (*)(uint32_t);
using DelayUsFunc = void (*)(uint32_t);
/**
* @brief Different OSR levels for the sensor
@ -31,12 +47,49 @@ namespace sta
};
/**
* @brief Driver class for the MS56xx pressure sensor series.
* @brief Different units supported by the pressure sensor.
*
*/
enum Unit
{
hPa,
Pa,
mbar
};
/**
* @brief All possible commands to send to the sensor
*
*/
enum Operations {
RESET = 0x1E,
READ_PROM = 0xA2,
D1_CONVERSION = 0x40,
D2_CONVERSION = 0x50,
ADC_RESULT = 0x00
};
/**
* @brief SPI driver for the MS56xx pressure sensor series.
*
* @param device The SPI device for bus communication.
* @param osr The output sampling rate for the sensor.
* @param delay Function for triggering microsecond delays.
* @param osr The oversampling rate for the sensor.
*
* @note Set PS pin to low for SPI. Maximum SPI frequency 20MHz, mode 0 and 3 are supported.
*/
MS56xx(SPIDevice * device, DelayMsFunc delay, osr=OsrLevel::_1024);
MS56xx(SPIDevice * device, DelayUsFunc delay, OsrLevel osr = OsrLevel::_1024);
/**
* @brief I2C driver for the MS56xx pressure sensor series.
*
* @param device The I2C device for bus communication.
* @param delay Function for triggering microsecond delays.
* @param osr The oversampling rate for the sensor.
*
* @note Set PS pin to high for I2C. Chip select pin represents LSB of address.
*/
MS56xx(I2CDevice * device, DelayUsFunc delay, OsrLevel osr = OsrLevel::_1024);
/**
* @brief Initialize the driver. Computes the pressure value at altitude 0.
@ -45,13 +98,61 @@ namespace sta
*/
bool init();
/**
* @brief Set the oversampling rate.
*
* @param osr The new oversampling rate.
*/
void setOsr(OsrLevel osr);
/**
* @brief Reads the current pressure value from the sensor.
*
* @param temperature The temperature in Celsius * 100 (e.g. 20.3 deg. C -> 2030)
* @param unit Specifies the unit for the pressure measurement. Default is hPa.
* @return uint32_t The measured value in the specified unit.
*/
int32_t getPressure(int32_t temperature, Unit unit = Unit::hPa);
/**
* @brief Reads the current pressure value from the sensor. Obtains the temperature value from the interal sensor.
*
* @param unit Specifies the unit for the pressure measurement. Default is hPa.
* @return int32_t The measured value in the specified unit.
*/
int32_t getPressure(Unit unit = Unit::hPa);
/**
* @brief Reads the current temperature value from the sensor.
*
* @return int32_t The measured temperature.
* @note There are better sensors for temperature measurements than the MS56xx.
*/
int32_t getTemperature();
/**
* @brief Set the Pressure Reference object
*
* @param pressRef The reference pressure value measured at a reference altitude.
* @param altRef The reference altitude for the reference pressure.
*/
void setPressureReference(float pressRef, float altRef);
/**
* @brief Provide a reference pressure value at a reference altitude in order to estimate the sealevel pressure.
*
* @param pressRef The reference pressure value measured at a reference altitude.
* @param altRef The reference altitude for the reference pressure.
* @param unit The unit of the provided pressure value.
*/
void setPressureReference(float pressRef, float altRef, Unit unit = Unit::hPa);
/**
* @brief Estimate the current altitude based on the pressure readings.
*
* @param temperature A temperature measurement from an external source.
* @return float The altitude estimate in meters.
*/
float getAltitudeEstimate(int32_t temperature);
/**
* @brief Estimate the current altitude based on the pressure readings.
@ -59,83 +160,70 @@ namespace sta
* @return float The altitude estimate in meters.
*/
float getAltitudeEstimate();
// Request Calculation of uncompensated pressure
// Takes a few ms -> Call, then do sth. else, then get Pressure with function
// If calculation already running, do nothing
void requestAdcReadout(uint32_t curTime);
// Function to get Pressure
// Parameter should be Temperature in Celsius * 100 (e.g. 20.3 deg. C -> 2030)
// If currently still processing, use old value
// If currently not processing, start processing to minimize potential waiting times
int32_t getPressure(int32_t temp, uint32_t curTime);
// --- DEPRECATED ---
// Parameterless version of function above
// Calls getTemperature, to get temp
// NOT RECOMMENDED
int32_t getPressure();
// Getter for temperature
// Deprecated because of time constrains
// NOT RECOMMENDED
int32_t getTemperature();
// --- DEPRECATED ---
void changeOsrLevel(OsrLevel newOsr) {
// Don't I need to write this to the sensor?
this->osr_ = newOsr;
}
private:
// Helper method to keep code clean
void pulseCS(uint32_t ms=1) {
this->device_->endTransmission();
sta::delayMs(ms);
this->device_->beginTransmission();
}
/**
* @brief Reset the sensor.
*
*/
void reset();
// Last Pressure Value measured; Used to give any output if still calculating ADC
int32_t lastPresVal;
// Value to store, when adc was started
// On STM32 with HAL_GetTick() (Gives ms since startup)
uint32_t adcStartTime;
// To prevent calculation of adc without reading value
bool presRead;
/**
* @brief Read all constants from the PROM
*
*/
void readPROM();
// STA internal object for SPi abstraction
SPIDevice * device_;
DelayMsFunc delay_;
OsrLevel osr_;
/**
* @brief
*
* @param ms
*/
void pulseCS(uint32_t ms=1);
// 6 Different constants; Includes Offsets, references etc.
uint16_t sens, off, tcs, tco, t_ref, tempsens;
// All possible commands to send to the sensor
enum Operations {
RESET = 0x1E,
READ_PROM = 0xA2,
D1_CONVERSION = 0x40,
D2_CONVERSION = 0x50,
ADC_RESULT = 0x00
};
/**
* @brief Compute the pressure values from the ADC output.
*
* @param d1
* @param dT
* @param unit The unit to return the pressure values in.
* @return int32_t
*/
int32_t calculatePressure(uint32_t d1, int32_t dT, Unit unit);
// Request pure ADC converted values from the sensor
// Calculations with offset required
uint32_t readPressure();
// Read temp should not be used, rather give temp from e.g. SCA3300 as parameter to readPres
uint32_t readTemp();
// Calculate pure ADC values to final pressure/temperature values
int32_t calculatePressure(uint32_t d1, int32_t dT);
/**
* @brief Compute the temperature values from the ADC output
*
* @param d2
* @return int32_t
*/
int32_t calculateTemperature(uint32_t d2);
/**
* @brief
*
* @param temp
* @return int32_t
*/
int32_t reverseTempCalc(int32_t temp);
void reset();
void readPROM();
uint16_t uint_8BufferTouint16_t(uint8_t* buffer);
private:
bool busCommand(uint8_t command);
bool busRead(uint8_t reg, uint8_t * buffer, size_t length);
bool busWrite(uint8_t reg, uint8_t * buffer, size_t length);
private:
// STA internal object for SPi abstraction
Device * device_;
DelayUsFunc delay_;
OsrLevel osr_;
// Pressure at sealevel.
int32_t sealevel_;
// 6 Different constants; Includes Offsets, references etc.
uint16_t sens, off, tcs, tco, t_ref, tempsens;
// Constants for waiting
const uint32_t RESET_DELAY = 2800; // in uS

View File

@ -1,22 +1,27 @@
#include <sta/drivers/MS56xx.hpp>
#include <sta/debug/assert.hpp>
namespace sta
{
// Forward declaration
uint16_t uint_8BufferTouint16_t(uint8_t* buffer);
MS56xx::MS56xx(SpiDevice * device, DelayMsFunc delay, OsrLevel level)
MS56xx::MS56xx(SPIDevice * device, DelayUsFunc delay, OsrLevel level /* = OsrLevel::_1024 */)
: device_{device},
delay_{delay},
osr_{level}
{
this->lastPresVal = -1;
this->adcStartTime = -1;
this->presRead = true;
STA_ASSERT(device != nullptr);
}
void MS56xx::init()
MS56xx::MS56xx(I2CDevice * device, DelayUsFunc delay, OsrLevel osr = OsrLevel::_1024)
: device_{device},
delay_{delay},
osr_{osr}
{
STA_ASSERT(device != nullptr);
}
bool MS56xx::init()
{
// Reset device on start-up
this->reset();
@ -25,145 +30,114 @@ namespace sta
this->readPROM();
}
void MS56xx::setPressureReference(float pressRef, float altRef)
void MS56xx::pulseCS(uint32_t ms=1) {
this->device_->endTransmission();
delay_(ms * 1000);
this->device_->beginTransmission();
}
void MS56xx::setOsr(OsrLevel osr)
{
osr_ = osr;
}
void MS56xx::reset()
{
busCommand(MS56xx::Operations::RESET);
delay_(MS56xx::RESET_DELAY);
}
// Helper method to keep code clean
void MS56xx::pulseCS(uint32_t ms=1) {
this->device_->endTransmission();
delay_(ms * 1000);
this->device_->beginTransmission();
}
int32_t MS56xx::getPressure(int32_t temperature, Unit unit /* = Unit::hPa */)
{
// Request the ADC to read pressure values.
busCommand(MS56xx::Operations::D1_CONVERSION + 2*this->osr_);
// This might be an incorrect duration.
delay_(10 * 1000);
uint8_t buffer[3] = { 0x00, 0x00, 0x00 };
busRead(MS56xx::Operations::ADC_RESULT, buffer, 3);
uint32_t d1_val = buffer[0] << 16 | buffer[1] << 8 | buffer[2];
int32_t pressure = calculatePressure(d1_val, reverseTempCalc(temperature), unit);
return pressure;
}
int32_t MS56xx::getPressure(Unit unit = Unit::hPa)
{
int32_t temperature = getTemperature();
return getPressure(temperature, unit);
}
int32_t MS56xx::getTemperature() {
busCommand(MS56xx::Operations::D2_CONVERSION + 2*this->osr_);
// This might be an incorrect duration.
delay_(10 * 1000);
uint8_t buffer[3] = { 0x00, 0x00, 0x00 };
busRead(MS56xx::Operations::ADC_RESULT, buffer, 3);
// Convert the raw values to actual temperature values.
uint32_t res = buffer[0] | buffer[1] << 8 | buffer[2] << 16;
int32_t temperature = calculateTemperature(res);
return temperature;
}
void MS56xx::setPressureReference(float pressRef, float altRef, Unit unit /* = Unit::hPa */)
{
// TODO
}
float MS56xx::getAltitudeEstimate(int32_t temperature)
{
int32_t pressure = getPressure(temperature, Unit::hPa);
}
float MS56xx::getAltitudeEstimate()
{
return 0.0; // TODO
}
int32_t temperature = getTemperature();
void MS56xx::requestAdcReadout(uint32_t time) {
// Get current state of calculation
if (time-adcStartTime < delayTimes(this->osr_) || !presRead) {
// Time since last adc command is not enough
return;
}
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::D1_CONVERSION + 2*this->osr_);
this->device_->endTransmission();
adcStartTime = time;
presRead = false;
}
int32_t MS56xx::getPressure(int32_t temp, uint32_t time) {
if (time-adcStartTime < delayTimes(this->osr_) || presRead) {
// Time since last adc command is not enough
return lastPresVal;
}
uint8_t buffer[3] = { 0 };
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::ADC_RESULT);
this->device_->receive(buffer, 3);
this->device_->endTransmission();
this->presRead = true;
uint32_t d1_val = buffer[0] << 16 | buffer[1] << 8 | buffer[2];
this->requestAdcReadout(time);
return calculatePressure(d1_val, reverseTempCalc(temp));
return getAltitudeEstimate(temperature);
}
int32_t MS56xx::reverseTempCalc(int32_t temp) {
return this->tempsens;
}
// DEPRECATED
int32_t MS56xx::getPressure() {
// Get pure ADC value from the sensor
uint32_t d1 = readPressure();
// Since pressure is temp-dependant, temperature needs to be polled
// Could be too costly timewise, since 8ms is needed for the ADC
// May need future optimisation
uint32_t d2 = readTemp();
int32_t dT = d2 - ( ((uint32_t)this->t_ref) << 8);
return calculatePressure(d1, dT);
}
// DON'T USE
uint32_t MS56xx::readPressure() {
// Request pressure value conversion
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::D1_CONVERSION + 2*this->osr_);
this->device_->endTransmission();
// Wait for ADC to finish
// Could do sth. else and schedule continuation with scheduler in RTOS
// TODO: Find out min
sta::delayMs(10);
// Request readout of ADC value
uint8_t d1Arr[3];
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::ADC_RESULT);
this->device_->receive(d1Arr, 3);
this->device_->endTransmission();
// Convert into best possible type
uint32_t res = 0;
// Shifting may not be necessary, but idk w/o testing
res |= d1Arr[0] | (d1Arr[1] << 8) | (d1Arr[2] << 16);
return res;
}
// Calculations from the Datasheet
// Probably problems with type conversions
// If we used Rust...
int32_t MS56xx::calculatePressure(uint32_t d1, int32_t dT) {
int32_t MS56xx::calculatePressure(uint32_t d1, int32_t dT, Unit unit) {
int64_t offset = ( ((uint64_t)this->off) << 17) + ( ( ((uint64_t)this->tco) * dT ) >> 6);
int64_t sensitivity = ( ((uint64_t)this->sens) << 16) + ( ( ((uint64_t)this->tcs) * dT ) >> 7);
int32_t pres = ( (( ((uint64_t)d1) * sensitivity) >> 21) - offset ) >> 15;
switch (unit)
{
case Unit::hPa:
/* code */
break;
case Unit::Pa:
break;
case Unit::mbar:
break;
default:
break;
}
return pres;
}
// NOT RECOMMENDED
// USE TEMP FROM SCA3300 OR STH. ELSE
int32_t MS56xx::getTemperature() {
// Get pure ADC value from the sensor
uint32_t d2 = readTemp();
return calculateTemperature(d2);
}
// OLD; DON'T USE
uint32_t MS56xx::readTemp() {
// Request ADC conversion of temperature
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::D2_CONVERSION + 2*this->osr_);
this->device_->endTransmission();
// Wait for ADC to finish
// Could do sth. else and schedule continuation with scheduler in RTOS
// TODO: Test out min
sta::delayMs(10);
// Request ADC readout
uint8_t d2Arr[3];
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::ADC_RESULT);
this->device_->receive(d2Arr, 3);
this->device_->endTransmission();
// Convert into best possible type
uint32_t res = 0;
// Shifting may be unnecessary? Don't know really w/o testing
res |= d2Arr[0] | d2Arr[1] << 8 | d2Arr[2] << 16;
return res;
}
// Calculations from the Datasheet
// Probably problems with type conversions
// If we used Rust...
int32_t MS56xx::calculateTemperature(uint32_t d2) {
int32_t dT = d2 - ( ((uint32_t)this->t_ref) << 8);
int32_t temp = 2000 + ((dT * ((uint32_t)this->tempsens)) >> 23);
@ -174,14 +148,6 @@ namespace sta
return temp;
}
// Reset sequence as described in datasheet
void MS56xx::reset() {
this->device_->beginTransmission();
this->device_->transfer(MS56xx::Operations::RESET);
this->device_->endTransmission();
delayUs(MS56xx::RESET_DELAY);
}
// Read all constants from the PROM
// May be moved to be called in reset() function in future
// Request value x -> Read value x; Then request value y etc.
@ -235,7 +201,31 @@ namespace sta
// Helper function:
// Take first bytes from buffer, swap them and store those in uint16_t
// Swap may not be necessary
uint16_t uint_8BufferTouint16_t(uint8_t* buffer) {
uint16_t MS56xx::uint_8BufferTouint16_t(uint8_t* buffer) {
return (buffer[0] << 8) | buffer[1];
}
bool MS56xx::busCommand(uint8_t command)
{
this->device_->beginTransmission();
this->device_->transfer(command);
this->device_->endTransmission();
return true;
}
bool MS56xx::busRead(uint8_t reg, uint8_t * buffer, size_t length)
{
this->device_->beginTransmission();
this->device_->transfer(reg);
this->device_->receive(buffer, length);
this->device_->endTransmission();
return true;
}
bool MS56xx::busWrite(uint8_t reg, uint8_t * buffer, size_t length)
{
return true;
}
}