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 <damien@micropython.org>
This commit is contained in:
Damien George
2023-10-11 13:48:49 +11:00
parent 48e0986666
commit 95d8b5fd55
47 changed files with 434 additions and 346 deletions

View File

@@ -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

View File

@@ -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, "<ADC channel=%u>", 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
);

View File

@@ -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) },

View File

@@ -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;

View File

@@ -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)