Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MS5837 high-resolution pressure and temperature sensor driver #942

Merged
merged 2 commits into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -725,39 +725,40 @@ you specific needs.
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mmc5603">MMC5603</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5611">MS5611</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ms5837">MS5837</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nokia5110">NOKIA5110</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nrf24">NRF24</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-parallel_tft_display">TFT-DISPLAY</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pat9125el">PAT9125EL</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pat9125el">PAT9125EL</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca8574">PCA8574</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9535">PCA9535</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9548a">PCA9548A</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9685">PCA9685</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sh1106">SH1106</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s65">SIEMENS-S65</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s65">SIEMENS-S65</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s75">SIEMENS-S75</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk6812">SK6812</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk9822">SK9822</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306">SSD1306</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7586s">ST7586S</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7789">ST7789</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7789">ST7789</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stts22h">STTS22H</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stusb4500">STUSB4500</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx1276">SX1276</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3414">TCS3414</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3472">TCS3472</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tlc594x">TLC594x</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tlc594x">TLC594x</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp102">TMP102</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp12x">TMP12x</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-touch2046">TOUCH2046</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl53l0">VL53L0</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl6180">VL6180</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl6180">VL6180</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ws2812">WS2812</a></td>
</tr>
</table>
Expand Down
73 changes: 73 additions & 0 deletions examples/nucleo_f042k6/ms5837/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2023, Vivien Henry
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <modm/board.hpp>
#include <modm/driver/pressure/ms5837.hpp>


using namespace Board;
using namespace std::chrono_literals;

using I2cSda = GpioA10;
using I2cScl = GpioA9;

int
main()
{
Board::initialize();
LedD13::setOutput();

MODM_LOG_INFO << "MS5837 demo" << modm::endl;

I2cMaster1::connect<I2cSda::Sda, I2cScl::Scl>();
I2cMaster1::initialize<SystemClock, 100_kBd>();

// Create a sensor object
modm::ms5837data::Data sensor_data;
modm::Ms5837<I2cMaster1> sensor(sensor_data);

int32_t press, temp;
float press_f, temp_f;

// Turn on and configure the pressure sensor
bool success = RF_CALL_BLOCKING(sensor.initialize());

if(!success)
{
MODM_LOG_DEBUG << "MS5837 Initialition failed" << modm::endl;
}

while (true)
{
//Read the sensor data and print it out
success = RF_CALL_BLOCKING(sensor.readout());

if(success)
{
press = sensor_data.getPressure(); // int32_t
//press_f = sensor_data.getPressure(); // float
sensor_data.getPressure(press_f); // float via reference

MODM_LOG_INFO << "Pressure: (tenth of mbar): " << press << " and in mbar (float)" << press_f << modm::endl;

temp = sensor_data.getTemperature();
//temp_f = sensor_data.getTemperature();
sensor_data.getTemperature(temp_f);

MODM_LOG_INFO << "Temp: (0.01°C): " << temp << " and °C" << temp_f << modm::endl;
}
else
{
MODM_LOG_INFO << "Sensor could not be read!" << modm::endl;
}
modm::delay(1s);
}
return 0;
}
11 changes: 11 additions & 0 deletions examples/nucleo_f042k6/ms5837/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<library>
<extends>modm:nucleo-f042k6</extends>
<options>
<option name="modm:build:build.path">../../../build/nucleo_f042k6/ms5837</option>
</options>
<modules>
<module>modm:build:scons</module>
<module>modm:driver:ms5837</module>
<module>modm:platform:i2c:1</module>
</modules>
</library>
133 changes: 133 additions & 0 deletions src/modm/driver/pressure/ms5837.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// coding: utf-8
// ----------------------------------------------------------------------------
/*
* Copyright (c) 2023, Vivien Henry
* Based on the ms5837, courtesy of Rasmus Kleist Hørlyck Sørensen
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#ifndef MODM_MS5837_HPP
#define MODM_MS5837_HPP

#include "ms5837_data.hpp"

#include <modm/processing/resumable.hpp>
#include <modm/architecture/interface/i2c_device.hpp>
#include <modm/architecture/interface/register.hpp>
#include <modm/processing/timer.hpp>


namespace modm
{

/// @ingroup modm_driver_ms5837
struct ms5837
{

using Prom = modm::ms5837data::Prom;
using Data = modm::ms5837data::Data;
using DataBase = modm::ms5837data::DataBase;

protected:
/// @cond
enum class
Command : uint8_t
{
Reset = 0x1E,
Convert = 0x40,
AdcRead = 0x00,
PromRead = 0xA0,
};

enum class
Conversion : uint8_t
{
Pressure = 0x00,
Temperature = 0x10,
};
/// @endcond

public:
enum class
OversamplingRatio : uint8_t
{
Osr256 = 0x00,
Osr512 = 0x02,
Osr1024 = 0x04,
Osr2048 = 0x06,
Osr4096 = 0x08,
};

protected:
/// @cond
static constexpr uint8_t
i(Command cmd) { return uint8_t(cmd); }
static constexpr uint8_t
i(Conversion conv) { return uint8_t(conv); }
static constexpr uint8_t
i(OversamplingRatio osr) { return uint8_t(osr); }
/// @endcond
};

/**
* @tparam I2cMaster I2C interface
*
* @author Vivien Henry
* @ingroup modm_driver_ms5837
*/
template < typename I2cMaster >
class Ms5837 : public ms5837, public modm::I2cDevice<I2cMaster, 2>
{
public:
/**
* @param data pointer to buffer of the internal data of type Data
*/
Ms5837(DataBase &data, uint8_t address=0x76);

/// Call this function before using the device to read the factory calibration
/// @warning calls to this function resets the device
modm::ResumableResult<bool>
initialize();

/// Do a readout sequence to convert and read temperature and then pressure from sensor
modm::ResumableResult<bool>
readout(OversamplingRatio osrPressure = OversamplingRatio::Osr256,
OversamplingRatio osrTemperature = OversamplingRatio::Osr256);

public:
/// Get the data object for this sensor
inline DataBase&
getData() { return data; }

private:
/// Read the PROM register at the address
modm::ResumableResult<uint16_t>
readProm(uint8_t addr);

private:
DataBase &data;
modm::ShortTimeout timeout;

/**
* Conversion time of the Analog Digital Convert for different oversampling ratios
* The conversion times are taken from the application note AN520
*/
static constexpr uint8_t conversionDelay[] = {1, 3, 4, 6, 10};

/// Command buffer for writing to and reading from the device
uint8_t buffer[2];

uint8_t factory_crc;
};

} // modm namespace

#include "ms5837_impl.hpp"

#endif // MODM_MS5837_HPP
36 changes: 36 additions & 0 deletions src/modm/driver/pressure/ms5837.lb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2023, Vivien Henry,
# Based on the ms5611, courtesy of Rasmus Kleist Hørlyck Sørensen
#
# This file is part of the modm project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# -----------------------------------------------------------------------------

def init(module):
module.name = ":driver:ms5837"
module.description = """\
# MS5837 Pressure Sensor

This sensor is optimized for water depth measurement systems with a resolution of 0.2cm.
"""

def prepare(module, options):
module.depends(
":architecture:i2c.device",
":architecture:register",
":math:utils",
":processing:resumable",
":processing:timer")
return True

def build(env):
env.outbasepath = "modm/src/modm/driver/pressure"
env.copy("ms5837.hpp")
env.copy("ms5837_impl.hpp")
env.copy("ms5837_data.hpp")
env.copy("ms5837_data_impl.hpp")
Loading