0.2.0 MS5611_SPI

This commit is contained in:
Rob Tillaart 2023-11-30 12:18:19 +01:00
parent ced9676b84
commit 4235d41b52
14 changed files with 178 additions and 160 deletions

View File

@ -6,11 +6,18 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.0] - 2023-11-30
- refactor constructor interface - breaking changes.
- minimize conditional code. -- create SPI_CLASS macro to solve it.
- update readme.md
- update examples
----
## [0.1.3] - 2023-11-14
- update readme.md
- update keywords
## [0.1.2] - 2022-12-14
- fix #6 support for Nano33,
- adding extra **begin()**

View File

@ -1,7 +1,7 @@
//
// FILE: MS5611_SPI.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.2.0
// PURPOSE: MS5611 (SPI) Temperature & Pressure library for Arduino
// URL: https://github.com/RobTillaart/MS5611_SPI
//
@ -23,7 +23,7 @@
//
// PUBLIC
//
MS5611_SPI::MS5611_SPI(uint8_t select, uint8_t dataOut, uint8_t dataIn, uint8_t clock)
MS5611_SPI::MS5611_SPI(uint8_t select, __SPI_CLASS__ * mySPI)
{
// _address = deviceAddress; // TODO
_samplingRate = OSR_ULTRA_LOW;
@ -36,12 +36,36 @@ MS5611_SPI::MS5611_SPI(uint8_t select, uint8_t dataOut, uint8_t dataIn, uint8_t
_temperatureOffset = 0;
_compensation = true;
// SPI
_select = select;
_dataIn = 255;
_dataOut = 255;
_clock = 255;
_hwSPI = true;
_mySPI = mySPI;
}
MS5611_SPI::MS5611_SPI(uint8_t select, uint8_t dataOut, uint8_t dataIn, uint8_t clock)
{
// _address = deviceAddress; // TODO
_samplingRate = OSR_ULTRA_LOW;
_temperature = MS5611_NOT_READ;
_pressure = MS5611_NOT_READ;
_result = MS5611_NOT_READ;
_lastRead = 0;
_deviceID = 0;
_pressureOffset = 0;
_temperatureOffset = 0;
_compensation = false;
// SPI
_select = select;
_dataIn = dataIn;
_dataOut = dataOut;
_clock = clock;
_hwSPI = (dataIn == 255) && (dataOut == 255) && (clock == 255);
_hwSPI = false;
_mySPI = NULL;
}
@ -57,26 +81,9 @@ bool MS5611_SPI::begin()
if(_hwSPI)
{
#if defined(ESP32)
if (_useHSPI) // HSPI
{
mySPI = new SPIClass(HSPI);
mySPI->end();
mySPI->begin(14, 12, 13, _select); // CLK=14 MISO=12 MOSI=13
}
else // VSPI
{
mySPI = new SPIClass(VSPI);
mySPI->end();
mySPI->begin(18, 19, 23, _select); // CLK=18 MISO=19 MOSI=23
}
#else // generic hardware SPI
// Serial.println("HW_SPI");
mySPI = &SPI;
mySPI->begin(); // FIX #6
mySPI->end();
mySPI->begin();
#endif
_mySPI->begin(); // FIX #6
_mySPI->end();
_mySPI->begin();
delay(1);
}
else
@ -114,7 +121,7 @@ bool MS5611_SPI::reset(uint8_t mathMode)
// initialize the C[] array
initConstants(mathMode);
// read factory calibrations from EEPROM.
// read factory calibrations from EEPROM.
bool ROM_OK = true;
for (uint8_t reg = 0; reg < 7; reg++)
{
@ -201,75 +208,75 @@ void MS5611_SPI::setOversampling(osr_t samplingRate)
osr_t MS5611_SPI::getOversampling() const
{
return (osr_t) _samplingRate;
};
}
float MS5611_SPI::getTemperature() const
{
if (_temperatureOffset == 0) return _temperature * 0.01;
return _temperature * 0.01 + _temperatureOffset;
};
}
float MS5611_SPI::getPressure() const
{
if (_pressureOffset == 0) return _pressure * 0.01;
return _pressure * 0.01 + _pressureOffset;
};
}
void MS5611_SPI::setPressureOffset(float offset)
{
_pressureOffset = offset;
};
}
float MS5611_SPI::getPressureOffset()
{
return _pressureOffset;
};
}
void MS5611_SPI::setTemperatureOffset(float offset)
{
_temperatureOffset = offset;
};
}
float MS5611_SPI::getTemperatureOffset()
{
return _temperatureOffset;
};
}
int MS5611_SPI::getLastResult() const
{
return _result;
};
}
uint32_t MS5611_SPI::lastRead() const
{
return _lastRead;
};
}
uint32_t MS5611_SPI::getDeviceID() const
{
return _deviceID;
};
}
void MS5611_SPI::setCompensation(bool flag)
{
_compensation = flag;
};
}
bool MS5611_SPI::getCompensation()
{
return _compensation;
};
}
// EXPERIMENTAL
@ -289,48 +296,20 @@ void MS5611_SPI::setSPIspeed(uint32_t speed)
{
_SPIspeed = speed;
_spi_settings = SPISettings(_SPIspeed, MSBFIRST, SPI_MODE0);
};
}
uint32_t MS5611_SPI::getSPIspeed()
{
return _SPIspeed;
};
}
bool MS5611_SPI::usesHWSPI()
{
return _hwSPI;
};
#if defined(ESP32)
void MS5611_SPI::selectHSPI() { _useHSPI = true; };
void MS5611_SPI::selectVSPI() { _useHSPI = false; };
bool MS5611_SPI::usesHSPI() { return _useHSPI; };
bool MS5611_SPI::usesVSPI() { return !_useHSPI; };
void MS5611_SPI::setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select)
{
_clock = clk;
_dataIn = miso;
_dataOut = mosi;
_select = select;
pinMode(_clock, OUTPUT);
pinMode(_dataIn, INPUT);
pinMode(_dataOut, OUTPUT);
pinMode(_select, OUTPUT);
digitalWrite(_clock, HIGH);
digitalWrite(_dataOut, LOW);
digitalWrite(_select, HIGH);
mySPI->end(); // disable SPI and restart
mySPI->begin(clk, miso, mosi, select);
}
#endif
/////////////////////////////////////////////////////
@ -370,12 +349,12 @@ uint16_t MS5611_SPI::readProm(uint8_t reg)
digitalWrite(_select, LOW);
if (_hwSPI)
{
mySPI->beginTransaction(_spi_settings);
mySPI->transfer(MS5611_CMD_READ_PROM + reg * 2);
value += mySPI->transfer(0x00);
_mySPI->beginTransaction(_spi_settings);
_mySPI->transfer(MS5611_CMD_READ_PROM + reg * 2);
value += _mySPI->transfer(0x00);
value <<= 8;
value += mySPI->transfer(0x00);
mySPI->endTransaction();
value += _mySPI->transfer(0x00);
_mySPI->endTransaction();
}
else // Software SPI
{
@ -398,14 +377,14 @@ uint32_t MS5611_SPI::readADC()
digitalWrite(_select, LOW);
if (_hwSPI)
{
mySPI->beginTransaction(_spi_settings);
mySPI->transfer(0x00);
value += mySPI->transfer(0x00);
_mySPI->beginTransaction(_spi_settings);
_mySPI->transfer(0x00);
value += _mySPI->transfer(0x00);
value <<= 8;
value += mySPI->transfer(0x00);
value += _mySPI->transfer(0x00);
value <<= 8;
value += mySPI->transfer(0x00);
mySPI->endTransaction();
value += _mySPI->transfer(0x00);
_mySPI->endTransaction();
}
else // Software SPI
{
@ -428,9 +407,9 @@ int MS5611_SPI::command(const uint8_t command)
digitalWrite(_select, LOW);
if (_hwSPI)
{
mySPI->beginTransaction(_spi_settings);
mySPI->transfer(command);
mySPI->endTransaction();
_mySPI->beginTransaction(_spi_settings);
_mySPI->transfer(command);
_mySPI->endTransaction();
}
else // Software SPI
{

View File

@ -2,7 +2,7 @@
//
// FILE: MS5611_SPI.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.2.0
// PURPOSE: S5611 (SPI) Temperature & Pressure library for Arduino
// URL: https://github.com/RobTillaart/MS5611_SPI
@ -30,7 +30,15 @@
// CS to GND ==> 0x77
#define MS5611_SPI_LIB_VERSION (F("0.1.3 EXPERIMENTAL"))
#define MS5611_SPI_LIB_VERSION (F("0.2.0 EXPERIMENTAL"))
#ifndef __SPI_CLASS__
#if defined(ARDUINO_ARCH_RP2040)
#define __SPI_CLASS__ SPIClassRP2040
#else
#define __SPI_CLASS__ SPIClass
#endif
#endif
#define MS5611_READ_OK 0
@ -51,7 +59,10 @@ enum osr_t
class MS5611_SPI
{
public:
explicit MS5611_SPI(uint8_t select, uint8_t dataOut = 255, uint8_t dataIn = 255, uint8_t clock = 255);
// HARDWARE SPI
explicit MS5611_SPI(uint8_t select, __SPI_CLASS__ * mySPI = &SPI);
// SOFTWARE SPI
explicit MS5611_SPI(uint8_t select, uint8_t dataOut, uint8_t dataIn, uint8_t clock);
bool begin();
bool isConnected();
@ -115,17 +126,6 @@ public:
// debugging
bool usesHWSPI();
// ESP32 specific
#if defined(ESP32)
void selectHSPI();
void selectVSPI();
bool usesHSPI();
bool usesVSPI();
// to overrule ESP32 default hardware pins
void setGPIOpins(uint8_t clk, uint8_t miso, uint8_t mosi, uint8_t select);
#endif
protected:
void convert(const uint8_t addr, uint8_t bits);
@ -154,11 +154,8 @@ protected:
uint32_t _SPIspeed = 1000000;
uint8_t swSPI_transfer(uint8_t value);
SPIClass * mySPI;
SPISettings _spi_settings;
#if defined(ESP32)
bool _useHSPI = true;
#endif
__SPI_CLASS__ * _mySPI;
SPISettings _spi_settings;
};

View File

@ -16,6 +16,8 @@ Arduino library (SPI) for MS5611 pressure and temperature sensor.
## Description
**WARNING: the MS5611 has problems with self heating when using SPI interface so use with care.**
The MS5611 is a high resolution pressure and temperature sensor a.k.a GY-63.
The high resolution is made possible by oversampling many times.
@ -24,6 +26,14 @@ It is based upon the 0.3.6 version of the I2C library,
see - https://github.com/RobTillaart/MS5611
#### 0.2.0 Breaking change
The version 0.2.0 has breaking changes in the interface.
The essence is removal of ESP32 specific code from the library.
This makes it possible to support the ESP32-S3 and other processors in the future.
Also it makes the library a bit simpler to maintain.
#### Compatibility
The library should be compatible with MS56XX, MS57xx and MS58xx devices (to be tested).
@ -32,7 +42,7 @@ Note: Some device types will return only 50% of the pressure value.
This is solved by calling **reset(1)** to select the math used.
#### Self heating
#### Self heating problem
In some configurations especially when using SPI the sensor showed a self heating effect.
First this was approached as a problem, so investigations were done to understand the
@ -84,10 +94,9 @@ dedicated temperature sensor for this (e.g. DS18B20).
#### Related libraries
For pressure conversions see - https://github.com/RobTillaart/pressure
For temperature conversions see - https://github.com/RobTillaart/Temperature
- https://github.com/RobTillaart/MS5611 - I2C version - working OK.
- https://github.com/RobTillaart/pressure - conversions.
- https://github.com/RobTillaart/Temperature - conversions.
## WARNING EXPERIMENTAL
@ -148,7 +157,9 @@ If you have experiences with this library please share them in the issues.
#### Base
- **MS5611_SPI(uint8_t select, uint8_t dataOut = 255, uint8_t dataIn = 255, uint8_t clock = 255)** constructor.
- **MS5611_SPI(uint8_t select, SPIClassRP2040 \* myspi = &SPI)** constructor, HW SPI RP2040.
- **MS5611_SPI(uint8_t select, SPIClass \* myspi = &SPI)** constructor, HW SPI other.
- **MS5611_SPI(uint8_t select, uint8_t dataOut, uint8_t dataIn, uint8_t clock)** constructor, SW SPI
- **bool begin()** initializes internals,
- **bool isConnected()** checks device by calling **read()**.
- **bool reset(uint8_t mathMode = 0)** resets the chip and loads constants from its ROM.

View File

@ -31,8 +31,11 @@
void setup()
{
Serial.begin(115200);
while(!Serial);
Serial.println();
Serial.println(__FILE__);
Wire.begin();
Wire.beginTransmission(0x76);
Wire.write(0);
@ -58,4 +61,4 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -35,8 +35,6 @@
// MS5611_SPI(select, dataOut, dataIn, clock);
// --------------------------------------------
// MS5611_SPI MS5611(10, 11, 12, 13); // UNO SW SPI (5V problem?
// MS5611_SPI MS5611(10); // UNO HW SPI
MS5611_SPI MS5611( 5, 23, 19, 18); // ESP32 SW SPI
@ -49,15 +47,14 @@ uint32_t start, stop;
void setup()
{
Serial.begin(115200);
while (!Serial);
// pinMode(LED_BUILTIN, OUTPUT);
while(!Serial);
Serial.println();
Serial.println(__FILE__);
Serial.print("MS5611_SPI_LIB_VERSION: ");
Serial.println(MS5611_SPI_LIB_VERSION);
// pinMode(LED_BUILTIN, OUTPUT);
if (MS5611.begin() == true)
{
Serial.print("MS5611 found: ");

View File

@ -35,30 +35,27 @@
// MS5611_SPI(select, dataOut, dataIn, clock);
// --------------------------------------------
// MS5611_SPI MS5611(10, 11, 12, 13); // UNO SW SPI (5V problem?
// MS5611_SPI MS5611(10); // UNO HW SPI
//
// MS5611_SPI MS5611( 5, 23, 19, 18); // ESP32 SW SPI
// MS5611_SPI MS5611(15, 13, 12, 14); // ESP32 SW SPI
// MS5611_SPI MS5611(15); // ESP32 HW SPI (HSPI)
MS5611_SPI MS5611(5); // ESP32 HW SPI (VSPI)
MS5611_SPI MS5611(15, 13, 12, 14); // ESP32 SW SPI
//
// SPIClass *mySPI = new SPIClass(HSPI);
// MS5611_SPI MS5611(15, mySPI); // ESP32 HW SPI (HSPI)
// SPIClass *mySPI = new SPIClass(VSPI);
// MS5611_SPI MS5611(5, mySPI); // ESP32 HW SPI (VSPI)
void setup()
{
Serial.begin(115200);
while(!Serial);
Serial.println();
Serial.println(__FILE__);
Serial.print("MS5611_SPI_LIB_VERSION: ");
Serial.println(MS5611_SPI_LIB_VERSION);
// ESP32 need this
// MS5611.selectVSPI();
if (MS5611.begin() == true)
{
Serial.print("MS5611 found: ");
@ -90,4 +87,4 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -35,12 +35,18 @@
// MS5611_SPI(select, dataOut, dataIn, clock);
// --------------------------------------------
// MS5611_SPI MS5611(10, 11, 12, 13); // UNO SW SPI (5V problem?
// MS5611_SPI MS5611(10); // UNO HW SPI
MS5611_SPI MS5611( 5, 23, 19, 18); // ESP32 SW SPI
// MS5611_SPI MS5611(5); // ESP32 HW SPI
//
// MS5611_SPI MS5611( 5, 23, 19, 18); // ESP32 SW SPI
MS5611_SPI MS5611(15, 13, 12, 14); // ESP32 SW SPI
//
// SPIClass *mySPI = new SPIClass(HSPI);
// MS5611_SPI MS5611(15, mySPI); // ESP32 HW SPI (HSPI)
//
// SPIClass *mySPI = new SPIClass(VSPI);
// MS5611_SPI MS5611(5, mySPI); // ESP32 HW SPI (VSPI)
uint32_t start, stop, count;
@ -49,7 +55,6 @@ void setup()
{
Serial.begin(115200);
while(!Serial);
Serial.println();
Serial.println(__FILE__);
Serial.print("MS5611_SPI_LIB_VERSION: ");

View File

@ -35,12 +35,18 @@
// MS5611_SPI(select, dataOut, dataIn, clock);
// --------------------------------------------
// MS5611_SPI MS5611(10, 11, 12, 13); // UNO SW SPI (5V problem?
// MS5611_SPI MS5611(10); // UNO HW SPI
MS5611_SPI MS5611( 5, 23, 19, 18); // ESP32 SW SPI
// MS5611_SPI MS5611(5); // ESP32 HW SPI
//
// MS5611_SPI MS5611( 5, 23, 19, 18); // ESP32 SW SPI
MS5611_SPI MS5611(15, 13, 12, 14); // ESP32 SW SPI
//
// SPIClass *mySPI = new SPIClass(HSPI);
// MS5611_SPI MS5611(15, mySPI); // ESP32 HW SPI (HSPI)
//
// SPIClass *mySPI = new SPIClass(VSPI);
// MS5611_SPI MS5611(5, mySPI); // ESP32 HW SPI (VSPI)
uint32_t start, stop;
@ -49,7 +55,6 @@ void setup()
{
Serial.begin(115200);
while(!Serial);
Serial.println();
Serial.println(__FILE__);
Serial.print("MS5611_SPI_LIB_VERSION: ");
@ -126,5 +131,5 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -35,12 +35,17 @@
// MS5611_SPI(select, dataOut, dataIn, clock);
// --------------------------------------------
// MS5611_SPI MS5611(10, 11, 12, 13); // UNO SW SPI (5V problem?
// MS5611_SPI MS5611(10); // UNO HW SPI
MS5611_SPI MS5611( 5, 23, 19, 18); // ESP32 SW SPI
// MS5611_SPI MS5611(5); // ESP32 HW SPI
//
// MS5611_SPI MS5611( 5, 23, 19, 18); // ESP32 SW SPI
MS5611_SPI MS5611(15, 13, 12, 14); // ESP32 SW SPI
//
// SPIClass *mySPI = new SPIClass(HSPI);
// MS5611_SPI MS5611(15, mySPI); // ESP32 HW SPI (HSPI)
//
// SPIClass *mySPI = new SPIClass(VSPI);
// MS5611_SPI MS5611(5, mySPI); // ESP32 HW SPI (VSPI)
uint32_t start, stop;
@ -49,15 +54,14 @@ uint32_t start, stop;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT);
while(!Serial);
Serial.println();
Serial.println(__FILE__);
Serial.print("MS5611_SPI_LIB_VERSION: ");
Serial.println(MS5611_SPI_LIB_VERSION);
pinMode(LED_BUILTIN, OUTPUT);
if (MS5611.begin() == true)
{
Serial.println("MS5611 found.");
@ -144,5 +148,5 @@ void test()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -35,12 +35,17 @@
// MS5611_SPI(select, dataOut, dataIn, clock);
// --------------------------------------------
// MS5611_SPI MS5611(10, 11, 12, 13); // UNO SW SPI (5V problem?
// MS5611_SPI MS5611(10); // UNO HW SPI
MS5611_SPI MS5611( 5, 23, 19, 18); // ESP32 SW SPI
// MS5611_SPI MS5611(5); // ESP32 HW SPI
//
// MS5611_SPI MS5611( 5, 23, 19, 18); // ESP32 SW SPI
MS5611_SPI MS5611(15, 13, 12, 14); // ESP32 SW SPI
//
// SPIClass *mySPI = new SPIClass(HSPI);
// MS5611_SPI MS5611(15, mySPI); // ESP32 HW SPI (HSPI)
//
// SPIClass *mySPI = new SPIClass(VSPI);
// MS5611_SPI MS5611(5, mySPI); // ESP32 HW SPI (VSPI)
uint32_t start, stop;
@ -49,15 +54,14 @@ uint32_t start, stop;
void setup()
{
Serial.begin(115200);
while (!Serial);
pinMode(LED_BUILTIN, OUTPUT);
while(!Serial);
Serial.println();
Serial.println(__FILE__);
Serial.print("MS5611_SPI_LIB_VERSION: ");
Serial.println(MS5611_SPI_LIB_VERSION);
pinMode(LED_BUILTIN, OUTPUT);
if (MS5611.begin() == true)
{
Serial.println("MS5611 found.");
@ -147,4 +151,4 @@ void test()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -35,18 +35,27 @@
// MS5611_SPI(select, dataOut, dataIn, clock);
// --------------------------------------------
// MS5611_SPI MS5611(10, 11, 12, 13); // UNO SW SPI (5V problem?
// MS5611_SPI MS5611(10); // UNO HW SPI
MS5611_SPI MS5611( 5, 23, 19, 18); // ESP32 SW SPI
// MS5611_SPI MS5611(5); // ESP32 HW SPI
//
// MS5611_SPI MS5611( 5, 23, 19, 18); // ESP32 SW SPI
MS5611_SPI MS5611(15, 13, 12, 14); // ESP32 SW SPI
//
// SPIClass *mySPI = new SPIClass(HSPI);
// MS5611_SPI MS5611(15, mySPI); // ESP32 HW SPI (HSPI)
//
// SPIClass *mySPI = new SPIClass(VSPI);
// MS5611_SPI MS5611(5, mySPI); // ESP32 HW SPI (VSPI)
void setup()
{
Serial.begin(115200);
while (!Serial);
while(!Serial);
Serial.println();
Serial.println(__FILE__);
Serial.print("MS5611_SPI_LIB_VERSION: ");
Serial.println(MS5611_SPI_LIB_VERSION);
pinMode(LED_BUILTIN, OUTPUT);
@ -84,4 +93,4 @@ void loop()
}
// -- END OF FILE --
// -- END OF FILE --

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/MS5611_SPI.git"
},
"version": "0.1.3",
"version": "0.2.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=MS5611_SPI
version=0.1.3
version=0.2.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library (SPI) for MS5611 temperature and pressure sensor