Skip to content

Commit 059490e

Browse files
committed
samd/machine_adc.c: Factor out machine.adc_timed().
After machine.ADC has been moved to extmod/machine_adc.c. Adding adc.read_timed() and adc.busy() to extmod/machine_adc.c with a corresponding flag to enable them. adc.read_times() and adc.busy() are by default enabled only for SAMD51 devices and SAMD21 devices with an external flash for the file system. Side change: Fix a type in pin_af.c, preventing to use a second ADC channel. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent 4c396d1 commit 059490e

File tree

5 files changed

+68
-38
lines changed

5 files changed

+68
-38
lines changed

extmod/machine_adc.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ STATIC mp_obj_t machine_adc_read(mp_obj_t self_in) {
140140
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_obj, machine_adc_read);
141141
#endif
142142

143+
#if MICROPY_PY_MACHINE_ADC_READ_TIMED
144+
// ADC.atten(value) -- this is a legacy method.
145+
STATIC mp_obj_t machine_adc_read_timed(mp_obj_t self_in, mp_obj_t values, mp_obj_t freq_in) {
146+
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
147+
mp_int_t freq = mp_obj_get_int(freq_in);
148+
mp_machine_adc_read_timed(self, values, freq);
149+
return mp_const_none;
150+
}
151+
STATIC MP_DEFINE_CONST_FUN_OBJ_3(machine_adc_read_timed_obj, machine_adc_read_timed);
152+
153+
// ADC.busy())
154+
STATIC mp_obj_t machine_adc_busy(mp_obj_t self_in) {
155+
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
156+
return mp_machine_adc_busy(self);
157+
}
158+
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_busy_obj, machine_adc_busy);
159+
#endif
160+
143161
STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
144162
#if MICROPY_PY_MACHINE_ADC_INIT
145163
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_adc_init_obj) },
@@ -164,6 +182,10 @@ STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = {
164182
#if MICROPY_PY_MACHINE_ADC_READ
165183
{ MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_adc_read_obj) },
166184
#endif
185+
#if MICROPY_PY_MACHINE_ADC_READ_TIMED
186+
{ MP_ROM_QSTR(MP_QSTR_read_timed), MP_ROM_PTR(&machine_adc_read_timed_obj) },
187+
{ MP_ROM_QSTR(MP_QSTR_busy), MP_ROM_PTR(&machine_adc_busy_obj) },
188+
#endif
167189

168190
// A port must add ADC class constants defining the following macro.
169191
// It can be defined to nothing if there are no constants.

ports/samd/machine_adc.c

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,13 @@
2828
// This file is never compiled standalone, it's included directly from
2929
// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE.
3030

31-
#if MICROPY_PY_MACHINE_ADC
32-
3331
#include <stdint.h>
3432
#include "py/obj.h"
35-
#include "py/runtime.h"
3633
#include "py/mperrno.h"
3734

38-
#include "py/mphal.h"
35+
#include "mphalport.h"
3936
#include "sam.h"
4037
#include "pin_af.h"
41-
#include "modmachine.h"
4238
#include "samd_soc.h"
4339
#include "dma_manager.h"
4440
#include "tc_manager.h"
@@ -50,7 +46,7 @@ typedef struct _machine_adc_obj_t {
5046
uint8_t avg;
5147
uint8_t bits;
5248
uint8_t vref;
53-
#if MICROPY_PY_MACHINE_ADC_TIMED
49+
#if MICROPY_PY_MACHINE_ADC_READ_TIMED
5450
int8_t dma_channel;
5551
int8_t tc_index;
5652
#endif
@@ -76,7 +72,7 @@ static uint8_t adc_vref_table[] = {
7672

7773
typedef struct _device_mgmt_t {
7874
bool init;
79-
#if MICROPY_PY_MACHINE_ADC_TIMED
75+
#if MICROPY_PY_MACHINE_ADC_READ_TIMED
8076
bool busy;
8177
mp_obj_t callback;
8278
mp_obj_t self;
@@ -124,7 +120,7 @@ static void adc_init(machine_adc_obj_t *self);
124120

125121
extern mp_int_t log2i(mp_int_t num);
126122

127-
#if MICROPY_PY_MACHINE_ADC_TIMED
123+
#if MICROPY_PY_MACHINE_ADC_READ_TIMED
128124

129125
// Active just for SAMD21, stops the freerun mode
130126
// 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
166162
self->adc_config.channel, self->bits, 1 << self->avg, self->vref);
167163
}
168164

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) {
171166

172167
enum { ARG_id, ARG_bits, ARG_average, ARG_vref, ARG_callback };
173168
static const mp_arg_t allowed_args[] = {
174169
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ },
175170
{ MP_QSTR_bits, MP_ARG_INT, {.u_int = DEFAULT_ADC_BITS} },
176171
{ MP_QSTR_average, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = DEFAULT_ADC_AVG} },
177172
{ 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
179174
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
180175
#endif
181176
};
@@ -209,7 +204,7 @@ STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_
209204
ch_busy_flags |= (1 << (self->adc_config.device * 16 + self->adc_config.channel));
210205
device_mgmt[self->adc_config.device].init = false;
211206

