Skyward boardcore
Loading...
Searching...
No Matches
MAX31856.cpp
Go to the documentation of this file.
1/* Copyright (c) 2023 Skyward Experimental Rocketry
2 * Author: Alberto Nidasio
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23#include "MAX31856.h"
24
26
27namespace Boardcore
28{
29
30MAX31856::MAX31856(SPIBusInterface& bus, miosix::GpioPin cs,
31 SPIBusConfig config, ThermocoupleType type)
32 : slave(bus, cs, config), type(type)
33{
34}
35
37{
38 SPIBusConfig spiConfig{};
40 spiConfig.mode = SPI::Mode::MODE_1;
41 spiConfig.writeBit = SPI::WriteBit::INVERTED;
42 return spiConfig;
43}
44
46{
47 // Set thermocouple type
49
50 // Reset the cold junction offset
52
53 {
54 SPITransaction spi{slave};
55
56 // Enable continuous conversion mode
57 spi.writeRegister(CR0, CR0_CMODE);
58 }
59
60 return true;
61}
62
63bool MAX31856::selfTest() { return true; }
64
66{
67 SPITransaction spi{slave};
68
69 // Enable open-circuit detection
70 spi.writeRegister(CR0, CR0_CMODE | CR0_OCFAULT_0);
71
72 // Wait for detection
73 // Detection takes 40ms, waiting more to be extra sure
74 miosix::Thread::sleep(100);
75
76 // Read fault register
77 auto fault = spi.readRegister(SR);
78
79 // Disable open-circuit detection
80 spi.writeRegister(CR0, CR0_CMODE);
81
82 return !(fault & SR_OPEN);
83}
84
86{
87 SPITransaction spi{slave};
88 spi.writeRegister(CR1, static_cast<uint8_t>(type));
89}
90
92{
93 SPITransaction spi{slave};
94 spi.writeRegister(CJTO,
95 static_cast<int8_t>(offset * 1 / CJ_TEMP_LSB_VALUE));
96}
97
99{
100 SPITransaction spi{slave};
101 int16_t cjRaw = spi.readRegister16(CJTH);
102 int32_t tcRaw = spi.readRegister24(LTCBH);
103
104 MAX31856Data result;
106
107 // The register has:
108 // - A leading sign bit (which is actually two's complement)
109 // - The other 18 bits
110 // - 5 unused trailing bits
111
112 // Since the 24 bit registers value is stored into a 32 bit variable, first
113 // we make a left shift to move the sign bit in the 31st bit, and then a
114 // right shift to remove the unused bits.
115 // This automatically extends the sign
116 tcRaw = tcRaw << 8;
117 tcRaw = tcRaw >> (8 + 5);
118
119 // Here we just make a right shift as the sign bit is already in the 15th
120 // bit. This will also perform the sign extension like in sampleImpl()
121 cjRaw = cjRaw >> 4;
122
123 // Convert the raw value into temperature
124 result.temperature = static_cast<float>(tcRaw) * TC_TEMP_LSB_VALUE;
125
126 // Convert the raw value into temperature
127 result.coldJunctionTemperature =
128 static_cast<float>(cjRaw) * CJ_TEMP_LSB_VALUE;
129
130 return result;
131}
132
133} // namespace Boardcore
MAX31856Data sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
Definition MAX31856.cpp:98
bool checkConnected()
Checks whether the thermocouple is connected or not.
Definition MAX31856.cpp:65
bool selfTest()
Check if the sensor is working.
Definition MAX31856.cpp:63
bool init()
Initialize the sensor.
Definition MAX31856.cpp:45
static SPIBusConfig getDefaultSPIConfig()
Definition MAX31856.cpp:36
void setColdJunctionOffset(float offset)
Definition MAX31856.cpp:91
void setThermocoupleType(ThermocoupleType type)
Definition MAX31856.cpp:85
MAX31856(SPIBusInterface &bus, miosix::GpioPin cs, SPIBusConfig config=getDefaultSPIConfig(), ThermocoupleType type=ThermocoupleType::K_TYPE)
Definition MAX31856.cpp:30
Interface for low level access of a SPI bus as a master.
Provides high-level access to the SPI Bus for a single transaction.
@ MODE_1
CPOL = 1, CPHA = 0 -> Clock high when idle, sample on first edge.
@ INVERTED
Inverted write bit settings (1 for write, 0 for reads)
uint64_t getTimestamp()
Returns the current timer value in microseconds.
This file includes all the types the logdecoder script will decode.
SPI Bus configuration for a specific slave.
SPI::ClockDivider clockDivider
< Peripheral clock division