mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/driver-ms56xx.git
synced 2025-06-10 01:55:59 +00:00
237 lines
7.1 KiB
C++
237 lines
7.1 KiB
C++
#ifndef STA_SENSORS_MS5607_HPP
|
|
#define STA_SENSORS_MS5607_HPP
|
|
|
|
#include <sta/bus/spi/device.hpp>
|
|
#include <sta/bus/i2c/device.hpp>
|
|
|
|
#include <sta/endian.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
|
|
{
|
|
/**
|
|
* @brief Driver class for communicating with the MS56xx pressure sensor via SPI.
|
|
*
|
|
*/
|
|
class MS56xx {
|
|
public:
|
|
/**
|
|
* @brief Signature for delay msec function.
|
|
*/
|
|
using DelayUsFunc = void (*)(uint32_t);
|
|
|
|
/**
|
|
* @brief Different OSR levels for the sensor
|
|
*
|
|
*/
|
|
enum OsrLevel {
|
|
_256 = 0,
|
|
_512 = 1,
|
|
_1024 = 2,
|
|
_2048 = 3,
|
|
_4096 = 4
|
|
};
|
|
|
|
/**
|
|
* @brief Different units supported by the pressure sensor.
|
|
*
|
|
*/
|
|
enum Unit
|
|
{
|
|
hPa,
|
|
Pa,
|
|
mbar
|
|
};
|
|
|
|
/**
|
|
* @brief The types of physical interfaces used for communication with the sensor.
|
|
*
|
|
*/
|
|
enum Intf
|
|
{
|
|
SPI,
|
|
I2C
|
|
};
|
|
|
|
/**
|
|
* @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 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);
|
|
|
|
/**
|
|
* @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.
|
|
*
|
|
* @return True if successful, false otherwise.
|
|
*/
|
|
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. 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.
|
|
*/
|
|
float 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 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.
|
|
*
|
|
* @return float The altitude estimate in meters.
|
|
*/
|
|
float getAltitudeEstimate();
|
|
private:
|
|
/**
|
|
* @brief Reset the sensor.
|
|
*
|
|
*/
|
|
void reset();
|
|
|
|
/**
|
|
* @brief Read all constants from the PROM
|
|
*
|
|
*/
|
|
void readPROM();
|
|
|
|
/**
|
|
* @brief Convert the pressure value given in Pa to a different unit.
|
|
*
|
|
* @param pressure A pressure value in Pa.
|
|
* @param unit The desired unit.
|
|
* @return float Returns the converted value.
|
|
*/
|
|
float convertPressure(float pressure, Unit unit);
|
|
|
|
/**
|
|
* @brief Small helper function for converting a uint8_t buffer to a single uint16_t.
|
|
*
|
|
* @param buffer A uint8_t of size 2. More bytes will be ignored.
|
|
* @return uint16_t A single uint16_t containing the first buffer value as MSB and the second as LSB.
|
|
*/
|
|
uint16_t uint_8BufferTouint16_t(uint8_t* buffer);
|
|
|
|
/**
|
|
* @brief Function to get the delay times needed for different OSR Levels
|
|
*
|
|
* @param level The oversampling rate chosen
|
|
* @return uint32_t Returs the delay time in uS.
|
|
*/
|
|
uint32_t osrDelay();
|
|
private:
|
|
/**
|
|
* @brief Send a command to the sensor via the bus.
|
|
*
|
|
* @param command The command to send.
|
|
* @return true if successful, false otherwise.
|
|
*/
|
|
bool busCommand(uint8_t command);
|
|
|
|
/**
|
|
* @brief Read data from one of the sensor's registers.
|
|
*
|
|
* @param reg The register to read from.
|
|
* @param buffer The buffer to write the received data to.
|
|
* @param length The number of bytes to read from the register.
|
|
* @return true if successful, false otherwise.
|
|
*/
|
|
bool busRead(uint8_t reg, uint8_t * buffer, size_t length);
|
|
|
|
/**
|
|
* @brief Write data to one of the sensor's registers.
|
|
*
|
|
* @param reg The register to write to.
|
|
* @param buffer The buffer of data to write to the register.
|
|
* @param length The number of bytes to write.
|
|
* @return true if successful, false otherwise.
|
|
*/
|
|
bool busWrite(uint8_t reg, uint8_t * buffer, size_t length);
|
|
private:
|
|
// STA internal object for SPi abstraction
|
|
Device * device_;
|
|
DelayUsFunc delay_;
|
|
OsrLevel osr_;
|
|
Intf intf_;
|
|
|
|
// Pressure at sealevel. Use the standard atmosphere per default.
|
|
float sealevel_ = 1013.25;
|
|
|
|
// 6 Different constants; Includes Offsets, references etc.
|
|
uint16_t sensibility_;
|
|
uint16_t offset_;
|
|
uint16_t tempSensCoeff_;
|
|
uint16_t tempOffsetCoeff_;
|
|
uint16_t refTemp_;
|
|
uint16_t tempCoeff_;
|
|
|
|
// Constants for waiting
|
|
const uint32_t RESET_DELAY = 2800; // in uS
|
|
};
|
|
}
|
|
|
|
#endif // ifndef STA_SENSORS_MS5607_HPP
|