Small potential fixes after real world experience with SPI

This commit is contained in:
Theodor Teslia 2023-03-09 20:12:36 +01:00
parent 71b9753860
commit e5181b23cd
2 changed files with 13 additions and 4 deletions

View File

@ -16,7 +16,7 @@ namespace sta {
_4096 = 4 _4096 = 4
}; };
MS5607(SpiDevice* device, OsrLevel osr); MS5607(SpiDevice* device, OsrLevel osr=OsrLevel::_1024);
int32_t getPressure(); int32_t getPressure();
int32_t getTemperature(); int32_t getTemperature();

View File

@ -24,13 +24,17 @@ namespace sta {
uint32_t MS5607::readPressure() { uint32_t MS5607::readPressure() {
this->device_->beginTransmission(); this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::D1_CONVERSION + 2*this->osr_); this->device_->transfer(MS5607::Operations::D1_CONVERSION + 2*this->osr_);
this->device_->endTransmission();
delayUs(MS5607::ADC_DELAY); delayUs(MS5607::ADC_DELAY);
uint8_t d1Arr[3]; uint8_t d1Arr[3];
this->device_->beginTransmission();
this->device_->receive(d1Arr, 3); this->device_->receive(d1Arr, 3);
this->device_->endTransmission(); this->device_->endTransmission();
uint32_t res = 0; uint32_t res = 0;
res |= d1Arr[0] | d1Arr[1] << 8 | d1Arr[2] << 16; res |= d1Arr[0] | (d1Arr[1] << 8) | (d1Arr[2] << 16);
return res; return res;
} }
@ -50,13 +54,18 @@ namespace sta {
uint32_t MS5607::readTemp() { uint32_t MS5607::readTemp() {
this->device_->beginTransmission(); this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::D2_CONVERSION + 2*this->osr_); this->device_->transfer(MS5607::Operations::D2_CONVERSION + 2*this->osr_);
this->device_->endTransmission();
delayUs(MS5607::ADC_DELAY); delayUs(MS5607::ADC_DELAY);
uint8_t d2Arr[3]; uint8_t d2Arr[3];
this->device_->beginTransmission();
this->device_->receive(d2Arr, 3); this->device_->receive(d2Arr, 3);
this->device_->endTransmission(); this->device_->endTransmission();
uint32_t res = 0; uint32_t res = 0;
res |= d2Arr[0] | d2Arr[1] << 8 | d2Arr[2] << 16; res |= d2Arr[0] | d2Arr[1] << 8 | d2Arr[2] << 16;
return res;
} }
int32_t MS5607::calculateTemperature(uint32_t d2) { int32_t MS5607::calculateTemperature(uint32_t d2) {
@ -72,8 +81,8 @@ namespace sta {
void MS5607::reset() { void MS5607::reset() {
this->device_->beginTransmission(); this->device_->beginTransmission();
this->device_->transfer(MS5607::Operations::RESET); this->device_->transfer(MS5607::Operations::RESET);
delayUs(MS5607::RESET_DELAY);
this->device_->endTransmission(); this->device_->endTransmission();
delayUs(MS5607::RESET_DELAY);
} }
void MS5607::readPROM() { void MS5607::readPROM() {
@ -113,6 +122,6 @@ namespace sta {
} }
uint16_t MS5607::uint_8BufferTouint16_t(uint8_t* buffer) { uint16_t MS5607::uint_8BufferTouint16_t(uint8_t* buffer) {
return buffer[0] | (buffer[1] << 8); return (buffer[0] << 8) | buffer[1];
} }
} }