Skip to content

Commit 98367b0

Browse files
committed
[driver] Add TMP121/TMP123/TMP125 temperature sensor driver
1 parent d57425a commit 98367b0

File tree

3 files changed

+207
-1
lines changed

3 files changed

+207
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,11 +667,12 @@ you specific needs.
667667
</tr><tr>
668668
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tlc594x">TLC594X</a></td>
669669
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp102">TMP102</a></td>
670+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp12x">TMP12X</a></td>
670671
<td align="center"><a href="https://modm.io/reference/module/modm-driver-tmp175">TMP175</a></td>
671672
<td align="center"><a href="https://modm.io/reference/module/modm-driver-touch2046">TOUCH2046</a></td>
672673
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl53l0">VL53L0</a></td>
673-
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl6180">VL6180</a></td>
674674
</tr><tr>
675+
<td align="center"><a href="https://modm.io/reference/module/modm-driver-vl6180">VL6180</a></td>
675676
<td align="center"><a href="https://modm.io/reference/module/modm-driver-ws2812">WS2812</a></td>
676677
</tr>
677678
</table>
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
* Copyright (c) 2020, Christopher Durand
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#ifndef MODM_TMP12x_HPP
13+
#define MODM_TMP12x_HPP
14+
15+
#include <modm/architecture/interface/spi_device.hpp>
16+
#include <modm/processing/resumable/nested_resumable.hpp>
17+
#include <array>
18+
#include <ratio>
19+
20+
namespace modm
21+
{
22+
23+
/**
24+
* TMP12x temperature data
25+
* Use aliases Tmp121Temperature, Tmp123Temperature, Tmp125Temperature
26+
* for the respective sensor.
27+
*
28+
* @ingroup modm_driver_tmp12x
29+
* @author Christopher Durand
30+
*
31+
* @tparam numBits Number of bits of measured temperature
32+
* @tparam offsetBits Invalid LSB of raw sensor value
33+
* @tparam numerator
34+
* @tparam denominator
35+
*/
36+
template<
37+
uint8_t NumBits,
38+
uint8_t OffsetBits,
39+
uint8_t Numerator,
40+
uint8_t Denominator
41+
>
42+
class Tmp12xTemperature
43+
{
44+
public:
45+
using Fraction = std::ratio<Numerator, Denominator>;
46+
47+
constexpr Tmp12xTemperature() = default;
48+
49+
constexpr explicit Tmp12xTemperature(uint16_t sensorData)
50+
{
51+
static_assert((NumBits + OffsetBits) <= 16);
52+
53+
// mask out invalid bits
54+
constexpr uint16_t mask = ((1U << NumBits) - 1u) << OffsetBits;
55+
const uint16_t rawTemp = sensorData & mask;
56+
// convert to int16_t 2's complement representation
57+
temperature = int16_t(rawTemp << (16 - OffsetBits - NumBits)) >> (16 - NumBits);
58+
}
59+
60+
constexpr float
61+
getTemperatureFloat() const
62+
{
63+
return temperature * (Fraction::num / float(Fraction::den));
64+
}
65+
66+
constexpr int16_t
67+
getTemperatureInteger() const
68+
{
69+
return temperature * Fraction::num / Fraction::den;
70+
}
71+
72+
constexpr int16_t
73+
getTemperatureFractional() const
74+
{
75+
return temperature;
76+
}
77+
78+
private:
79+
int16_t temperature = 0;
80+
};
81+
82+
/// @ingroup modm_driver_tmp12x
83+
using Tmp121Temperature = Tmp12xTemperature<13, 3, 1, 16>; // 12+1 bit, 1/16th °C resolution
84+
85+
/// @ingroup modm_driver_tmp12x
86+
using Tmp123Temperature = Tmp121Temperature;
87+
88+
/// @ingroup modm_driver_tmp12x
89+
using Tmp125Temperature = Tmp12xTemperature<10, 5, 1, 4>; // 10 bit, 1/4 °C resolution
90+
91+
/**
92+
* TMP12x temperature sensor driver
93+
* Use aliases Tmp121, Tmp123, Tmp125 for convenience.
94+
*
95+
* @ingroup modm_driver_tmp12x
96+
* @author Christopher Durand
97+
*
98+
* @tparam SpiMaster SPI master interface
99+
* @tparam Cs Chip select output
100+
* @tparam TemperatureT Sensor temperature type
101+
*/
102+
template<typename SpiMaster, typename Cs, typename TemperatureT>
103+
class Tmp12x : public modm::SpiDevice<SpiMaster>, protected modm::NestedResumable<1>
104+
{
105+
public:
106+
using Temperature = TemperatureT;
107+
108+
Tmp12x()
109+
{
110+
this->attachConfigurationHandler([]() {
111+
SpiMaster::setDataMode(SpiMaster::DataMode::Mode0);
112+
SpiMaster::setDataOrder(SpiMaster::DataOrder::MsbFirst);
113+
SpiMaster::setDataSize(SpiMaster::DataSize::Bit8);
114+
});
115+
}
116+
117+
void
118+
initialize()
119+
{
120+
Cs::setOutput(true);
121+
}
122+
123+
modm::ResumableResult<Temperature>
124+
read()
125+
{
126+
RF_BEGIN();
127+
128+
RF_WAIT_UNTIL(this->acquireMaster());
129+
Cs::reset();
130+
RF_CALL(SpiMaster::transfer(nullptr, buffer_.data(), 2));
131+
132+
if (this->releaseMaster()) {
133+
Cs::set();
134+
}
135+
136+
RF_END_RETURN(Temperature(buffer_[1] | (buffer_[0] << 8)));
137+
}
138+
private:
139+
std::array<uint8_t, 2> buffer_{};
140+
};
141+
142+
/**
143+
* TMP121 temperature sensor driver
144+
*
145+
* @tparam SpiMaster SPI master interface
146+
* @tparam Cs Chip select output
147+
*/
148+
template<typename SpiMaster, typename Cs>
149+
using Tmp121 = Tmp12x<SpiMaster, Cs, Tmp121Temperature>;
150+
151+
/**
152+
* TMP123 temperature sensor driver
153+
*
154+
* @tparam SpiMaster SPI master interface
155+
* @tparam Cs Chip select output
156+
*/
157+
template<typename SpiMaster, typename Cs>
158+
using Tmp123 = Tmp12x<SpiMaster, Cs, Tmp123Temperature>;
159+
160+
/**
161+
* TMP125 temperature sensor driver
162+
*
163+
* @tparam SpiMaster SPI master interface
164+
* @tparam Cs Chip select output
165+
*/
166+
template<typename SpiMaster, typename Cs>
167+
using Tmp125 = Tmp12x<SpiMaster, Cs, Tmp125Temperature>;
168+
169+
} // namespace modm
170+
171+
#endif // MODM_TMP12x_HPP

src/modm/driver/temperature/tmp12x.lb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
#
4+
# Copyright (c) 2020, Christopher Durand
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:tmp12x"
16+
module.description = """\
17+
# TMP121/123/125 Thermometer
18+
19+
TMP121/123/125 temperature sensor driver
20+
21+
This driver supports the TMP125 device but not its optional shutdown function.
22+
"""
23+
24+
def prepare(module, options):
25+
module.depends(
26+
":architecture:gpio",
27+
":architecture:register",
28+
":architecture:spi.device",
29+
":processing:resumable")
30+
return True
31+
32+
def build(env):
33+
env.outbasepath = "modm/src/modm/driver/temperature"
34+
env.copy("tmp12x.hpp")

0 commit comments

Comments
 (0)