From 95d8b5fd555ba26ebd79b29fade45d2bf0abc6c4 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 11 Oct 2023 13:48:49 +1100 Subject: [PATCH] extmod/machine_adc: Factor ports' ADC Python bindings to common code. No functional change, just code factoring to have the Python bindings in one location, and all the ports use those same bindings. Signed-off-by: Damien George --- extmod/extmod.cmake | 1 + extmod/extmod.mk | 1 + extmod/machine_adc.c | 183 +++++++++++++++++++++++++ extmod/modmachine.h | 38 +++++ ports/esp32/esp32_common.cmake | 1 - ports/esp32/machine_adc.c | 146 ++++++++------------ ports/esp32/machine_adc.h | 8 +- ports/esp32/machine_adcblock.c | 2 +- ports/esp32/modmachine.c | 2 + ports/esp32/modmachine.h | 1 - ports/esp32/mpconfigport.h | 7 + ports/esp8266/Makefile | 1 - ports/esp8266/machine_adc.c | 43 ++---- ports/esp8266/modmachine.c | 2 + ports/esp8266/modmachine.h | 1 - ports/esp8266/mpconfigport.h | 3 + ports/mimxrt/Makefile | 1 - ports/mimxrt/machine_adc.c | 45 ++---- ports/mimxrt/modmachine.c | 2 + ports/mimxrt/modmachine.h | 1 - ports/mimxrt/mpconfigport.h | 2 + ports/nrf/Makefile | 1 - ports/nrf/modules/machine/adc.c | 55 ++------ ports/nrf/modules/machine/adc.h | 2 - ports/nrf/modules/machine/modmachine.c | 3 - ports/nrf/mpconfigport.h | 1 + ports/renesas-ra/Makefile | 1 - ports/renesas-ra/machine_adc.c | 55 +++----- ports/renesas-ra/modmachine.c | 2 + ports/renesas-ra/modmachine.h | 1 - ports/renesas-ra/mpconfigport.h | 3 + ports/rp2/CMakeLists.txt | 1 - ports/rp2/machine_adc.c | 35 ++--- ports/rp2/modmachine.c | 2 + ports/rp2/modmachine.h | 1 - ports/rp2/mpconfigport.h | 2 + ports/samd/Makefile | 1 - ports/samd/machine_adc.c | 48 ++----- ports/samd/modmachine.h | 3 - ports/samd/mpconfigport.h | 2 + ports/stm32/Makefile | 1 - ports/stm32/machine_adc.c | 59 ++++---- ports/stm32/mboot/Makefile | 2 +- ports/stm32/mboot/adc.c | 3 + ports/stm32/modmachine.c | 2 + ports/stm32/modmachine.h | 1 - ports/stm32/mpconfigport.h | 2 + 47 files changed, 434 insertions(+), 346 deletions(-) create mode 100644 extmod/machine_adc.c create mode 100644 ports/stm32/mboot/adc.c diff --git a/extmod/extmod.cmake b/extmod/extmod.cmake index ccb6f0465bff..6f01bafe4639 100644 --- a/extmod/extmod.cmake +++ b/extmod/extmod.cmake @@ -7,6 +7,7 @@ set(MICROPY_SOURCE_EXTMOD ${MICROPY_DIR}/shared/libc/abort_.c ${MICROPY_DIR}/shared/libc/printf.c ${MICROPY_EXTMOD_DIR}/btstack/modbluetooth_btstack.c + ${MICROPY_EXTMOD_DIR}/machine_adc.c ${MICROPY_EXTMOD_DIR}/machine_bitstream.c ${MICROPY_EXTMOD_DIR}/machine_i2c.c ${MICROPY_EXTMOD_DIR}/machine_i2s.c diff --git a/extmod/extmod.mk b/extmod/extmod.mk index 44b41f5ffb09..76dfbf0a45a4 100644 --- a/extmod/extmod.mk +++ b/extmod/extmod.mk @@ -2,6 +2,7 @@ # and provides rules to build 3rd-party components for extmod modules. SRC_EXTMOD_C += \ + extmod/machine_adc.c \ extmod/machine_bitstream.c \ extmod/machine_i2c.c \ extmod/machine_i2s.c \ diff --git a/extmod/machine_adc.c b/extmod/machine_adc.c new file mode 100644 index 000000000000..9849163de875 --- /dev/null +++ b/extmod/machine_adc.c @@ -0,0 +1,183 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2023 Damien P. George + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "py/runtime.h" + +#if MICROPY_PY_MACHINE_ADC + +#include "extmod/modmachine.h" + +// The port must provide implementations of these low-level ADC functions. + +STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind); +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 *args); +STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self); + +#if MICROPY_PY_MACHINE_ADC_INIT +STATIC void mp_machine_adc_init_helper(machine_adc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args); +#endif + +#if MICROPY_PY_MACHINE_ADC_DEINIT +STATIC void mp_machine_adc_deinit(machine_adc_obj_t *self); +#endif + +#if MICROPY_PY_MACHINE_ADC_BLOCK +STATIC mp_obj_t mp_machine_adc_block(machine_adc_obj_t *self); +#endif + +#if MICROPY_PY_MACHINE_ADC_READ_UV +STATIC mp_int_t mp_machine_adc_read_uv(machine_adc_obj_t *self); +#endif + +#if MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH +STATIC void mp_machine_adc_atten_set(machine_adc_obj_t *self, mp_int_t atten); +STATIC void mp_machine_adc_width_set(machine_adc_obj_t *self, mp_int_t width); +#endif + +#if MICROPY_PY_MACHINE_ADC_READ +STATIC mp_int_t mp_machine_adc_read(machine_adc_obj_t *self); +#endif + +// The port provides implementations of the above in this file. +#include MICROPY_PY_MACHINE_ADC_INCLUDEFILE + +#if MICROPY_PY_MACHINE_ADC_INIT +// ADC.init(...) +STATIC mp_obj_t machine_adc_init(size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + machine_adc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); + mp_machine_adc_init_helper(self, n_pos_args - 1, pos_args + 1, kw_args); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_adc_init_obj, 1, machine_adc_init); +#endif + +#if MICROPY_PY_MACHINE_ADC_DEINIT +// ADC.deinit() +STATIC mp_obj_t machine_adc_deinit(mp_obj_t self_in) { + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_machine_adc_deinit(self); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_deinit_obj, machine_adc_deinit); +#endif + +#if MICROPY_PY_MACHINE_ADC_BLOCK +// ADC.block() +STATIC mp_obj_t machine_adc_block(mp_obj_t self_in) { + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); + return mp_machine_adc_block(self); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_block_obj, machine_adc_block); +#endif + +// ADC.read_u16() +STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(mp_machine_adc_read_u16(self)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16); + +#if MICROPY_PY_MACHINE_ADC_READ_UV +// ADC.read_uv() +STATIC mp_obj_t machine_adc_read_uv(mp_obj_t self_in) { + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(mp_machine_adc_read_uv(self)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_uv_obj, machine_adc_read_uv); +#endif + +#if MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH + +// ADC.atten(value) -- this is a legacy method. +STATIC mp_obj_t machine_adc_atten(mp_obj_t self_in, mp_obj_t atten_in) { + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_int_t atten = mp_obj_get_int(atten_in); + mp_machine_adc_atten_set(self, atten); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_adc_atten_obj, machine_adc_atten); + +// ADC.width(value) -- this is a legacy method. +STATIC mp_obj_t machine_adc_width(mp_obj_t self_in, mp_obj_t width_in) { + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); + mp_int_t width = mp_obj_get_int(width_in); + mp_machine_adc_width_set(self, width); + return mp_const_none; +} +STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_adc_width_obj, machine_adc_width); + +#endif + +#if MICROPY_PY_MACHINE_ADC_READ +// ADC.read() -- this is a legacy method. +STATIC mp_obj_t machine_adc_read(mp_obj_t self_in) { + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); + return MP_OBJ_NEW_SMALL_INT(mp_machine_adc_read(self)); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_obj, machine_adc_read); +#endif + +STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { + #if MICROPY_PY_MACHINE_ADC_INIT + { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_adc_init_obj) }, + #endif + #if MICROPY_PY_MACHINE_ADC_DEINIT + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_adc_deinit_obj) }, + #endif + #if MICROPY_PY_MACHINE_ADC_BLOCK + { MP_ROM_QSTR(MP_QSTR_block), MP_ROM_PTR(&machine_adc_block_obj) }, + #endif + + { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) }, + #if MICROPY_PY_MACHINE_ADC_READ_UV + { MP_ROM_QSTR(MP_QSTR_read_uv), MP_ROM_PTR(&machine_adc_read_uv_obj) }, + #endif + + // Legacy methods. + #if MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH + { MP_ROM_QSTR(MP_QSTR_atten), MP_ROM_PTR(&machine_adc_atten_obj) }, + { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&machine_adc_width_obj) }, + #endif + #if MICROPY_PY_MACHINE_ADC_READ + { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_adc_read_obj) }, + #endif + + // A port must add ADC class constants defining the following macro. + // It can be defined to nothing if there are no constants. + MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS +}; +STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); + +MP_DEFINE_CONST_OBJ_TYPE( + machine_adc_type, + MP_QSTR_ADC, + MP_TYPE_FLAG_NONE, + make_new, mp_machine_adc_make_new, + print, mp_machine_adc_print, + locals_dict, &machine_adc_locals_dict + ); + +#endif // MICROPY_PY_MACHINE_ADC diff --git a/extmod/modmachine.h b/extmod/modmachine.h index b0235dcf645b..e635eedaae0a 100644 --- a/extmod/modmachine.h +++ b/extmod/modmachine.h @@ -29,7 +29,44 @@ #include "py/obj.h" +// Whether to enable the ADC.init() method. +// Requires a port to implement mp_machine_adc_init_helper(). +#ifndef MICROPY_PY_MACHINE_ADC_INIT +#define MICROPY_PY_MACHINE_ADC_INIT (0) +#endif + +// Whether to enable the ADC.deinit() method. +// Requires a port to implement mp_machine_adc_deinit(). +#ifndef MICROPY_PY_MACHINE_ADC_DEINIT +#define MICROPY_PY_MACHINE_ADC_DEINIT (0) +#endif + +// Whether to enable the ADC.block() method. +// Requires a port to implement mp_machine_adc_block(). +#ifndef MICROPY_PY_MACHINE_ADC_BLOCK +#define MICROPY_PY_MACHINE_ADC_BLOCK (0) +#endif + +// Whether to enable the ADC.read_uv() method. +// Requires a port to implement mp_machine_adc_read_uv(). +#ifndef MICROPY_PY_MACHINE_ADC_READ_UV +#define MICROPY_PY_MACHINE_ADC_READ_UV (0) +#endif + +// Whether to enable the ADC.atten() and ADC.width() methods. +// Note: these are legacy and should not be used on new ports. +#ifndef MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH +#define MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH (0) +#endif + +// Whether to enable the ADC.read() method. +// Note: this is legacy and should not be used on new ports. +#ifndef MICROPY_PY_MACHINE_ADC_READ +#define MICROPY_PY_MACHINE_ADC_READ (0) +#endif + // A port must provide these types, but they are otherwise opaque. +typedef struct _machine_adc_obj_t machine_adc_obj_t; typedef struct _machine_i2s_obj_t machine_i2s_obj_t; typedef struct _machine_pwm_obj_t machine_pwm_obj_t; typedef struct _machine_wdt_obj_t machine_wdt_obj_t; @@ -37,6 +74,7 @@ typedef struct _machine_wdt_obj_t machine_wdt_obj_t; // These classes correspond to machine.Type entries in the machine module. // Their Python bindings are implemented in extmod, and their implementation // is provided by a port. +extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_i2c_type; extern const mp_obj_type_t machine_i2s_type; extern const mp_obj_type_t machine_pwm_type; diff --git a/ports/esp32/esp32_common.cmake b/ports/esp32/esp32_common.cmake index 2c82c1f10f3c..4c90282c9f45 100644 --- a/ports/esp32/esp32_common.cmake +++ b/ports/esp32/esp32_common.cmake @@ -66,7 +66,6 @@ list(APPEND MICROPY_SOURCE_PORT machine_timer.c machine_pin.c machine_touchpad.c - machine_adc.c machine_adcblock.c machine_dac.c machine_i2c.c diff --git a/ports/esp32/machine_adc.c b/ports/esp32/machine_adc.c index 1e20186b97be..42746cb4bd68 100644 --- a/ports/esp32/machine_adc.c +++ b/ports/esp32/machine_adc.c @@ -25,22 +25,53 @@ * THE SOFTWARE. */ -#include +// This file is never compiled standalone, it's included directly from +// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE. #include "esp_log.h" #include "driver/gpio.h" #include "driver/adc.h" -#include "py/runtime.h" #include "py/mphal.h" -#include "modmachine.h" #include "machine_adc.h" #define ADCBLOCK1 (&madcblock_obj[0]) #define ADCBLOCK2 (&madcblock_obj[1]) -STATIC const madc_obj_t madc_obj[] = { +#if CONFIG_IDF_TARGET_ESP32 +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_9_10_11 \ + { MP_ROM_QSTR(MP_QSTR_WIDTH_9BIT), MP_ROM_INT(9) }, \ + { MP_ROM_QSTR(MP_QSTR_WIDTH_10BIT), MP_ROM_INT(10) }, \ + { MP_ROM_QSTR(MP_QSTR_WIDTH_11BIT), MP_ROM_INT(11) }, +#else +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_9_10_11 +#endif + +#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_12 \ + { MP_ROM_QSTR(MP_QSTR_WIDTH_12BIT), MP_ROM_INT(12) }, +#else +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_12 +#endif + +#if CONFIG_IDF_TARGET_ESP32S2 +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_13 \ + { MP_ROM_QSTR(MP_QSTR_WIDTH_13BIT), MP_ROM_INT(13) }, +#else +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_13 +#endif + +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \ + { MP_ROM_QSTR(MP_QSTR_ATTN_0DB), MP_ROM_INT(ADC_ATTEN_DB_0) }, \ + { MP_ROM_QSTR(MP_QSTR_ATTN_2_5DB), MP_ROM_INT(ADC_ATTEN_DB_2_5) }, \ + { MP_ROM_QSTR(MP_QSTR_ATTN_6DB), MP_ROM_INT(ADC_ATTEN_DB_6) }, \ + { MP_ROM_QSTR(MP_QSTR_ATTN_11DB), MP_ROM_INT(ADC_ATTEN_DB_11) }, \ + MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_9_10_11 \ + MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_12 \ + MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_WIDTH_13 \ + +STATIC const machine_adc_obj_t madc_obj[] = { #if CONFIG_IDF_TARGET_ESP32 {{&machine_adc_type}, ADCBLOCK1, ADC_CHANNEL_0, GPIO_NUM_36}, {{&machine_adc_type}, ADCBLOCK1, ADC_CHANNEL_1, GPIO_NUM_37}, @@ -96,18 +127,18 @@ STATIC const madc_obj_t madc_obj[] = { // can be distinguished from the initialised state. STATIC uint8_t madc_obj_atten[MP_ARRAY_SIZE(madc_obj)]; -static inline adc_atten_t madc_atten_get(const madc_obj_t *self) { +static inline adc_atten_t madc_atten_get(const machine_adc_obj_t *self) { uint8_t value = madc_obj_atten[self - &madc_obj[0]]; return value == 0 ? ADC_ATTEN_MAX : value - 1; } -static inline void madc_atten_set(const madc_obj_t *self, adc_atten_t atten) { +static inline void madc_atten_set(const machine_adc_obj_t *self, adc_atten_t atten) { madc_obj_atten[self - &madc_obj[0]] = atten + 1; } -const madc_obj_t *madc_search_helper(madcblock_obj_t *block, adc_channel_t channel_id, gpio_num_t gpio_id) { +const machine_adc_obj_t *madc_search_helper(madcblock_obj_t *block, adc_channel_t channel_id, gpio_num_t gpio_id) { for (int i = 0; i < MP_ARRAY_SIZE(madc_obj); i++) { - const madc_obj_t *adc = &madc_obj[i]; + const machine_adc_obj_t *adc = &madc_obj[i]; if ((block == NULL || block == adc->block) && (channel_id == -1 || channel_id == adc->channel_id) && (gpio_id == -1 || gpio_id == adc->gpio_id)) { return adc; } @@ -115,12 +146,12 @@ const madc_obj_t *madc_search_helper(madcblock_obj_t *block, adc_channel_t chann return NULL; } -STATIC void madc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { - const madc_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + const machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_printf(print, "ADC(Pin(%u), atten=%u)", self->gpio_id, madc_atten_get(self)); } -STATIC void madc_atten_helper(const madc_obj_t *self, mp_int_t atten) { +STATIC void madc_atten_helper(const machine_adc_obj_t *self, mp_int_t atten) { esp_err_t err; if (self->block->unit_id == ADC_UNIT_1) { err = adc1_config_channel_atten(self->channel_id, atten); @@ -133,7 +164,7 @@ STATIC void madc_atten_helper(const madc_obj_t *self, mp_int_t atten) { madc_atten_set(self, atten); } -void madc_init_helper(const madc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { +void madc_init_helper(const machine_adc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { enum { ARG_atten, }; @@ -153,10 +184,14 @@ void madc_init_helper(const madc_obj_t *self, size_t n_pos_args, const mp_obj_t } } -STATIC mp_obj_t madc_make_new(const mp_obj_type_t *type, size_t n_pos_args, size_t n_kw_args, const mp_obj_t *args) { +STATIC void mp_machine_adc_init_helper(machine_adc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + madc_init_helper(self, n_pos_args, pos_args, kw_args); +} + +STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type, size_t n_pos_args, size_t n_kw_args, const mp_obj_t *args) { mp_arg_check_num(n_pos_args, n_kw_args, 1, MP_OBJ_FUN_ARGS_MAX, true); gpio_num_t gpio_id = machine_pin_get_id(args[0]); - const madc_obj_t *self = madc_search_helper(NULL, -1, gpio_id); + const machine_adc_obj_t *self = madc_search_helper(NULL, -1, gpio_id); if (!self) { mp_raise_ValueError(MP_ERROR_TEXT("invalid pin")); } @@ -172,95 +207,32 @@ STATIC mp_obj_t madc_make_new(const mp_obj_type_t *type, size_t n_pos_args, size return MP_OBJ_FROM_PTR(self); } -STATIC mp_obj_t madc_init(size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - const madc_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); - madc_init_helper(self, n_pos_args - 1, pos_args + 1, kw_args); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(madc_init_obj, 1, madc_init); - -STATIC mp_obj_t madc_block(mp_obj_t self_in) { - const madc_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_obj_t mp_machine_adc_block(machine_adc_obj_t *self) { return MP_OBJ_FROM_PTR(self->block); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(madc_block_obj, madc_block); -STATIC mp_obj_t madc_read(mp_obj_t self_in) { - const madc_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_int_t mp_machine_adc_read(machine_adc_obj_t *self) { mp_int_t raw = madcblock_read_helper(self->block, self->channel_id); - return MP_OBJ_NEW_SMALL_INT(raw); + return raw; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(madc_read_obj, madc_read); -STATIC mp_obj_t madc_read_u16(mp_obj_t self_in) { - const madc_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) { mp_uint_t raw = madcblock_read_helper(self->block, self->channel_id); // Scale raw reading to 16 bit value using a Taylor expansion (for 8 <= bits <= 16) mp_int_t bits = self->block->bits; mp_uint_t u16 = raw << (16 - bits) | raw >> (2 * bits - 16); - return MP_OBJ_NEW_SMALL_INT(u16); + return u16; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(madc_read_u16_obj, madc_read_u16); -STATIC mp_obj_t madc_read_uv(mp_obj_t self_in) { - const madc_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_int_t mp_machine_adc_read_uv(machine_adc_obj_t *self) { adc_atten_t atten = madc_atten_get(self); - return MP_OBJ_NEW_SMALL_INT(madcblock_read_uv_helper(self->block, self->channel_id, atten)); + return madcblock_read_uv_helper(self->block, self->channel_id, atten); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(madc_read_uv_obj, madc_read_uv); -STATIC mp_obj_t madc_atten(mp_obj_t self_in, mp_obj_t atten_in) { - const madc_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_int_t atten = mp_obj_get_int(atten_in); +STATIC void mp_machine_adc_atten_set(machine_adc_obj_t *self, mp_int_t atten) { madc_atten_helper(self, atten); - return mp_const_none; } -MP_DEFINE_CONST_FUN_OBJ_2(madc_atten_obj, madc_atten); -STATIC mp_obj_t madc_width(mp_obj_t self_in, mp_obj_t bits_in) { - const madc_obj_t *self = MP_OBJ_TO_PTR(self_in); - mp_int_t bits = mp_obj_get_int(bits_in); - madcblock_bits_helper(self->block, bits); - return mp_const_none; +STATIC void mp_machine_adc_width_set(machine_adc_obj_t *self, mp_int_t width) { + madcblock_bits_helper(self->block, width); } -MP_DEFINE_CONST_FUN_OBJ_2(madc_width_obj, madc_width); - -STATIC const mp_rom_map_elem_t madc_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&madc_init_obj) }, - { MP_ROM_QSTR(MP_QSTR_block), MP_ROM_PTR(&madc_block_obj) }, - { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&madc_read_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&madc_read_u16_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_uv), MP_ROM_PTR(&madc_read_uv_obj) }, - - // Legacy API methods: - { MP_ROM_QSTR(MP_QSTR_atten), MP_ROM_PTR(&madc_atten_obj) }, - { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&madc_width_obj) }, - - { MP_ROM_QSTR(MP_QSTR_ATTN_0DB), MP_ROM_INT(ADC_ATTEN_DB_0) }, - { MP_ROM_QSTR(MP_QSTR_ATTN_2_5DB), MP_ROM_INT(ADC_ATTEN_DB_2_5) }, - { MP_ROM_QSTR(MP_QSTR_ATTN_6DB), MP_ROM_INT(ADC_ATTEN_DB_6) }, - { MP_ROM_QSTR(MP_QSTR_ATTN_11DB), MP_ROM_INT(ADC_ATTEN_DB_11) }, - - #if CONFIG_IDF_TARGET_ESP32 - { MP_ROM_QSTR(MP_QSTR_WIDTH_9BIT), MP_ROM_INT(9) }, - { MP_ROM_QSTR(MP_QSTR_WIDTH_10BIT), MP_ROM_INT(10) }, - { MP_ROM_QSTR(MP_QSTR_WIDTH_11BIT), MP_ROM_INT(11) }, - #endif - #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 - { MP_ROM_QSTR(MP_QSTR_WIDTH_12BIT), MP_ROM_INT(12) }, - #endif - #if CONFIG_IDF_TARGET_ESP32S2 - { MP_ROM_QSTR(MP_QSTR_WIDTH_13BIT), MP_ROM_INT(13) }, - #endif - -}; -STATIC MP_DEFINE_CONST_DICT(madc_locals_dict, madc_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_adc_type, - MP_QSTR_ADC, - MP_TYPE_FLAG_NONE, - make_new, madc_make_new, - print, madc_print, - locals_dict, &madc_locals_dict - ); diff --git a/ports/esp32/machine_adc.h b/ports/esp32/machine_adc.h index 0f229a2c5cca..6d06486c30c6 100644 --- a/ports/esp32/machine_adc.h +++ b/ports/esp32/machine_adc.h @@ -3,14 +3,14 @@ #include "machine_adcblock.h" -typedef struct _madc_obj_t { +typedef struct _machine_adc_obj_t { mp_obj_base_t base; madcblock_obj_t *block; adc_channel_t channel_id; gpio_num_t gpio_id; -} madc_obj_t; +} machine_adc_obj_t; -extern const madc_obj_t *madc_search_helper(madcblock_obj_t *block, adc_channel_t channel_id, gpio_num_t gpio_id); -extern void madc_init_helper(const madc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args); +const machine_adc_obj_t *madc_search_helper(madcblock_obj_t *block, adc_channel_t channel_id, gpio_num_t gpio_id); +void madc_init_helper(const machine_adc_obj_t *self, size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args); #endif // MICROPY_INCLUDED_MACHINE_ADC_H diff --git a/ports/esp32/machine_adcblock.c b/ports/esp32/machine_adcblock.c index afe8fdea40b2..8d5296e2e5c4 100644 --- a/ports/esp32/machine_adcblock.c +++ b/ports/esp32/machine_adcblock.c @@ -157,7 +157,7 @@ STATIC mp_obj_t madcblock_connect(size_t n_pos_args, const mp_obj_t *pos_args, m mp_raise_TypeError(MP_ERROR_TEXT("too many positional args")); } - const madc_obj_t *adc = madc_search_helper(self, channel_id, gpio_id); + const machine_adc_obj_t *adc = madc_search_helper(self, channel_id, gpio_id); if (adc != NULL) { madc_init_helper(adc, 0, pos_args + n_pos_args, kw_args); return MP_OBJ_FROM_PTR(adc); diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index fe6d18e7863a..b1c9011cb6ae 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -312,7 +312,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 { MP_ROM_QSTR(MP_QSTR_TouchPad), MP_ROM_PTR(&machine_touchpad_type) }, #endif + #if MICROPY_PY_MACHINE_ADC { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_ADCBlock), MP_ROM_PTR(&machine_adcblock_type) }, #if MICROPY_PY_MACHINE_DAC { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) }, diff --git a/ports/esp32/modmachine.h b/ports/esp32/modmachine.h index abf029c473d6..68b7d4ffd586 100644 --- a/ports/esp32/modmachine.h +++ b/ports/esp32/modmachine.h @@ -12,7 +12,6 @@ typedef enum { extern const mp_obj_type_t machine_timer_type; extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_touchpad_type; -extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_adcblock_type; extern const mp_obj_type_t machine_dac_type; extern const mp_obj_type_t machine_i2c_type; diff --git a/ports/esp32/mpconfigport.h b/ports/esp32/mpconfigport.h index 8b59dc1225ae..1718f31f6cd1 100644 --- a/ports/esp32/mpconfigport.h +++ b/ports/esp32/mpconfigport.h @@ -96,6 +96,13 @@ #define MICROPY_PY_OS_UNAME (1) #define MICROPY_PY_OS_URANDOM (1) #define MICROPY_PY_MACHINE (1) +#define MICROPY_PY_MACHINE_ADC (1) +#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/esp32/machine_adc.c" +#define MICROPY_PY_MACHINE_ADC_ATTEN_WIDTH (1) +#define MICROPY_PY_MACHINE_ADC_BLOCK (1) +#define MICROPY_PY_MACHINE_ADC_INIT (1) +#define MICROPY_PY_MACHINE_ADC_READ (1) +#define MICROPY_PY_MACHINE_ADC_READ_UV (1) #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_PY_MACHINE_BITSTREAM (1) #define MICROPY_PY_MACHINE_PULSE (1) diff --git a/ports/esp8266/Makefile b/ports/esp8266/Makefile index 5982987d7b61..0485f9bc0599 100644 --- a/ports/esp8266/Makefile +++ b/ports/esp8266/Makefile @@ -114,7 +114,6 @@ SRC_C = \ machine_bitstream.c \ machine_pin.c \ machine_rtc.c \ - machine_adc.c \ machine_uart.c \ machine_hspi.c \ modesp.c \ diff --git a/ports/esp8266/machine_adc.c b/ports/esp8266/machine_adc.c index f4fd32db9c76..83384eea971a 100644 --- a/ports/esp8266/machine_adc.c +++ b/ports/esp8266/machine_adc.c @@ -24,20 +24,19 @@ * THE SOFTWARE. */ -#include -#include +// This file is never compiled standalone, it's included directly from +// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE. -#include "py/runtime.h" -#include "py/mphal.h" #include "user_interface.h" +// The ADC class doesn't have any constants for this port. +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS + typedef struct _machine_adc_obj_t { mp_obj_base_t base; bool isvdd; } machine_adc_obj_t; -extern const mp_obj_type_t machine_adc_type; - STATIC machine_adc_obj_t machine_adc_vdd3 = {{&machine_adc_type}, true}; STATIC machine_adc_obj_t machine_adc_adc = {{&machine_adc_type}, false}; @@ -48,12 +47,13 @@ STATIC uint16_t adc_read(machine_adc_obj_t *self) { return system_adc_read(); } } -void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { + +STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_printf(print, "ADC(%u)", self->isvdd); } -mp_obj_t machine_adc_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { +STATIC mp_obj_t mp_machine_adc_make_new(const mp_obj_type_t *type_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_arg_check_num(n_args, n_kw, 1, 1, false); mp_int_t chn = mp_obj_get_int(args[0]); @@ -69,31 +69,12 @@ mp_obj_t machine_adc_make_new(const mp_obj_type_t *type_in, size_t n_args, size_ } // read_u16() -STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { - machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) { uint32_t value = adc_read(self); - return MP_OBJ_NEW_SMALL_INT(value * 65535 / 1024); + return value * 65535 / 1024; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16); // Legacy method -STATIC mp_obj_t machine_adc_read(mp_obj_t self_in) { - machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); - return mp_obj_new_int(adc_read(self)); +STATIC mp_int_t mp_machine_adc_read(machine_adc_obj_t *self) { + return adc_read(self); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_obj, machine_adc_read); - -STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) }, - { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_adc_read_obj) } -}; -STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_adc_type, - MP_QSTR_ADC, - MP_TYPE_FLAG_NONE, - make_new, machine_adc_make_new, - print, machine_adc_print, - locals_dict, &machine_adc_locals_dict - ); diff --git a/ports/esp8266/modmachine.c b/ports/esp8266/modmachine.c index 95048952c42f..43e94e0c8a7d 100644 --- a/ports/esp8266/modmachine.c +++ b/ports/esp8266/modmachine.c @@ -427,7 +427,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&pyb_pin_type) }, { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, + #if MICROPY_PY_MACHINE_ADC { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&pyb_uart_type) }, #if MICROPY_PY_MACHINE_I2C { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, diff --git a/ports/esp8266/modmachine.h b/ports/esp8266/modmachine.h index 9b7a5e3cb226..469d25e81d89 100644 --- a/ports/esp8266/modmachine.h +++ b/ports/esp8266/modmachine.h @@ -4,7 +4,6 @@ #include "py/obj.h" extern const mp_obj_type_t pyb_pin_type; -extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t pyb_rtc_type; extern const mp_obj_type_t pyb_uart_type; extern const mp_obj_type_t pyb_i2c_type; diff --git a/ports/esp8266/mpconfigport.h b/ports/esp8266/mpconfigport.h index 2a51f8bcbbce..4e3248513e2a 100644 --- a/ports/esp8266/mpconfigport.h +++ b/ports/esp8266/mpconfigport.h @@ -61,6 +61,9 @@ #define MICROPY_PY_LWIP (1) #define MICROPY_PY_LWIP_SOCK_RAW (1) #define MICROPY_PY_MACHINE (1) +#define MICROPY_PY_MACHINE_ADC (1) +#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/esp8266/machine_adc.c" +#define MICROPY_PY_MACHINE_ADC_READ (1) #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_PY_MACHINE_BITSTREAM (1) #define MICROPY_PY_MACHINE_PULSE (1) diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 2f4b13ad9264..b631c829bf0e 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -195,7 +195,6 @@ SRC_C += \ hal/pwm_backport.c \ help.c \ led.c \ - machine_adc.c \ machine_bitstream.c \ machine_i2c.c \ machine_led.c \ diff --git a/ports/mimxrt/machine_adc.c b/ports/mimxrt/machine_adc.c index 36e5fafc984b..0be5f9a7d2be 100644 --- a/ports/mimxrt/machine_adc.c +++ b/ports/mimxrt/machine_adc.c @@ -24,9 +24,9 @@ * THE SOFTWARE. */ -#include -#include "py/obj.h" -#include "py/runtime.h" +// This file is never compiled standalone, it's included directly from +// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE. + #include "py/mphal.h" #if defined(MIMXRT117x_SERIES) @@ -38,7 +38,8 @@ #include "fsl_gpio.h" #include "fsl_iomuxc.h" -#include "modmachine.h" +// The ADC class doesn't have any constants for this port. +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS typedef struct _machine_adc_obj_t { mp_obj_base_t base; @@ -50,9 +51,9 @@ typedef struct _machine_adc_obj_t { STATIC ADC_Type *const adc_bases[] = ADC_BASE_PTRS; -STATIC void adc_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) { +STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { (void)kind; - machine_adc_obj_t *self = MP_OBJ_TO_PTR(o); + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); // Get ADC adc id for (int i = 1; i < sizeof(adc_bases) / sizeof(ADC_Type *); ++i) { @@ -63,11 +64,11 @@ STATIC void adc_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t k } } -STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { +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) { mp_arg_check_num(n_args, n_kw, 1, 1, false); // Unpack and check parameter - const machine_pin_obj_t *pin = pin_find(args[0]); + const machine_pin_obj_t *pin = pin_find(all_args[0]); if (pin->adc_list_len == 0) { mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("Pin(%q) does not have ADC capabilities"), pin->name); @@ -98,8 +99,7 @@ STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_ // read_u16() #if defined(MIMXRT117x_SERIES) -STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { - machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) { lpadc_conv_command_config_t adc_config; lpadc_conv_trigger_config_t trigger_config; @@ -120,9 +120,8 @@ STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { while (!LPADC_GetConvResult(self->adc, &result_struct)) { } - return MP_OBJ_NEW_SMALL_INT(result_struct.convValue * 2); + return result_struct.convValue * 2; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16); void machine_adc_init(void) { lpadc_config_t adc_config; // Set ADC configuration @@ -134,9 +133,7 @@ void machine_adc_init(void) { #else -STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { - machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); - +STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) { // Initiate conversion adc_channel_config_t channel_config = { .channelNumber = self->channel, @@ -151,9 +148,8 @@ STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { // Measure input voltage uint32_t value = ADC_GetChannelConversionValue(self->adc, (uint32_t)self->channel_group); - return MP_OBJ_NEW_SMALL_INT(value * 65535 / self->resolution); + return value * 65535 / self->resolution; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16); void machine_adc_init(void) { for (int i = 1; i < sizeof(adc_bases) / sizeof(ADC_Type *); ++i) { @@ -173,18 +169,3 @@ void machine_adc_init(void) { } } #endif - -STATIC const mp_rom_map_elem_t adc_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) }, -}; - -STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_adc_type, - MP_QSTR_ADC, - MP_TYPE_FLAG_NONE, - make_new, adc_obj_make_new, - print, adc_obj_print, - locals_dict, &adc_locals_dict - ); diff --git a/ports/mimxrt/modmachine.c b/ports/mimxrt/modmachine.c index c2f3559cf82e..33394e62dd2b 100644 --- a/ports/mimxrt/modmachine.c +++ b/ports/mimxrt/modmachine.c @@ -189,7 +189,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&machine_led_type) }, #endif { MP_ROM_QSTR(MP_QSTR_Pin), MP_ROM_PTR(&machine_pin_type) }, + #if MICROPY_PY_MACHINE_ADC { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_Timer), MP_ROM_PTR(&machine_timer_type) }, { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) }, #if MICROPY_PY_MACHINE_SDCARD diff --git a/ports/mimxrt/modmachine.h b/ports/mimxrt/modmachine.h index fed75fdd5411..ff8ac0f085cb 100644 --- a/ports/mimxrt/modmachine.h +++ b/ports/mimxrt/modmachine.h @@ -29,7 +29,6 @@ #include "py/obj.h" -extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_i2c_type; extern const mp_obj_type_t machine_rtc_type; extern const mp_obj_type_t machine_sdcard_type; diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 492686843a92..9df6397a8755 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -78,6 +78,8 @@ uint32_t trng_random_u32(void); #define MICROPY_PY_OS_URANDOM (1) #define MICROPY_PY_RANDOM_SEED_INIT_FUNC (trng_random_u32()) #define MICROPY_PY_MACHINE (1) +#define MICROPY_PY_MACHINE_ADC (1) +#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/mimxrt/machine_adc.c" #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_PY_MACHINE_BITSTREAM (1) #define MICROPY_PY_MACHINE_PULSE (1) diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 284b82281b24..b5d832577376 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -324,7 +324,6 @@ DRIVERS_SRC_C += $(addprefix modules/,\ machine/uart.c \ machine/spi.c \ machine/i2c.c \ - machine/adc.c \ machine/pin.c \ machine/timer.c \ machine/rtcounter.c \ diff --git a/ports/nrf/modules/machine/adc.c b/ports/nrf/modules/machine/adc.c index df9d23465a8d..9bebcbe0869f 100644 --- a/ports/nrf/modules/machine/adc.c +++ b/ports/nrf/modules/machine/adc.c @@ -24,15 +24,10 @@ * THE SOFTWARE. */ -#include -#include +// This file is never compiled standalone, it's included directly from +// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE. -#include "py/nlr.h" -#include "py/runtime.h" #include "py/mphal.h" - -#if MICROPY_PY_MACHINE_ADC - #include "adc.h" #if NRF51 @@ -100,19 +95,23 @@ STATIC int adc_find(mp_obj_t id) { mp_raise_ValueError(MP_ERROR_TEXT("ADC doesn't exist")); } -/// \method __str__() -/// Return a string describing the ADC object. -STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) { +/******************************************************************************/ +/* MicroPython bindings for machine API */ + +// These are ad-hoc legacy methods and need to be removed. +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \ + { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&mp_machine_adc_value_obj) }, /* instance method */ \ + { MP_ROM_QSTR(MP_QSTR_battery_level), MP_ROM_PTR(&mp_machine_adc_battery_level_obj) }, /* class method */ \ + +// Return a string describing the ADC object. +STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) { machine_adc_obj_t *self = o; mp_printf(print, "ADC(%u)", self->id); } -/******************************************************************************/ -/* MicroPython bindings for machine API */ - // for make_new -STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { +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) { enum { ARG_id }; static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1) } }, @@ -172,8 +171,7 @@ int16_t machine_adc_value_read(machine_adc_obj_t * adc_obj) { } // read_u16() -STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { - machine_adc_obj_t *self = self_in; +STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) { int16_t raw = machine_adc_value_read(self); #if defined(NRF52_SERIES) // raw is signed but the channel is in single-ended mode and this method cannot return negative values @@ -182,9 +180,8 @@ STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { } #endif // raw is an 8-bit value - return MP_OBJ_NEW_SMALL_INT(raw << 8 | raw); + return (raw << 8) | raw; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(mp_machine_adc_read_u16_obj, machine_adc_read_u16); /// \method value() /// Read adc level. @@ -282,25 +279,3 @@ mp_obj_t machine_adc_battery_level(void) { return MP_OBJ_NEW_SMALL_INT(batt_in_percent); } STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_machine_adc_battery_level_obj, machine_adc_battery_level); - -STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { - // instance methods - { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&mp_machine_adc_read_u16_obj) }, - { MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&mp_machine_adc_value_obj) }, - - // class methods - { MP_ROM_QSTR(MP_QSTR_battery_level), MP_ROM_PTR(&mp_machine_adc_battery_level_obj) }, -}; - -STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_adc_type, - MP_QSTR_ADC, - MP_TYPE_FLAG_NONE, - make_new, machine_adc_make_new, - locals_dict, &machine_adc_locals_dict, - print, machine_adc_print - ); - -#endif // MICROPY_PY_MACHINE_ADC diff --git a/ports/nrf/modules/machine/adc.h b/ports/nrf/modules/machine/adc.h index cefccff6b2c5..84e5cdbb85f3 100644 --- a/ports/nrf/modules/machine/adc.h +++ b/ports/nrf/modules/machine/adc.h @@ -29,8 +29,6 @@ typedef struct _machine_adc_obj_t machine_adc_obj_t; -extern const mp_obj_type_t machine_adc_type; - void adc_init0(void); int16_t machine_adc_value_read(machine_adc_obj_t * adc_obj); diff --git a/ports/nrf/modules/machine/modmachine.c b/ports/nrf/modules/machine/modmachine.c index 59f163dee24c..f8a6e424af90 100644 --- a/ports/nrf/modules/machine/modmachine.c +++ b/ports/nrf/modules/machine/modmachine.c @@ -46,9 +46,6 @@ #if MICROPY_PY_MACHINE_HW_PWM || MICROPY_PY_MACHINE_SOFT_PWM #include "pwm.h" #endif -#if MICROPY_PY_MACHINE_ADC -#include "adc.h" -#endif #if MICROPY_PY_MACHINE_TEMP #include "temp.h" #endif diff --git a/ports/nrf/mpconfigport.h b/ports/nrf/mpconfigport.h index 5a956311e2f8..79a74e8566ce 100644 --- a/ports/nrf/mpconfigport.h +++ b/ports/nrf/mpconfigport.h @@ -179,6 +179,7 @@ #ifndef MICROPY_PY_MACHINE_ADC #define MICROPY_PY_MACHINE_ADC (0) #endif +#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/nrf/modules/machine/adc.c" #ifndef MICROPY_PY_MACHINE_I2C #define MICROPY_PY_MACHINE_I2C (0) diff --git a/ports/renesas-ra/Makefile b/ports/renesas-ra/Makefile index 8126dbe7772d..4630c30fc0e7 100644 --- a/ports/renesas-ra/Makefile +++ b/ports/renesas-ra/Makefile @@ -319,7 +319,6 @@ SRC_C += \ uart.c \ gccollect.c \ help.c \ - machine_adc.c \ machine_dac.c \ machine_i2c.c \ machine_spi.c \ diff --git a/ports/renesas-ra/machine_adc.c b/ports/renesas-ra/machine_adc.c index b71c3db4b025..4ba30dff01e1 100644 --- a/ports/renesas-ra/machine_adc.c +++ b/ports/renesas-ra/machine_adc.c @@ -25,7 +25,9 @@ * THE SOFTWARE. */ -#include "py/runtime.h" +// This file is never compiled standalone, it's included directly from +// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE. + #include "py/mphal.h" #include "ra_adc.h" @@ -47,7 +49,18 @@ typedef struct { /******************************************************************************/ // MicroPython bindings for machine.ADC -const mp_obj_type_t machine_adc_type; +#if defined(ADC_CHANNEL_VBAT) +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VBAT \ + { MP_ROM_QSTR(MP_QSTR_CORE_VBAT), MP_ROM_INT(ADC_CHANNEL_VBAT) }, +#else +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VBAT +#endif + +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \ + { MP_ROM_QSTR(MP_QSTR_VREF), MP_ROM_INT(ADC_CHANNEL_VREF) }, \ + { MP_ROM_QSTR(MP_QSTR_CORE_VREF), MP_ROM_INT(ADC_CHANNEL_VREFINT) }, \ + { MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_CHANNEL_TEMPSENSOR) }, \ + MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VBAT \ typedef struct _machine_adc_obj_t { mp_obj_base_t base; @@ -57,14 +70,14 @@ typedef struct _machine_adc_obj_t { uint32_t sample_time; } machine_adc_obj_t; -STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); uint8_t resolution = (uint8_t)ra_adc_get_resolution(); mp_printf(print, "", resolution, self->channel); } // ADC(id) -STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { +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) { // Check number of arguments mp_arg_check_num(n_args, n_kw, 1, 1, false); @@ -97,40 +110,14 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, s return MP_OBJ_FROM_PTR(o); } -STATIC mp_obj_t machine_adc_read(mp_obj_t self_in) { - machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(ra_adc_read((uint32_t)(self->pin))); +STATIC mp_int_t mp_machine_adc_read(machine_adc_obj_t *self) { + return ra_adc_read((uint32_t)(self->pin)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_obj, machine_adc_read); -STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { - const machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) { mp_uint_t raw = (mp_uint_t)ra_adc_read((uint32_t)(self->pin)); mp_int_t bits = (mp_int_t)ra_adc_get_resolution(); // Scale raw reading to 16 bit value using a Taylor expansion (for 8 <= bits <= 16) mp_uint_t u16 = raw << (16 - bits) | raw >> (2 * bits - 16); - return MP_OBJ_NEW_SMALL_INT(u16); + return u16; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16); - -STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&machine_adc_read_obj) }, - { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) }, - - { MP_ROM_QSTR(MP_QSTR_VREF), MP_ROM_INT(ADC_CHANNEL_VREF) }, - { MP_ROM_QSTR(MP_QSTR_CORE_VREF), MP_ROM_INT(ADC_CHANNEL_VREFINT) }, - { MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_CHANNEL_TEMPSENSOR) }, - #if defined(ADC_CHANNEL_VBAT) - { MP_ROM_QSTR(MP_QSTR_CORE_VBAT), MP_ROM_INT(ADC_CHANNEL_VBAT) }, - #endif -}; -STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_adc_type, - MP_QSTR_ADC, - MP_TYPE_FLAG_NONE, - make_new, machine_adc_make_new, - locals_dict, &machine_adc_locals_dict, - print, machine_adc_print - ); diff --git a/ports/renesas-ra/modmachine.c b/ports/renesas-ra/modmachine.c index 6f7b8b8c07ac..9b93b69480f0 100644 --- a/ports/renesas-ra/modmachine.c +++ b/ports/renesas-ra/modmachine.c @@ -280,7 +280,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&machine_rtc_type) }, + #if MICROPY_PY_MACHINE_ADC { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, + #endif #if MICROPY_PY_MACHINE_DAC { MP_ROM_QSTR(MP_QSTR_DAC), MP_ROM_PTR(&machine_dac_type) }, #endif diff --git a/ports/renesas-ra/modmachine.h b/ports/renesas-ra/modmachine.h index ec494b798c8a..1dd0eec758db 100644 --- a/ports/renesas-ra/modmachine.h +++ b/ports/renesas-ra/modmachine.h @@ -32,7 +32,6 @@ extern const mp_obj_type_t machine_timer_type; extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_touchpad_type; -extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_adcblock_type; extern const mp_obj_type_t machine_dac_type; extern const mp_obj_type_t machine_i2c_type; diff --git a/ports/renesas-ra/mpconfigport.h b/ports/renesas-ra/mpconfigport.h index 255605a57e8c..71d5cab82089 100644 --- a/ports/renesas-ra/mpconfigport.h +++ b/ports/renesas-ra/mpconfigport.h @@ -130,6 +130,9 @@ #define MICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP) #ifndef MICROPY_PY_MACHINE #define MICROPY_PY_MACHINE (1) +#define MICROPY_PY_MACHINE_ADC (1) +#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/renesas-ra/machine_adc.c" +#define MICROPY_PY_MACHINE_ADC_READ (1) #ifndef MICROPY_PY_MACHINE_BITSTREAM #define MICROPY_PY_MACHINE_BITSTREAM (1) #endif diff --git a/ports/rp2/CMakeLists.txt b/ports/rp2/CMakeLists.txt index d5302e336906..c93a8a55d84f 100644 --- a/ports/rp2/CMakeLists.txt +++ b/ports/rp2/CMakeLists.txt @@ -117,7 +117,6 @@ set(MICROPY_SOURCE_DRIVERS set(MICROPY_SOURCE_PORT fatfs_port.c help.c - machine_adc.c machine_bitstream.c machine_i2c.c machine_pin.c diff --git a/ports/rp2/machine_adc.c b/ports/rp2/machine_adc.c index 5d6f0cc29cbc..80c4d0533e25 100644 --- a/ports/rp2/machine_adc.c +++ b/ports/rp2/machine_adc.c @@ -24,7 +24,9 @@ * THE SOFTWARE. */ -#include "py/runtime.h" +// This file is never compiled standalone, it's included directly from +// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE. + #include "py/mphal.h" #include "hardware/adc.h" #include "machine_pin.h" @@ -44,7 +46,8 @@ STATIC uint16_t adc_config_and_read_u16(uint32_t channel) { /******************************************************************************/ // MicroPython bindings for machine.ADC -const mp_obj_type_t machine_adc_type; +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \ + { MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_CHANNEL_TEMPSENSOR) }, \ typedef struct _machine_adc_obj_t { mp_obj_base_t base; @@ -54,13 +57,13 @@ typedef struct _machine_adc_obj_t { #endif } machine_adc_obj_t; -STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_printf(print, "", self->channel); } // ADC(id) -STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { +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) { // Check number of arguments mp_arg_check_num(n_args, n_kw, 1, 1, false); @@ -127,29 +130,11 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, s } // read_u16() -STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { - machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) { #if MICROPY_HW_ADC_EXT_COUNT if (self->is_ext) { - return MP_OBJ_NEW_SMALL_INT(machine_pin_ext_read_u16(self->channel)); + return machine_pin_ext_read_u16(self->channel); } #endif - return MP_OBJ_NEW_SMALL_INT(adc_config_and_read_u16(self->channel)); + return adc_config_and_read_u16(self->channel); } -MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16); - -STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) }, - - { MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(ADC_CHANNEL_TEMPSENSOR) }, -}; -STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_adc_type, - MP_QSTR_ADC, - MP_TYPE_FLAG_NONE, - make_new, machine_adc_make_new, - print, machine_adc_print, - locals_dict, &machine_adc_locals_dict - ); diff --git a/ports/rp2/modmachine.c b/ports/rp2/modmachine.c index 11de929e7b6b..b47af1aba0d1 100644 --- a/ports/rp2/modmachine.c +++ b/ports/rp2/modmachine.c @@ -254,7 +254,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_mem16), MP_ROM_PTR(&machine_mem16_obj) }, { MP_ROM_QSTR(MP_QSTR_mem32), MP_ROM_PTR(&machine_mem32_obj) }, + #if MICROPY_PY_MACHINE_ADC { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, + #endif { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_SoftI2C), MP_ROM_PTR(&mp_machine_soft_i2c_type) }, { MP_ROM_QSTR(MP_QSTR_I2S), MP_ROM_PTR(&machine_i2s_type) }, diff --git a/ports/rp2/modmachine.h b/ports/rp2/modmachine.h index 39a6e9c81e4e..5feafdcea885 100644 --- a/ports/rp2/modmachine.h +++ b/ports/rp2/modmachine.h @@ -3,7 +3,6 @@ #include "py/obj.h" -extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_i2c_type; extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_rtc_type; diff --git a/ports/rp2/mpconfigport.h b/ports/rp2/mpconfigport.h index e35980d10bdf..b69a06387e97 100644 --- a/ports/rp2/mpconfigport.h +++ b/ports/rp2/mpconfigport.h @@ -111,6 +111,8 @@ #define MICROPY_PY_TIME_INCLUDEFILE "ports/rp2/modtime.c" #define MICROPY_PY_RANDOM_SEED_INIT_FUNC (rosc_random_u32()) #define MICROPY_PY_MACHINE (1) +#define MICROPY_PY_MACHINE_ADC (1) +#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/rp2/machine_adc.c" #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new #define MICROPY_PY_MACHINE_BITSTREAM (1) #define MICROPY_PY_MACHINE_PULSE (1) diff --git a/ports/samd/Makefile b/ports/samd/Makefile index 8e4327073ca5..841e6a992827 100644 --- a/ports/samd/Makefile +++ b/ports/samd/Makefile @@ -96,7 +96,6 @@ MPY_CROSS_FLAGS += -march=$(MPY_CROSS_MCU_ARCH) SRC_C += \ mcu/$(MCU_SERIES_LOWER)/clock_config.c \ help.c \ - machine_adc.c \ machine_bitstream.c \ machine_dac.c \ machine_i2c.c \ diff --git a/ports/samd/machine_adc.c b/ports/samd/machine_adc.c index 1d848e32bc50..981318d33f2f 100644 --- a/ports/samd/machine_adc.c +++ b/ports/samd/machine_adc.c @@ -25,17 +25,12 @@ * THE SOFTWARE. */ -#include "py/runtime.h" +// This file is never compiled standalone, it's included directly from +// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE. -#if MICROPY_PY_MACHINE_ADC - -#include -#include "py/obj.h" #include "py/mphal.h" - #include "sam.h" #include "pin_af.h" -#include "modmachine.h" typedef struct _machine_adc_obj_t { mp_obj_base_t base; @@ -77,6 +72,9 @@ static uint8_t adc_vref_table[] = { #endif // defined(MCU_SAMD21) +// The ADC class doesn't have any constants for this port. +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS + Adc *const adc_bases[] = ADC_INSTS; uint32_t busy_flags = 0; bool init_flags[2] = {false, false}; @@ -87,16 +85,16 @@ static uint8_t resolution[] = { extern mp_int_t log2i(mp_int_t num); -STATIC void adc_obj_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) { +STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { (void)kind; - machine_adc_obj_t *self = MP_OBJ_TO_PTR(o); + machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_printf(print, "ADC(%s, device=%u, channel=%u, bits=%u, average=%u, vref=%d)", pin_name(self->id), self->adc_config.device, self->adc_config.channel, self->bits, 1 << self->avg, self->vref); } -STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { +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) { enum { ARG_id, ARG_bits, ARG_average, ARG_vref }; static const mp_arg_t allowed_args[] = { { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ }, @@ -140,8 +138,7 @@ STATIC mp_obj_t adc_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_ } // read_u16() -STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { - machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); +STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) { Adc *adc = adc_bases[self->adc_config.device]; // Set the reference voltage. Default: external AREFA. adc->REFCTRL.reg = adc_vref_table[self->vref]; @@ -155,18 +152,13 @@ STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { while (adc->INTFLAG.bit.RESRDY == 0) { } // Get and return the result - return MP_OBJ_NEW_SMALL_INT(adc->RESULT.reg * (65536 / (1 << self->bits))); + return adc->RESULT.reg * (65536 / (1 << self->bits)); } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16); // deinit() : release the ADC channel -STATIC mp_obj_t machine_adc_deinit(mp_obj_t self_in) { - machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); - +STATIC void mp_machine_adc_deinit(machine_adc_obj_t *self) { busy_flags &= ~((1 << (self->adc_config.device * 16 + self->adc_config.channel))); - return mp_const_none; } -STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_deinit_obj, machine_adc_deinit); void adc_deinit_all(void) { busy_flags = 0; @@ -174,22 +166,6 @@ void adc_deinit_all(void) { init_flags[1] = 0; } -STATIC const mp_rom_map_elem_t adc_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) }, - { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_adc_deinit_obj) }, -}; - -STATIC MP_DEFINE_CONST_DICT(adc_locals_dict, adc_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_adc_type, - MP_QSTR_ADC, - MP_TYPE_FLAG_NONE, - make_new, adc_obj_make_new, - print, adc_obj_print, - locals_dict, &adc_locals_dict - ); - static void adc_init(machine_adc_obj_t *self) { // ADC & clock init is done only once per ADC if (init_flags[self->adc_config.device] == false) { @@ -270,5 +246,3 @@ static void adc_init(machine_adc_obj_t *self) { // Set the port as given in self->id as ADC mp_hal_set_pin_mux(self->id, ALT_FCT_ADC); } - -#endif diff --git a/ports/samd/modmachine.h b/ports/samd/modmachine.h index faa43b7af7f7..81b30781711d 100644 --- a/ports/samd/modmachine.h +++ b/ports/samd/modmachine.h @@ -29,9 +29,6 @@ #include "py/obj.h" #include "shared/timeutils/timeutils.h" -#if MICROPY_PY_MACHINE_ADC -extern const mp_obj_type_t machine_adc_type; -#endif #if MICROPY_PY_MACHINE_DAC extern const mp_obj_type_t machine_dac_type; #endif diff --git a/ports/samd/mpconfigport.h b/ports/samd/mpconfigport.h index 3505d8db1e37..201e657c6894 100644 --- a/ports/samd/mpconfigport.h +++ b/ports/samd/mpconfigport.h @@ -96,6 +96,8 @@ #ifndef MICROPY_PY_MACHINE_ADC #define MICROPY_PY_MACHINE_ADC (1) #endif +#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/samd/machine_adc.c" +#define MICROPY_PY_MACHINE_ADC_DEINIT (1) #ifndef MICROPY_PY_MACHINE_DAC #define MICROPY_PY_MACHINE_DAC (1) #endif diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index 9c73c5c956f2..d1248e0bb8e5 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -332,7 +332,6 @@ SRC_C += \ eth.c \ gccollect.c \ help.c \ - machine_adc.c \ machine_bitstream.c \ machine_i2c.c \ machine_spi.c \ diff --git a/ports/stm32/machine_adc.c b/ports/stm32/machine_adc.c index f41b80e0ad7b..4b644e6c9711 100644 --- a/ports/stm32/machine_adc.c +++ b/ports/stm32/machine_adc.c @@ -24,7 +24,9 @@ * THE SOFTWARE. */ -#include "py/runtime.h" +// This file is never compiled standalone, it's included directly from +// extmod/machine_adc.c via MICROPY_PY_MACHINE_ADC_INCLUDEFILE. + #include "py/mphal.h" #include "adc.h" @@ -490,7 +492,26 @@ uint32_t adc_config_and_read_u16(ADC_TypeDef *adc, uint32_t channel, uint32_t sa #if !BUILDING_MBOOT -const mp_obj_type_t machine_adc_type; +#if defined(ADC_CHANNEL_VBAT) +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VBAT \ + { MP_ROM_QSTR(MP_QSTR_CORE_VBAT), MP_ROM_INT(MACHINE_ADC_INT_CH_VBAT) }, +#else +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VBAT +#endif + +#if defined(ADC_CHANNEL_VDDCORE) +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VDD \ + { MP_ROM_QSTR(MP_QSTR_CORE_VDD), MP_ROM_INT(MACHINE_ADC_INT_CH_VDDCORE) }, +#else +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VDD +#endif + +#define MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS \ + { MP_ROM_QSTR(MP_QSTR_VREF), MP_ROM_INT(MACHINE_ADC_CH_VREF) }, \ + { MP_ROM_QSTR(MP_QSTR_CORE_VREF), MP_ROM_INT(MACHINE_ADC_INT_CH_VREFINT) }, \ + { MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(MACHINE_ADC_INT_CH_TEMPSENSOR) }, \ + MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VBAT \ + MICROPY_PY_MACHINE_ADC_CLASS_CONSTANTS_CORE_VDD \ typedef struct _machine_adc_obj_t { mp_obj_base_t base; @@ -499,7 +520,7 @@ typedef struct _machine_adc_obj_t { uint32_t sample_time; } machine_adc_obj_t; -STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { +STATIC void mp_machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); unsigned adc_id = 1; #if defined(ADC2) @@ -516,7 +537,7 @@ STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_prin } // ADC(id) -STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { +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) { // Check number of arguments mp_arg_check_num(n_args, n_kw, 1, 1, false); @@ -586,34 +607,8 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, s } // read_u16() -STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { - machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(adc_config_and_read_u16(self->adc, self->channel, self->sample_time)); +STATIC mp_int_t mp_machine_adc_read_u16(machine_adc_obj_t *self) { + return adc_config_and_read_u16(self->adc, self->channel, self->sample_time); } -MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16); - -STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { - { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) }, - - { MP_ROM_QSTR(MP_QSTR_VREF), MP_ROM_INT(MACHINE_ADC_CH_VREF) }, - { MP_ROM_QSTR(MP_QSTR_CORE_VREF), MP_ROM_INT(MACHINE_ADC_INT_CH_VREFINT) }, - { MP_ROM_QSTR(MP_QSTR_CORE_TEMP), MP_ROM_INT(MACHINE_ADC_INT_CH_TEMPSENSOR) }, - #if defined(ADC_CHANNEL_VBAT) - { MP_ROM_QSTR(MP_QSTR_CORE_VBAT), MP_ROM_INT(MACHINE_ADC_INT_CH_VBAT) }, - #endif - #if defined(ADC_CHANNEL_VDDCORE) - { MP_ROM_QSTR(MP_QSTR_CORE_VDD), MP_ROM_INT(MACHINE_ADC_INT_CH_VDDCORE) }, - #endif -}; -STATIC MP_DEFINE_CONST_DICT(machine_adc_locals_dict, machine_adc_locals_dict_table); - -MP_DEFINE_CONST_OBJ_TYPE( - machine_adc_type, - MP_QSTR_ADC, - MP_TYPE_FLAG_NONE, - make_new, machine_adc_make_new, - print, machine_adc_print, - locals_dict, &machine_adc_locals_dict - ); #endif diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index 630327d7002b..54fc8f483082 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -109,6 +109,7 @@ LIB_SRC_C += \ lib/uzlib/tinflate.c SRC_C += \ + adc.c \ main.c \ elem.c \ fsload.c \ @@ -124,7 +125,6 @@ SRC_C += \ ports/stm32/flash.c \ ports/stm32/flashbdev.c \ ports/stm32/i2cslave.c \ - ports/stm32/machine_adc.c \ ports/stm32/powerctrlboot.c \ ports/stm32/qspi.c \ ports/stm32/spibdev.c \ diff --git a/ports/stm32/mboot/adc.c b/ports/stm32/mboot/adc.c new file mode 100644 index 000000000000..c7b9749244d4 --- /dev/null +++ b/ports/stm32/mboot/adc.c @@ -0,0 +1,3 @@ +// Include the main ADC driver, so mboot can use adc_config() and adc_config_and_read_u16(). +#include "py/obj.h" +#include "../machine_adc.c" diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index a0e2da89f896..070d0e11694f 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -430,7 +430,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_Signal), MP_ROM_PTR(&machine_signal_type) }, { MP_ROM_QSTR(MP_QSTR_RTC), MP_ROM_PTR(&pyb_rtc_type) }, + #if MICROPY_PY_MACHINE_ADC { MP_ROM_QSTR(MP_QSTR_ADC), MP_ROM_PTR(&machine_adc_type) }, + #endif #if MICROPY_PY_MACHINE_I2C #if MICROPY_HW_ENABLE_HW_I2C { MP_ROM_QSTR(MP_QSTR_I2C), MP_ROM_PTR(&machine_i2c_type) }, diff --git a/ports/stm32/modmachine.h b/ports/stm32/modmachine.h index 9838eafdb0f6..882057c60131 100644 --- a/ports/stm32/modmachine.h +++ b/ports/stm32/modmachine.h @@ -28,7 +28,6 @@ #include "py/obj.h" -extern const mp_obj_type_t machine_adc_type; extern const mp_obj_type_t machine_i2c_type; extern const mp_obj_type_t machine_spi_type; extern const mp_obj_type_t machine_timer_type; diff --git a/ports/stm32/mpconfigport.h b/ports/stm32/mpconfigport.h index c27302d9bf1e..0cb929f5582a 100644 --- a/ports/stm32/mpconfigport.h +++ b/ports/stm32/mpconfigport.h @@ -110,6 +110,8 @@ #define MICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP) #ifndef MICROPY_PY_MACHINE #define MICROPY_PY_MACHINE (1) +#define MICROPY_PY_MACHINE_ADC (1) +#define MICROPY_PY_MACHINE_ADC_INCLUDEFILE "ports/stm32/machine_adc.c" #ifndef MICROPY_PY_MACHINE_BITSTREAM #define MICROPY_PY_MACHINE_BITSTREAM (1) #endif