diff --git a/examples/stm32f469_discovery/max31865/main.cpp b/examples/stm32f469_discovery/max31865/main.cpp index de47fc7130..af655aeeea 100644 --- a/examples/stm32f469_discovery/max31865/main.cpp +++ b/examples/stm32f469_discovery/max31865/main.cpp @@ -53,7 +53,7 @@ class ThermocoupleThread : public modm::pt::Protothread } private: - using Max31865 = modm::Max31865; + using Max31865 = modm::Max31865; Max31865::Data data; Max31865 pt100; diff --git a/src/modm/driver/temperature/max31865.hpp b/src/modm/driver/temperature/max31865.hpp index c7fa6e5dfb..7ab27dfecf 100644 --- a/src/modm/driver/temperature/max31865.hpp +++ b/src/modm/driver/temperature/max31865.hpp @@ -23,29 +23,28 @@ namespace modm { -struct Pt -{ - float R0; - float Rref; - float alpha; - double a; - double b; - double c; -}; - -/// Standard values for IEC 751 (PT100) with 430 Ohm reference -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}, -}; - /// @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 { @@ -126,56 +125,55 @@ struct max31865 WriteLowFaultThresholdMsb = 0x85, WriteLowFaultThresholdLsb = 0x86, }; -}; // struct max31865 -template -/// @ingroup modm_driver_max31865 -struct modm_packed max31865Data -{ - /// @return measure resistance in ohm - constexpr float - getResistance() const + template + struct modm_packed Data { - 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 measure resistance in ohm + constexpr float + getResistance() const + { + const uint16_t adccode = data >> 1; + return adccode / 32768.f * pt.Rref; + } - /// @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++) + /// @return fast temperature in degrees celsius, about 0.3 degrees error between 0 and 100 + /// degrees celsius + constexpr float + getTemperatureFast() const { - 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 (getResistance() - pt.R0) / pt.alpha / 100.f; } - return T; - } + /// @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; -}; + uint16_t data; + }; +}; // struct max31865 /** * @tparam SpiMaster @@ -184,13 +182,13 @@ struct modm_packed max31865Data * @author Henrik Hose * @ingroup modm_driver_max31865 */ -template +template class Max31865 : public max31865, public modm::SpiDevice, protected modm::NestedResumable<3> { public: - using Data = max31865Data; + using Data = max31865::Data; /** * @param data pointer to buffer of the internal data of type Data diff --git a/src/modm/driver/temperature/max31865_impl.hpp b/src/modm/driver/temperature/max31865_impl.hpp index 89648b392a..82fb2a4372 100644 --- a/src/modm/driver/temperature/max31865_impl.hpp +++ b/src/modm/driver/temperature/max31865_impl.hpp @@ -18,14 +18,14 @@ namespace modm { -template +template Max31865::Max31865(Data &data) : data(data) { this->attachConfigurationHandler([]() { SpiMaster::setDataMode(SpiMaster::DataMode::Mode3); }); Cs::setOutput(modm::Gpio::High); } -template +template modm::ResumableResult Max31865::initialize() { @@ -38,7 +38,7 @@ Max31865::initialize() RF_END(); } -template +template modm::ResumableResult Max31865::readout() { @@ -61,7 +61,7 @@ Max31865::readout() RF_END(); } -template +template modm::ResumableResult Max31865::readSingleRegister(Register address) { @@ -78,7 +78,7 @@ Max31865::readSingleRegister(Register address) RF_END_RETURN(buffer[0]); } -template +template modm::ResumableResult Max31865::readTwoRegisters(Register address) { @@ -95,7 +95,7 @@ Max31865::readTwoRegisters(Register address) RF_END_RETURN(static_cast(buffer[0] << 8 | buffer[1])); } -template +template modm::ResumableResult Max31865::writeSingleRegister(Register address, uint8_t data) {