mirror of
https://git.intern.spaceteamaachen.de/ALPAKA/driver-ms56xx.git
synced 2025-06-10 18:16:00 +00:00
Comment code
This commit is contained in:
parent
e5181b23cd
commit
265245e6ef
@ -6,8 +6,10 @@
|
|||||||
#include<sta/stm32/delay.hpp>
|
#include<sta/stm32/delay.hpp>
|
||||||
|
|
||||||
namespace sta {
|
namespace sta {
|
||||||
|
// Class to represent a MS5607 pressure sensor which is used with SPI
|
||||||
class MS5607 {
|
class MS5607 {
|
||||||
public:
|
public:
|
||||||
|
// Different OSR levels for the sensor
|
||||||
enum OsrLevel {
|
enum OsrLevel {
|
||||||
_256 = 0,
|
_256 = 0,
|
||||||
_512 = 1,
|
_512 = 1,
|
||||||
@ -18,17 +20,25 @@ namespace sta {
|
|||||||
|
|
||||||
MS5607(SpiDevice* device, OsrLevel osr=OsrLevel::_1024);
|
MS5607(SpiDevice* device, OsrLevel osr=OsrLevel::_1024);
|
||||||
|
|
||||||
|
// Getter for pressure
|
||||||
int32_t getPressure();
|
int32_t getPressure();
|
||||||
|
// Getter for temperature
|
||||||
int32_t getTemperature();
|
int32_t getTemperature();
|
||||||
|
|
||||||
void changeOsrLevel(OsrLevel newOsr) { this->osr_ = newOsr; }
|
void changeOsrLevel(OsrLevel newOsr) {
|
||||||
|
// Don't I need to write this to the sensor?
|
||||||
|
this->osr_ = newOsr;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
// STA internal object for SPi abstraction
|
||||||
SpiDevice* device_;
|
SpiDevice* device_;
|
||||||
OsrLevel osr_;
|
OsrLevel osr_;
|
||||||
|
|
||||||
|
// 6 Different constants; Includes Offsets, references etc.
|
||||||
uint16_t sens, off, tcs, tco, t_ref, tempsens;
|
uint16_t sens, off, tcs, tco, t_ref, tempsens;
|
||||||
|
|
||||||
|
// All possible commands to send to the sensor
|
||||||
enum Operations {
|
enum Operations {
|
||||||
RESET = 0x1E,
|
RESET = 0x1E,
|
||||||
READ_PROM = 0xA2,
|
READ_PROM = 0xA2,
|
||||||
@ -37,17 +47,18 @@ namespace sta {
|
|||||||
ADC_RESULT = 0x00
|
ADC_RESULT = 0x00
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Request pure ADC converted values from the sensor
|
||||||
|
// Calculations with offset required
|
||||||
uint32_t readPressure();
|
uint32_t readPressure();
|
||||||
uint32_t readTemp();
|
uint32_t readTemp();
|
||||||
|
// Calculate pure ADC values to final pressure/temperature values
|
||||||
int32_t calculatePressure(uint32_t d1, int32_t dT);
|
int32_t calculatePressure(uint32_t d1, int32_t dT);
|
||||||
int32_t calculateTemperature(uint32_t d2);
|
int32_t calculateTemperature(uint32_t d2);
|
||||||
|
|
||||||
void reset();
|
void reset();
|
||||||
void readPROM();
|
void readPROM();
|
||||||
|
|
||||||
// Take first bytes from buffer, swap them and store those in uint16_t
|
// Constants for waiting
|
||||||
static uint16_t uint_8BufferTouint16_t(uint8_t* buffer);
|
|
||||||
|
|
||||||
const char RESET_DELAY = 2800; // in uS
|
const char RESET_DELAY = 2800; // in uS
|
||||||
const char ADC_DELAY = 8220; // in uS; not sure if thats correct since the datasheet doesn't say anything explicitly
|
const char ADC_DELAY = 8220; // in uS; not sure if thats correct since the datasheet doesn't say anything explicitly
|
||||||
};
|
};
|
||||||
|
@ -5,11 +5,14 @@ namespace sta {
|
|||||||
this->device_ = device;
|
this->device_ = device;
|
||||||
this->osr_ = level;
|
this->osr_ = level;
|
||||||
|
|
||||||
|
// Reset device on start-up
|
||||||
this->reset();
|
this->reset();
|
||||||
|
// Read the PROM e.g. constants
|
||||||
this->readPROM();
|
this->readPROM();
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t MS5607::getPressure() {
|
int32_t MS5607::getPressure() {
|
||||||
|
// Get pure ADC value from the sensor
|
||||||
uint32_t d1 = readPressure();
|
uint32_t d1 = readPressure();
|
||||||
|
|
||||||
// Since pressure is temp-dependant, temperature needs to be polled
|
// Since pressure is temp-dependant, temperature needs to be polled
|
||||||
@ -22,22 +25,29 @@ namespace sta {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint32_t MS5607::readPressure() {
|
uint32_t MS5607::readPressure() {
|
||||||
|
// Request pressure value conversion
|
||||||
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();
|
this->device_->endTransmission();
|
||||||
|
|
||||||
|
// Wait for ADC to finish
|
||||||
delayUs(MS5607::ADC_DELAY);
|
delayUs(MS5607::ADC_DELAY);
|
||||||
uint8_t d1Arr[3];
|
|
||||||
|
|
||||||
|
// Request read of ADC value
|
||||||
|
uint8_t d1Arr[3];
|
||||||
this->device_->beginTransmission();
|
this->device_->beginTransmission();
|
||||||
this->device_->receive(d1Arr, 3);
|
this->device_->receive(d1Arr, 3);
|
||||||
this->device_->endTransmission();
|
this->device_->endTransmission();
|
||||||
|
|
||||||
|
// Convert into best possible type
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculations from the Datasheet
|
||||||
|
// Probably problems with type conversions
|
||||||
|
// If we used Rust...
|
||||||
int32_t MS5607::calculatePressure(uint32_t d1, int32_t dT) {
|
int32_t MS5607::calculatePressure(uint32_t d1, int32_t dT) {
|
||||||
int64_t offset = (this->off << 17) + ((this->tco * dT) >> 6);
|
int64_t offset = (this->off << 17) + ((this->tco * dT) >> 6);
|
||||||
int64_t sensitivity = (this->sens << 16) + ((this->tcs * dT) >> 7);
|
int64_t sensitivity = (this->sens << 16) + ((this->tcs * dT) >> 7);
|
||||||
@ -46,28 +56,36 @@ namespace sta {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int32_t MS5607::getTemperature() {
|
int32_t MS5607::getTemperature() {
|
||||||
|
// Get pure ADC value from the sensor
|
||||||
uint32_t d2 = readTemp();
|
uint32_t d2 = readTemp();
|
||||||
|
|
||||||
calculateTemperature(d2);
|
return calculateTemperature(d2);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t MS5607::readTemp() {
|
uint32_t MS5607::readTemp() {
|
||||||
|
// Request ADC conversion of temperature
|
||||||
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();
|
this->device_->endTransmission();
|
||||||
|
|
||||||
|
// Wait for ADC to finish
|
||||||
delayUs(MS5607::ADC_DELAY);
|
delayUs(MS5607::ADC_DELAY);
|
||||||
uint8_t d2Arr[3];
|
|
||||||
|
|
||||||
|
// Request ADC readout
|
||||||
|
uint8_t d2Arr[3];
|
||||||
this->device_->beginTransmission();
|
this->device_->beginTransmission();
|
||||||
this->device_->receive(d2Arr, 3);
|
this->device_->receive(d2Arr, 3);
|
||||||
this->device_->endTransmission();
|
this->device_->endTransmission();
|
||||||
|
|
||||||
|
// Convert into best possible type
|
||||||
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;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Calculations from the Datasheet
|
||||||
|
// Probably problems with type conversions
|
||||||
|
// If we used Rust...
|
||||||
int32_t MS5607::calculateTemperature(uint32_t d2) {
|
int32_t MS5607::calculateTemperature(uint32_t d2) {
|
||||||
int32_t dT = d2 - (this->t_ref << 8);
|
int32_t dT = d2 - (this->t_ref << 8);
|
||||||
int32_t temp = 2000 + ((dT * this->tempsens) >> 23);
|
int32_t temp = 2000 + ((dT * this->tempsens) >> 23);
|
||||||
@ -78,6 +96,7 @@ namespace sta {
|
|||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset sequence as described in datasheet
|
||||||
void MS5607::reset() {
|
void MS5607::reset() {
|
||||||
this->device_->beginTransmission();
|
this->device_->beginTransmission();
|
||||||
this->device_->transfer(MS5607::Operations::RESET);
|
this->device_->transfer(MS5607::Operations::RESET);
|
||||||
@ -85,6 +104,10 @@ namespace sta {
|
|||||||
delayUs(MS5607::RESET_DELAY);
|
delayUs(MS5607::RESET_DELAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read all constants from the PROM
|
||||||
|
// May be moved to be called in reset() function in future
|
||||||
|
// Request value x -> Read value x; Then request value y etc.
|
||||||
|
// Could be optimized; Not as important since only needed once at start-up
|
||||||
void MS5607::readPROM() {
|
void MS5607::readPROM() {
|
||||||
this->device_->beginTransmission();
|
this->device_->beginTransmission();
|
||||||
|
|
||||||
@ -121,7 +144,10 @@ namespace sta {
|
|||||||
this->device_->endTransmission();
|
this->device_->endTransmission();
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t MS5607::uint_8BufferTouint16_t(uint8_t* buffer) {
|
// Helper function:
|
||||||
|
// Take first bytes from buffer, swap them and store those in uint16_t
|
||||||
|
// Swap may not be necessary
|
||||||
|
uint16_t uint_8BufferTouint16_t(uint8_t* buffer) {
|
||||||
return (buffer[0] << 8) | buffer[1];
|
return (buffer[0] << 8) | buffer[1];
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user