diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..c239d91 Binary files /dev/null and b/.DS_Store differ 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);