From b208cf23e290c169bf346137c30bc2a0176a223d Mon Sep 17 00:00:00 2001 From: Paul Grayson Date: Sat, 24 Dec 2022 09:55:12 -0800 Subject: [PATCH] rp2/mphalport: Change order of pin operations to prevent glitches. When switching from a special function like SPI to an input or output, there was a brief period after the function was disabled but before the pin's I/O state was configured, in which the state would be poorly defined. This fixes the problem by switching off the special function after fully configuring the I/O state. Fixes #10226. Signed-off-by: Paul Grayson --- ports/rp2/mphalport.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/rp2/mphalport.h b/ports/rp2/mphalport.h index 73a503f5a9..2cb9121fa2 100644 --- a/ports/rp2/mphalport.h +++ b/ports/rp2/mphalport.h @@ -91,22 +91,22 @@ static inline unsigned int mp_hal_pin_name(mp_hal_pin_obj_t pin) { } static inline void mp_hal_pin_input(mp_hal_pin_obj_t pin) { - gpio_set_function(pin, GPIO_FUNC_SIO); gpio_set_dir(pin, GPIO_IN); machine_pin_open_drain_mask &= ~(1 << pin); + gpio_set_function(pin, GPIO_FUNC_SIO); } static inline void mp_hal_pin_output(mp_hal_pin_obj_t pin) { - gpio_set_function(pin, GPIO_FUNC_SIO); 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) { - gpio_set_function(pin, GPIO_FUNC_SIO); gpio_set_dir(pin, GPIO_IN); gpio_put(pin, 0); machine_pin_open_drain_mask |= 1 << pin; + gpio_set_function(pin, GPIO_FUNC_SIO); } static inline void mp_hal_pin_config(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint32_t alt) {