Small cleanup and new methods

This commit is contained in:
dario 2024-03-25 23:05:14 +01:00
parent 15d53829d7
commit 4c4f2658e2
3 changed files with 92 additions and 39 deletions

View File

@ -1,6 +0,0 @@
// Needed for sta/endian.hpp
#define STA_MCU_LITTLE_ENDIAN
// Needed for sta/stm32/delay.hpp
#define STA_PLATFORM_STM32
#define STA_STM32_DELAY_US_TIM

View File

@ -1,15 +1,27 @@
#ifndef STA_SENSORS_MS5607_HPP #ifndef STA_SENSORS_MS5607_HPP
#define STA_SENSORS_MS5607_HPP #define STA_SENSORS_MS5607_HPP
#include<sta/spi/device.hpp> #include <sta/bus/spi/device.hpp>
#include<sta/endian.hpp> #include <sta/endian.hpp>
#include<sta/stm32/delay.hpp> #include <sta/stm32/delay.hpp>
namespace sta { namespace sta
// Class to represent a MS5607 pressure sensor which is used with SPI {
class MS5607 { /**
* @brief Driver class for communicating with the MS56xx pressure sensor via SPI.
*
*/
class MS56xx {
public: public:
// Different OSR levels for the sensor /**
* @brief Signature for delay msec function.
*/
using DelayMsFunc = void (*)(uint32_t);
/**
* @brief Different OSR levels for the sensor
*
*/
enum OsrLevel { enum OsrLevel {
_256 = 0, _256 = 0,
_512 = 1, _512 = 1,
@ -18,7 +30,35 @@ namespace sta {
_4096 = 4 _4096 = 4
}; };
MS5607(SpiDevice* device, OsrLevel osr=OsrLevel::_1024); /**
* @brief Driver class for the MS56xx pressure sensor series.
*
* @param device The SPI device for bus communication.
* @param osr The output sampling rate for the sensor.
*/
MS56xx(SPIDevice * device, DelayMsFunc delay, osr=OsrLevel::_1024);
/**
* @brief Initialize the driver. Computes the pressure value at altitude 0.
*
* @return True if successful, false otherwise.
*/
bool init();
/**
* @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 Estimate the current altitude based on the pressure readings.
*
* @return float The altitude estimate in meters.
*/
float getAltitudeEstimate();
// Request Calculation of uncompensated pressure // Request Calculation of uncompensated pressure
// Takes a few ms -> Call, then do sth. else, then get Pressure with function // Takes a few ms -> Call, then do sth. else, then get Pressure with function
@ -65,7 +105,8 @@ namespace sta {
bool presRead; bool presRead;
// STA internal object for SPi abstraction // STA internal object for SPi abstraction
SpiDevice* device_; SPIDevice * device_;
DelayMsFunc delay_;
OsrLevel osr_; OsrLevel osr_;
// 6 Different constants; Includes Offsets, references etc. // 6 Different constants; Includes Offsets, references etc.

View File

@ -1,23 +1,41 @@
#include "sta/MS5607.hpp" #include <sta/drivers/MS56xx.hpp>
namespace sta {
namespace sta
{
// Forward declaration // Forward declaration
uint16_t uint_8BufferTouint16_t(uint8_t* buffer); uint16_t uint_8BufferTouint16_t(uint8_t* buffer);
MS5607::MS5607(SpiDevice* device, OsrLevel level) { MS56xx::MS56xx(SpiDevice * device, DelayMsFunc delay, OsrLevel level)
this->device_ = device; : device_{device},
this->osr_ = level; delay_{delay},
osr_{level}
{
this->lastPresVal = -1; this->lastPresVal = -1;
this->adcStartTime = -1; this->adcStartTime = -1;
this->presRead = true; this->presRead = true;
}
void MS56xx::init()
{
// Reset device on start-up // Reset device on start-up
this->reset(); this->reset();
// Read the PROM e.g. constants // Read the PROM e.g. constants
this->readPROM(); this->readPROM();
} }
void MS5607::requestAdcReadout(uint32_t time) { void MS56xx::setPressureReference(float pressRef, float altRef)
{
// TODO
}
float MS56xx::getAltitudeEstimate()
{
return 0.0; // TODO
}
void MS56xx::requestAdcReadout(uint32_t time) {
// Get current state of calculation // Get current state of calculation
if (time-adcStartTime < delayTimes(this->osr_) || !presRead) { if (time-adcStartTime < delayTimes(this->osr_) || !presRead) {
// Time since last adc command is not enough // Time since last adc command is not enough
@ -25,14 +43,14 @@ namespace sta {
} }
this->device_->beginTransmission(); this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::D1_CONVERSION + 2*this->osr_); this->device_->transfer(MS56xx::Operations::D1_CONVERSION + 2*this->osr_);
this->device_->endTransmission(); this->device_->endTransmission();
adcStartTime = time; adcStartTime = time;
presRead = false; presRead = false;
} }
int32_t MS5607::getPressure(int32_t temp, uint32_t time) { int32_t MS56xx::getPressure(int32_t temp, uint32_t time) {
if (time-adcStartTime < delayTimes(this->osr_) || presRead) { if (time-adcStartTime < delayTimes(this->osr_) || presRead) {
// Time since last adc command is not enough // Time since last adc command is not enough
return lastPresVal; return lastPresVal;
@ -41,7 +59,7 @@ namespace sta {
uint8_t buffer[3] = { 0 }; uint8_t buffer[3] = { 0 };
this->device_->beginTransmission(); this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::ADC_RESULT); this->device_->transfer(MS56xx::Operations::ADC_RESULT);
this->device_->receive(buffer, 3); this->device_->receive(buffer, 3);
this->device_->endTransmission(); this->device_->endTransmission();
this->presRead = true; this->presRead = true;
@ -53,12 +71,12 @@ namespace sta {
return calculatePressure(d1_val, reverseTempCalc(temp)); return calculatePressure(d1_val, reverseTempCalc(temp));
} }
int32_t MS5607::reverseTempCalc(int32_t temp) { int32_t MS56xx::reverseTempCalc(int32_t temp) {
return this->tempsens; return this->tempsens;
} }
// DEPRECATED // DEPRECATED
int32_t MS5607::getPressure() { int32_t MS56xx::getPressure() {
// Get pure ADC value from the sensor // Get pure ADC value from the sensor
uint32_t d1 = readPressure(); uint32_t d1 = readPressure();
@ -72,10 +90,10 @@ namespace sta {
} }
// DON'T USE // DON'T USE
uint32_t MS5607::readPressure() { uint32_t MS56xx::readPressure() {
// Request pressure value conversion // Request pressure value conversion
this->device_->beginTransmission(); this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::D1_CONVERSION + 2*this->osr_); this->device_->transfer(MS56xx::Operations::D1_CONVERSION + 2*this->osr_);
this->device_->endTransmission(); this->device_->endTransmission();
// Wait for ADC to finish // Wait for ADC to finish
@ -86,7 +104,7 @@ namespace sta {
// Request readout of ADC value // Request readout of ADC value
uint8_t d1Arr[3]; uint8_t d1Arr[3];
this->device_->beginTransmission(); this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::ADC_RESULT); this->device_->transfer(MS56xx::Operations::ADC_RESULT);
this->device_->receive(d1Arr, 3); this->device_->receive(d1Arr, 3);
this->device_->endTransmission(); this->device_->endTransmission();
@ -100,7 +118,7 @@ namespace sta {
// Calculations from the Datasheet // Calculations from the Datasheet
// Probably problems with type conversions // Probably problems with type conversions
// If we used Rust... // If we used Rust...
int32_t MS5607::calculatePressure(uint32_t d1, int32_t dT) { int32_t MS56xx::calculatePressure(uint32_t d1, int32_t dT) {
int64_t offset = ( ((uint64_t)this->off) << 17) + ( ( ((uint64_t)this->tco) * dT ) >> 6); 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); int64_t sensitivity = ( ((uint64_t)this->sens) << 16) + ( ( ((uint64_t)this->tcs) * dT ) >> 7);
int32_t pres = ( (( ((uint64_t)d1) * sensitivity) >> 21) - offset ) >> 15; int32_t pres = ( (( ((uint64_t)d1) * sensitivity) >> 21) - offset ) >> 15;
@ -109,7 +127,7 @@ namespace sta {
// NOT RECOMMENDED // NOT RECOMMENDED
// USE TEMP FROM SCA3300 OR STH. ELSE // USE TEMP FROM SCA3300 OR STH. ELSE
int32_t MS5607::getTemperature() { int32_t MS56xx::getTemperature() {
// Get pure ADC value from the sensor // Get pure ADC value from the sensor
uint32_t d2 = readTemp(); uint32_t d2 = readTemp();
@ -117,10 +135,10 @@ namespace sta {
} }
// OLD; DON'T USE // OLD; DON'T USE
uint32_t MS5607::readTemp() { uint32_t MS56xx::readTemp() {
// Request ADC conversion of temperature // Request ADC conversion of temperature
this->device_->beginTransmission(); this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::D2_CONVERSION + 2*this->osr_); this->device_->transfer(MS56xx::Operations::D2_CONVERSION + 2*this->osr_);
this->device_->endTransmission(); this->device_->endTransmission();
// Wait for ADC to finish // Wait for ADC to finish
@ -132,7 +150,7 @@ namespace sta {
// Request ADC readout // Request ADC readout
uint8_t d2Arr[3]; uint8_t d2Arr[3];
this->device_->beginTransmission(); this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::ADC_RESULT); this->device_->transfer(MS56xx::Operations::ADC_RESULT);
this->device_->receive(d2Arr, 3); this->device_->receive(d2Arr, 3);
this->device_->endTransmission(); this->device_->endTransmission();
@ -146,7 +164,7 @@ namespace sta {
// Calculations from the Datasheet // Calculations from the Datasheet
// Probably problems with type conversions // Probably problems with type conversions
// If we used Rust... // If we used Rust...
int32_t MS5607::calculateTemperature(uint32_t d2) { int32_t MS56xx::calculateTemperature(uint32_t d2) {
int32_t dT = d2 - ( ((uint32_t)this->t_ref) << 8); int32_t dT = d2 - ( ((uint32_t)this->t_ref) << 8);
int32_t temp = 2000 + ((dT * ((uint32_t)this->tempsens)) >> 23); int32_t temp = 2000 + ((dT * ((uint32_t)this->tempsens)) >> 23);
@ -157,18 +175,18 @@ namespace sta {
} }
// Reset sequence as described in datasheet // Reset sequence as described in datasheet
void MS5607::reset() { void MS56xx::reset() {
this->device_->beginTransmission(); this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::RESET); this->device_->transfer(MS56xx::Operations::RESET);
this->device_->endTransmission(); this->device_->endTransmission();
delayUs(MS5607::RESET_DELAY); delayUs(MS56xx::RESET_DELAY);
} }
// Read all constants from the PROM // Read all constants from the PROM
// May be moved to be called in reset() function in future // May be moved to be called in reset() function in future
// Request value x -> Read value x; Then request value y etc. // Request value x -> Read value x; Then request value y etc.
// Could be optimized; Not as important since only needed once at start-up // Could be optimized; Not as important since only needed once at start-up
void MS5607::readPROM() { void MS56xx::readPROM() {
this->device_->beginTransmission(); this->device_->beginTransmission();
this->device_->transfer(Operations::READ_PROM); this->device_->transfer(Operations::READ_PROM);