-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathads101x.c
executable file
·624 lines (506 loc) · 16.7 KB
/
ads101x.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
/**
******************************************************************************
* @file : ads101x.c
* @author : Mauricio Barroso Benavides
* @date : Nov 6, 2023
* @brief : todo: write brief
******************************************************************************
* @attention
*
* MIT License
*
* Copyright (c) 2023 Mauricio Barroso Benavides
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "ads101x.h"
#include "esp_err.h"
#include "esp_log.h"
#include "esp_timer.h"
/* Private macros ------------------------------------------------------------*/
#define NOP() asm volatile ("nop")
/* External variables --------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static const char *TAG = "ads101x";
/* Private function prototypes -----------------------------------------------*/
/**
* @brief Function that implements the default I2C read transaction
*
* @param reg_addr : Register address to be read
* @param reg_data : Pointer to the data to be read from reg_addr
* @param data_len : Length of the data transfer
* @param intf : Pointer to the interface descriptor
*
* @return 0 if successful, non-zero otherwise
*/
static int8_t i2c_read(uint8_t reg_addr, uint16_t *reg_data, void *intf);
/**
* @brief Function that implements the default I2C write transaction
*
* @param reg_addr : Register address to be written
* @param reg_data : Data to be written to reg_addr
* @param data_len : Length of the data transfer
* @param intf : Pointer to the interface descriptor
*
* @return 0 if successful, non-zero otherwise
*/
static int8_t i2c_write(uint8_t reg_addr, const uint16_t reg_data,
void *intf);
/**
* @brief Function that implements a micro seconds delay
*
* @param period_us: Time in us to delay
*/
static void delay_us(uint32_t period_us);
/**
* @brief Function that implements a micro seconds delay
*
* @param arg: todo: write
*/
static void isr_handler(void *arg);
/* Exported functions definitions --------------------------------------------*/
/**
* @brief Function to initialize a ADS101x instance
*/
esp_err_t ads101x_init(ads101x_t *const me, ads101x_model_t model, gpio_num_t int_pin,
i2c_master_bus_handle_t i2c_bus_handle, uint8_t dev_addr) {
/* Print initializing message */
ESP_LOGI(TAG, "Initializing instance...");
/* Variable to return error code */
esp_err_t ret = ESP_OK;
/**/
me->model = model;
me->bit_shift = 4;
me->gain = ADS101X_GAIN_TWOTHIRDS;
me->data_rate = ADS101X_DATA_RATE_1600SPS;
me->is_complete = false;
me->int_pin = int_pin;
/* Add device to I2C bus */
i2c_device_config_t i2c_dev_conf = {
.scl_speed_hz = 400000,
.device_address = dev_addr
};
if (i2c_master_bus_add_device(i2c_bus_handle, &i2c_dev_conf, &me->i2c_dev) != ESP_OK) {
ESP_LOGE(TAG, "Failed to add device to I2C bus");
return ret;
}
/* Configure interrupt pin */
gpio_config_t gpio_conf;
gpio_conf.intr_type = GPIO_INTR_NEGEDGE;
gpio_conf.mode = GPIO_MODE_INPUT;
gpio_conf.pin_bit_mask = 1ULL << me->int_pin;
gpio_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
gpio_conf.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&gpio_conf);
gpio_install_isr_service(0);
gpio_isr_handler_add(me->int_pin, isr_handler, (void *)me);
/* Print successful initialization message */
ESP_LOGI(TAG, "Instance initialized successfully");
/* Return ESP_OK */
return ret;
}
/**
* @brief Function that reads a specific single-ended ADC channel.
*/
esp_err_t ads101x_read_single_ended(ads101x_t *const me,
ads101x_channel_t channel,
int16_t *adc_result) {
/* Variable to return error code */
esp_err_t ret = ESP_OK;
/* Check channel argument */
if (me->model == ADS101X_MODEL_5) { /* ADS1015 */
if (channel > ADS101X_CHANNEL_3) {
ESP_LOGE(TAG, "Failed to select channel, must be less than 3");
return ESP_FAIL;
}
}
else { /* ADS1013 and ADS1014 */
if (channel > ADS101X_CHANNEL_1) {
ESP_LOGE(TAG, "Failed to select channel, must be less than 1");
return ESP_FAIL;
}
}
/* Perform a oneshot ADC reading */
ret = ads101x_start_reading(me, channel, ADS101X_MODE_ONESHOT);
if (ret != ESP_OK) {
return ESP_FAIL;
}
/* Check if the conversion is complete */
// do {
// delay_us(5 * 1000); /* Wait for 5 ms */
// ads101x_conversion_complete(me, &conversion_is_complete);
//
// } while (!me->is_complete);
while (!me->is_complete) {};
me->is_complete = false;
/* Get the las ADC conversion result */
ret = ads101x_get_last_conversion_results(me, adc_result);
if (ret != ESP_OK) {
return ESP_FAIL;
}
/* Return ESP_OK */
return ret;
}
/**
* @brief Function that reads the voltage difference between the P (AIN0) and N
* (AIN1) input.
*/
esp_err_t ads101x_read_differential_0_1(ads101x_t *const me,
int16_t *adc_result) {
/* Variable to return error code */
esp_err_t ret = ESP_OK;
/* Perform a oneshot ADC reading */
ret = ads101x_start_reading(me, ADS101X_REG_CONFIG_MUX_DIFF_0_1, ADS101X_MODE_ONESHOT);
if (ret != ESP_OK) {
return ESP_FAIL;
}
/* Check if the conversion is complete */
bool conversion_is_complete = false;
do {
delay_us(5 * 1000); /* Wait for 5 ms */
ads101x_conversion_complete(me, &conversion_is_complete);
} while (!conversion_is_complete);
/* Get the las ADC conversion result */
ret = ads101x_get_last_conversion_results(me, adc_result);
if (ret != ESP_OK) {
return ESP_FAIL;
}
/* Return ESP_OK */
return ret;
}
/**
* @brief Function that reads the voltage difference between the P (AIN0) and N
* (AIN3) input.
*/
esp_err_t ads101x_read_differential_0_3(ads101x_t *const me,
int16_t *adc_result) {
/* Variable to return error code */
esp_err_t ret = ESP_OK;
/* Perform a oneshot ADC reading */
ret = ads101x_start_reading(me, ADS101X_REG_CONFIG_MUX_DIFF_0_3, ADS101X_MODE_ONESHOT);
if (ret != ESP_OK) {
return ESP_FAIL;
}
/* Check if the conversion is complete */
bool conversion_is_complete = false;
do {
delay_us(5 * 1000); /* Wait for 5 ms */
ads101x_conversion_complete(me, &conversion_is_complete);
} while (!conversion_is_complete);
/* Get the las ADC conversion result */
ret = ads101x_get_last_conversion_results(me, adc_result);
if (ret != ESP_OK) {
return ESP_FAIL;
}
/* Return ESP_OK */
return ret;
}
/**
* @brief Function that reads the voltage difference between the P (AIN1) and N
* (AIN3) input.
*/
esp_err_t ads101x_read_differential_1_3(ads101x_t *const me,
int16_t *adc_result) {
/* Variable to return error code */
esp_err_t ret = ESP_OK;
/* Perform a oneshot ADC reading */
ret = ads101x_start_reading(me, ADS101X_REG_CONFIG_MUX_DIFF_1_3, ADS101X_MODE_ONESHOT);
if (ret != ESP_OK) {
return ESP_FAIL;
}
/* Check if the conversion is complete */
bool conversion_is_complete = false;
do {
delay_us(5 * 1000); /* Wait for 5 ms */
ads101x_conversion_complete(me, &conversion_is_complete);
} while (!conversion_is_complete);
/* Get the las ADC conversion result */
ret = ads101x_get_last_conversion_results(me, adc_result);
if (ret != ESP_OK) {
return ESP_FAIL;
}
/* Return ESP_OK */
return ret;
}
/**
* @brief Function that reads the voltage difference between the P (AIN2) and N
* (AIN3) input.
*/
esp_err_t ads101x_read_differential_2_3(ads101x_t *const me,
int16_t *adc_result) {
/* Variable to return error code */
esp_err_t ret = ESP_OK;
/* Perform a oneshot ADC reading */
ret = ads101x_start_reading(me, ADS101X_REG_CONFIG_MUX_DIFF_2_3, ADS101X_MODE_ONESHOT);
if (ret != ESP_OK) {
return ESP_FAIL;
}
/* Check if the conversion is complete */
bool conversion_is_complete = false;
do {
delay_us(5 * 1000); /* Wait for 5 ms */
ads101x_conversion_complete(me, &conversion_is_complete);
} while (!conversion_is_complete);
/* Get the las ADC conversion result */
ret = ads101x_get_last_conversion_results(me, adc_result);
if (ret != ESP_OK) {
return ESP_FAIL;
}
/* Return ESP_OK */
return ret;
}
/**
* @brief Function that sets up the comparator to operator in basic mode,
* causing the ALRT/RDY pin to assert when the ADC value exceeeds the
* specified value.
*/
esp_err_t ads101x_start_comparator_single_ended(ads101x_t *const me,
ads101x_channel_t channel,
int16_t threshold) {
/* Variable to return error code */
esp_err_t ret = ESP_OK;
/* Fill the ADS101x configuration value */
uint16_t config = ADS101X_REG_CONFIG_CQUE_1CONV | /* Comparator enabled and
asserts on 1 match */
ADS101X_REG_CONFIG_CLAT_LATCH | /* Latching mode */
ADS101X_REG_CONFIG_CPOL_ACTVLOW | /* Alert/ready active low */
ADS101X_REG_CONFIG_CMODE_TRAD | /* Traditional comparator */
ADS101X_REG_CONFIG_MODE_CONTIN; /* Continuos conversion
mode */
/* Set PGA/voltage range */
config |= me->gain;
/* Set data rate */
config |= me->data_rate;
/* Set channel */
config |= channel;
/* Set the high threshold register */
if (i2c_write(ADS101X_REG_POINTER_HITHRESH, threshold << me->bit_shift, me->i2c_dev) < 0) {
return ESP_FAIL;
}
/* Set the new ADC configuration */
if (i2c_write(ADS101X_REG_POINTER_CONFIG, config, me->i2c_dev) < 0) {
return ESP_FAIL;
}
/* Return ESP_OK */
return ret;
}
/**
* @brief Function that reads the last conversion results without changin the
* configuration value.
*/
esp_err_t ads101x_get_last_conversion_results(ads101x_t *const me,
int16_t *adc_result) {
/* Variable to return error code */
esp_err_t ret = ESP_OK;
/* Read the conversion result */
uint16_t result = 0;
if (i2c_read(ADS101X_REG_POINTER_CONVERT, &result, me->i2c_dev) < 0) {
return ESP_FAIL;
}
result >>= me->bit_shift;
if (me->bit_shift == 0) {
*adc_result = (int16_t)result;
}
else {
/* Shift 12-bit results right 4 bits for the ADS101x,
* making sure we keep the sign bit intact */
if (result > 0x07FF) {
/* Negative number, extend the sign to 16th bit */
result |= 0xF000;
}
*adc_result = (int16_t)result;
}
/* Return ESP_OK */
return ret;
}
/**
* @brief Function that computes the voltage value from ADC raw value
*/
float ads101x_compute_volts(ads101x_t *const me, int16_t counts) {
float fs_range;
switch (me->gain) {
case ADS101X_GAIN_TWOTHIRDS:
fs_range = 6.144f;
break;
case ADS101X_GAIN_ONE:
fs_range = 4.096f;
break;
case ADS101X_GAIN_TWO:
fs_range = 2.048f;
break;
case ADS101X_GAIN_FOUR:
fs_range = 1.024f;
break;
case ADS101X_GAIN_EIGHT:
fs_range = 0.512f;
break;
case ADS101X_GAIN_SIXTEEN:
fs_range = 0.256f;
break;
default:
fs_range = 0.0f;
}
return (counts * (fs_range / (32768 >> me->bit_shift)));
}
/**
* @brief Function that sets the ADS101x gain
*/
void ads101x_set_gain(ads101x_t *const me, ads101x_gain_t gain) {
me->gain = gain;
}
/**
* @brief Function that get the ADS101x gain
*/
ads101x_gain_t ads101x_get_gain(ads101x_t *const me) {
return me->gain;
}
/**
* @brief Function that sets the ADS101x data rate
*/
void ads101x_set_data_rate(ads101x_t *const me, ads101x_data_rate_t data_rate) {
me->data_rate = data_rate;
}
/**
* @brief Function that gets ADS101x data rate
*/
ads101x_data_rate_t ads101x_get_data_rate(ads101x_t *const me) {
return me->data_rate;
}
/**
* @brief Function that stars the conversion function
*/
esp_err_t ads101x_start_reading(ads101x_t *const me, uint16_t mux,
ads101x_mode_t mode) {
/* Variable to return error code */
esp_err_t ret = ESP_OK;
/* Fill the ADS101x configuration value */
uint16_t config = ADS101X_REG_CONFIG_CQUE_1CONV | /* Set CQUE to any value
other than None so we
can use it in RDY mode
*/
ADS101X_REG_CONFIG_CLAT_NONLAT | /* Non-latching */
ADS101X_REG_CONFIG_CPOL_ACTVLOW | /* ALERT/RDY active low */
ADS101X_REG_CONFIG_CMODE_TRAD; /* Traditional comparator */
/* Configure the reading mode */
if (mode == ADS101X_MODE_CONTINUOS) {
config |= ADS101X_REG_CONFIG_MODE_CONTIN;
}
else {
config |= ADS101X_REG_CONFIG_MODE_SINGLE;
}
/* Set PGA/voltage range */
config |= me->gain;
/* Set data rate */
config |= me->data_rate;
/* Set channel */
config |= mux;
/* Set to start a single-conversion */
config |= ADS101X_REG_CONFIG_OS_SINGLE;
/* Set the new ADC configuration */
if (i2c_write(ADS101X_REG_POINTER_CONFIG, config, me->i2c_dev) < 0) {
return ESP_FAIL;
}
/* Set ALERT/RDY to RDY mode */
if (i2c_write(ADS101X_REG_POINTER_HITHRESH, 0x8000, me->i2c_dev) < 0) {
return ESP_FAIL;
}
if (i2c_write(ADS101X_REG_POINTER_LOWTHRESH, 0x0000, me->i2c_dev) < 0) {
return ESP_FAIL;
}
/* Return ESP_OK */
return ret;
}
/**
* @brief Function that check if the ADC reading is complete
*/
esp_err_t ads101x_conversion_complete(ads101x_t *const me, bool *is_complete) {
/* Variable to return error code */
esp_err_t ret = ESP_OK;
/* Check if the device is performing a conversion */
uint16_t rx_data = 0;
if (i2c_read(ADS101X_REG_POINTER_CONFIG, &rx_data, me->i2c_dev) < 0) {
return ESP_FAIL;
}
*is_complete = (bool)(rx_data & 0x8000);
/* Return ESP_OK */
return ret;
}
/* Private function definitions ----------------------------------------------*/
/**
* @brief Function that implements the default I2C read transaction
*/
static int8_t i2c_read(uint8_t reg_addr, uint16_t *reg_data, void *intf) {
i2c_master_dev_handle_t i2c_dev = (i2c_master_dev_handle_t)intf;
uint8_t buffer[2] = {0};
if (i2c_master_transmit_receive(i2c_dev, ®_addr, 1, buffer, 2, -1) != ESP_OK) {
return -1;
}
*reg_data = (uint16_t)((buffer[0] << 8) | buffer[1]);
return 0;
}
/**
* @brief Function that implements the default I2C write transaction
*/
static int8_t i2c_write(uint8_t reg_addr, const uint16_t reg_data,
void *intf) {
// int8_t ret = 0;
i2c_master_dev_handle_t i2c_dev = (i2c_master_dev_handle_t)intf;
uint8_t buffer[32] = {0};
/* Copy the register address to buffer */
uint8_t addr_len = sizeof(reg_addr);
for (uint8_t i = 0; i < addr_len; i++) {
buffer[i] = (reg_addr & (0xFF << ((addr_len - 1 - i) * 8))) >> ((addr_len - 1 - i) * 8);
}
/* Copy the data to buffer */
uint8_t data_len = sizeof(reg_data);
for (uint8_t i = 0; i < data_len; i++) {
buffer[i + addr_len] = (reg_data & (0xFF << ((data_len - 1 - i) * 8))) >> ((data_len - 1 - i) * 8);
}
/* Transmit buffer */
if (i2c_master_transmit(i2c_dev, buffer, addr_len + data_len, -1) != ESP_OK) {
return -1;
}
return 0;
}
/**
* @brief Function that implements a micro seconds delay
*/
static void delay_us(uint32_t period_us) {
uint64_t m = (uint64_t)esp_timer_get_time();
if (period_us) {
uint64_t e = (m + period_us);
if (m > e) { /* overflow */
while ((uint64_t)esp_timer_get_time() > e) {
NOP();
}
}
while ((uint64_t)esp_timer_get_time() < e) {
NOP();
}
}
}
static void isr_handler(void *arg) {
ads101x_t *ads101x = (ads101x_t *)arg;
ads101x->is_complete = true;
}
/***************************** END OF FILE ************************************/