From e5181b23cd56de1a58b9fb53ed3407b03f44b782 Mon Sep 17 00:00:00 2001 From: Theodor Teslia Date: Thu, 9 Mar 2023 20:12:36 +0100 Subject: [PATCH] Small potential fixes after real world experience with SPI --- include/sta/MS5607.hpp | 2 +- src/MS5607.cpp | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/sta/MS5607.hpp b/include/sta/MS5607.hpp index bc0f33c..32531e5 100644 --- a/include/sta/MS5607.hpp +++ b/include/sta/MS5607.hpp @@ -16,7 +16,7 @@ namespace sta { _4096 = 4 }; - MS5607(SpiDevice* device, OsrLevel osr); + MS5607(SpiDevice* device, OsrLevel osr=OsrLevel::_1024); int32_t getPressure(); int32_t getTemperature(); diff --git a/src/MS5607.cpp b/src/MS5607.cpp index a477f29..46db2f9 100644 --- a/src/MS5607.cpp +++ b/src/MS5607.cpp @@ -24,13 +24,17 @@ namespace sta { uint32_t MS5607::readPressure() { this->device_->beginTransmission(); this->device_->transfer(MS5607::Operations::D1_CONVERSION + 2*this->osr_); + this->device_->endTransmission(); + delayUs(MS5607::ADC_DELAY); uint8_t d1Arr[3]; + + this->device_->beginTransmission(); this->device_->receive(d1Arr, 3); this->device_->endTransmission(); uint32_t res = 0; - res |= d1Arr[0] | d1Arr[1] << 8 | d1Arr[2] << 16; + res |= d1Arr[0] | (d1Arr[1] << 8) | (d1Arr[2] << 16); return res; } @@ -50,13 +54,18 @@ namespace sta { uint32_t MS5607::readTemp() { this->device_->beginTransmission(); this->device_->transfer(MS5607::Operations::D2_CONVERSION + 2*this->osr_); + this->device_->endTransmission(); + delayUs(MS5607::ADC_DELAY); uint8_t d2Arr[3]; + + this->device_->beginTransmission(); this->device_->receive(d2Arr, 3); this->device_->endTransmission(); uint32_t res = 0; res |= d2Arr[0] | d2Arr[1] << 8 | d2Arr[2] << 16; + return res; } int32_t MS5607::calculateTemperature(uint32_t d2) { @@ -72,8 +81,8 @@ namespace sta { void MS5607::reset() { this->device_->beginTransmission(); this->device_->transfer(MS5607::Operations::RESET); - delayUs(MS5607::RESET_DELAY); this->device_->endTransmission(); + delayUs(MS5607::RESET_DELAY); } void MS5607::readPROM() { @@ -113,6 +122,6 @@ namespace sta { } uint16_t MS5607::uint_8BufferTouint16_t(uint8_t* buffer) { - return buffer[0] | (buffer[1] << 8); + return (buffer[0] << 8) | buffer[1]; } } \ No newline at end of file