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

[driver] Add MAX31865 driver with STM32F469 Discovery example #993

Merged
merged 2 commits into from
May 2, 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
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -738,47 +738,48 @@ you specific needs.
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ltc2984">LTC2984</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max31855">MAX31855</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max31865">MAX31865</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max6966">MAX6966</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max7219">MAX7219</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp23x17">MCP23x17</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp2515">MCP2515</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp7941x">MCP7941x</a></td>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp7941x">MCP7941x</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp990x">MCP990X</a></td>
<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>
</tr><tr>
<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>
<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>
</tr><tr>
<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>
<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>
</tr><tr>
<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>
<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>
</tr><tr>
<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>
<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>
</tr><tr>
<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>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ws2812">WS2812</a></td>
Expand Down
88 changes: 88 additions & 0 deletions examples/stm32f469_discovery/max31865/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// coding: utf-8
/*
* Copyright (c) 2023, Henrik Hose
*
* 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/temperature/max31865.hpp>
#include <modm/processing.hpp>

using namespace Board;

using SpiMaster = modm::platform::SpiMaster2;

using Cs = D9;
using Mosi = D11;
using Miso = D12;
using Sck = D13;

class ThermocoupleThread : public modm::pt::Protothread
{
public:
ThermocoupleThread() : data{}, pt100(data) {}

bool
run()
{
PT_BEGIN();
PT_CALL(pt100.initialize());

while (true)
{
MODM_LOG_INFO << "\nNew readout:" << modm::endl;
PT_CALL(pt100.readout());

MODM_LOG_INFO << " resistance : " << data.getResistance() << " Ohm"
<< modm::endl;
MODM_LOG_INFO << " temperature fast: " << data.getTemperatureFast() << " degrees"
<< modm::endl;
MODM_LOG_INFO << " temperature precise: " << data.getTemperaturePrecise() << " degrees"
<< modm::endl;

timeout.restart(std::chrono::milliseconds(1000));
PT_WAIT_UNTIL(timeout.isExpired());
}

PT_END();
}

private:
using Max31865 = modm::Max31865<SpiMaster, Cs, modm::max31865::pt100>;
Max31865::Data data;
Max31865 pt100;

modm::ShortTimeout timeout;
};

ThermocoupleThread pt100Thread{};

int
main()
{
Board::initialize();
Cs::setOutput(modm::Gpio::High);

SpiMaster::connect<Miso::Miso, Mosi::Mosi, Sck::Sck>();
SpiMaster::initialize<Board::SystemClock, 351_kHz>();

MODM_LOG_INFO << "==========MAX 31865 Test==========" << modm::endl;
MODM_LOG_DEBUG << "Debug logging here" << modm::endl;
MODM_LOG_INFO << "Info logging here" << modm::endl;
MODM_LOG_WARNING << "Warning logging here" << modm::endl;
MODM_LOG_ERROR << "Error logging here" << modm::endl;
MODM_LOG_INFO << "===============================" << modm::endl;

while (true)
{
pt100Thread.run();
Board::LedOrange::toggle();
}

return 0;
}
14 changes: 14 additions & 0 deletions examples/stm32f469_discovery/max31865/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<library>
<extends>modm:disco-f469ni</extends>
<options>
<option name="modm:build:build.path">../../../build/stm32f469_discovery/max31865</option>
</options>
<modules>
<module>modm:driver:max31865</module>
<module>modm:platform:gpio</module>
<module>modm:platform:spi:2</module>
<module>modm:processing:protothread</module>
<module>modm:processing:timer</module>
<module>modm:build:scons</module>
</modules>
</library>
230 changes: 230 additions & 0 deletions src/modm/driver/temperature/max31865.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// coding: utf-8
// ----------------------------------------------------------------------------
/*
* Copyright (c) 2023, Henrik Hose
*
* 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_MAX31865_HPP
#define MODM_MAX31865_HPP

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

namespace modm
{

/// @ingroup modm_driver_max31865
struct max31865
{
struct Pt
{
float R0;
float Rref;
float alpha;
double a;
double b;
double c;
};

/// Standard values for IEC 751 (PT100) with 430 Ohm reference
static constexpr Pt pt100{
.R0 = 100.f,
.Rref = 430.f,
.alpha = 3.85055e-3f,
.a = double{3.90830e-3},
.b = double{-5.77500e-7},
.c = double{-4.18301e-12},
};

enum class Fault : uint8_t
{
RtdHighThreshold = Bit7,
RtdLowThreshold = Bit6,
RefinMGreater85Percentbias = Bit5,
RefinMLess85PercentVbias = Bit4,
RtdinMLess85PercentVbias = Bit3,
OverUnderVoltage = Bit2
};
MODM_FLAGS8(Fault)

enum class Config : uint8_t
{
VBias = Bit7,
ConversionModeAuto = Bit6,
OneShot = Bit5,
ThreeWire = Bit4,
FaultDetectionCycle1 = Bit3,
FaultDetectionCycle0 = Bit2,
FaultStatusClear = Bit1,
Rejection = Bit0
};
MODM_FLAGS8(Config)

enum class FaultDetectionWrite : uint8_t
{
NoAction = 0b00,
FaultDetectionAutomaticDelay = 0b01,
RunFaultDetectionWithManualDelayCycle1 = 0b10,
FinishFaultDetectionWithManualDelayCycle2 = 0b11,
};
using FaultDetectionWrite_t =
Configuration<Config_t, FaultDetectionWrite, 0b11, 2>; // Bit 8..10

enum class FaultDetectionRead : uint8_t
{
FaultDetectionFinished = 0b00,
AutomaticDetectionStillRunning = 0b01,
ManualCycle1Running =
0b10, // waiting for user to write FinishFaultDetectionWithManualDelayCycle2
ManualCycle2StillRunning = 0b11,
};
using FaultDetectionRead_t = Configuration<Config_t, FaultDetectionRead, 0b11, 2>; // Bit 8..10

enum class Rejection : uint8_t
{
Rejection60Hz = 0b0,
Rejection50Hz = 0b1,
};
using Rejection_t = Configuration<Config_t, Rejection, 0b1>; // Bit 8..10

static constexpr uint16_t
rtdfaultthresh(const uint16_t res)
{
return res << 1;
};

enum class Register : uint8_t
{
ReadConfiguration = 0x00,
ReadRtdMsb = 0x01,
ReadRtdLsb = 0x02,
ReadHighFaultThresholdMsb = 0x03,
ReadHighFaultThresholdLsb = 0x04,
ReadLowFaultThresholdMsb = 0x05,
ReadLowFaultThresholdLsb = 0x06,
ReadFaultStatus = 0x07,

WriteConfiguration = 0x80,
WriteHighFaultThresholdMsb = 0x83,
WriteHighFaultThresholdLsb = 0x84,
WriteLowFaultThresholdMsb = 0x85,
WriteLowFaultThresholdLsb = 0x86,
};

template<Pt pt>
struct modm_packed Data
hshose marked this conversation as resolved.
Show resolved Hide resolved
{
/// @return measure resistance in ohm
constexpr float
getResistance() const
{
const uint16_t adccode = data >> 1;
return adccode / 32768.f * pt.Rref;
}

/// @return fast temperature in degrees celsius, about 0.3 degrees error between 0 and 100
/// degrees celsius
constexpr float
getTemperatureFast() const
{
return (getResistance() - pt.R0) / pt.alpha / 100.f;
}

/// @return slow but accurate temperature in degrees celsius
constexpr double
getTemperaturePrecise() const
{
const double res = getResistance();
double T = getTemperatureFast();
const double c0 = T <= 0 ? pt.c : 0;

// Do some fixed number of newton steps for root finding
// on Callendar Van Dusen equation:
// R = R0*(1+a*T+b*T*T+c*(T-100)*T*T*T)
// Newton seems to need double precision to achieve 1.e-10 residual?!
for (int i = 0; i < 10; i++)
{
const double R = double{pt.R0} * (1 + (pt.a * T) + (pt.b * T * T) +
(c0 * (T - 100) * T * T * T)) -
res;
const double Rdash =
double{pt.R0} * (pt.a + (2 * pt.b * T) + c0 * (((4 * T) - 300) * T * T));
T -= R / Rdash;
if (std::abs(R) <= 1.e-10) { break; }
}

return T;
}

uint16_t data;
};
}; // struct max31865

/**
* @tparam SpiMaster
* @tparam Cs
*
* @author Henrik Hose
* @ingroup modm_driver_max31865
*/
template<typename SpiMaster, typename Cs, max31865::Pt pt = max31865::pt100>
class Max31865 : public max31865,
public modm::SpiDevice<SpiMaster>,
protected modm::NestedResumable<3>
{
public:
using Data = max31865::Data<pt>;

/**
* @param data pointer to buffer of the internal data of type Data
*/
Max31865(Data &data);

/// Call this function once before using the device
modm::ResumableResult<void>
initialize();

/// Read the raw data from the sensor
modm::ResumableResult<void>
readout();

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

private:
Data &data;
std::array<uint8_t, 2> buffer;
Config_t config;
uint16_t d;

modm::ShortTimeout timeout;

modm::ResumableResult<void>
writeSingleRegister(Register address, uint8_t data);

modm::ResumableResult<uint8_t>
readSingleRegister(Register address);

modm::ResumableResult<uint16_t>
readTwoRegisters(Register address);
};

} // namespace modm

#include "max31865_impl.hpp"

#endif // MODM_MAX31865_HPP
Loading