212-
#if MICROPY_PY_MACHINE_ADC_TIMED
207+
#if MICROPY_PY_MACHINE_ADC_READ_TIMED
213208
device_mgmt[adc_config.device].callback = args[ARG_callback].u_obj;
214209
if (device_mgmt[adc_config.device].callback == mp_const_none) {
215210
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) {
231226
// Set the reference voltage. Default: external AREFA.
232227
adc->REFCTRL.reg = adc_vref_table[self->vref];
233228

234-
#if MICROPY_PY_MACHINE_ADC_TIMED
229+
#if MICROPY_PY_MACHINE_ADC_READ_TIMED
235230
if (device_mgmt[self->adc_config.device].busy != 0) {
236231
mp_raise_OSError(MP_EBUSY);
237232
}
@@ -260,13 +255,13 @@ STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) {
260255
return adc->RESULT.reg * (65536 / (1 << self->bits));
261256
}
262257

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) {
265261
Adc *adc = adc_bases[self->adc_config.device];
266262
mp_buffer_info_t src;
267263
mp_get_buffer_raise(values, &src, MP_BUFFER_READ);
268264
if (src.len >= 2) {
269-
int freq = mp_obj_get_int(freq_in);
270265
if (self->tc_index == -1) {
271266
self->tc_index = allocate_tc_instance();
272267
}
@@ -359,12 +354,19 @@ STATIC void machine_adc_read_timed(mp_obj_t self_in, mp_obj_t values, mp_obj_t f
359354
#endif // defined SAMD21 or SAMD51
360355

361356
}
362-
return mp_const_none;
363357
}
364358

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+
365366
// deinit() : release the ADC channel
366367
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
368370
if (self->dma_channel >= 0) {
369371
#if defined(MCU_SAMD51)
370372
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) {
380382
free_tc_instance(self->tc_index);
381383
self->tc_index = -1;
382384
}
385+
#endif
383386
}
384387

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
394389
void adc_deinit_all(void) {
395390
ch_busy_flags = 0;
396391
device_mgmt[0].init = 0;

ports/samd/mcu/samd21/mpconfigmcu.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ unsigned long trng_random_u32(int delay);
6363

6464
#define VFS_BLOCK_SIZE_BYTES (1536) // 24x 64B flash pages;
6565

66+
#ifndef MICROPY_PY_MACHINE_PIN_BOARD_CPU
67+
#define MICROPY_PY_MACHINE_PIN_BOARD_CPU (1)
68+
#endif
69+
70+
#if MICROPY_HW_CODESIZE > 184
71+
#ifndef MICROPY_PY_MACHINE_ADC_READ_TIMED
72+
#define MICROPY_PY_MACHINE_ADC_READ_TIMED (1)
73+
#endif
74+
#ifndef MICROPY_PY_MACHINE_DAC_TIMED
75+
#define MICROPY_PY_MACHINE_DAC_TIMED (1)
76+
#endif
77+
#endif
78+
6679
#define CPU_FREQ (48000000)
6780
#define DFLL48M_FREQ (48000000)
6881
#define MAX_CPU_FREQ (54000000)

ports/samd/mcu/samd51/mpconfigmcu.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
unsigned long trng_random_u32(void);
3333

3434
// fatfs configuration used in ffconf.h
35-
#define MICROPY_FATFS_ENABLE_LFN (1)
36-
#define MICROPY_FATFS_RPATH (2)
37-
#define MICROPY_FATFS_MAX_SS (4096)
38-
#define MICROPY_FATFS_LFN_CODE_PAGE 437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
35+
#define MICROPY_FATFS_ENABLE_LFN (1)
36+
#define MICROPY_FATFS_RPATH (2)
37+
#define MICROPY_FATFS_MAX_SS (4096)
38+
#define MICROPY_FATFS_LFN_CODE_PAGE 437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
3939

4040
#define VFS_BLOCK_SIZE_BYTES (1536) //
4141

@@ -45,6 +45,12 @@ unsigned long trng_random_u32(void);
4545
#ifndef MICROPY_HW_UART_RTSCTS
4646
#define MICROPY_HW_UART_RTSCTS (1)
4747
#endif
48+
#ifndef MICROPY_PY_MACHINE_ADC_READ_TIMED
49+
#define MICROPY_PY_MACHINE_ADC_READ_TIMED (1)
50+
#endif
51+
#ifndef MICROPY_PY_MACHINE_DAC_TIMED
52+
#define MICROPY_PY_MACHINE_DAC_TIMED (1)
53+
#endif
4854

4955
#define CPU_FREQ (120000000)
5056
#define DFLL48M_FREQ (48000000)

ports/samd/mpconfigport.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,7 @@
151151
#define MICROPY_PY_PLATFORM (1)
152152
#define MICROPY_PLATFORM_VERSION "ASF4"
153153

154-
#ifndef MICROPY_PY_MACHINE_DAC_TIMED
155-
#define MICROPY_PY_MACHINE_DAC_TIMED (1)
156-
#endif
157-
#ifndef MICROPY_PY_MACHINE_ADC_TIMED
158-
#define MICROPY_PY_MACHINE_ADC_TIMED (1)
159-
#endif
160-
#if MICROPY_PY_MACHINE_DAC_TIMED || MICROPY_PY_MACHINE_ADC_TIMED
154+
#if MICROPY_PY_MACHINE_DAC_TIMED || MICROPY_PY_MACHINE_ADC_READ_TIMED
161155
#define MICROPY_HW_DMA_MANAGER (1)
162156
#define MICROPY_HW_TC_MANAGER (1)
163157
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy