rp2/machine_pin: Fix configuring OPEN_DRAIN with initial value.

Prior to this commit, Pin(Pin.OPEN_DRAIN, value=0) would not set the
initial value of the open-drain pin to low, instead it would be high.

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2023-01-24 17:29:45 +11:00
parent 8a0353525f
commit 67fac4ebc5
2 changed files with 18 additions and 8 deletions

View File

@ -236,13 +236,10 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_
mp_raise_ValueError("alternate functions are not supported for external pins");
}
// get initial value of pin (only valid for OUT and OPEN_DRAIN modes)
int value = -1;
if (args[ARG_value].u_obj != mp_const_none) {
value = mp_obj_is_true(args[ARG_value].u_obj);
// set initial value (do this before configuring mode/pull)
if (!is_ext_pin(self)) {
gpio_put(self->id, value);
}
}
// configure mode
@ -257,9 +254,13 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_
} else if (mode == MACHINE_PIN_MODE_IN) {
mp_hal_pin_input(self->id);
} else if (mode == MACHINE_PIN_MODE_OUT) {
if (value != -1) {
// set initial output value before configuring mode
gpio_put(self->id, value);
}
mp_hal_pin_output(self->id);
} else if (mode == MACHINE_PIN_MODE_OPEN_DRAIN) {
mp_hal_pin_open_drain(self->id);
mp_hal_pin_open_drain_with_value(self->id, value == -1 ? 1 : value);
} else {
// Configure alternate function.
mp_uint_t af = args[ARG_alt].u_int;

View File

@ -102,13 +102,22 @@ static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) {
gpio_set_function(pin, GPIO_FUNC_SIO);
}
static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) {
gpio_set_dir(pin, GPIO_IN);
gpio_put(pin, 0);
static inline void mp_hal_pin_open_drain_with_value(mp_hal_pin_obj_t pin, int v) {
if (v) {
gpio_set_dir(pin, GPIO_IN);
gpio_put(pin, 0);
} else {
gpio_put(pin, 0);
gpio_set_dir(pin, GPIO_OUT);
}
machine_pin_open_drain_mask |= 1 << pin;
gpio_set_function(pin, GPIO_FUNC_SIO);
}
static inline void mp_hal_pin_open_drain(mp_hal_pin_obj_t pin) {
mp_hal_pin_open_drain_with_value(pin, 1);
}
static inline void mp_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint32_t alt) {
assert((mode == MP_HAL_PIN_MODE_INPUT || mode == MP_HAL_PIN_MODE_OUTPUT) && alt == 0);
gpio_set_dir(pin, mode);