Skip to content

Commit

Permalink
Max38155 driver
Browse files Browse the repository at this point in the history
Add module file
  • Loading branch information
sarahvilete authored and rasmuskleist committed Apr 21, 2022
1 parent 599e0ba commit 9a58045
Show file tree
Hide file tree
Showing 3 changed files with 191 additions and 0 deletions.
104 changes: 104 additions & 0 deletions src/modm/driver/temperature/max31855.hpp
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
Fault getFault()
{
return static_cast<Fault>(data[3] & 0b111);
}

/// @return the thermocouple temperature scaled according to the device documentation
float
getThermocoupleTemperature()
{
// 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
float
getReferenceJunctionTemperature()
{
// 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 data from the sensor into
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
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")
55 changes: 55 additions & 0 deletions src/modm/driver/temperature/max31855_impl.hpp
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

0 comments on commit 9a58045

Please sign in to comment.