Skip to content

Commit

Permalink
Max31855 driver with example for the Nucleo-G474RE board
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahvilete authored and rasmuskleist committed Apr 21, 2022
1 parent 599e0ba commit 2e34b11
Show file tree
Hide file tree
Showing 6 changed files with 311 additions and 6 deletions.
13 changes: 7 additions & 6 deletions README.md
Expand Up @@ -683,46 +683,47 @@ you specific needs.
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-lsm6ds33">LSM6DS33</a></td>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ltc2984">LTC2984</a></td>
<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-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>
</tr><tr>
<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>
<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-nokia5110">NOKIA5110</a></td>
<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>
</tr><tr>
<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>
<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>
</tr><tr>
<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>
<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-stts22h">STTS22H</a></td>
</tr><tr>
<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>
<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>
</tr><tr>
<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>
<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>
</tr><tr>
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ws2812">WS2812</a></td>
</tr>
</table>
<!--/drivertable-->
Expand Down
99 changes: 99 additions & 0 deletions examples/nucleo_g474re/max31855/main.cpp
@@ -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;
}
14 changes: 14 additions & 0 deletions examples/nucleo_g474re/max31855/project.xml
@@ -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>
104 changes: 104 additions & 0 deletions src/modm/driver/temperature/max31855.hpp
@@ -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
32 changes: 32 additions & 0 deletions src/modm/driver/temperature/max31855.lb
@@ -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")
55 changes: 55 additions & 0 deletions src/modm/driver/temperature/max31855_impl.hpp
@@ -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

0 comments on commit 2e34b11

Please sign in to comment.