|
| 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 |
0 commit comments