From f3097b7f259b2c140d42984513850ffd27d01e78 Mon Sep 17 00:00:00 2001 From: Theodor Teslia Date: Thu, 27 Apr 2023 11:03:49 +0200 Subject: [PATCH] Add some possible fixes (pulseCS and explicit type casts) --- .DS_Store | Bin 0 -> 6148 bytes include/sta/MS5607.hpp | 7 +++++++ src/MS5607.cpp | 32 ++++++++++++++++++++++++-------- 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c239d91a689e0bc52be6961a487edee2a6602513 GIT binary patch literal 6148 zcmeHKzfZzI6n+OaR$WMRaO~tJ35ju~BCalMP6`+#0ZR}ZociB5I=ZQYlcR}=BRcEI zq%nT)?vx${HjOdwC3j!@e)N5OP474&Ql);qK$IgQAA`|b#5BP;&Ld?R>sbLRenzWN zsMMl%CzUArtK>)TEyC{dybk$yhrom#eL)3ZH*|mcM*^zTI-;^_lU` zQDSZI2Pma^^gX2$s!>FBI&`{IrkKu1a(GyKe7d>U9LAp0jz&zTXE}WI`UgHuItH5- z_0xR(ZOzBYNb5(Z_)O1o`2=W2S!mOxnybt5>nM`!e>dD*xHkQ5T~dd1 zbV5HL{d#mj>-vp!$KFVGZ-~yRoFs>CXZOWwFd=WG!mK=p*W3Ut#ePTu?AdJol0lh8 z0Z~8{_*Q_|hX8{yv=|%IO9v)>1OR$)Yr{34CAcQA7+Q=CA_7yU6lhA7Jz^+R4u4?t zLW{9MQ%*{E#&c|EWltzdcZWaF>7+t~GK&JDfU7{(+~#=y-*8u zSIQ;)B)hk!ejM+$0meQC8|TFabqOZB9qS9;idQkTAr|ljFtivOga;;n1hfn?i2}c> Fz$f4=ua^J- literal 0 HcmV?d00001 diff --git a/include/sta/MS5607.hpp b/include/sta/MS5607.hpp index eaad14b..0860cd5 100644 --- a/include/sta/MS5607.hpp +++ b/include/sta/MS5607.hpp @@ -31,6 +31,13 @@ namespace sta { } private: + // Helper method to keep code clean + void pulseCS(uint32_t ms=1) { + this->device_->endTransmission(); + sta::delayMs(ms); + this->device_->beginTransmission(); + } + // STA internal object for SPi abstraction SpiDevice* device_; OsrLevel osr_; diff --git a/src/MS5607.cpp b/src/MS5607.cpp index c50f6ae..c4de976 100644 --- a/src/MS5607.cpp +++ b/src/MS5607.cpp @@ -19,7 +19,7 @@ namespace sta { // Could be too costly timewise, since 8ms is needed for the ADC // May need future optimisation uint32_t d2 = readTemp(); - int32_t dT = d2 - (this->t_ref << 8); + int32_t dT = d2 - ( ((uint32_t)this->t_ref) << 8); return calculatePressure(d1, dT); } @@ -31,16 +31,19 @@ namespace sta { this->device_->endTransmission(); // Wait for ADC to finish + // Could do sth. else and schedule continuation with scheduler in RTOS delayUs(MS5607::ADC_DELAY); - // Request read of ADC value + // Request readout of ADC value uint8_t d1Arr[3]; this->device_->beginTransmission(); + this->device_->transfer(MS5607::Operations::ADC_RESULT); this->device_->receive(d1Arr, 3); this->device_->endTransmission(); // Convert into best possible type uint32_t res = 0; + // Shifting may not be necessary, but idk w/o testing res |= d1Arr[0] | (d1Arr[1] << 8) | (d1Arr[2] << 16); return res; } @@ -49,9 +52,9 @@ namespace sta { // Probably problems with type conversions // If we used Rust... int32_t MS5607::calculatePressure(uint32_t d1, int32_t dT) { - int64_t offset = (this->off << 17) + ((this->tco * dT) >> 6); - int64_t sensitivity = (this->sens << 16) + ((this->tcs * dT) >> 7); - int32_t pres = (((d1 * sensitivity) >> 21) - offset) >> 15; + int64_t offset = ( ((uint32_t)this->off) << 17) + ( ( ((uint32_t)this->tco) * dT ) >> 6); + int64_t sensitivity = ( ((uint32_t)this->sens) << 16) + ( ( ((uint32_t)this->tcs) * dT ) >> 7); + int32_t pres = ( (( ((uint64_t)d1) * sensitivity) >> 21) - offset ) >> 15; return pres; } @@ -69,16 +72,19 @@ namespace sta { this->device_->endTransmission(); // Wait for ADC to finish + // Could do sth. else and schedule continuation with scheduler in RTOS delayUs(MS5607::ADC_DELAY); // Request ADC readout uint8_t d2Arr[3]; this->device_->beginTransmission(); + this->device_->transfer(MS5607::Operations::ADC_RESULT); this->device_->receive(d2Arr, 3); - this->device_->endTransmission(); + this->device_->endTransmission(); // Convert into best possible type uint32_t res = 0; + // Shifting may be unnecessary? Don't know really w/o testing res |= d2Arr[0] | d2Arr[1] << 8 | d2Arr[2] << 16; return res; } @@ -87,8 +93,8 @@ namespace sta { // Probably problems with type conversions // If we used Rust... int32_t MS5607::calculateTemperature(uint32_t d2) { - int32_t dT = d2 - (this->t_ref << 8); - int32_t temp = 2000 + ((dT * this->tempsens) >> 23); + 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 @@ -116,26 +122,36 @@ namespace sta { 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);