-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Max31855 driver with example for the Nucleo-G474RE board
- Loading branch information
1 parent
599e0ba
commit 2e34b11
Showing
6 changed files
with
311 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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,99 @@ | ||
// coding: utf-8 | ||
/* | ||
* Copyright (c) 2022, Sarah Vilete | ||
* Copyright (c) 2022, 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/. | ||
*/ | ||
|
||
#include <modm/board.hpp> | ||
#include <modm/processing.hpp> | ||
#include <modm/driver/temperature/max31855.hpp> | ||
|
||
using SpiMaster = modm::platform::SpiMaster2; | ||
|
||
using Cs = modm::platform::GpioA10; | ||
using Miso = modm::platform::GpioB14; | ||
using Sck = modm::platform::GpioB13; | ||
|
||
using namespace Board; | ||
|
||
class ThermocoupleThread : public modm::pt::Protothread | ||
{ | ||
public: | ||
ThermocoupleThread() : thermocouple(data) {} | ||
|
||
bool | ||
run() | ||
{ | ||
PT_BEGIN(); | ||
|
||
thermocouple.initialize(); | ||
|
||
MODM_LOG_INFO << "Max38155 initialized succeded" << modm::endl; | ||
|
||
while (true) | ||
{ | ||
PT_CALL(thermocouple.readout()); | ||
|
||
switch (data.getFault()) | ||
{ | ||
case modm::max31855::Fault::ShortCircuitVcc: | ||
MODM_LOG_ERROR << "Thermocouple error short circuit vcc" << modm::endl; | ||
break; | ||
|
||
case modm::max31855::Fault::ShortCircuitGnd: | ||
MODM_LOG_ERROR << "Thermocouple error short circuit gnd" << modm::endl; | ||
break; | ||
|
||
case modm::max31855::Fault::OpenCircuit: | ||
MODM_LOG_ERROR << "Thermocouple error open circuit" << modm::endl; | ||
break; | ||
|
||
default: | ||
MODM_LOG_INFO << "Thermocouple Temperature: " << data.getThermocoupleTemperature() << " degrees Centigrade" << modm::endl; | ||
MODM_LOG_INFO << "Internal Temperature: " << data.getReferenceJunctionTemperature() << " degrees Centigrade" << modm::endl; | ||
break; | ||
} | ||
|
||
timeout.restart(std::chrono::milliseconds(1000)); | ||
PT_WAIT_UNTIL(timeout.isExpired()); | ||
} | ||
|
||
PT_END(); | ||
} | ||
|
||
private: | ||
modm::max31855::Data data; | ||
modm::Max31855<SpiMaster, Cs> thermocouple; | ||
|
||
modm::ShortTimeout timeout; | ||
} thermocoupleThread; | ||
|
||
int | ||
main() | ||
{ | ||
Board::initialize(); | ||
Cs::setOutput(modm::Gpio::High); | ||
|
||
SpiMaster::connect<Miso::Miso, Sck::Sck>(); | ||
SpiMaster::initialize<Board::SystemClock, 656250>(); | ||
|
||
MODM_LOG_INFO << "==========MAX 31855 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) | ||
{ | ||
thermocoupleThread.run(); | ||
} | ||
|
||
return 0; | ||
} |
This file contains 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:nucleo-g474re</extends> | ||
<options> | ||
<option name="modm:build:build.path">../../../build/nucleo_g474re/max31855</option> | ||
</options> | ||
<modules> | ||
<module>modm:driver:max31855</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 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,104 @@ | ||
// coding: utf-8 | ||
// ---------------------------------------------------------------------------- | ||
/* | ||
* Copyright (c) 2022, Sarah Vilete | ||
* | ||
* 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_MAX31855_HPP | ||
#define MODM_MAX31855_HPP | ||
|
||
#include <modm/processing/resumable.hpp> | ||
#include <modm/architecture/interface/spi_device.hpp> | ||
|
||
namespace modm | ||
{ | ||
|
||
/// @ingroup modm_driver_max31855 | ||
struct max31855 | ||
{ | ||
/// Thermocouple fault bits | ||
enum class | ||
Fault : uint8_t | ||
{ | ||
ShortCircuitVcc = Bit2, | ||
ShortCircuitGnd = Bit1, | ||
OpenCircuit = Bit0 | ||
}; | ||
|
||
struct modm_packed | ||
Data | ||
{ | ||
/// @return value associated with the respective fault | ||
constexpr Fault getFault() const | ||
{ | ||
return static_cast<Fault>(data[3] & 0b111); | ||
} | ||
|
||
/// @return the thermocouple temperature scaled according to the device documentation | ||
constexpr float | ||
getThermocoupleTemperature() const | ||
{ | ||
// convert raw 14 bit readout in 2's complement to a 16 bit signed | ||
const int16_t rawTemp = static_cast<int16_t>((data[0] << 8) | (data[1] & 0b11111100)) / 4; | ||
return 0.25f * rawTemp; | ||
} | ||
|
||
/// @return the reference junction temperature scaled according to the device documentation | ||
constexpr float | ||
getReferenceJunctionTemperature() const | ||
{ | ||
// convert raw 12 bit readout in 2's complement to a 16 bit signed | ||
const int16_t rawTemp = static_cast<int16_t>((data[2] << 8) | (data[3] & 0b11110000)) / 16; | ||
return 0.0625f * rawTemp; | ||
} | ||
|
||
uint8_t data[4]; | ||
}; | ||
}; // struct max31855 | ||
|
||
/** | ||
* @tparam SpiMaster | ||
* @tparam Cs | ||
* | ||
* @author Sarah Vilete | ||
* @ingroup modm_driver_max31855 | ||
*/ | ||
template <typename SpiMaster, typename Cs> | ||
class Max31855 : public max31855, public modm::SpiDevice<SpiMaster>, protected modm::NestedResumable<1> | ||
{ | ||
public: | ||
/** | ||
* @param data pointer to buffer of the internal data of type Data | ||
*/ | ||
Max31855(Data &data); | ||
|
||
/// Call this function once before using the device | ||
void | ||
initialize(); | ||
|
||
/// Read the raw data from the sensor | ||
modm::ResumableResult<void> | ||
readout(); | ||
|
||
public: | ||
/// Get the data object for this sensor | ||
inline Data& | ||
getData() | ||
{ return data; } | ||
|
||
private: | ||
Data &data; | ||
}; | ||
|
||
} // namespace modm | ||
|
||
#include "max31855_impl.hpp" | ||
|
||
#endif // MODM_MAX31855_HPP |
This file contains 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,32 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (c) 2022, 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:max31855" | ||
module.description = """\ | ||
# MAX31855 Thermocouple-to-Digital Converter | ||
[Datasheet](https://datasheets.maximintegrated.com/en/ds/MAX31855.pdf) | ||
""" | ||
|
||
def prepare(module, options): | ||
module.depends( | ||
":architecture:gpio", | ||
":architecture:spi.device", | ||
":processing:resumable") | ||
return True | ||
|
||
def build(env): | ||
env.outbasepath = "modm/src/modm/driver/temperature" | ||
env.copy("max31855.hpp") | ||
env.copy("max31855_impl.hpp") |
This file contains 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,55 @@ | ||
// coding: utf-8 | ||
// ---------------------------------------------------------------------------- | ||
/* | ||
* Copyright (c) 2022, Sarah Vilete | ||
* | ||
* 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_MAX31855_HPP | ||
# error "Don't include this file directly, use 'max31855.hpp' instead!" | ||
#endif | ||
|
||
namespace modm | ||
{ | ||
|
||
template <typename SpiMaster, typename Cs> | ||
Max31855<SpiMaster, Cs>::Max31855(Data &data) : data(data) | ||
{ | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
template <typename SpiMaster, typename Cs> | ||
void | ||
Max31855<SpiMaster, Cs>::initialize() | ||
{ | ||
Cs::setOutput(modm::Gpio::High); | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
template <typename SpiMaster, typename Cs> | ||
modm::ResumableResult<void> | ||
Max31855<SpiMaster, Cs>::readout() | ||
{ | ||
RF_BEGIN(); | ||
RF_WAIT_UNTIL(this->acquireMaster()); | ||
|
||
Cs::reset(); | ||
RF_CALL(SpiMaster::transfer(nullptr, data.data, 4)); | ||
|
||
if (this->releaseMaster()) | ||
{ | ||
Cs::set(); | ||
} | ||
|
||
RF_END(); | ||
} | ||
|
||
} // namespace modm |