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

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

View File

@@ -24,15 +24,10 @@
* THE SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
// 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

View File

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

View File

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

View File

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