forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc_cc32xx.c
328 lines (272 loc) · 7.94 KB
/
adc_cc32xx.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
/*
* Copyright (c) 2021 Pavlo Hamov <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0
*/
#define DT_DRV_COMPAT ti_cc32xx_adc
#include <errno.h>
#include <zephyr/drivers/adc.h>
#include <zephyr/device.h>
#include <zephyr/kernel.h>
#include <zephyr/init.h>
#include <soc.h>
/* Driverlib includes */
#include <inc/hw_types.h>
#include <driverlib/pin.h>
#include <driverlib/rom.h>
#include <driverlib/rom_map.h>
#include <driverlib/prcm.h>
#include <driverlib/adc.h>
#define CHAN_COUNT 4
#define ADC_CONTEXT_USES_KERNEL_TIMER
#include "adc_context.h"
#define LOG_LEVEL CONFIG_ADC_LOG_LEVEL
#include <zephyr/logging/log.h>
#include <zephyr/irq.h>
LOG_MODULE_REGISTER(adc_cc32xx);
#define ISR_MASK (ADC_DMA_DONE | ADC_FIFO_OVERFLOW | ADC_FIFO_UNDERFLOW \
| ADC_FIFO_EMPTY | ADC_FIFO_FULL)
struct adc_cc32xx_data {
struct adc_context ctx;
const struct device *dev;
uint16_t *buffer;
uint16_t *repeat_buffer;
uint32_t channels;
uint8_t offset[CHAN_COUNT];
size_t active_channels;
};
struct adc_cc32xx_cfg {
unsigned long base;
void (*irq_cfg_func)(void);
};
static const int s_chPin[CHAN_COUNT] = {
PIN_57,
PIN_58,
PIN_59,
PIN_60,
};
static const int s_channel[CHAN_COUNT] = {
ADC_CH_0,
ADC_CH_1,
ADC_CH_2,
ADC_CH_3,
};
static inline void start_sampling(unsigned long base, int ch)
{
MAP_ADCChannelEnable(base, ch);
for (int i = 0; i < 5; i++) {
while (!MAP_ADCFIFOLvlGet(base, ch)) {
}
MAP_ADCFIFORead(base, ch);
}
MAP_ADCIntClear(base, ch, ISR_MASK);
MAP_ADCIntEnable(base, ch, ISR_MASK);
}
static void adc_context_start_sampling(struct adc_context *ctx)
{
struct adc_cc32xx_data *data =
CONTAINER_OF(ctx, struct adc_cc32xx_data, ctx);
const struct adc_cc32xx_cfg *config = data->dev->config;
data->channels = ctx->sequence.channels;
data->repeat_buffer = data->buffer;
for (int i = 0; i < CHAN_COUNT; ++i) {
if (ctx->sequence.channels & BIT(i)) {
start_sampling(config->base, s_channel[i]);
}
}
}
static void adc_context_update_buffer_pointer(struct adc_context *ctx,
bool repeat)
{
struct adc_cc32xx_data *data =
CONTAINER_OF(ctx, struct adc_cc32xx_data, ctx);
if (repeat) {
data->buffer = data->repeat_buffer;
} else {
data->buffer += data->active_channels;
}
}
static int adc_cc32xx_init(const struct device *dev)
{
struct adc_cc32xx_data *data = dev->data;
const struct adc_cc32xx_cfg *config = dev->config;
data->dev = dev;
LOG_DBG("Initializing....");
for (int i = 0; i < CHAN_COUNT; ++i) {
const int ch = s_channel[i];
MAP_ADCIntDisable(config->base, ch, ISR_MASK);
MAP_ADCChannelDisable(config->base, ch);
MAP_ADCDMADisable(config->base, ch);
MAP_ADCIntClear(config->base, ch, ISR_MASK);
}
MAP_ADCEnable(config->base);
config->irq_cfg_func();
adc_context_unlock_unconditionally(&data->ctx);
return 0;
}
static int adc_cc32xx_channel_setup(const struct device *dev,
const struct adc_channel_cfg *channel_cfg)
{
const struct adc_cc32xx_cfg *config = dev->config;
const uint8_t ch = channel_cfg->channel_id;
if (ch >= CHAN_COUNT) {
LOG_ERR("Channel %d is not supported, max %d", ch, CHAN_COUNT);
return -EINVAL;
}
if (channel_cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
LOG_ERR("Acquisition time is not valid");
return -EINVAL;
}
if (channel_cfg->differential) {
LOG_ERR("Differential channels are not supported");
return -EINVAL;
}
if (channel_cfg->gain != ADC_GAIN_1) {
LOG_ERR("Gain is not valid");
return -EINVAL;
}
if (channel_cfg->reference != ADC_REF_INTERNAL) {
LOG_ERR("Reference is not valid");
return -EINVAL;
}
LOG_DBG("Setup %d", ch);
MAP_ADCChannelDisable(config->base, s_channel[ch]);
MAP_ADCIntDisable(config->base, s_channel[ch], ISR_MASK);
MAP_PinDirModeSet(s_chPin[ch], PIN_DIR_MODE_IN);
MAP_PinTypeADC(s_chPin[ch], PIN_MODE_255);
return 0;
}
static int cc32xx_read(const struct device *dev,
const struct adc_sequence *sequence,
bool asynchronous,
struct k_poll_signal *sig)
{
struct adc_cc32xx_data *data = dev->data;
int rv;
size_t exp_size;
if (sequence->resolution != 12) {
LOG_ERR("Only 12 Resolution is supported, but %d got",
sequence->resolution);
return -EINVAL;
}
data->active_channels = 0;
for (int i = 0; i < CHAN_COUNT; ++i) {
if (!(sequence->channels & BIT(i))) {
continue;
}
data->offset[i] = data->active_channels++;
}
exp_size = data->active_channels * sizeof(uint16_t);
if (sequence->options) {
exp_size *= (1 + sequence->options->extra_samplings);
}
if (sequence->buffer_size < exp_size) {
LOG_ERR("Required buffer size is %u, but %u got",
exp_size, sequence->buffer_size);
return -ENOMEM;
}
data->buffer = sequence->buffer;
adc_context_lock(&data->ctx, asynchronous, sig);
adc_context_start_read(&data->ctx, sequence);
rv = adc_context_wait_for_completion(&data->ctx);
adc_context_release(&data->ctx, rv);
return rv;
}
static int adc_cc32xx_read(const struct device *dev,
const struct adc_sequence *sequence)
{
return cc32xx_read(dev, sequence, false, NULL);
}
#ifdef CONFIG_ADC_ASYNC
static int adc_cc32xx_read_async(const struct device *dev,
const struct adc_sequence *sequence,
struct k_poll_signal *async)
{
return cc32xx_read(dev, sequence, true, async);
}
#endif
static void adc_cc32xx_isr(const struct device *dev, int no)
{
const struct adc_cc32xx_cfg *config = dev->config;
struct adc_cc32xx_data *data = dev->data;
const int chan = s_channel[no];
unsigned long mask = MAP_ADCIntStatus(config->base, chan);
int cnt = 0;
int rv = 0;
MAP_ADCIntClear(config->base, chan, mask);
if ((mask & ADC_FIFO_EMPTY) || !(mask & ADC_FIFO_FULL)) {
return;
}
while (MAP_ADCFIFOLvlGet(config->base, chan)) {
rv += (MAP_ADCFIFORead(config->base, chan) >> 2) & 0x0FFF;
cnt++;
}
*(data->buffer + data->offset[no]) = rv / cnt;
data->channels &= ~BIT(no);
MAP_ADCIntDisable(config->base, chan, ISR_MASK);
MAP_ADCChannelDisable(config->base, chan);
LOG_DBG("ISR %d, 0x%lX %d %d", chan, mask, rv, cnt);
if (!data->channels) {
adc_context_on_sampling_done(&data->ctx, dev);
}
}
static void adc_cc32xx_isr_ch0(const struct device *dev)
{
adc_cc32xx_isr(dev, 0);
}
static void adc_cc32xx_isr_ch1(const struct device *dev)
{
adc_cc32xx_isr(dev, 1);
}
static void adc_cc32xx_isr_ch2(const struct device *dev)
{
adc_cc32xx_isr(dev, 2);
}
static void adc_cc32xx_isr_ch3(const struct device *dev)
{
adc_cc32xx_isr(dev, 3);
}
static const struct adc_driver_api cc32xx_driver_api = {
.channel_setup = adc_cc32xx_channel_setup,
.read = adc_cc32xx_read,
#ifdef CONFIG_ADC_ASYNC
.read_async = adc_cc32xx_read_async,
#endif
.ref_internal = 1467,
};
#define cc32xx_ADC_IRQ_CONNECT(index, chan) \
do { \
IRQ_CONNECT(DT_INST_IRQ_BY_IDX(index, chan, irq), \
DT_INST_IRQ_BY_IDX(index, chan, priority), \
adc_cc32xx_isr_ch##chan, \
DEVICE_DT_INST_GET(index), 0); \
irq_enable(DT_INST_IRQ_BY_IDX(index, chan, irq)); \
} while (false)
#define cc32xx_ADC_INIT(index) \
\
static void adc_cc32xx_cfg_func_##index(void); \
\
static const struct adc_cc32xx_cfg adc_cc32xx_cfg_##index = { \
.base = DT_INST_REG_ADDR(index), \
.irq_cfg_func = adc_cc32xx_cfg_func_##index, \
}; \
static struct adc_cc32xx_data adc_cc32xx_data_##index = { \
ADC_CONTEXT_INIT_TIMER(adc_cc32xx_data_##index, ctx), \
ADC_CONTEXT_INIT_LOCK(adc_cc32xx_data_##index, ctx), \
ADC_CONTEXT_INIT_SYNC(adc_cc32xx_data_##index, ctx), \
}; \
\
DEVICE_DT_INST_DEFINE(index, \
&adc_cc32xx_init, NULL, &adc_cc32xx_data_##index, \
&adc_cc32xx_cfg_##index, POST_KERNEL, \
CONFIG_ADC_INIT_PRIORITY, \
&cc32xx_driver_api); \
\
static void adc_cc32xx_cfg_func_##index(void) \
{ \
cc32xx_ADC_IRQ_CONNECT(index, 0); \
cc32xx_ADC_IRQ_CONNECT(index, 1); \
cc32xx_ADC_IRQ_CONNECT(index, 2); \
cc32xx_ADC_IRQ_CONNECT(index, 3); \
}
DT_INST_FOREACH_STATUS_OKAY(cc32xx_ADC_INIT)