28
28
// This file is never compiled standalone, it's included directly from
29
29
// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE.
30
30
31
- #if MICROPY_PY_MACHINE_ADC
32
-
33
31
#include <stdint.h>
34
32
#include "py/obj.h"
35
- #include "py/runtime.h"
36
33
#include "py/mperrno.h"
37
34
38
- #include "py/mphal .h"
35
+ #include "mphalport .h"
39
36
#include "sam.h"
40
37
#include "pin_af.h"
41
- #include "modmachine.h"
42
38
#include "samd_soc.h"
43
39
#include "dma_manager.h"
44
40
#include "tc_manager.h"
@@ -50,7 +46,7 @@ typedef struct _machine_adc_obj_t {
50
46
uint8_t avg ;
51
47
uint8_t bits ;
52
48
uint8_t vref ;
53
- #if MICROPY_PY_MACHINE_ADC_TIMED
49
+ #if MICROPY_PY_MACHINE_ADC_READ_TIMED
54
50
int8_t dma_channel ;
55
51
int8_t tc_index ;
56
52
#endif
@@ -76,7 +72,7 @@ static uint8_t adc_vref_table[] = {
76
72
77
73
typedef struct _device_mgmt_t {
78
74
bool init ;
79
- #if MICROPY_PY_MACHINE_ADC_TIMED
75
+ #if MICROPY_PY_MACHINE_ADC_READ_TIMED
80
76
bool busy ;
81
77
mp_obj_t callback ;
82
78
mp_obj_t self ;
@@ -124,7 +120,7 @@ static void adc_init(machine_adc_obj_t *self);
124
120
125
121
extern mp_int_t log2i (mp_int_t num );
126
122
127
- #if MICROPY_PY_MACHINE_ADC_TIMED
123
+ #if MICROPY_PY_MACHINE_ADC_READ_TIMED
128
124
129
125
// Active just for SAMD21, stops the freerun mode
130
126
// For SAMD51, just the INT flag is reset.
@@ -166,16 +162,15 @@ STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_p
166
162
self -> adc_config .channel , self -> bits , 1 << self -> avg , self -> vref );
167
163
}
168
164
169
- STATIC mp_obj_t adc_obj_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw ,
170
- const mp_obj_t * all_args ) {
165
+ STATIC mp_obj_t mp_machine_adc_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
171
166
172
167
enum { ARG_id , ARG_bits , ARG_average , ARG_vref , ARG_callback };
173
168
static const mp_arg_t allowed_args [] = {
174
169
{ MP_QSTR_id , MP_ARG_REQUIRED | MP_ARG_OBJ },
175
170
{ MP_QSTR_bits , MP_ARG_INT , {.u_int = DEFAULT_ADC_BITS } },
176
171
{ MP_QSTR_average , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = DEFAULT_ADC_AVG } },
177
172
{ MP_QSTR_vref , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = DEFAULT_ADC_VREF } },
178
- #if MICROPY_PY_MACHINE_ADC_TIMED
173
+ #if MICROPY_PY_MACHINE_ADC_READ_TIMED
179
174
{ MP_QSTR_callback , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
180
175
#endif
181
176
};
@@ -209,7 +204,7 @@ STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_
209
204
ch_busy_flags |= (1 << (self -> adc_config .device * 16 + self -> adc_config .channel ));
210
205
device_mgmt [self -> adc_config .device ].init = false;
211
206
212
- #if MICROPY_PY_MACHINE_ADC_TIMED
207
+ #if MICROPY_PY_MACHINE_ADC_READ_TIMED
213
208
device_mgmt [adc_config .device ].callback = args [ARG_callback ].u_obj ;
214
209
if (device_mgmt [adc_config .device ].callback == mp_const_none ) {
215
210
device_mgmt [adc_config .device ].callback = MP_OBJ_NULL ;
@@ -231,7 +226,7 @@ STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
231
226
// Set the reference voltage. Default: external AREFA.
232
227
adc -> REFCTRL .reg = adc_vref_table [self -> vref ];
233
228
234
- #if MICROPY_PY_MACHINE_ADC_TIMED
229
+ #if MICROPY_PY_MACHINE_ADC_READ_TIMED
235
230
if (device_mgmt [self -> adc_config .device ].busy != 0 ) {
236
231
mp_raise_OSError (MP_EBUSY );
237
232
}
@@ -260,13 +255,13 @@ STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
260
255
return adc -> RESULT .reg * (65536 / (1 << self -> bits ));
261
256
}
262
257
263
- STATIC void machine_adc_read_timed (mp_obj_t self_in , mp_obj_t values , mp_obj_t freq_in ) {
264
- machine_adc_obj_t * self = self_in ;
258
+ #if MICROPY_PY_MACHINE_ADC_READ_TIMED
259
+
260
+ STATIC void mp_machine_adc_read_timed (machine_adc_obj_t * self , mp_obj_t values , mp_int_t freq ) {
265
261
Adc * adc = adc_bases [self -> adc_config .device ];
266
262
mp_buffer_info_t src ;
267
263
mp_get_buffer_raise (values , & src , MP_BUFFER_READ );
268
264
if (src .len >= 2 ) {
269
- int freq = mp_obj_get_int (freq_in );
270
265
if (self -> tc_index == -1 ) {
271
266
self -> tc_index = allocate_tc_instance ();
272
267
}
@@ -359,12 +354,19 @@ STATIC void machine_adc_read_timed(mp_obj_t self_in, mp_obj_t values, mp_obj_t f
359
354
#endif // defined SAMD21 or SAMD51
360
355
361
356
}
362
- return mp_const_none ;
363
357
}
364
358
359
+ // busy() : Report, if the ADC device is busy
360
+ STATIC mp_obj_t mp_machine_adc_busy (machine_adc_obj_t * self ) {
361
+ return device_mgmt [self -> adc_config .device ].busy ? mp_const_true : mp_const_false ;
362
+ }
363
+
364
+ #endif
365
+
365
366
// deinit() : release the ADC channel
366
367
STATIC void mp_machine_adc_deinit (machine_adc_obj_t * self ) {
367
- busy_flags &= ~((1 << (self -> adc_config .device * 16 + self -> adc_config .channel )));
368
+ ch_busy_flags &= ~((1 << (self -> adc_config .device * 16 + self -> adc_config .channel )));
369
+ #if MICROPY_PY_MACHINE_ADC_READ_TIMED
368
370
if (self -> dma_channel >= 0 ) {
369
371
#if defined(MCU_SAMD51 )
370
372
if (self -> dma_channel == device_mgmt [self -> adc_config .device ].dma_channel ) {
@@ -380,17 +382,10 @@ STATIC void mp_machine_adc_deinit(machine_adc_obj_t *self) {
380
382
free_tc_instance (self -> tc_index );
381
383
self -> tc_index = -1 ;
382
384
}
385
+ #endif
383
386
}
384
387
385
- // busy() : Report, if the ADC device is busy
386
- STATIC mp_int_t machine_adc_busy (mp_obj_t self_in ) {
387
- machine_adc_obj_t * self = MP_OBJ_TO_PTR (self_in );
388
- return device_mgmt [self -> adc_config .device ].busy ? true : false;
389
- }
390
-
391
- #endif
392
-
393
- #if MICROPY_PY_MACHINE_ADC_TIMED
388
+ #if MICROPY_PY_MACHINE_ADC_READ_TIMED
394
389
void adc_deinit_all (void ) {
395
390
ch_busy_flags = 0 ;
396
391
device_mgmt [0 ].init = 0 ;
0 commit comments