Skip to content

Commit 2e34b11

Browse files
sarahvileterasmuskleist
authored andcommitted
Max31855 driver with example for the Nucleo-G474RE board
1 parent 599e0ba commit 2e34b11

File tree

6 files changed

+311
-6
lines changed

6 files changed

+311
-6
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -683,46 +683,47 @@ you specific needs.
683683
</tr><tr>
684684
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm6ds33">LSM6DS33</a></td>
685685
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ltc2984">LTC2984</a></td>
686+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max31855">MAX31855</a></td>
686687
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max6966">MAX6966</a></td>
687688
<td align="center"><a href="https://modm.io/reference/module/modm-driver-max7219">MAX7219</a></td>
688689
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp23x17">MCP23x17</a></td>
689-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp2515">MCP2515</a></td>
690690
</tr><tr>
691+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp2515">MCP2515</a></td>
691692
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mcp7941x">MCP7941x</a></td>
692693
<td align="center"><a href="https://modm.io/reference/module/modm-driver-mmc5603">MMC5603</a></td>
693694
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nokia5110">NOKIA5110</a></td>
694695
<td align="center"><a href="https://modm.io/reference/module/modm-driver-nrf24">NRF24</a></td>
695696
<td align="center"><a href="https://modm.io/reference/module/modm-driver-parallel_tft_display">TFT-DISPLAY</a></td>
696-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pat9125el">PAT9125EL</a></td>
697697
</tr><tr>
698+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pat9125el">PAT9125EL</a></td>
698699
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca8574">PCA8574</a></td>
699700
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9535">PCA9535</a></td>
700701
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9548a">PCA9548A</a></td>
701702
<td align="center"><a href="https://modm.io/reference/module/modm-driver-pca9685">PCA9685</a></td>
702703
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sh1106">SH1106</a></td>
703-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s65">SIEMENS-S65</a></td>
704704
</tr><tr>
705+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s65">SIEMENS-S65</a></td>
705706
<td align="center"><a href="https://modm.io/reference/module/modm-driver-siemens_s75">SIEMENS-S75</a></td>
706707
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk6812">SK6812</a></td>
707708
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sk9822">SK9822</a></td>
708709
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ssd1306">SSD1306</a></td>
709710
<td align="center"><a href="https://modm.io/reference/module/modm-driver-st7586s">ST7586S</a></td>
710-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stts22h">STTS22H</a></td>
711711
</tr><tr>
712+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stts22h">STTS22H</a></td>
712713
<td align="center"><a href="https://modm.io/reference/module/modm-driver-stusb4500">STUSB4500</a></td>
713714
<td align="center"><a href="https://modm.io/reference/module/modm-driver-sx1276">SX1276</a></td>
714715
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3414">TCS3414</a></td>
715716
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tcs3472">TCS3472</a></td>
716717
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tlc594x">TLC594x</a></td>
717-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp102">TMP102</a></td>
718718
</tr><tr>
719+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp102">TMP102</a></td>
719720
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp12x">TMP12x</a></td>
720721
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
721722
<td align="center"><a href="https://modm.io/reference/module/modm-driver-touch2046">TOUCH2046</a></td>
722723
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl53l0">VL53L0</a></td>
723724
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl6180">VL6180</a></td>
724-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ws2812">WS2812</a></td>
725725
</tr><tr>
726+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ws2812">WS2812</a></td>
726727
</tr>
727728
</table>
728729
<!--/drivertable-->
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// coding: utf-8
2+
/*
3+
* Copyright (c) 2022, Sarah Vilete
4+
* Copyright (c) 2022, Rasmus Kleist Hørlyck Sørensen
5+
*
6+
* This file is part of the modm project.
7+
*
8+
* This Source Code Form is subject to the terms of the Mozilla Public
9+
* License, v. 2.0. If a copy of the MPL was not distributed with this
10+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
*/
12+
13+
#include <modm/board.hpp>
14+
#include <modm/processing.hpp>
15+
#include <modm/driver/temperature/max31855.hpp>
16+
17+
using SpiMaster = modm::platform::SpiMaster2;
18+
19+
using Cs = modm::platform::GpioA10;
20+
using Miso = modm::platform::GpioB14;
21+
using Sck = modm::platform::GpioB13;
22+
23+
using namespace Board;
24+
25+
class ThermocoupleThread : public modm::pt::Protothread
26+
{
27+
public:
28+
ThermocoupleThread() : thermocouple(data) {}
29+
30+
bool
31+
run()
32+
{
33+
PT_BEGIN();
34+
35+
thermocouple.initialize();
36+
37+
MODM_LOG_INFO << "Max38155 initialized succeded" << modm::endl;
38+
39+
while (true)
40+
{
41+
PT_CALL(thermocouple.readout());
42+
43+
switch (data.getFault())
44+
{
45+
case modm::max31855::Fault::ShortCircuitVcc:
46+
MODM_LOG_ERROR << "Thermocouple error short circuit vcc" << modm::endl;
47+
break;
48+
49+
case modm::max31855::Fault::ShortCircuitGnd:
50+
MODM_LOG_ERROR << "Thermocouple error short circuit gnd" << modm::endl;
51+
break;
52+
53+
case modm::max31855::Fault::OpenCircuit:
54+
MODM_LOG_ERROR << "Thermocouple error open circuit" << modm::endl;
55+
break;
56+
57+
default:
58+
MODM_LOG_INFO << "Thermocouple Temperature: " << data.getThermocoupleTemperature() << " degrees Centigrade" << modm::endl;
59+
MODM_LOG_INFO << "Internal Temperature: " << data.getReferenceJunctionTemperature() << " degrees Centigrade" << modm::endl;
60+
break;
61+
}
62+
63+
timeout.restart(std::chrono::milliseconds(1000));
64+
PT_WAIT_UNTIL(timeout.isExpired());
65+
}
66+
67+
PT_END();
68+
}
69+
70+
private:
71+
modm::max31855::Data data;
72+
modm::Max31855<SpiMaster, Cs> thermocouple;
73+
74+
modm::ShortTimeout timeout;
75+
} thermocoupleThread;
76+
77+
int
78+
main()
79+
{
80+
Board::initialize();
81+
Cs::setOutput(modm::Gpio::High);
82+
83+
SpiMaster::connect<Miso::Miso, Sck::Sck>();
84+
SpiMaster::initialize<Board::SystemClock, 656250>();
85+
86+
MODM_LOG_INFO << "==========MAX 31855 Test==========" << modm::endl;
87+
MODM_LOG_DEBUG << "Debug logging here" << modm::endl;
88+
MODM_LOG_INFO << "Info logging here" << modm::endl;
89+
MODM_LOG_WARNING << "Warning logging here" << modm::endl;
90+
MODM_LOG_ERROR << "Error logging here" << modm::endl;
91+
MODM_LOG_INFO << "===============================" << modm::endl;
92+
93+
while (true)
94+
{
95+
thermocoupleThread.run();
96+
}
97+
98+
return 0;
99+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<library>
2+
<extends>modm:nucleo-g474re</extends>
3+
<options>
4+
<option name="modm:build:build.path">../../../build/nucleo_g474re/max31855</option>
5+
</options>
6+
<modules>
7+
<module>modm:driver:max31855</module>
8+
<module>modm:platform:gpio</module>
9+
<module>modm:platform:spi:2</module>
10+
<module>modm:processing:protothread</module>
11+
<module>modm:processing:timer</module>
12+
<module>modm:build:scons</module>
13+
</modules>
14+
</library>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// coding: utf-8
2+
// ----------------------------------------------------------------------------
3+
/*
4+
* Copyright (c) 2022, Sarah Vilete
5+
*
6+
* This file is part of the modm project.
7+
*
8+
* This Source Code Form is subject to the terms of the Mozilla Public
9+
* License, v. 2.0. If a copy of the MPL was not distributed with this
10+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
*/
12+
// ----------------------------------------------------------------------------
13+
14+
#ifndef MODM_MAX31855_HPP
15+
#define MODM_MAX31855_HPP
16+
17+
#include <modm/processing/resumable.hpp>
18+
#include <modm/architecture/interface/spi_device.hpp>
19+
20+
namespace modm
21+
{
22+
23+
/// @ingroup modm_driver_max31855
24+
struct max31855
25+
{
26+
/// Thermocouple fault bits
27+
enum class
28+
Fault : uint8_t
29+
{
30+
ShortCircuitVcc = Bit2,
31+
ShortCircuitGnd = Bit1,
32+
OpenCircuit = Bit0
33+
};
34+
35+
struct modm_packed
36+
Data
37+
{
38+
/// @return value associated with the respective fault
39+
constexpr Fault getFault() const
40+
{
41+
return static_cast<Fault>(data[3] & 0b111);
42+
}
43+
44+
/// @return the thermocouple temperature scaled according to the device documentation
45+
constexpr float
46+
getThermocoupleTemperature() const
47+
{
48+
// convert raw 14 bit readout in 2's complement to a 16 bit signed
49+
const int16_t rawTemp = static_cast<int16_t>((data[0] << 8) | (data[1] & 0b11111100)) / 4;
50+
return 0.25f * rawTemp;
51+
}
52+
53+
/// @return the reference junction temperature scaled according to the device documentation
54+
constexpr float
55+
getReferenceJunctionTemperature() const
56+
{
57+
// convert raw 12 bit readout in 2's complement to a 16 bit signed
58+
const int16_t rawTemp = static_cast<int16_t>((data[2] << 8) | (data[3] & 0b11110000)) / 16;
59+
return 0.0625f * rawTemp;
60+
}
61+
62+
uint8_t data[4];
63+
};
64+
}; // struct max31855
65+
66+
/**
67+
* @tparam SpiMaster
68+
* @tparam Cs
69+
*
70+
* @author Sarah Vilete
71+
* @ingroup modm_driver_max31855
72+
*/
73+
template <typename SpiMaster, typename Cs>
74+
class Max31855 : public max31855, public modm::SpiDevice<SpiMaster>, protected modm::NestedResumable<1>
75+
{
76+
public:
77+
/**
78+
* @param data pointer to buffer of the internal data of type Data
79+
*/
80+
Max31855(Data &data);
81+
82+
/// Call this function once before using the device
83+
void
84+
initialize();
85+
86+
/// Read the raw data from the sensor
87+
modm::ResumableResult<void>
88+
readout();
89+
90+
public:
91+
/// Get the data object for this sensor
92+
inline Data&
93+
getData()
94+
{ return data; }
95+
96+
private:
97+
Data &data;
98+
};
99+
100+
} // namespace modm
101+
102+
#include "max31855_impl.hpp"
103+
104+
#endif // MODM_MAX31855_HPP
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2022, Rasmus Kleist Hørlyck Sørensen
5+
#
6+
# This file is part of the modm project.
7+
#
8+
# This Source Code Form is subject to the terms of the Mozilla Public
9+
# License, v. 2.0. If a copy of the MPL was not distributed with this
10+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
# -----------------------------------------------------------------------------
12+
13+
14+
def init(module):
15+
module.name = ":driver:max31855"
16+
module.description = """\
17+
# MAX31855 Thermocouple-to-Digital Converter
18+
19+
[Datasheet](https://datasheets.maximintegrated.com/en/ds/MAX31855.pdf)
20+
"""
21+
22+
def prepare(module, options):
23+
module.depends(
24+
":architecture:gpio",
25+
":architecture:spi.device",
26+
":processing:resumable")
27+
return True
28+
29+
def build(env):
30+
env.outbasepath = "modm/src/modm/driver/temperature"
31+
env.copy("max31855.hpp")
32+
env.copy("max31855_impl.hpp")
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// coding: utf-8
2+
// ----------------------------------------------------------------------------
3+
/*
4+
* Copyright (c) 2022, Sarah Vilete
5+
*
6+
* This file is part of the modm project.
7+
*
8+
* This Source Code Form is subject to the terms of the Mozilla Public
9+
* License, v. 2.0. If a copy of the MPL was not distributed with this
10+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
11+
*/
12+
// ----------------------------------------------------------------------------
13+
14+
#ifndef MODM_MAX31855_HPP
15+
# error "Don't include this file directly, use 'max31855.hpp' instead!"
16+
#endif
17+
18+
namespace modm
19+
{
20+
21+
template <typename SpiMaster, typename Cs>
22+
Max31855<SpiMaster, Cs>::Max31855(Data &data) : data(data)
23+
{
24+
}
25+
26+
// -----------------------------------------------------------------------------
27+
28+
template <typename SpiMaster, typename Cs>
29+
void
30+
Max31855<SpiMaster, Cs>::initialize()
31+
{
32+
Cs::setOutput(modm::Gpio::High);
33+
}
34+
35+
// -----------------------------------------------------------------------------
36+
37+
template <typename SpiMaster, typename Cs>
38+
modm::ResumableResult<void>
39+
Max31855<SpiMaster, Cs>::readout()
40+
{
41+
RF_BEGIN();
42+
RF_WAIT_UNTIL(this->acquireMaster());
43+
44+
Cs::reset();
45+
RF_CALL(SpiMaster::transfer(nullptr, data.data, 4));
46+
47+
if (this->releaseMaster())
48+
{
49+
Cs::set();
50+
}
51+
52+
RF_END();
53+
}
54+
55+
} // namespace modm

0 commit comments

Comments
 (0)