From c0eb36fbe05e91d1bc60e14d4589538c6aef9ae7 Mon Sep 17 00:00:00 2001 From: dario Date: Fri, 17 May 2024 00:42:53 +0200 Subject: [PATCH] Reverted a few changes but fixed a few issues --- include/sta/drivers/MS56xx.hpp | 41 +++++++++++++++-------- src/MS56xx.cpp | 61 ++++++++++++++++++++++------------ 2 files changed, 67 insertions(+), 35 deletions(-) diff --git a/include/sta/drivers/MS56xx.hpp b/include/sta/drivers/MS56xx.hpp index 83e8723..b70005f 100644 --- a/include/sta/drivers/MS56xx.hpp +++ b/include/sta/drivers/MS56xx.hpp @@ -35,6 +35,16 @@ namespace sta */ using DelayUsFunc = void (*)(uint32_t); + /** + * @brief Represents the two different supported sensors. + * + */ + enum Version + { + _MS5611, + _MS5607 + }; + /** * @brief Different OSR levels for the sensor * @@ -81,6 +91,10 @@ namespace sta ADC_RESULT = 0x00 }; + /** + * @brief Represents the two datatypes available for this sensor. + * + */ enum DataType { PRESSURE, @@ -91,23 +105,25 @@ namespace sta * @brief SPI driver for the MS56xx pressure sensor series. * * @param device The SPI device for bus communication. + * @param version The version of the sensor that the driver is used for. * @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, DelayUsFunc delay, OsrLevel osr = OsrLevel::_1024); + MS56xx(SPIDevice * device, Version version, DelayUsFunc delay, OsrLevel osr = OsrLevel::_1024); /** * @brief I2C driver for the MS56xx pressure sensor series. * * @param device The I2C device for bus communication. + * @param version The version of the sensor that the driver is used for. * @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); + MS56xx(I2CDevice * device, Version version, DelayUsFunc delay, OsrLevel osr = OsrLevel::_1024); /** * @brief Initialize the driver. Computes the pressure value at altitude 0. @@ -125,18 +141,6 @@ namespace sta */ void setOsr(OsrLevel osr); - /** - * @brief - * - * @param type - * @param blocking - */ - void requestData(DataType type); - - void requestPressure(); - - void requestTemperature(); - /** * @brief Reads the current pressure value from the sensor. Obtains the temperature value from the interal sensor. * @@ -176,6 +180,13 @@ namespace sta */ void reset(); + /** + * @brief Request an ADC readout from the sensor. + * + * @param type The type of data to read. + */ + void requestData(DataType type); + /** * @brief Read all constants from the PROM * @@ -235,6 +246,8 @@ namespace sta private: // STA internal object for SPi abstraction Device * device_; + Version version_; + Version type_; DelayUsFunc delay_; OsrLevel osr_; Intf intf_; diff --git a/src/MS56xx.cpp b/src/MS56xx.cpp index 7860060..aeb1b9b 100644 --- a/src/MS56xx.cpp +++ b/src/MS56xx.cpp @@ -9,18 +9,20 @@ namespace sta { - MS56xx::MS56xx(SPIDevice * device, DelayUsFunc delay, OsrLevel level /* = OsrLevel::_1024 */) + MS56xx::MS56xx(SPIDevice * device, Version version, DelayUsFunc delay, OsrLevel osr = OsrLevel::_1024) : device_{device}, + version_{version}, delay_{delay}, - osr_{level}, + osr_{osr}, intf_{Intf::SPI}, C_{} { STA_ASSERT(device != nullptr); } - MS56xx::MS56xx(I2CDevice * device, DelayUsFunc delay, OsrLevel osr /* = OsrLevel::_1024 */) + MS56xx::MS56xx(I2CDevice * device, Version version, DelayUsFunc delay, OsrLevel osr /* = OsrLevel::_1024 */) : device_{device}, + version_{version}, delay_{delay}, osr_{osr}, intf_{Intf::I2C}, @@ -64,21 +66,20 @@ namespace sta // Request the ADC to read new data. busCommand(op + 2*this->osr_); - } - void MS56xx::requestPressure() - { - requestData(PRESSURE); - } - - void MS56xx::requestTemperature() - { - requestData(TEMPERATURE); + delay_(osrDelay()); } float MS56xx::getPressure(Unit unit /* = Unit::hPa */) { + requestData(TEMPERATURE); uint8_t buffer[3] = { 0x00, 0x00, 0x00 }; + busRead(MS56xx::Operations::ADC_RESULT, buffer, 3); + + uint32_t D2 = buffer[0] << 16 | buffer[1] << 8 | buffer[2]; + dT_ = D2 - C_[5]; + + requestData(PRESSURE); busRead(MS56xx::Operations::ADC_RESULT, buffer, 3); uint32_t D1 = buffer[0] << 16 | buffer[1] << 8 | buffer[2]; @@ -86,8 +87,10 @@ namespace sta float sens = C_[1] + dT_ * C_[3]; // The pressure in Pa. - float pressure = (D1 * sens * 4.76837158205E-7 - offset) * 3.051757813E-5 * 0.01; - // TODO: conversion + float pressure = (D1 * sens * 4.76837158205E-7 - offset) * 3.051757813E-5; + + // Convert to desired unit. + pressure = convertPressure(pressure, unit); return pressure; } @@ -100,7 +103,7 @@ namespace sta uint32_t D2 = buffer[0] << 16 | buffer[1] << 8 | buffer[2]; dT_ = D2 - C_[5]; - return 2000 + dT_ * C_[6] * 0.01; + return (2000 + dT_ * C_[6]) * 0.01; } void MS56xx::setPressureReference(float pressRef, float altRef, Unit unit /* = Unit::hPa */) @@ -133,12 +136,28 @@ namespace sta void MS56xx::initConstants() { C_[0] = 1; - C_[1] = 65536L; - C_[2] = 131072; - C_[3] = 7.8125E-3; - C_[4] = 0.015625; - C_[5] = 256; - C_[6] = 1.1920928955E-7; + + switch (version_) + { + case _MS5607: + C_[1] = 65536L; + C_[2] = 131072; + C_[3] = 7.8125E-3; + C_[4] = 0.015625; + C_[5] = 256; + C_[6] = 1.1920928955E-7; + + break; + case _MS5611: + C_[1] = 32768L; + C_[2] = 65536L; + C_[3] = 3.90625E-3; + C_[4] = 7.8125E-3; + C_[5] = 256; + C_[6] = 1.1920928955E-7; + default: + break; + } } inline float MS56xx::convertPressure(float pressure, Unit unit)