Skyward boardcore
Loading...
Searching...
No Matches
ADS1118.h
Go to the documentation of this file.
1/* Copyright (c) 2020 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#pragma once
24
26#include <sensors/Sensor.h>
27
28#include "ADS1118Data.h"
29
30namespace Boardcore
31{
32
71class ADS1118 : public Sensor<ADS1118Data>
72{
73public:
85
87 {
88 FSR_6_144 = 0x0,
89 FSR_4_096 = 0x1,
90 FSR_2_048 = 0x2,
91 FSR_1_024 = 0x3,
92 FSR_0_512 = 0x4,
93 FSR_0_256 = 0x5
94 };
95
101
103 {
104 DR_8 = 0x0,
105 DR_16 = 0x1,
106 DR_32 = 0x2,
107 DR_64 = 0x3,
108 DR_128 = 0x4,
109 DR_250 = 0x5,
110 DR_475 = 0x6,
111 DR_860 = 0x7
112 };
113
115 {
116 ADC_MODE = 0x0,
117 TEMP_SENSOR_MODE = 0x1
118 };
119
121 {
123 PULL_UP_EN = 0x1
124 };
125
127 {
128 struct
129 {
133 uint8_t singleShot : 1;
134 uint8_t reserved : 1;
135 uint8_t noOp : 2;
140
141 struct
142 {
143 uint8_t msb;
144 uint8_t lsb;
146
147 uint16_t word;
148 };
149
155
156 static constexpr uint8_t VALID_OPERATION =
157 0x1;
158
159 static const ADS1118Config
161
162 static constexpr int8_t TEMP_CHANNEL = 8;
163
164 static constexpr int8_t NUM_OF_CHANNELS = 9;
165
166 static constexpr int8_t INVALID_CHANNEL = -1;
167
172 ADS1118(SPIBusInterface& bus, miosix::GpioPin cs, ADS1118Config config_,
173 SPIBusConfig spiConfig = getDefaultSPIConfig());
174
188 explicit ADS1118(SPISlave spiSlave_,
190 bool busyWait_ = false, int16_t tempDivider_ = 100);
191
198
205 bool init() override;
206
213 void enableInput(ADS1118Mux mux);
214
223
224 void disableInput(ADS1118Mux mux);
225
226 void disableAllInputs();
227
228 void enableTemperature();
229
230 void disableTemperature();
231
233
235
239 void enableConfigCheck();
240
244 void disableConfigCheck();
245
253
260
265
270
274 int getConversionTime(int8_t channel);
275
282 bool selfTest() override;
283
284protected:
291 ADS1118Data sampleImpl() override;
292
293private:
303 void readChannel(int8_t nextChannel, int8_t prevChannel);
304
308 void readChannel(int8_t channel);
309
310 int8_t findNextEnabledChannel(int8_t startChannel);
311
312 const SPISlave spiSlave;
313 ADS1118Config baseConfig;
314
316 bool configCheck = false;
317
319 const bool busyWait;
320
321 ADS1118Config channelsConfig[NUM_OF_CHANNELS];
323
324 ADS1118Config lastConfig;
325 uint8_t lastConfigIndex = 0;
326
328 const uint16_t tempDivider;
329
330 uint16_t sampleCounter = 0;
331
333 const float PGA_LSB_SIZE[6] = {0.187, 0.125, 0.0625,
334 0.03125, 0.015625, 0.0078125};
335
337 const int CONV_TIME[8] = {125000, 62500, 31250, 15625,
338 7813, 4000, 2106, 1163};
339
340 static constexpr float TEMP_LSB_SIZE = 0.03125;
341 static constexpr uint16_t TEMP_CONFIG = 0xF281;
342
347 static constexpr uint16_t CONFIG_MASK = 0xFE7F;
348};
349
350} // namespace Boardcore
Driver for ADS1118 adc.
Definition ADS1118.h:72
TemperatureData getTemperature()
Returns the last temperature value.
Definition ADS1118.cpp:128
void enableTemperature()
Definition ADS1118.cpp:99
TemperatureData readTemperatureAndWait()
Reads on the fly the temperature.
Definition ADS1118.cpp:120
int getConversionTime(int8_t channel)
Returns the conversion time in us for the specified channel.
Definition ADS1118.cpp:134
ADS1118Data getVoltage(ADS1118Mux mux)
Returns the last read voltage value for the specified channel.
Definition ADS1118.cpp:126
static constexpr int8_t NUM_OF_CHANNELS
Definition ADS1118.h:164
void enableConfigCheck()
Enables the configuration check after it's writing to the device.
Definition ADS1118.cpp:110
@ FSR_0_512
FSR is ±0.512 V.
Definition ADS1118.h:92
@ FSR_0_256
FSR is ±0.256 V.
Definition ADS1118.h:93
@ FSR_1_024
FSR is ±1.024 V.
Definition ADS1118.h:91
@ FSR_4_096
FSR is ±4.096 V.
Definition ADS1118.h:89
@ FSR_2_048
FSR is ±2.048 V (default)
Definition ADS1118.h:90
@ FSR_6_144
FSR is ±6.144 V.
Definition ADS1118.h:88
ADS1118(SPIBusInterface &bus, miosix::GpioPin cs, ADS1118Config config_, SPIBusConfig spiConfig=getDefaultSPIConfig())
Construct a new ADS1118 object specifying spi bus, spi config and cs pin as well as device configurat...
Definition ADS1118.cpp:35
void disableConfigCheck()
Disables the configuration check after it's writing to the device.
Definition ADS1118.cpp:112
void enablePullUpResistor()
Definition ADS1118.cpp:106
@ MUX_AIN0_AIN3
AINp is AIN0 and AINn is AIN3.
Definition ADS1118.h:77
@ MUX_AIN1_GND
AINp is AIN1 and AINn is GND.
Definition ADS1118.h:81
@ MUX_AIN3_GND
AINp is AIN3 and AINn is GND.
Definition ADS1118.h:83
@ MUX_AIN2_GND
AINp is AIN2 and AINn is GND.
Definition ADS1118.h:82
@ MUX_AIN1_AIN3
AINp is AIN1 and AINn is AIN3.
Definition ADS1118.h:78
@ MUX_AIN0_AIN1
AINp is AIN0 and AINn is AIN1 (default)
Definition ADS1118.h:76
@ MUX_AIN0_GND
AINp is AIN0 and AINn is GND.
Definition ADS1118.h:80
@ MUX_AIN2_AIN3
AINp is AIN2 and AINn is AIN3.
Definition ADS1118.h:79
@ PULL_UP_DIS
Pullup resistor disabled on DOUT pin.
Definition ADS1118.h:122
@ PULL_UP_EN
Pullup resistor enabled on DOUT pin (default)
Definition ADS1118.h:123
@ CONTINUOUS_CONV_MODE
Continuous-conversion mode.
Definition ADS1118.h:98
@ SINGLE_SHOT_MODE
Power-down and single-shot mode (default)
Definition ADS1118.h:99
ADS1118Data sampleImpl() override
Reads the previously configured channel while writing the next enabled configuration.
Definition ADS1118.cpp:173
void disablePullUpResistor()
Definition ADS1118.cpp:108
static constexpr uint8_t VALID_OPERATION
Indicates a valid configuration.
Definition ADS1118.h:156
bool selfTest() override
Writes the temperature configuration and check if it is read back correctly.
Definition ADS1118.cpp:142
void enableInput(ADS1118Mux mux)
Enables the sapling of a specific mux configuration with the main configuration specified in the cons...
Definition ADS1118.cpp:71
void disableInput(ADS1118Mux mux)
Definition ADS1118.cpp:91
static const ADS1118Config ADS1118_DEFAULT_CONFIG
Default configuration.
Definition ADS1118.h:160
bool init() override
Initialize the configuration.
Definition ADS1118.cpp:69
ADS1118Data readInputAndWait(ADS1118Mux mux)
Reads on the fly the specified input.
Definition ADS1118.cpp:114
@ DR_475
475 SPS
Definition ADS1118.h:110
@ DR_250
250 SPS
Definition ADS1118.h:109
@ DR_128
128 SPS (default)
Definition ADS1118.h:108
@ DR_860
860 SPS
Definition ADS1118.h:111
@ TEMP_SENSOR_MODE
Temperature sensor mode.
Definition ADS1118.h:117
@ ADC_MODE
ADC mode (default)
Definition ADS1118.h:116
void disableAllInputs()
Definition ADS1118.cpp:93
void disableTemperature()
Definition ADS1118.cpp:104
static SPIBusConfig getDefaultSPIConfig()
Definition ADS1118.cpp:61
static constexpr int8_t TEMP_CHANNEL
Temperature channel number.
Definition ADS1118.h:162
static constexpr int8_t INVALID_CHANNEL
Definition ADS1118.h:166
Interface for low level access of a SPI bus as a master.
Base sensor class with has to be extended by any sensor driver.
Definition Sensor.h:91
This file includes all the types the logdecoder script will decode.
ADS1118Mux mux
Input's mux configuration.
Definition ADS1118.h:152
ADS1118DataRate rate
Input's data rate configuration.
Definition ADS1118.h:153
SPI Bus configuration for a specific slave.
Contains information about a single SPI slave device.
Structure of configuration word.
Definition ADS1118.h:127
ADS1118PullUp pullUp
Pullup enable.
Definition ADS1118.h:136
ADS1118DataRate rate
Data rate.
Definition ADS1118.h:138
ADS1118Mode mode
Device operating mode.
Definition ADS1118.h:130
ADS1118Pga pga
Programmable gain amplifier configuration.
Definition ADS1118.h:131
struct Boardcore::ADS1118::ADS1118Config::@0 bits
Includes all the configuration bits.
uint8_t singleShot
Single-shot conversion start.
Definition ADS1118.h:133
ADS1118TempMode tempMode
Temperature sensor mode.
Definition ADS1118.h:137
struct Boardcore::ADS1118::ADS1118Config::@1 byte
Includes the msb and lsb bytes.
uint8_t reserved
Reserved, doesn't matter.
Definition ADS1118.h:134
uint8_t noOp
No operation.
Definition ADS1118.h:135
uint16_t word
Representation in word (16-bits) format.
Definition ADS1118.h:147
ADS1118Mux mux
Input multiplexer configuration.
Definition ADS1118.h:132