forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc_ads1112.c
397 lines (321 loc) · 9.51 KB
/
adc_ads1112.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
/*
* Copyright (c) 2019 Vestas Wind Systems A/S
* Copyright (c) 2020 Innoseis BV
* Copyright (c) 2023 Cruise LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/byteorder.h>
#include <zephyr/sys/util.h>
#define ADC_CONTEXT_USES_KERNEL_TIMER 1
#include "adc_context.h"
#define DT_DRV_COMPAT ti_ads1112
LOG_MODULE_REGISTER(ADS1112, CONFIG_ADC_LOG_LEVEL);
#define ADS1112_CONFIG_GAIN(x) ((x)&BIT_MASK(2))
#define ADS1112_CONFIG_DR(x) (((x)&BIT_MASK(2)) << 2)
#define ADS1112_CONFIG_CM(x) (((x)&BIT_MASK(1)) << 4)
#define ADS1112_CONFIG_MUX(x) (((x)&BIT_MASK(2)) << 5)
#define ADS1112_CONFIG_MASK_READY BIT(7)
#define ADS1112_DEFAULT_CONFIG 0x8C
#define ADS1112_REF_INTERNAL 2048
enum ads1112_reg {
ADS1112_REG_OUTPUT = 0,
ADS1112_REG_CONFIG = 1,
};
enum {
ADS1112_CONFIG_MUX_DIFF_0_1 = 0,
ADS1112_CONFIG_MUX_BOTH_2_3 = 1,
ADS1112_CONFIG_MUX_SINGLE_0_3 = 2,
ADS1112_CONFIG_MUX_SINGLE_1_3 = 3,
};
enum {
ADS1112_CONFIG_DR_RATE_240_RES_12 = 0,
ADS1112_CONFIG_DR_RATE_60_RES_14 = 1,
ADS1112_CONFIG_DR_RATE_30_RES_15 = 2,
ADS1112_CONFIG_DR_RATE_15_RES_16 = 3,
ADS1112_CONFIG_DR_DEFAULT = ADS1112_CONFIG_DR_RATE_15_RES_16,
};
enum {
ADS1112_CONFIG_GAIN_1 = 0,
ADS1112_CONFIG_GAIN_2 = 1,
ADS1112_CONFIG_GAIN_4 = 2,
ADS1112_CONFIG_GAIN_8 = 3,
};
enum {
ADS1112_CONFIG_CM_SINGLE = 0,
ADS1112_CONFIG_CM_CONTINUOUS = 1,
};
struct ads1112_config {
const struct i2c_dt_spec bus;
};
struct ads1112_data {
struct adc_context ctx;
k_timeout_t ready_time;
struct k_sem acq_sem;
int16_t *buffer;
int16_t *buffer_ptr;
bool differential;
};
static int ads1112_read_reg(const struct device *dev, enum ads1112_reg reg_addr, uint8_t *reg_val)
{
const struct ads1112_config *config = dev->config;
uint8_t buf[3] = {0};
int rc = i2c_read_dt(&config->bus, buf, sizeof(buf));
if (reg_addr == ADS1112_REG_OUTPUT) {
reg_val[0] = buf[0];
reg_val[1] = buf[1];
} else {
reg_val[0] = buf[2];
}
return rc;
}
static int ads1112_write_reg(const struct device *dev, uint8_t reg)
{
uint8_t msg[1] = {reg};
const struct ads1112_config *config = dev->config;
/* It's only possible to write the config register, so the ADS1112
* assumes all writes are going to that register and omits the register
* parameter from write transactions
*/
return i2c_write_dt(&config->bus, msg, sizeof(msg));
}
static inline int ads1112_acq_time_to_dr(const struct device *dev, uint16_t acq_time)
{
struct ads1112_data *data = dev->data;
int odr = -EINVAL;
uint16_t acq_value = ADC_ACQ_TIME_VALUE(acq_time);
uint32_t ready_time_us = 0;
if (acq_time == ADC_ACQ_TIME_DEFAULT) {
acq_value = ADS1112_CONFIG_DR_DEFAULT;
} else if (ADC_ACQ_TIME_UNIT(acq_time) != ADC_ACQ_TIME_TICKS) {
return -EINVAL;
}
switch (acq_value) {
case ADS1112_CONFIG_DR_RATE_15_RES_16:
odr = ADS1112_CONFIG_DR_RATE_15_RES_16;
ready_time_us = (1000 * 1000) / 15;
break;
case ADS1112_CONFIG_DR_RATE_30_RES_15:
odr = ADS1112_CONFIG_DR_RATE_30_RES_15;
ready_time_us = (1000 * 1000) / 30;
break;
case ADS1112_CONFIG_DR_RATE_60_RES_14:
odr = ADS1112_CONFIG_DR_RATE_60_RES_14;
ready_time_us = (1000 * 1000) / 60;
break;
case ADS1112_CONFIG_DR_RATE_240_RES_12:
odr = ADS1112_CONFIG_DR_RATE_240_RES_12;
ready_time_us = (1000 * 1000) / 240;
break;
default:
break;
}
/* Add some additional time to ensure that the data is truly ready,
* as chips in this family often require some additional time beyond
* the listed times
*/
data->ready_time = K_USEC(ready_time_us + 10);
return odr;
}
static int ads1112_wait_data_ready(const struct device *dev)
{
int rc;
struct ads1112_data *data = dev->data;
k_sleep(data->ready_time);
uint8_t status = 0;
rc = ads1112_read_reg(dev, ADS1112_REG_CONFIG, &status);
if (rc != 0) {
return rc;
}
while ((status & ADS1112_CONFIG_MASK_READY) == 0) {
k_sleep(K_USEC(100));
rc = ads1112_read_reg(dev, ADS1112_REG_CONFIG, &status);
if (rc != 0) {
return rc;
}
}
return 0;
}
static int ads1112_read_sample(const struct device *dev, uint16_t *buff)
{
int res;
uint8_t sample[2] = {0};
const struct ads1112_config *config = dev->config;
res = ads1112_read_reg(dev, ADS1112_REG_OUTPUT, sample);
buff[0] = sys_get_be16(sample);
return res;
}
static int ads1112_channel_setup(const struct device *dev,
const struct adc_channel_cfg *channel_cfg)
{
struct ads1112_data *data = dev->data;
uint8_t config = 0;
int dr = 0;
if (channel_cfg->channel_id != 0) {
return -EINVAL;
}
if (channel_cfg->differential) {
if (channel_cfg->input_positive == 0 && channel_cfg->input_negative == 1) {
config |= ADS1112_CONFIG_MUX(ADS1112_CONFIG_MUX_DIFF_0_1);
} else if (channel_cfg->input_positive == 2 && channel_cfg->input_negative == 3) {
config |= ADS1112_CONFIG_MUX(ADS1112_CONFIG_MUX_BOTH_2_3);
} else {
return -EINVAL;
}
} else {
if (channel_cfg->input_positive == 0) {
config |= ADS1112_CONFIG_MUX(ADS1112_CONFIG_MUX_SINGLE_0_3);
} else if (channel_cfg->input_positive == 1) {
config |= ADS1112_CONFIG_MUX(ADS1112_CONFIG_MUX_SINGLE_1_3);
} else if (channel_cfg->input_positive == 2) {
config |= ADS1112_CONFIG_MUX(ADS1112_CONFIG_MUX_BOTH_2_3);
} else {
return -EINVAL;
}
}
data->differential = channel_cfg->differential;
dr = ads1112_acq_time_to_dr(dev, channel_cfg->acquisition_time);
if (dr < 0) {
return dr;
}
config |= ADS1112_CONFIG_DR(dr);
switch (channel_cfg->gain) {
case ADC_GAIN_1:
config |= ADS1112_CONFIG_GAIN(ADS1112_CONFIG_GAIN_1);
break;
case ADC_GAIN_2:
config |= ADS1112_CONFIG_GAIN(ADS1112_CONFIG_GAIN_2);
break;
case ADC_GAIN_3:
config |= ADS1112_CONFIG_GAIN(ADS1112_CONFIG_GAIN_4);
break;
case ADC_GAIN_4:
config |= ADS1112_CONFIG_GAIN(ADS1112_CONFIG_GAIN_8);
break;
default:
return -EINVAL;
}
config |= ADS1112_CONFIG_CM(ADS1112_CONFIG_CM_SINGLE); /* Only single shot supported */
return ads1112_write_reg(dev, config);
}
static int ads1112_validate_buffer_size(const struct adc_sequence *sequence)
{
size_t needed = sizeof(int16_t);
if (sequence->options) {
needed *= (1 + sequence->options->extra_samplings);
}
if (sequence->buffer_size < needed) {
LOG_ERR("Insufficient buffer %i < %i", sequence->buffer_size, needed);
return -ENOMEM;
}
return 0;
}
static int ads1112_validate_sequence(const struct device *dev, const struct adc_sequence *sequence)
{
const struct ads1112_data *data = dev->data;
if (sequence->channels != BIT(0)) {
LOG_ERR("Invalid Channel 0x%x", sequence->channels);
return -EINVAL;
}
if (sequence->oversampling) {
LOG_ERR("Oversampling not supported");
return -EINVAL;
}
return ads1112_validate_buffer_size(sequence);
}
static void adc_context_update_buffer_pointer(struct adc_context *ctx, bool repeat_sampling)
{
struct ads1112_data *data = CONTAINER_OF(ctx, struct ads1112_data, ctx);
if (repeat_sampling) {
data->buffer = data->buffer_ptr;
}
}
static void adc_context_start_sampling(struct adc_context *ctx)
{
struct ads1112_data *data = CONTAINER_OF(ctx, struct ads1112_data, ctx);
data->buffer_ptr = data->buffer;
k_sem_give(&data->acq_sem);
}
static int ads1112_adc_start_read(const struct device *dev, const struct adc_sequence *sequence,
bool wait)
{
int rc = 0;
struct ads1112_data *data = dev->data;
rc = ads1112_validate_sequence(dev, sequence);
if (rc != 0) {
return rc;
}
data->buffer = sequence->buffer;
adc_context_start_read(&data->ctx, sequence);
if (wait) {
rc = adc_context_wait_for_completion(&data->ctx);
}
return rc;
}
static int ads1112_adc_perform_read(const struct device *dev)
{
int rc;
struct ads1112_data *data = dev->data;
k_sem_take(&data->acq_sem, K_FOREVER);
rc = ads1112_wait_data_ready(dev);
if (rc != 0) {
adc_context_complete(&data->ctx, rc);
return rc;
}
rc = ads1112_read_sample(dev, data->buffer);
if (rc != 0) {
adc_context_complete(&data->ctx, rc);
return rc;
}
data->buffer++;
adc_context_on_sampling_done(&data->ctx, dev);
return rc;
}
static int ads1112_read(const struct device *dev, const struct adc_sequence *sequence)
{
int rc;
struct ads1112_data *data = dev->data;
adc_context_lock(&data->ctx, false, NULL);
rc = ads1112_adc_start_read(dev, sequence, false);
while (rc == 0 && k_sem_take(&data->ctx.sync, K_NO_WAIT) != 0) {
rc = ads1112_adc_perform_read(dev);
}
adc_context_release(&data->ctx, rc);
return rc;
}
static int ads1112_init(const struct device *dev)
{
int rc = 0;
uint8_t status;
const struct ads1112_config *config = dev->config;
struct ads1112_data *data = dev->data;
adc_context_init(&data->ctx);
k_sem_init(&data->acq_sem, 0, 1);
if (!device_is_ready(config->bus.bus)) {
return -ENODEV;
}
rc = ads1112_write_reg(dev, ADS1112_DEFAULT_CONFIG);
if (rc) {
LOG_ERR("Could not set default config 0x%x", ADS1112_DEFAULT_CONFIG);
return rc;
}
adc_context_unlock_unconditionally(&data->ctx);
return rc;
}
static const struct adc_driver_api api = {
.channel_setup = ads1112_channel_setup,
.read = ads1112_read,
.ref_internal = ADS1112_REF_INTERNAL,
};
#define ADC_ADS1112_INST_DEFINE(n) \
static const struct ads1112_config config_##n = {.bus = I2C_DT_SPEC_INST_GET(n)}; \
static struct ads1112_data data_##n; \
DEVICE_DT_INST_DEFINE(n, ads1112_init, NULL, &data_##n, &config_##n, POST_KERNEL, \
CONFIG_ADC_INIT_PRIORITY, &api);
DT_INST_FOREACH_STATUS_OKAY(ADC_ADS1112_INST_DEFINE);