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

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

View File

@@ -24,9 +24,9 @@
* THE SOFTWARE.
*/
#include <stdint.h>
#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
);

View File

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

View File

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

View File

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