-
Notifications
You must be signed in to change notification settings - Fork 154
[driver] Add MAX31865 driver with STM32F469 Discovery example #993
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
/// @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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.