1
1
/*
2
2
* Copyright (c) 2022, Jonas Kazem Andersen
3
3
* Copyright (c) 2022, Rasmus Kleist Hørlyck Sørensen
4
+ * Copyright (c) 2025, Niklas Hauser
4
5
*
5
6
* This file is part of the modm project.
6
7
*
10
11
*/
11
12
// ----------------------------------------------------------------------------
12
13
13
- #ifndef MODM_ADS101X_HPP
14
- #define MODM_ADS101X_HPP
14
+ #pragma once
15
15
16
16
#include < modm/architecture/interface/i2c_device.hpp>
17
+ #include < modm/architecture/interface/register.hpp>
17
18
18
19
namespace modm
19
20
{
@@ -187,62 +188,118 @@ struct ads101x
187
188
* @ingroup modm_driver_ads101x
188
189
*/
189
190
template <typename I2cMaster>
190
- class Ads101x : public ads101x , public modm ::I2cDevice<I2cMaster, 2 >
191
+ class Ads101x : public ads101x , public modm ::I2cDevice<I2cMaster>
191
192
{
192
193
public:
193
194
/* *
194
195
* @param data
195
196
* @param address
196
197
*/
197
- Ads101x (Data &data, uint8_t address = 0x49 );
198
+ Ads101x (Data &data, uint8_t address = 0x49 )
199
+ : I2cDevice<I2cMaster>(address), data(data) {}
198
200
199
201
// / Call this function before using the device
200
- modm::ResumableResult<bool >
201
- initialize ();
202
+ bool
203
+ initialize ()
204
+ {
205
+ InputMultiplexer_t::set (config, InputMultiplexer::Input0);
206
+ FullScaleRange_t::set (config, FullScaleRange::V2_048);
207
+ DeviceOperatingMode_t::set (config, DeviceOperatingMode::SingleShot);
208
+ DataRate_t::set (config, DataRate::Sps1600);
209
+ ComparatorQueue_t::set (config, ComparatorQueue::Disable);
210
+ data.lsbSizeIndex = i (FullScaleRange::V2_048) >> 9 ;
211
+
212
+ return writeRegister (Register::Config, config.value );
213
+ }
202
214
203
215
// / Determine if the device is currently performing a conversion
204
- modm::ResumableResult<bool >
205
- isBusy ();
216
+ bool
217
+ isBusy ()
218
+ {
219
+ uint8_t buffer[2 ] = {i (Register::Config)};
220
+ if (not this ->writeRead (buffer, 1 , buffer, 2 )) return false ;
221
+ return not ((uint16_t (buffer[0 ]) << 8 ) & i (ConfigRegister::OS));
222
+ }
206
223
207
224
// / Start a single conversion with the specified input
208
- modm::ResumableResult<bool >
209
- startSingleShotConversion ();
225
+ bool
226
+ startSingleShotConversion ()
227
+ {
228
+ DeviceOperatingMode_t::set (config, DeviceOperatingMode::SingleShot);
229
+ return writeRegister (Register::Config, i (ConfigRegister::OS) | config.value );
230
+ }
210
231
211
232
// / Start continuous conversions with the specified datarate and input
212
- modm::ResumableResult<bool >
213
- startContinuousConversion (DataRate dataRate = DataRate::Sps1600);
233
+ bool
234
+ startContinuousConversion (DataRate dataRate = DataRate::Sps1600)
235
+ {
236
+ DataRate_t::set (config, dataRate);
237
+ DeviceOperatingMode_t::set (config, DeviceOperatingMode::Continuous);
238
+ return writeRegister (Register::Config, i (ConfigRegister::OS) | config.value );
239
+ }
214
240
215
241
// / Start a single conversion with the specified input
216
242
// / @warning ADS1015 only
217
- modm::ResumableResult<bool >
218
- startSingleShotConversion (InputMultiplexer input = InputMultiplexer::Input0);
243
+ bool
244
+ startSingleShotConversion (InputMultiplexer input = InputMultiplexer::Input0)
245
+ {
246
+ InputMultiplexer_t::set (config, input);
247
+ DeviceOperatingMode_t::set (config, DeviceOperatingMode::SingleShot);
248
+ return writeRegister (Register::Config, i (ConfigRegister::OS) | config.value );
249
+ }
219
250
220
251
// / Start continuous conversions with the specified datarate and input
221
252
// / @warning ADS1015 only
222
- modm::ResumableResult<bool >
223
- startContinuousConversion (InputMultiplexer input = InputMultiplexer::Input0, DataRate dataRate = DataRate::Sps1600);
253
+ bool
254
+ startContinuousConversion (InputMultiplexer input = InputMultiplexer::Input0, DataRate dataRate = DataRate::Sps1600)
255
+ {
256
+ DataRate_t::set (config, dataRate);
257
+ InputMultiplexer_t::set (config, input);
258
+ DeviceOperatingMode_t::set (config, DeviceOperatingMode::Continuous);
259
+ return writeRegister (Register::Config, config.value );
260
+ }
224
261
225
262
// / Read the last conversion result
226
263
// / @attention Following power-up, the conversion result remains zero until the first conversion is completed
227
- modm::ResumableResult<bool >
228
- readConversionResult ();
264
+ bool
265
+ readConversionResult ()
266
+ {
267
+ uint8_t buffer = i (Register::Conversion);
268
+ return this ->writeRead (&buffer, 1 , data.data , 2 );
269
+ }
229
270
230
271
// / Enable the conversion-ready function of the ALERT/RDY pin
231
272
// / @attention enabling the conversion-ready function disables the comparator and sets the ComparatorQue value to one conversion
232
- modm::ResumableResult<bool >
233
- enableConversionReadyFunction ();
273
+ bool
274
+ enableConversionReadyFunction ()
275
+ {
276
+ if (not (writeRegister (Register::LowThreshold, 0x0000 ) and
277
+ writeRegister (Register::HighThreshold, 0xFFFF )))
278
+ return false ;
279
+
280
+ ComparatorQueue_t::set (config, ComparatorQueue::OneConversion);
281
+ return writeRegister (Register::Config, config.value );
282
+ }
234
283
235
284
// / Enable the comparator
236
285
// / @warning ADS1014 and ADS1015 only
237
286
// / @warning To use the comparator-function the high threshold must be greater than the low threshold
238
- modm::ResumableResult<bool >
239
- enableComparator (ComparatorMode mode, ComparatorPolarity polarity, ComparatorLatch latch, ComparatorQueue queue);
287
+ bool
288
+ enableComparator (ComparatorMode mode, ComparatorPolarity polarity, ComparatorLatch latch, ComparatorQueue queue)
289
+ {
290
+ ComparatorMode_t::set (config, mode);
291
+ ComparatorPolarity_t::set (config, polarity);
292
+ ComparatorLatch_t::set (config, latch);
293
+ ComparatorQueue_t::set (config, queue);
294
+
295
+ return writeRegister (Register::Config, config.value );
296
+ }
240
297
241
298
// / Set the low threshold used by the comparator queue
242
299
// / @warning ADS1014 and ADS1015 only
243
300
// / @warning The low threshold value must be smaller than the high threshold value
244
301
// / @attention The high threshold value must be updated whenever the PGA settings are changed
245
- modm::ResumableResult< bool >
302
+ bool
246
303
setLowThreshold (uint16_t threshold)
247
304
{
248
305
return writeRegister (Register::LowThreshold, (threshold << 4 ) & 0xFFF0 );
@@ -252,31 +309,33 @@ class Ads101x : public ads101x, public modm::I2cDevice<I2cMaster, 2>
252
309
// / @warning ADS1014 and ADS1015 only
253
310
// / @warning The high threshold value must be greater than the low threshold value
254
311
// / @attention The high threshold value must be updated whenever the PGA settings are changed
255
- modm::ResumableResult< bool >
312
+ bool
256
313
setHighThreshold (uint16_t threshold)
257
314
{
258
315
return writeRegister (Register::HighThreshold, (threshold << 4 ) | 0x0F );
259
316
}
260
317
261
318
// / Set the full scale range by programming the PGA and corresponding LSB size
262
- modm::ResumableResult<bool >
263
- setFullScaleRange (FullScaleRange fullScaleRange);
319
+ bool
320
+ setFullScaleRange (FullScaleRange fullScaleRange)
321
+ {
322
+ FullScaleRange_t::set (config, fullScaleRange);
323
+ data.lsbSizeIndex = i (fullScaleRange) >> 9 ;
324
+ return writeRegister (Register::Config, config.value );
325
+ }
264
326
265
327
private:
266
- modm::ResumableResult<bool >
267
- writeRegister (Register reg, uint16_t data);
328
+ bool
329
+ writeRegister (Register reg, uint16_t data)
330
+ {
331
+ uint8_t buffer[] = {i (reg), data >> 8 , data};
332
+ return this ->write (buffer, sizeof (buffer));
333
+ }
268
334
269
335
Data &data;
270
336
271
- // / Command buffer for writing to device
272
- uint8_t buffer[3 ];
273
-
274
337
// / Configuration variable for writing to the configuration register
275
338
ConfigRegister_t config;
276
339
};
277
340
278
341
}
279
-
280
- #include " ads101x_impl.hpp"
281
-
282
- #endif // MODM_ADS101X_HPP
0 commit comments