diff --git a/include/sta/MS5607.hpp b/include/sta/MS5607.hpp index 2772349..e2c935a 100644 --- a/include/sta/MS5607.hpp +++ b/include/sta/MS5607.hpp @@ -28,9 +28,10 @@ namespace sta { // Function to get Pressure // Parameter should be Temperature in Celsius * 100 (e.g. 20.3 deg. C -> 2030) // If currently still processing, use old value - // If currently not processing, start processing to minmize potential wating times + // If currently not processing, start processing to minimize potential waiting times int32_t getPressure(int32_t temp, uint32_t curTime); + // --- DEPRECATED --- // Parameterless version of function above // Calls getTemperature, to get temp // NOT RECOMMENDED @@ -40,6 +41,7 @@ namespace sta { // Deprecated because of time constrains // NOT RECOMMENDED int32_t getTemperature(); + // --- DEPRECATED --- void changeOsrLevel(OsrLevel newOsr) { // Don't I need to write this to the sensor? @@ -54,11 +56,13 @@ namespace sta { this->device_->beginTransmission(); } - // Last Presure Value meassured; Used to give any output if still calculating ADC + // Last Pressure Value measured; Used to give any output if still calculating ADC int32_t lastPresVal; // Value to store, when adc was started // On STM32 with HAL_GetTick() (Gives ms since startup) uint32_t adcStartTime; + // To prevent calculation of adc without reading value + bool presRead; // STA internal object for SPi abstraction SpiDevice* device_; @@ -87,11 +91,13 @@ namespace sta { int32_t calculatePressure(uint32_t d1, int32_t dT); int32_t calculateTemperature(uint32_t d2); + int32_t reverseTempCalc(int32_t temp); + void reset(); void readPROM(); // Constants for waiting - const char RESET_DELAY = 2800; // in uS + 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) @@ -99,7 +105,15 @@ namespace sta { static uint8_t delayTimes(OsrLevel level) { switch (level) { case _256: - return + return 1; + case _512: + return 2; + case _1024: + return 3; + case _2048: + return 5; + case _4096: + return 10; } } }; diff --git a/src/MS5607.cpp b/src/MS5607.cpp index 8943f5e..d9fd72c 100644 --- a/src/MS5607.cpp +++ b/src/MS5607.cpp @@ -1,10 +1,15 @@ #include "sta/MS5607.hpp" namespace sta { + // Forward declaration + uint16_t uint_8BufferTouint16_t(uint8_t* buffer); + MS5607::MS5607(SpiDevice* device, OsrLevel level) { this->device_ = device; this->osr_ = level; this->lastPresVal = -1; + this->adcStartTime = -1; + this->presRead = true; // Reset device on start-up this->reset(); @@ -12,17 +17,44 @@ namespace sta { this->readPROM(); } - void MS5607::requestAdcReadout() { + void MS5607::requestAdcReadout(uint32_t time) { // Get current state of calculation - + if (time-adcStartTime < delayTimes(this->osr_) || !presRead) { + // Time since last adc command is not enough + return; + } this->device_->beginTransmission(); this->device_->transfer(MS5607::Operations::D1_CONVERSION + 2*this->osr_); this->device_->endTransmission(); + + adcStartTime = time; + presRead = false; } - int32_t MS5607::getPressure(int32_t temp) { + int32_t MS5607::getPressure(int32_t temp, uint32_t time) { + if (time-adcStartTime < delayTimes(this->osr_) || presRead) { + // Time since last adc command is not enough + return lastPresVal; + } + uint8_t buffer[3] = { 0 }; + + this->device_->beginTransmission(); + this->device_->transfer(MS5607::Operations::ADC_RESULT); + this->device_->receive(buffer, 3); + this->device_->endTransmission(); + this->presRead = true; + + uint32_t d1_val = buffer[0] << 16 | buffer[1] << 8 | buffer[2]; + + this->requestAdcReadout(time); + + return calculatePressure(d1_val, reverseTempCalc(temp)); + } + + int32_t MS5607::reverseTempCalc(int32_t temp) { + return this->tempsens; } // DEPRECATED @@ -48,14 +80,8 @@ namespace sta { // Wait for ADC to finish // Could do sth. else and schedule continuation with scheduler in RTOS - uint8_t buf[1] = { 0 }; - do { - *buf = 0; - uint8_t tx[1] = { 0b11111110 }; - this->device_->beginTransmission(); - this->device_->transfer(tx, buf, 1); - this->device_->endTransmission(); - } while (*buf == 0x00); + // TODO: Find out min + sta::delayMs(10); // Request readout of ADC value uint8_t d1Arr[3]; @@ -99,14 +125,8 @@ namespace sta { // Wait for ADC to finish // Could do sth. else and schedule continuation with scheduler in RTOS - uint8_t buf[1] = { 0 }; - do { - *buf = 0; - uint8_t tx[1] = { 0b11111110 }; - this->device_->beginTransmission(); - this->device_->transfer(tx, buf, 1); - this->device_->endTransmission(); - } while (*buf == 0x00); + // TODO: Test out min + sta::delayMs(10); // Request ADC readout