Skip to content

Commit

Permalink
samd: Configure Pin.cpu and Pin.board separately.
Browse files Browse the repository at this point in the history
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 <robert@hammelrath.com>
  • Loading branch information
robert-hh committed Aug 10, 2023
1 parent ea11431 commit eb98259
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 34 deletions.
4 changes: 3 additions & 1 deletion ports/samd/boards/make-pin-table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
14 changes: 9 additions & 5 deletions ports/samd/machine_pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;

Expand Down Expand Up @@ -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) },
Expand Down
7 changes: 5 additions & 2 deletions ports/samd/mcu/samd21/mpconfigmcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions ports/samd/mcu/samd51/mpconfigmcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 5 additions & 23 deletions ports/samd/pin_af.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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"));
}
Expand Down
4 changes: 3 additions & 1 deletion ports/samd/pin_af.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit eb98259

Please sign in to comment.