Skip to content

Commit d59e450

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. Signed-off-by: robert-hh <robert@hammelrath.com>
1 parent f1e20ee commit d59e450

File tree

6 files changed

+65
-39
lines changed

6 files changed

+65
-39
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/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ CFLAGS += $(INC) -Wall -Werror -std=c99 -nostdlib -mthumb $(CFLAGS_MCU) -fsingle
6565
CFLAGS += -DMCU_$(MCU_SERIES) -D__$(CMSIS_MCU)__
6666
CFLAGS += $(CFLAGS_EXTRA)
6767

68-
CFLAGS += -DMICROPY_HW_CODESIZE=$(MICROPY_HW_CODESIZE)
68+
CFLAGS += -DMICROPY_HW_CODESIZE=$(strip $(subst K,' ', $(MICROPY_HW_CODESIZE)))
6969

7070
LDFLAGS += -nostdlib $(addprefix -T,$(LD_FILES)) -Map=$@.map --cref
7171
LDFLAGS += --defsym=_codesize=$(MICROPY_HW_CODESIZE)

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
}
@@ -357,12 +352,19 @@ STATIC void machine_adc_read_timed(mp_obj_t self_in, mp_obj_t values, mp_obj_t f
357352
#endif // defined SAMD21 or SAMD51
358353

359354
}
360-
return mp_const_none;
361355
}
362356

357+
// busy() : Report, if the ADC device is busy
358+
STATIC mp_obj_t mp_machine_adc_busy(machine_adc_obj_t *self) {
359+
return device_mgmt[self->adc_config.device].busy ? mp_const_true : mp_const_false;
360+
}
361+
362+
#endif
363+
363364
// deinit() : release the ADC channel
364365
STATIC void mp_machine_adc_deinit(machine_adc_obj_t *self) {
365-
busy_flags &= ~((1 << (self->adc_config.device * 16 + self->adc_config.channel)));
366+
ch_busy_flags &= ~((1 << (self->adc_config.device * 16 + self->adc_config.channel)));
367+
#if MICROPY_PY_MACHINE_ADC_READ_TIMED
366368
if (self->dma_channel >= 0) {
367369
#if defined(MCU_SAMD51)
368370
if (self->dma_channel == device_mgmt[self->adc_config.device].dma_channel) {
@@ -378,17 +380,10 @@ STATIC void mp_machine_adc_deinit(machine_adc_obj_t *self) {
378380
free_tc_instance(self->tc_index);
379381
self->tc_index = -1;
380382
}
383+
#endif
381384
}
382385

383-
// busy() : Report, if the ADC device is busy
384-
STATIC mp_int_t machine_adc_busy(mp_obj_t self_in) {
385-
machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in);
386-
return device_mgmt[self->adc_config.device].busy ? true : false;
387-
}
388-
389-
#endif
390-
391-
#if MICROPY_PY_MACHINE_ADC_TIMED
386+
#if MICROPY_PY_MACHINE_ADC_READ_TIMED
392387
void adc_deinit_all(void) {
393388
ch_busy_flags = 0;
394389
device_mgmt[0].init = 0;

ports/samd/mcu/samd21/mpconfigmcu.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,15 @@ unsigned long trng_random_u32(int delay);
4141
#define MICROPY_PY_MACHINE_PIN_BOARD_CPU (1)
4242
#endif
4343

44+
#if MICROPY_HW_CODESIZE > 184
45+
#ifndef MICROPY_PY_MACHINE_ADC_READ_TIMED
46+
#define MICROPY_PY_MACHINE_ADC_READ_TIMED (1)
47+
#endif
48+
#ifndef MICROPY_PY_MACHINE_DAC_TIMED
49+
#define MICROPY_PY_MACHINE_DAC_TIMED (1)
50+
#endif
51+
#endif
52+
4453
#define CPU_FREQ (48000000)
4554
#define DFLL48M_FREQ (48000000)
4655
#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
@@ -35,10 +35,10 @@ unsigned long trng_random_u32(void);
3535
#endif
3636

3737
// fatfs configuration used in ffconf.h
38-
#define MICROPY_FATFS_ENABLE_LFN (1)
39-
#define MICROPY_FATFS_RPATH (2)
40-
#define MICROPY_FATFS_MAX_SS (4096)
41-
#define MICROPY_FATFS_LFN_CODE_PAGE 437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
38+
#define MICROPY_FATFS_ENABLE_LFN (1)
39+
#define MICROPY_FATFS_RPATH (2)
40+
#define MICROPY_FATFS_MAX_SS (4096)
41+
#define MICROPY_FATFS_LFN_CODE_PAGE 437 /* 1=SFN/ANSI 437=LFN/U.S.(OEM) */
4242

4343
#define VFS_BLOCK_SIZE_BYTES (1536) //
4444

@@ -48,6 +48,12 @@ unsigned long trng_random_u32(void);
4848
#ifndef MICROPY_HW_UART_RTSCTS
4949
#define MICROPY_HW_UART_RTSCTS (1)
5050
#endif
51+
#ifndef MICROPY_PY_MACHINE_ADC_READ_TIMED
52+
#define MICROPY_PY_MACHINE_ADC_READ_TIMED (1)
53+
#endif
54+
#ifndef MICROPY_PY_MACHINE_DAC_TIMED
55+
#define MICROPY_PY_MACHINE_DAC_TIMED (1)
56+
#endif
5157

5258
#define CPU_FREQ (120000000)
5359
#define DFLL48M_FREQ (48000000)

ports/samd/mpconfigport.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,7 @@
136136
#define MICROPY_PY_PLATFORM (1)
137137
#define MICROPY_PLATFORM_VERSION "ASF4"
138138

139-
#ifndef MICROPY_PY_MACHINE_DAC_TIMED
140-
#define MICROPY_PY_MACHINE_DAC_TIMED (1)
141-
#endif
142-
#ifndef MICROPY_PY_MACHINE_ADC_TIMED
143-
#define MICROPY_PY_MACHINE_ADC_TIMED (1)
144-
#endif
145-
#if MICROPY_PY_MACHINE_DAC_TIMED || MICROPY_PY_MACHINE_ADC_TIMED
139+
#if MICROPY_PY_MACHINE_DAC_TIMED || MICROPY_PY_MACHINE_ADC_READ_TIMED
146140
#define MICROPY_HW_DMA_MANAGER (1)
147141
#define MICROPY_HW_TC_MANAGER (1)
148142
#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