From 4c4f2658e22a6cf7ee9d19f8c8c7d88693bc25c3 Mon Sep 17 00:00:00 2001 From: dario Date: Mon, 25 Mar 2024 23:05:14 +0100 Subject: [PATCH] Small cleanup and new methods --- include/sta/config.hpp | 6 -- .../sta/{MS5607.hpp => drivers/MS56xx.hpp} | 59 ++++++++++++++--- src/{MS5607.cpp => MS56xx.cpp} | 66 ++++++++++++------- 3 files changed, 92 insertions(+), 39 deletions(-) delete mode 100644 include/sta/config.hpp rename include/sta/{MS5607.hpp => drivers/MS56xx.hpp} (69%) rename src/{MS5607.cpp => MS56xx.cpp} (81%) diff --git a/include/sta/config.hpp b/include/sta/config.hpp deleted file mode 100644 index 0e15371..0000000 --- a/include/sta/config.hpp +++ /dev/null @@ -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 \ No newline at end of file diff --git a/include/sta/MS5607.hpp b/include/sta/drivers/MS56xx.hpp similarity index 69% rename from include/sta/MS5607.hpp rename to include/sta/drivers/MS56xx.hpp index e2c935a..785422c 100644 --- a/include/sta/MS5607.hpp +++ b/include/sta/drivers/MS56xx.hpp @@ -1,15 +1,27 @@ #ifndef STA_SENSORS_MS5607_HPP #define STA_SENSORS_MS5607_HPP -#include -#include -#include +#include +#include +#include -namespace sta { - // Class to represent a MS5607 pressure sensor which is used with SPI - class MS5607 { +namespace sta +{ + /** + * @brief Driver class for communicating with the MS56xx pressure sensor via SPI. + * + */ + class MS56xx { 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 { _256 = 0, _512 = 1, @@ -18,7 +30,35 @@ namespace sta { _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 // Takes a few ms -> Call, then do sth. else, then get Pressure with function @@ -65,7 +105,8 @@ namespace sta { bool presRead; // STA internal object for SPi abstraction - SpiDevice* device_; + SPIDevice * device_; + DelayMsFunc delay_; OsrLevel osr_; // 6 Different constants; Includes Offsets, references etc. diff --git a/src/MS5607.cpp b/src/MS56xx.cpp similarity index 81% rename from src/MS5607.cpp rename to src/MS56xx.cpp index d9fd72c..8441213 100644 --- a/src/MS5607.cpp +++ b/src/MS56xx.cpp @@ -1,23 +1,41 @@ -#include "sta/MS5607.hpp" +#include -namespace sta { + +namespace sta +{ // Forward declaration uint16_t uint_8BufferTouint16_t(uint8_t* buffer); - MS5607::MS5607(SpiDevice* device, OsrLevel level) { - this->device_ = device; - this->osr_ = level; + MS56xx::MS56xx(SpiDevice * device, DelayMsFunc delay, OsrLevel level) + : device_{device}, + delay_{delay}, + osr_{level} + { this->lastPresVal = -1; this->adcStartTime = -1; this->presRead = true; + } + void MS56xx::init() + { // Reset device on start-up this->reset(); + // Read the PROM e.g. constants 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 if (time-adcStartTime < delayTimes(this->osr_) || !presRead) { // Time since last adc command is not enough @@ -25,14 +43,14 @@ namespace sta { } 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(); adcStartTime = time; 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) { // Time since last adc command is not enough return lastPresVal; @@ -41,7 +59,7 @@ namespace sta { uint8_t buffer[3] = { 0 }; this->device_->beginTransmission(); - this->device_->transfer(MS5607::Operations::ADC_RESULT); + this->device_->transfer(MS56xx::Operations::ADC_RESULT); this->device_->receive(buffer, 3); this->device_->endTransmission(); this->presRead = true; @@ -53,12 +71,12 @@ namespace sta { return calculatePressure(d1_val, reverseTempCalc(temp)); } - int32_t MS5607::reverseTempCalc(int32_t temp) { + int32_t MS56xx::reverseTempCalc(int32_t temp) { return this->tempsens; } // DEPRECATED - int32_t MS5607::getPressure() { + int32_t MS56xx::getPressure() { // Get pure ADC value from the sensor uint32_t d1 = readPressure(); @@ -72,10 +90,10 @@ namespace sta { } // DON'T USE - uint32_t MS5607::readPressure() { + uint32_t MS56xx::readPressure() { // Request pressure value conversion 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(); // Wait for ADC to finish @@ -86,7 +104,7 @@ namespace sta { // Request readout of ADC value uint8_t d1Arr[3]; this->device_->beginTransmission(); - this->device_->transfer(MS5607::Operations::ADC_RESULT); + this->device_->transfer(MS56xx::Operations::ADC_RESULT); this->device_->receive(d1Arr, 3); this->device_->endTransmission(); @@ -100,7 +118,7 @@ namespace sta { // Calculations from the Datasheet // Probably problems with type conversions // 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 sensitivity = ( ((uint64_t)this->sens) << 16) + ( ( ((uint64_t)this->tcs) * dT ) >> 7); int32_t pres = ( (( ((uint64_t)d1) * sensitivity) >> 21) - offset ) >> 15; @@ -109,7 +127,7 @@ namespace sta { // NOT RECOMMENDED // USE TEMP FROM SCA3300 OR STH. ELSE - int32_t MS5607::getTemperature() { + int32_t MS56xx::getTemperature() { // Get pure ADC value from the sensor uint32_t d2 = readTemp(); @@ -117,10 +135,10 @@ namespace sta { } // OLD; DON'T USE - uint32_t MS5607::readTemp() { + uint32_t MS56xx::readTemp() { // Request ADC conversion of temperature 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(); // Wait for ADC to finish @@ -132,7 +150,7 @@ namespace sta { // Request ADC readout uint8_t d2Arr[3]; this->device_->beginTransmission(); - this->device_->transfer(MS5607::Operations::ADC_RESULT); + this->device_->transfer(MS56xx::Operations::ADC_RESULT); this->device_->receive(d2Arr, 3); this->device_->endTransmission(); @@ -146,7 +164,7 @@ namespace sta { // Calculations from the Datasheet // Probably problems with type conversions // 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 temp = 2000 + ((dT * ((uint32_t)this->tempsens)) >> 23); @@ -157,18 +175,18 @@ namespace sta { } // Reset sequence as described in datasheet - void MS5607::reset() { + void MS56xx::reset() { this->device_->beginTransmission(); - this->device_->transfer(MS5607::Operations::RESET); + this->device_->transfer(MS56xx::Operations::RESET); this->device_->endTransmission(); - delayUs(MS5607::RESET_DELAY); + 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. // Could be optimized; Not as important since only needed once at start-up - void MS5607::readPROM() { + void MS56xx::readPROM() { this->device_->beginTransmission(); this->device_->transfer(Operations::READ_PROM);