samd/machine_pin: Add OPEN_DRAIN mode for pins.

Changes in this commit are:
- Use mphal_xx functions whenever possible.
- Remove obsolete includes.
- Clean up traces of a non-functional pin.irq() from earlier builds.
  Pin.irq() will be added in further commits in a working manner.
This commit is contained in:
robert-hh 2022-06-04 20:33:48 +02:00 committed by Damien George
parent 98ae312640
commit 33eaf739d2
2 changed files with 50 additions and 78 deletions

View File

@ -31,35 +31,25 @@
#include "extmod/virtpin.h" #include "extmod/virtpin.h"
#include "modmachine.h" #include "modmachine.h"
#include "samd_soc.h" #include "samd_soc.h"
#include "pins.h" // boards/<BOARD>/ #include "pins.h"
// ASF4 (MCU package specific pin defs in 'boards')
#include "hal_gpio.h" #include "hal_gpio.h"
#include "hpl_gpio.h"
#include "hal_atomic.h"
#define GPIO_MODE_IN (0) #define GPIO_MODE_IN (0)
#define GPIO_MODE_OUT (1) #define GPIO_MODE_OUT (1)
// #define GPIO_MODE_ALT (3) #define GPIO_MODE_OPEN_DRAIN (2)
#define GPIO_STRENGTH_2MA (0) #define GPIO_STRENGTH_2MA (0)
#define GPIO_STRENGTH_8MA (1) #define GPIO_STRENGTH_8MA (1)
// asf4 hpl_gpio.h gpio_pull_mode uint32_t machine_pin_open_drain_mask[4];
/* // Open drain behaviour is simulated.
typedef struct _machine_pin_irq_obj_t { #define GPIO_IS_OPEN_DRAIN(id) (machine_pin_open_drain_mask[id / 32] & (1 << (id % 32)))
mp_irq_obj_t base;
uint32_t flags;
uint32_t trigger;
} machine_pin_irq_obj_t;
STATIC const mp_irq_methods_t machine_pin_irq_methods;
*/
STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { STATIC void machine_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
machine_pin_obj_t *self = self_in; machine_pin_obj_t *self = self_in;
mp_printf(print, "Pin(%u)", self->id); mp_printf(print, "GPIO P%c%02u", "ABCD"[self->id / 32], self->id % 32);
} }
STATIC void pin_validate_drive(bool strength) { STATIC void pin_validate_drive(bool strength) {
@ -84,24 +74,26 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_
// set initial value (do this before configuring mode/pull) // set initial value (do this before configuring mode/pull)
if (args[ARG_value].u_obj != mp_const_none) { if (args[ARG_value].u_obj != mp_const_none) {
gpio_set_pin_level(self->id, mp_obj_is_true(args[ARG_value].u_obj)); mp_hal_pin_write(self->id, mp_obj_is_true(args[ARG_value].u_obj));
} }
// configure mode // configure mode
if (args[ARG_mode].u_obj != mp_const_none) { if (args[ARG_mode].u_obj != mp_const_none) {
mp_int_t mode = mp_obj_get_int(args[ARG_mode].u_obj); mp_int_t mode = mp_obj_get_int(args[ARG_mode].u_obj);
if (mode == GPIO_MODE_IN) { if (mode == GPIO_MODE_IN) {
gpio_set_pin_direction(self->id, GPIO_DIRECTION_IN); mp_hal_pin_input(self->id);
} else if (mode == GPIO_MODE_OUT) { } else if (mode == GPIO_MODE_OUT) {
gpio_set_pin_direction(self->id, GPIO_DIRECTION_OUT); mp_hal_pin_output(self->id);
} else if (mode == GPIO_MODE_OPEN_DRAIN) {
mp_hal_pin_open_drain(self->id);
} else { } else {
gpio_set_pin_direction(self->id, GPIO_DIRECTION_IN); // If no args are given, the Pin is 'input'. mp_hal_pin_input(self->id); // If no args are given, the Pin is 'input'.
} }
} }
// configure pull. Only to be used with IN mode. The function sets the pin to INPUT. // configure pull. Only to be used with IN mode. The function sets the pin to INPUT.
uint32_t pull = 0; uint32_t pull = 0;
mp_int_t mode = mp_obj_get_int(args[ARG_mode].u_obj); mp_int_t dir = mp_hal_get_pin_direction(self->id);
if (mode == GPIO_MODE_OUT && args[ARG_pull].u_obj != mp_const_none) { if (dir == GPIO_DIRECTION_OUT && args[ARG_pull].u_obj != mp_const_none) {
mp_raise_ValueError(MP_ERROR_TEXT("OUT incompatible with pull")); mp_raise_ValueError(MP_ERROR_TEXT("OUT incompatible with pull"));
} else if (args[ARG_pull].u_obj != mp_const_none) { } else if (args[ARG_pull].u_obj != mp_const_none) {
pull = mp_obj_get_int(args[ARG_pull].u_obj); pull = mp_obj_get_int(args[ARG_pull].u_obj);
@ -143,17 +135,24 @@ mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw,
} }
// fast method for getting/setting pin value // fast method for getting/setting pin value
STATIC mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) { mp_obj_t machine_pin_call(mp_obj_t self_in, size_t n_args, size_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false); mp_arg_check_num(n_args, n_kw, 0, 1, false);
machine_pin_obj_t *self = self_in; machine_pin_obj_t *self = self_in;
if (n_args == 0) { if (n_args == 0) {
// get pin // get pin
return MP_OBJ_NEW_SMALL_INT(gpio_get_pin_level(self->id)); return MP_OBJ_NEW_SMALL_INT(mp_hal_pin_read(self->id));
} else { } else {
// set pin // set pin
bool value = mp_obj_is_true(args[0]); bool value = mp_obj_is_true(args[0]);
gpio_set_pin_level(self->id, value); if (GPIO_IS_OPEN_DRAIN(self->id)) {
if (value == 0) {
mp_hal_pin_od_low(self->id);
} else {
mp_hal_pin_od_high(self->id);
}
} else {
mp_hal_pin_write(self->id, value);
}
return mp_const_none; return mp_const_none;
} }
} }
@ -165,7 +164,7 @@ STATIC mp_obj_t machine_pin_obj_init(size_t n_args, const mp_obj_t *args, mp_map
MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_init_obj, 1, machine_pin_obj_init); MP_DEFINE_CONST_FUN_OBJ_KW(machine_pin_init_obj, 1, machine_pin_obj_init);
// Pin.value([value]) // Pin.value([value])
STATIC mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) { mp_obj_t machine_pin_value(size_t n_args, const mp_obj_t *args) {
return machine_pin_call(args[0], n_args - 1, 0, args + 1); return machine_pin_call(args[0], n_args - 1, 0, args + 1);
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_pin_value); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pin_value_obj, 1, 2, machine_pin_value);
@ -181,9 +180,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_disable_obj, machine_pin_disable);
// Pin.low() Totem-pole (push-pull) // Pin.low() Totem-pole (push-pull)
STATIC mp_obj_t machine_pin_low(mp_obj_t self_in) { STATIC mp_obj_t machine_pin_low(mp_obj_t self_in) {
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
gpio_set_pin_direction(self->id, GPIO_DIRECTION_OUT); if (GPIO_IS_OPEN_DRAIN(self->id)) {
gpio_set_pin_level(self->id, false); mp_hal_pin_od_low(self->id);
} else {
mp_hal_pin_low(self->id);
}
return mp_const_none; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_low_obj, machine_pin_low); STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_low_obj, machine_pin_low);
@ -191,9 +192,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_low_obj, machine_pin_low);
// Pin.high() Totem-pole (push-pull) // Pin.high() Totem-pole (push-pull)
STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) { STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) {
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in); machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
gpio_set_pin_direction(self->id, GPIO_DIRECTION_OUT); if (GPIO_IS_OPEN_DRAIN(self->id)) {
gpio_set_pin_level(self->id, true); mp_hal_pin_od_high(self->id);
} else {
mp_hal_pin_high(self->id);
}
return mp_const_none; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_high_obj, machine_pin_high); STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_high_obj, machine_pin_high);
@ -205,18 +208,18 @@ STATIC mp_obj_t machine_pin_toggle(mp_obj_t self_in) {
// Determine DIRECTION of PIN. // Determine DIRECTION of PIN.
bool pin_dir; bool pin_dir;
pin_dir = (PORT->Group[(enum gpio_port)GPIO_PORT(self->id)].DIR.reg // Get PORT# if (GPIO_IS_OPEN_DRAIN(self->id)) {
& (1 << GPIO_PIN(self->id))) // Isolate the Pin in question pin_dir = mp_hal_get_pin_direction(self->id);
>> GPIO_PIN(self->id); // Shift to LSB for binary result. if (pin_dir) {
// Pin is output, thus low, switch to high
if (pin_dir) { mp_hal_pin_od_high(self->id);
// Pin is OUTPUT } else {
gpio_set_pin_direction(self->id, GPIO_DIRECTION_OUT); mp_hal_pin_od_low(self->id);
gpio_toggle_pin_level(self->id); }
} else { } else {
mp_raise_ValueError(MP_ERROR_TEXT("Cannot TOGGLE INPUT pin!\n")); gpio_toggle_pin_level(self->id);
} }
return mp_const_true; return mp_const_none;
} }
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_toggle_obj, machine_pin_toggle); STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pin_toggle_obj, machine_pin_toggle);
@ -250,20 +253,15 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&machine_pin_disable_obj) }, { MP_ROM_QSTR(MP_QSTR_disable), MP_ROM_PTR(&machine_pin_disable_obj) },
{ MP_ROM_QSTR(MP_QSTR_drive), MP_ROM_PTR(&machine_pin_drive_obj) }, { 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) },
// class constants // class constants
{ MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_MODE_IN) }, { MP_ROM_QSTR(MP_QSTR_IN), MP_ROM_INT(GPIO_MODE_IN) },
{ MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(GPIO_MODE_OUT) }, { MP_ROM_QSTR(MP_QSTR_OUT), MP_ROM_INT(GPIO_MODE_OUT) },
// { MP_ROM_QSTR(MP_QSTR_ALT), MP_ROM_INT(GPIO_MODE_ALT) }, { MP_ROM_QSTR(MP_QSTR_OPEN_DRAIN), MP_ROM_INT(GPIO_MODE_OPEN_DRAIN) },
{ MP_ROM_QSTR(MP_QSTR_PULL_OFF), MP_ROM_INT(GPIO_PULL_OFF) }, { MP_ROM_QSTR(MP_QSTR_PULL_OFF), MP_ROM_INT(GPIO_PULL_OFF) },
{ MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULL_UP) }, { MP_ROM_QSTR(MP_QSTR_PULL_UP), MP_ROM_INT(GPIO_PULL_UP) },
{ MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULL_DOWN) }, { MP_ROM_QSTR(MP_QSTR_PULL_DOWN), MP_ROM_INT(GPIO_PULL_DOWN) },
{ MP_ROM_QSTR(MP_QSTR_LOW_POWER), MP_ROM_INT(GPIO_STRENGTH_2MA) }, { MP_ROM_QSTR(MP_QSTR_LOW_POWER), MP_ROM_INT(GPIO_STRENGTH_2MA) },
{ MP_ROM_QSTR(MP_QSTR_HIGH_POWER), MP_ROM_INT(GPIO_STRENGTH_8MA) }, { MP_ROM_QSTR(MP_QSTR_HIGH_POWER), MP_ROM_INT(GPIO_STRENGTH_8MA) },
// { MP_ROM_QSTR(MP_QSTR_IRQ_RISING), MP_ROM_INT(GPIO_IRQ_EDGE_RISE) },
// { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_IRQ_EDGE_FALL) },
}; };
STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table); STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table);
@ -298,34 +296,6 @@ MP_DEFINE_CONST_OBJ_TYPE(
locals_dict, &machine_pin_locals_dict locals_dict, &machine_pin_locals_dict
); );
/*
STATIC mp_uint_t machine_pin_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) {
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_obj[self->id]);
gpio_set_irq_enabled(self->id, GPIO_IRQ_ALL, false);
irq->flags = 0;
irq->trigger = new_trigger;
gpio_set_irq_enabled(self->id, new_trigger, true);
return 0;
}
STATIC mp_uint_t machine_pin_irq_info(mp_obj_t self_in, mp_uint_t info_type) {
machine_pin_obj_t *self = MP_OBJ_TO_PTR(self_in);
machine_pin_irq_obj_t *irq = MP_STATE_PORT(machine_pin_irq_obj[self->id]);
if (info_type == MP_IRQ_INFO_FLAGS) {
return irq->flags;
} else if (info_type == MP_IRQ_INFO_TRIGGERS) {
return irq->trigger;
}
return 0;
}
STATIC const mp_irq_methods_t machine_pin_irq_methods = {
.trigger = machine_pin_irq_trigger,
.info = machine_pin_irq_info,
};
*/
mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj) { mp_hal_pin_obj_t mp_hal_get_pin_obj(mp_obj_t obj) {
if (!mp_obj_is_type(obj, &machine_pin_type)) { if (!mp_obj_is_type(obj, &machine_pin_type)) {
mp_raise_ValueError(MP_ERROR_TEXT("expecting a Pin")); mp_raise_ValueError(MP_ERROR_TEXT("expecting a Pin"));

View File

@ -101,7 +101,7 @@ static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) {
static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) { static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) {
gpio_set_pin_direction(pin, GPIO_DIRECTION_IN); gpio_set_pin_direction(pin, GPIO_DIRECTION_IN);
gpio_set_pin_level(pin, 0); gpio_set_pin_pull_mode(pin, GPIO_PULL_UP);
machine_pin_open_drain_mask[pin / 32] |= 1 << (pin % 32); machine_pin_open_drain_mask[pin / 32] |= 1 << (pin % 32);
} }
@ -127,10 +127,12 @@ static inline void mp_hal_pin_high(mp_hal_pin_obj_t pin) {
static inline void mp_hal_pin_od_low(mp_hal_pin_obj_t pin) { static inline void mp_hal_pin_od_low(mp_hal_pin_obj_t pin) {
gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT); gpio_set_pin_direction(pin, GPIO_DIRECTION_OUT);
gpio_set_pin_level(pin, 0);
} }
static inline void mp_hal_pin_od_high(mp_hal_pin_obj_t pin) { static inline void mp_hal_pin_od_high(mp_hal_pin_obj_t pin) {
gpio_set_pin_direction(pin, GPIO_DIRECTION_IN); gpio_set_pin_direction(pin, GPIO_DIRECTION_IN);
gpio_set_pin_pull_mode(pin, GPIO_PULL_UP);
} }
#endif // MICROPY_INCLUDED_SAMD_MPHALPORT_H #endif // MICROPY_INCLUDED_SAMD_MPHALPORT_H