diff --git a/include/sta/drivers/MS56xx.hpp b/include/sta/drivers/MS56xx.hpp index c7170f3..3b41e96 100644 --- a/include/sta/drivers/MS56xx.hpp +++ b/include/sta/drivers/MS56xx.hpp @@ -39,11 +39,11 @@ namespace sta * */ enum OsrLevel { - _256 = 0, - _512 = 1, - _1024 = 2, - _2048 = 3, - _4096 = 4 + _256 = 0, + _512 = 1, + _1024 = 2, + _2048 = 3, + _4096 = 4 }; /** @@ -57,6 +57,16 @@ namespace sta 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 * @@ -105,22 +115,13 @@ namespace sta */ 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); + float getPressure(Unit unit = Unit::hPa); /** * @brief Reads the current temperature value from the sensor. @@ -130,13 +131,6 @@ namespace sta */ 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. - */ - /** * @brief Provide a reference pressure value at a reference altitude in order to estimate the sealevel pressure. * @@ -146,14 +140,6 @@ namespace sta */ 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. * @@ -174,79 +160,78 @@ namespace sta void readPROM(); /** - * @brief + * @brief Convert the pressure value given in Pa to a different unit. * - * @param ms + * @param pressure A pressure value in Pa. + * @param unit The desired unit. + * @return float Returns the converted value. */ - void pulseCS(uint32_t ms=1); + float convertPressure(float pressure, Unit unit); /** - * @brief Compute the pressure values from the ADC output. + * @brief Small helper function for converting a uint8_t buffer to a single uint16_t. * - * @param d1 - * @param dT - * @param unit The unit to return the pressure values in. - * @return int32_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. */ - int32_t calculatePressure(uint32_t d1, int32_t dT, Unit unit); - - /** - * @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); - 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. - int32_t sealevel_; + // Pressure at sealevel. Use the standard atmosphere per default. + float sealevel_ = 1013.25; // 6 Different constants; Includes Offsets, references etc. - uint16_t sens, off, tcs, tco, t_ref, tempsens; + 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 - - // Function to get the delay times needed for different OSR Levels - // Values not found in datasheet (facepalm) - // Thus partly googled, partly tested out - static uint8_t delayTimes(OsrLevel level) { - switch (level) { - case _256: - return 1; - case _512: - return 2; - case _1024: - return 3; - case _2048: - return 5; - case _4096: - return 10; - } - } }; - } #endif // ifndef STA_SENSORS_MS5607_HPP \ No newline at end of file diff --git a/src/MS56xx.cpp b/src/MS56xx.cpp index 52c592d..db9f146 100644 --- a/src/MS56xx.cpp +++ b/src/MS56xx.cpp @@ -1,6 +1,9 @@ #include #include +#include + +#include namespace sta @@ -8,7 +11,8 @@ namespace sta MS56xx::MS56xx(SPIDevice * device, DelayUsFunc delay, OsrLevel level /* = OsrLevel::_1024 */) : device_{device}, delay_{delay}, - osr_{level} + osr_{level}, + intf_{Intf::SPI} { STA_ASSERT(device != nullptr); } @@ -16,7 +20,8 @@ namespace sta MS56xx::MS56xx(I2CDevice * device, DelayUsFunc delay, OsrLevel osr = OsrLevel::_1024) : device_{device}, delay_{delay}, - osr_{osr} + osr_{osr}, + intf_{Intf::I2C} { STA_ASSERT(device != nullptr); } @@ -30,12 +35,6 @@ namespace sta this->readPROM(); } - void MS56xx::pulseCS(uint32_t ms=1) { - this->device_->endTransmission(); - delay_(ms * 1000); - this->device_->beginTransmission(); - } - void MS56xx::setOsr(OsrLevel osr) { osr_ = osr; @@ -47,161 +46,122 @@ namespace sta 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 */) + float MS56xx::getPressure(Unit unit /* = Unit::hPa */) { - // Request the ADC to read pressure values. - busCommand(MS56xx::Operations::D1_CONVERSION + 2*this->osr_); + // Request the ADC to read temperature values. + busCommand(MS56xx::Operations::D2_CONVERSION + 2*this->osr_); - // This might be an incorrect duration. - delay_(10 * 1000); + // 8.22 ms conversion according to the datasheet. + delay_(osrDelay()); 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); + // Difference between actual and reference temperature. + uint32_t rawTemperature = buffer[0] << 16 | buffer[1] << 8 | buffer[2]; + uint32_t dT = rawTemperature - refTemp_ * (0x01<<8); + + // Request the ADC to read pressure values. + busCommand(MS56xx::Operations::D1_CONVERSION + 2*this->osr_); + + // 8.22 ms conversion according to the datasheet. + delay_(osrDelay()); + + busRead(MS56xx::Operations::ADC_RESULT, buffer, 3); + + uint32_t rawPressure = buffer[0] << 16 | buffer[1] << 8 | buffer[2]; + + // offset and sensitivity at actual temperature. + uint64_t offset = offset_ * (0x01<<16) + (tempOffsetCoeff_ * dT) / (0x01<<7); + uint64_t sensibility = sensibility_ * (0x01<<15) + (tempSensCoeff_ * dT) / (0x01<<8); + + // The pressure in Pa. + float pressure = (rawPressure * (sensibility / 0x01<<21) - offset) / (0x01<<15); + pressure = convertPressure(pressure, 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); + // 8.22 ms conversion according to the datasheet. + delay_(osrDelay()); uint8_t buffer[3] = { 0x00, 0x00, 0x00 }; busRead(MS56xx::Operations::ADC_RESULT, buffer, 3); + uint32_t rawTemperature = buffer[0] | buffer[1] << 8 | buffer[2] << 16; // Convert the raw values to actual temperature values. - uint32_t res = buffer[0] | buffer[1] << 8 | buffer[2] << 16; - int32_t temperature = calculateTemperature(res); + uint32_t dT = rawTemperature - refTemp_ * (0x01<<8); + uint32_t temperature = 20 * dT * tempCoeff_ / (0x01<<23); 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); + // Taken from: https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf, page 17. + sealevel_ = pressRef / (std::pow((1 - altRef / 44330), 5.255)); } float MS56xx::getAltitudeEstimate() { - int32_t temperature = getTemperature(); + float pressure = getPressure(Unit::hPa); - return getAltitudeEstimate(temperature); + // Taken from: https://cdn-shop.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf, page 16 + float altitude = 44330 * (1 - std::pow(pressure / sealevel_, 1 / 5.255)); } - int32_t MS56xx::reverseTempCalc(int32_t temp) { - return this->tempsens; + void MS56xx::readPROM() { + uint8_t buffer[2]; + + busRead(Operations::READ_PROM, buffer, 2); + sensibility_ = uint_8BufferTouint16_t(buffer); + + delay_(1000); + + busRead(Operations::READ_PROM+2, buffer, 2); + offset_ = uint_8BufferTouint16_t(buffer); + + delay_(1000); + + busRead(Operations::READ_PROM+4, buffer, 2); + tempSensCoeff_ = uint_8BufferTouint16_t(buffer); + + delay_(1000); + + busRead(Operations::READ_PROM+6, buffer, 2); + tempOffsetCoeff_ = uint_8BufferTouint16_t(buffer); + + delay_(1000); + + busRead(Operations::READ_PROM+8, buffer, 2); + refTemp_ = uint_8BufferTouint16_t(buffer); + + delay_(1000); + + busRead(Operations::READ_PROM+10, buffer, 2); + tempCoeff_ = uint_8BufferTouint16_t(buffer); } - 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; - + inline float MS56xx::convertPressure(float pressure, Unit unit) + { switch (unit) { - case Unit::hPa: - /* code */ - break; - case Unit::Pa: - - break; case Unit::mbar: - break; - + return pressure / 100.0f; + case Unit::hPa: + return pressure / 100.0f; + case Unit::Pa: + return pressure; default: + STA_UNREACHABLE(); break; } - - return pres; } - 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); - - // Further calculations for low (<20) and very low (<(-15)) could be possible - // But I don't know whether they are necessary - - return temp; - } - - // 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 MS56xx::readPROM() { - this->device_->beginTransmission(); - - this->device_->transfer(Operations::READ_PROM); - uint8_t sensArr[2]; - this->device_->receive(sensArr, 2); - this->sens = uint_8BufferTouint16_t(sensArr); - - pulseCS(); - - this->device_->transfer(Operations::READ_PROM+2); - uint8_t offArr[2]; - this->device_->receive(offArr, 2); - this->off = uint_8BufferTouint16_t(offArr); - - pulseCS(); - - this->device_->transfer(Operations::READ_PROM+4); - uint8_t tcsArr[2]; - this->device_->receive(tcsArr, 2); - this->sens = uint_8BufferTouint16_t(sensArr); - - pulseCS(); - - this->device_->transfer(Operations::READ_PROM+6); - uint8_t tcoArr[2]; - this->device_->receive(tcoArr, 2); - this->tco = uint_8BufferTouint16_t(tcoArr); - - pulseCS(); - - this->device_->transfer(Operations::READ_PROM+8); - uint8_t t_refArr[2]; - this->device_->receive(t_refArr, 2); - this->t_ref = uint_8BufferTouint16_t(t_refArr); - - pulseCS(); - - this->device_->transfer(Operations::READ_PROM+0xA); - uint8_t tempsensArr[2]; - this->device_->receive(tempsensArr, 2); - this->tempsens = uint_8BufferTouint16_t(tempsensArr); - - this->device_->endTransmission(); - } - - // Helper function: - // Take first bytes from buffer, swap them and store those in uint16_t - // Swap may not be necessary - uint16_t MS56xx::uint_8BufferTouint16_t(uint8_t* buffer) { + inline uint16_t MS56xx::uint_8BufferTouint16_t(uint8_t* buffer) { return (buffer[0] << 8) | buffer[1]; } @@ -214,6 +174,21 @@ namespace sta return true; } + uint32_t MS56xx::osrDelay() { + switch (osr_) { + case _256: + return 1000; + case _512: + return 2000; + case _1024: + return 3000; + case _2048: + return 5000; + case _4096: + return 10000; + } + } + bool MS56xx::busRead(uint8_t reg, uint8_t * buffer, size_t length) { this->device_->beginTransmission();