From eb98259aed471c8d4935f7fb550e6fa92039b3c9 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Tue, 18 Jul 2023 13:30:55 +0200 Subject: [PATCH] samd: Configure Pin.cpu and Pin.board separately. Enabling Pin.cpu by default only for SAMD51. Enabling Pin.board by default for both SAMD21 and SAMD51. Saves ~650 bytes for SAMD21. Signed-off-by: robert-hh --- ports/samd/boards/make-pin-table.py | 4 +++- ports/samd/machine_pin.c | 14 +++++++++----- ports/samd/mcu/samd21/mpconfigmcu.h | 7 +++++-- ports/samd/mcu/samd51/mpconfigmcu.h | 7 +++++-- ports/samd/pin_af.c | 28 +++++----------------------- ports/samd/pin_af.h | 4 +++- 6 files changed, 30 insertions(+), 34 deletions(-) diff --git a/ports/samd/boards/make-pin-table.py b/ports/samd/boards/make-pin-table.py index a9d653db6576..34a2cf1cc115 100644 --- a/ports/samd/boards/make-pin-table.py +++ b/ports/samd/boards/make-pin-table.py @@ -105,7 +105,7 @@ def print_table(self, table_filename, mcu_name): # Create the CPU pins dictionary table - table_file.write("\n#if MICROPY_PY_MACHINE_PIN_BOARD_CPU\n") + table_file.write("\n#if MICROPY_PY_MACHINE_PIN_CPU\n") table_file.write("\n// The cpu pins dictionary\n\n") table_file.write( "STATIC const mp_rom_map_elem_t pin_cpu_pins_locals_dict_table[] = {\n" @@ -122,9 +122,11 @@ def print_table(self, table_filename, mcu_name): table_file.write( "MP_DEFINE_CONST_DICT(machine_pin_cpu_pins_locals_dict, pin_cpu_pins_locals_dict_table);\n" ) + table_file.write("#endif\n") # Create the board pins dictionary table + table_file.write("\n#if MICROPY_PY_MACHINE_PIN_BOARD\n") table_file.write("\n// The board pins dictionary\n\n") table_file.write( "STATIC const mp_rom_map_elem_t pin_board_pins_locals_dict_table[] = {\n" diff --git a/ports/samd/machine_pin.c b/ports/samd/machine_pin.c index f8e1b962804c..ccf897e06691 100644 --- a/ports/samd/machine_pin.c +++ b/ports/samd/machine_pin.c @@ -54,7 +54,7 @@ typedef struct _machine_pin_irq_obj_t { uint8_t pin_id; } machine_pin_irq_obj_t; -#if MICROPY_PY_MACHINE_PIN_BOARD_CPU +#if MICROPY_PY_MACHINE_PIN_CPU // Pin mapping dictionaries MP_DEFINE_CONST_OBJ_TYPE( machine_pin_cpu_pins_obj_type, @@ -63,13 +63,15 @@ MP_DEFINE_CONST_OBJ_TYPE( locals_dict, &machine_pin_cpu_pins_locals_dict ); +#endif // MICROPY_PY_MACHINE_PIN_CPU +#if MICROPY_PY_MACHINE_PIN_BOARD MP_DEFINE_CONST_OBJ_TYPE( machine_pin_board_pins_obj_type, MP_QSTR_board, MP_TYPE_FLAG_NONE, locals_dict, &machine_pin_board_pins_locals_dict ); -#endif // MICROPY_PY_MACHINE_PIN_BOARD_CPU +#endif // MICROPY_PY_MACHINE_PIN_BOARD STATIC const mp_irq_methods_t machine_pin_irq_methods; @@ -428,11 +430,13 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_drive), MP_ROM_PTR(&machine_pin_drive_obj) }, { MP_ROM_QSTR(MP_QSTR_irq), MP_ROM_PTR(&machine_pin_irq_obj) }, - #if MICROPY_PY_MACHINE_PIN_BOARD_CPU + #if MICROPY_PY_MACHINE_PIN_CPU // class attributes - { MP_ROM_QSTR(MP_QSTR_board), MP_ROM_PTR(&machine_pin_board_pins_obj_type) }, { MP_ROM_QSTR(MP_QSTR_cpu), MP_ROM_PTR(&machine_pin_cpu_pins_obj_type) }, - #endif // MICROPY_PY_MACHINE_PIN_BOARD_CPU + #endif // MICROPY_PY_MACHINE_PIN_CPU + #if MICROPY_PY_MACHINE_PIN_BOARD + { MP_ROM_QSTR(MP_QSTR_board), MP_ROM_PTR(&machine_pin_board_pins_obj_type) }, + #endif // MICROPY_PY_MACHINE_PIN_BOARD // class constants { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_MODE_IN) }, diff --git a/ports/samd/mcu/samd21/mpconfigmcu.h b/ports/samd/mcu/samd21/mpconfigmcu.h index f70981acd53c..3b2040f3157e 100644 --- a/ports/samd/mcu/samd21/mpconfigmcu.h +++ b/ports/samd/mcu/samd21/mpconfigmcu.h @@ -37,8 +37,11 @@ unsigned long trng_random_u32(int delay); #define MICROPY_PY_OS_URANDOM (1) -#ifndef MICROPY_PY_MACHINE_PIN_BOARD_CPU -#define MICROPY_PY_MACHINE_PIN_BOARD_CPU (1) +#ifndef MICROPY_PY_MACHINE_PIN_CPU +#define MICROPY_PY_MACHINE_PIN_CPU (0) +#endif +#ifndef MICROPY_PY_MACHINE_PIN_BOARD +#define MICROPY_PY_MACHINE_PIN_BOARD (1) #endif #define CPU_FREQ (48000000) diff --git a/ports/samd/mcu/samd51/mpconfigmcu.h b/ports/samd/mcu/samd51/mpconfigmcu.h index 399b48c040d6..0dd11b15033e 100644 --- a/ports/samd/mcu/samd51/mpconfigmcu.h +++ b/ports/samd/mcu/samd51/mpconfigmcu.h @@ -30,8 +30,11 @@ #define MICROPY_PY_RANDOM_SEED_INIT_FUNC (trng_random_u32()) unsigned long trng_random_u32(void); -#ifndef MICROPY_PY_MACHINE_PIN_BOARD_CPU -#define MICROPY_PY_MACHINE_PIN_BOARD_CPU (1) +#ifndef MICROPY_PY_MACHINE_PIN_CPU +#define MICROPY_PY_MACHINE_PIN_CPU (1) +#endif +#ifndef MICROPY_PY_MACHINE_PIN_BOARD +#define MICROPY_PY_MACHINE_PIN_BOARD (1) #endif // fatfs configuration used in ffconf.h diff --git a/ports/samd/pin_af.c b/ports/samd/pin_af.c index 419b81477601..e5098c1c2317 100644 --- a/ports/samd/pin_af.c +++ b/ports/samd/pin_af.c @@ -54,7 +54,7 @@ const machine_pin_obj_t *get_pin_obj_ptr(int pin_id) { mp_raise_ValueError(MP_ERROR_TEXT("not a Pin")); } -#if MICROPY_PY_MACHINE_PIN_BOARD_CPU +#if MICROPY_PY_MACHINE_PIN_BOARD || MICROPY_PY_MACHINE_PIN_CPU STATIC const machine_pin_obj_t *pin_find_named_pin(const mp_obj_dict_t *named_pins, mp_obj_t name) { mp_map_t *named_map = mp_obj_dict_get_map((mp_obj_t)named_pins); mp_map_elem_t *named_elem = mp_map_lookup(named_map, name, MP_MAP_LOOKUP); @@ -75,39 +75,21 @@ const machine_pin_obj_t *pin_find(mp_obj_t pin) { return get_pin_obj_ptr(mp_obj_get_int(pin)); } - #if MICROPY_PY_MACHINE_PIN_BOARD_CPU + #if MICROPY_PY_MACHINE_PIN_BOARD const machine_pin_obj_t *self = NULL; // See if the pin name matches a board pin self = pin_find_named_pin(&machine_pin_board_pins_locals_dict, pin); if (self != NULL) { return self; } + #endif + #if MICROPY_PY_MACHINE_PIN_CPU // See if the pin name matches a cpu pin self = pin_find_named_pin(&machine_pin_cpu_pins_locals_dict, pin); if (self != NULL) { return self; } - #else - if (mp_obj_is_str(pin)) { - // Search by name - size_t slen; - const char *s = mp_obj_str_get_data(pin, &slen); - // Check for a string like PA02 or PD12 - if (slen == 4 && s[0] == 'P' && strchr("ABCD", s[1]) != NULL && - strchr("0123456789", s[2]) != NULL && strchr("0123456789", s[2]) != NULL) { - int num = (s[1] - 'A') * 32 + (s[2] - '0') * 10 + (s[3] - '0'); - return get_pin_obj_ptr(num); - } else { - for (int i = 0; i < MP_ARRAY_SIZE(pin_af_table); i++) { - size_t len; - const char *name = (char *)qstr_data(pin_af_table[i]->name, &len); - if (slen == len && strncmp(s, name, slen) == 0) { - return pin_af_table[i]; - } - } - } - } - #endif // MICROPY_PY_MACHINE_PIN_BOARD_CPU + #endif // MICROPY_PY_MACHINE_PIN_CPU mp_raise_ValueError(MP_ERROR_TEXT("not a Pin")); } diff --git a/ports/samd/pin_af.h b/ports/samd/pin_af.h index 3a15ae7f356a..54e9f653e519 100644 --- a/ports/samd/pin_af.h +++ b/ports/samd/pin_af.h @@ -92,8 +92,10 @@ typedef struct _pwm_config_t { #define ALT_FCT_SERCOM1 2 #define ALT_FCT_SERCOM2 3 -#if MICROPY_PY_MACHINE_PIN_BOARD_CPU +#if MICROPY_PY_MACHINE_PIN_CPU extern const mp_obj_dict_t machine_pin_cpu_pins_locals_dict; +#endif +#if MICROPY_PY_MACHINE_PIN_BOARD extern const mp_obj_dict_t machine_pin_board_pins_locals_dict; #endif