18
18
#include <em_cmu.h>
19
19
#include <em_gpio.h>
20
20
21
-
22
21
#include <zephyr/logging/log.h>
23
22
LOG_MODULE_REGISTER (adc_gecko , CONFIG_ADC_LOG_LEVEL );
24
23
25
24
#define ADC_GECKO_MAX_RESOLUTION 12
26
- #define ADC_GECKO_CHANNEL_NUM 32
25
+ #define ADC_GECKO_CHANNEL_NUM 32
27
26
28
27
struct adc_gecko_config {
29
28
ADC_TypeDef * base ;
@@ -50,17 +49,17 @@ static int get_resolution(uint8_t resolution)
50
49
{
51
50
52
51
switch (resolution ) {
53
- case 12 :
54
- return adcRes12Bit ;
55
- case 8 :
56
- return adcRes8Bit ;
57
- case 6 :
58
- return adcRes6Bit ;
59
- case 0 :
60
- return adcResOVS ;
61
- default :
62
- LOG_ERR ("ADC resolution value %d is not valid" , resolution );
63
- return - EINVAL ;
52
+ case 12 :
53
+ return adcRes12Bit ;
54
+ case 8 :
55
+ return adcRes8Bit ;
56
+ case 6 :
57
+ return adcRes6Bit ;
58
+ case 0 :
59
+ return adcResOVS ;
60
+ default :
61
+ LOG_ERR ("ADC resolution value %d is not valid" , resolution );
62
+ return - EINVAL ;
64
63
}
65
64
}
66
65
@@ -81,14 +80,15 @@ static inline uint16_t adc_get_result(const struct device *dev)
81
80
return (uint16_t )ADC_DataSingleGet (config -> base );
82
81
}
83
82
84
- static int adc_get_channel_num (uint8_t id ) {
85
- switch (id ) {
86
- case 1 :
87
- return adcPosSelAPORT3XCH10 ;
88
- case 2 :
89
- return adcPosSelAPORT4XCH23 ;
90
- default :
91
- break ;
83
+ static int adc_get_channel_num (uint8_t id )
84
+ {
85
+ switch (id ) {
86
+ case 1 :
87
+ return adcPosSelAPORT3XCH10 ;
88
+ case 2 :
89
+ return adcPosSelAPORT4XCH23 ;
90
+ default :
91
+ break ;
92
92
}
93
93
return -1 ;
94
94
}
@@ -99,7 +99,7 @@ static void adc_select_input(const struct device *dev, uint16_t channel_input)
99
99
struct adc_gecko_data * data = dev -> data ;
100
100
101
101
config -> initSingle_bv .resolution = get_resolution (data -> resolution );
102
- config -> initSingle_bv .posSel = adc_get_channel_num (channel_input + 1 );
102
+ config -> initSingle_bv .posSel = adc_get_channel_num (channel_input + 1 );
103
103
}
104
104
105
105
/**
@@ -112,8 +112,8 @@ static void adc_gecko_irq_handler(const struct device *dev)
112
112
uint16_t result ;
113
113
uint8_t ainsel ;
114
114
115
- uint32_t flags = ADC_IntGet (ADC0 ); // get all interrupt flags
116
- if (flags & ADC_IF_SINGLE ) {
115
+ uint32_t flags = ADC_IntGet (ADC0 ); /* get all interrupt flags */
116
+ if (flags & ADC_IF_SINGLE ) {
117
117
ADC_IntClear (config -> base , flags );
118
118
119
119
/* Fetch result */
@@ -132,32 +132,31 @@ static void adc_gecko_irq_handler(const struct device *dev)
132
132
133
133
/* Kick next channel conversion */
134
134
ainsel = (uint8_t )(find_lsb_set (data -> channels ) - 1 );
135
- adc_select_input (dev ,ainsel );
135
+ adc_select_input (dev , ainsel );
136
136
adc_start_once (dev );
137
137
}
138
-
139
138
}
140
139
141
140
static int get_reference_voltage (int reference )
142
141
{
143
142
int ref ;
144
143
145
144
switch (reference ) {
146
- case ADC_REF_INTERNAL :
147
- ref = adcRef2V5 ;
148
- break ;
149
- case ADC_REF_VDD_1 :
150
- ref = adcRefVDD ;
151
- break ;
152
- case ADC_REF_VDD_1_2 :
153
- ref = adcRef2V5 ;
154
- break ;
155
- case ADC_REF_VDD_1_4 :
156
- ref = adcRef1V25 ;
157
- break ;
158
- default :
159
- ref = -1 ;
160
- break ;
145
+ case ADC_REF_INTERNAL :
146
+ ref = adcRef2V5 ;
147
+ break ;
148
+ case ADC_REF_VDD_1 :
149
+ ref = adcRefVDD ;
150
+ break ;
151
+ case ADC_REF_VDD_1_2 :
152
+ ref = adcRef2V5 ;
153
+ break ;
154
+ case ADC_REF_VDD_1_4 :
155
+ ref = adcRef1V25 ;
156
+ break ;
157
+ default :
158
+ ref = -1 ;
159
+ break ;
161
160
}
162
161
return ref ;
163
162
}
@@ -168,42 +167,42 @@ static int get_acquisition_time(uint16_t acq)
168
167
169
168
/* Check acquisition time */
170
169
switch (acq ) {
171
- case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 1 ):
172
- sample_cycl = adcAcqTime1 ;
173
- break ;
174
- case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 2 ):
175
- sample_cycl = adcAcqTime2 ;
176
- break ;
177
- case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 4 ):
178
- sample_cycl = adcAcqTime4 ;
179
- break ;
180
- case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 8 ):
181
- sample_cycl = adcAcqTime8 ;
182
- break ;
183
- case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 16 ):
184
- sample_cycl = adcAcqTime16 ;
185
- break ;
186
- case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 32 ):
187
- sample_cycl = adcAcqTime32 ;
188
- break ;
189
- case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 64 ):
190
- sample_cycl = adcAcqTime64 ;
191
- break ;
192
- case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 128 ):
193
- sample_cycl = adcAcqTime128 ;
194
- break ;
195
- case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 256 ):
196
- sample_cycl = adcAcqTime256 ;
197
- break ;
198
- default :
199
- sample_cycl = 0 ;
200
- break ;
170
+ case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 1 ):
171
+ sample_cycl = adcAcqTime1 ;
172
+ break ;
173
+ case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 2 ):
174
+ sample_cycl = adcAcqTime2 ;
175
+ break ;
176
+ case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 4 ):
177
+ sample_cycl = adcAcqTime4 ;
178
+ break ;
179
+ case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 8 ):
180
+ sample_cycl = adcAcqTime8 ;
181
+ break ;
182
+ case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 16 ):
183
+ sample_cycl = adcAcqTime16 ;
184
+ break ;
185
+ case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 32 ):
186
+ sample_cycl = adcAcqTime32 ;
187
+ break ;
188
+ case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 64 ):
189
+ sample_cycl = adcAcqTime64 ;
190
+ break ;
191
+ case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 128 ):
192
+ sample_cycl = adcAcqTime128 ;
193
+ break ;
194
+ case ADC_ACQ_TIME (ADC_ACQ_TIME_TICKS , 256 ):
195
+ sample_cycl = adcAcqTime256 ;
196
+ break ;
197
+ default :
198
+ sample_cycl = 0 ;
199
+ break ;
201
200
}
202
201
return sample_cycl ;
203
202
}
204
203
205
204
static int adc_gecko_check_buffer_size (const struct device * dev ,
206
- const struct adc_sequence * sequence )
205
+ const struct adc_sequence * sequence )
207
206
{
208
207
const struct adc_gecko_config * config = dev -> config ;
209
208
uint8_t channels = 0 ;
@@ -228,33 +227,29 @@ static int adc_gecko_check_buffer_size(const struct device *dev,
228
227
return 0 ;
229
228
}
230
229
231
-
232
230
static void adc_context_start_sampling (struct adc_context * ctx )
233
231
{
234
- struct adc_gecko_data * data = CONTAINER_OF (ctx , struct adc_gecko_data ,
235
- ctx );
232
+ struct adc_gecko_data * data = CONTAINER_OF (ctx , struct adc_gecko_data , ctx );
236
233
237
234
data -> channels = ctx -> sequence .channels ;
238
235
data -> repeat_buf = data -> buf ;
239
236
data -> resolution = ctx -> sequence .resolution ;
240
237
/* Find next channel and start conversion */
241
- adc_select_input (data -> dev ,find_lsb_set (data -> channels ) - 1 );
238
+ adc_select_input (data -> dev , find_lsb_set (data -> channels ) - 1 );
242
239
adc_start_once (data -> dev );
243
240
}
244
241
245
- static void adc_context_update_buffer_pointer (struct adc_context * ctx ,
246
- bool repeat_sampling )
242
+ static void adc_context_update_buffer_pointer (struct adc_context * ctx , bool repeat_sampling )
247
243
{
248
- struct adc_gecko_data * data = CONTAINER_OF (ctx , struct adc_gecko_data ,
249
- ctx );
244
+ struct adc_gecko_data * data = CONTAINER_OF (ctx , struct adc_gecko_data , ctx );
250
245
251
246
if (repeat_sampling ) {
252
247
data -> buf = data -> repeat_buf ;
253
248
}
254
249
}
255
250
256
251
static int adc_gecko_channel_setup (const struct device * dev ,
257
- const struct adc_channel_cfg * channel_cfg )
252
+ const struct adc_channel_cfg * channel_cfg )
258
253
{
259
254
struct adc_gecko_config * config = (struct adc_gecko_config * )dev -> config ;
260
255
if (channel_cfg -> channel_id >= config -> num_channels ) {
@@ -274,22 +269,19 @@ static int adc_gecko_channel_setup(const struct device *dev,
274
269
return 0 ;
275
270
}
276
271
277
- static int adc_gecko_start_read (const struct device * dev ,
278
- const struct adc_sequence * sequence )
272
+ static int adc_gecko_start_read (const struct device * dev , const struct adc_sequence * sequence )
279
273
{
280
274
const struct adc_gecko_config * config = dev -> config ;
281
275
struct adc_gecko_data * data = dev -> data ;
282
276
int err ;
283
277
284
- if (sequence -> resolution > ADC_GECKO_MAX_RESOLUTION ||
285
- sequence -> resolution == 0 ) {
278
+ if (sequence -> resolution > ADC_GECKO_MAX_RESOLUTION || sequence -> resolution == 0 ) {
286
279
LOG_ERR ("unsupported resolution %d" , sequence -> resolution );
287
280
return - ENOTSUP ;
288
281
}
289
282
290
283
if (find_msb_set (sequence -> channels ) > config -> num_channels ) {
291
- LOG_ERR ("unsupported channels in mask: 0x%08x" ,
292
- sequence -> channels );
284
+ LOG_ERR ("unsupported channels in mask: 0x%08x" , sequence -> channels );
293
285
return - ENOTSUP ;
294
286
}
295
287
@@ -305,9 +297,8 @@ static int adc_gecko_start_read(const struct device *dev,
305
297
return adc_context_wait_for_completion (& data -> ctx );
306
298
}
307
299
308
- static int adc_gecko_read_async (const struct device * dev ,
309
- const struct adc_sequence * sequence ,
310
- struct k_poll_signal * async )
300
+ static int adc_gecko_read_async (const struct device * dev , const struct adc_sequence * sequence ,
301
+ struct k_poll_signal * async )
311
302
{
312
303
struct adc_gecko_data * data = dev -> data ;
313
304
int err ;
@@ -319,15 +310,14 @@ static int adc_gecko_read_async(const struct device *dev,
319
310
return err ;
320
311
}
321
312
322
- static int adc_gecko_read (const struct device * dev ,
323
- const struct adc_sequence * sequence )
313
+ static int adc_gecko_read (const struct device * dev , const struct adc_sequence * sequence )
324
314
{
325
315
return adc_gecko_read_async (dev , sequence , NULL );
326
316
}
327
317
328
318
#ifdef CONFIG_ADC_ASYNC
329
319
static int adc_gecko_read_async (const struct device * dev , const struct adc_sequence * sequence ,
330
- struct k_poll_signal * async )
320
+ struct k_poll_signal * async )
331
321
{
332
322
return - ENOTSUP ;
333
323
}
@@ -361,32 +351,30 @@ static int adc_gecko_init(const struct device *dev)
361
351
return 0 ;
362
352
}
363
353
364
- #define IRQ_CONFIGURE_FUNC (idx ) \
365
- static void adc_gecko_configure_func_##idx(void) \
366
- { \
367
- IRQ_CONNECT(ADC0_IRQn, 0, \
368
- adc_gecko_irq_handler, DEVICE_DT_INST_GET(idx), 0); \
369
- irq_enable(ADC0_IRQn); \
370
- }
354
+ #define IRQ_CONFIGURE_FUNC (idx ) \
355
+ static void adc_gecko_configure_func_##idx(void) \
356
+ { \
357
+ IRQ_CONNECT(ADC0_IRQn, 0, adc_gecko_irq_handler, DEVICE_DT_INST_GET(idx), 0); \
358
+ irq_enable(ADC0_IRQn); \
359
+ }
371
360
372
361
#define IRQ_CONFIGURE_DEFINE (idx ) .irq_configure = adc_gecko_configure_func_##idx
373
362
374
363
#define GECKO_ADC_INIT (inst ) \
375
- IRQ_CONFIGURE_FUNC(inst) \
376
- static const struct adc_driver_api adc_gecko_api = { \
377
- .channel_setup = adc_gecko_channel_setup, \
378
- .read = adc_gecko_read, \
379
- .ref_internal = DT_INST_PROP(inst, vref_mv), \
380
- IF_ENABLED(CONFIG_ADC_ASYNC, (.read_async = adc_gecko_read_async,)) \
381
- }; \
382
- \
364
+ IRQ_CONFIGURE_FUNC(inst) \
365
+ static const struct adc_driver_api adc_gecko_api = { \
366
+ .channel_setup = adc_gecko_channel_setup, \
367
+ .read = adc_gecko_read, \
368
+ .ref_internal = DT_INST_PROP(inst, vref_mv), \
369
+ IF_ENABLED(CONFIG_ADC_ASYNC, (.read_async = adc_gecko_read_async, ))}; \
370
+ \
383
371
static struct adc_gecko_config adc_gecko_config##inst = { \
384
- .num_channels = ADC_GECKO_CHANNEL_NUM, \
385
- IRQ_CONFIGURE_DEFINE(inst), \
386
- }; \
372
+ .num_channels = ADC_GECKO_CHANNEL_NUM, \
373
+ IRQ_CONFIGURE_DEFINE(inst), \
374
+ }; \
387
375
static struct adc_gecko_data adc_gecko_data##inst; \
388
- \
376
+ \
389
377
DEVICE_DT_INST_DEFINE(inst, &adc_gecko_init, NULL, &adc_gecko_data##inst, \
390
- &adc_gecko_config##inst, POST_KERNEL, \
391
- CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &adc_gecko_api);
378
+ &adc_gecko_config##inst, POST_KERNEL, \
379
+ CONFIG_KERNEL_INIT_PRIORITY_DEVICE, &adc_gecko_api);
392
380
DT_INST_FOREACH_STATUS_OKAY (GECKO_ADC_INIT )
0 commit comments