Skip to content

Commit 71afed1

Browse files
iabdalkaderdpgeorge
authored andcommitted
stm32/mphalport: Fix I/O glitches when switching GPIO mode to alt-func.
To avoid any I/O glitches in mp_hal_pin_config, make sure a valid alternate function is set in AFR first before switching the pin mode. When switching from AF to INPUT or OUTPUT, the AF in AFR will remain valid up until the pin mode is switched.
1 parent b50fe9a commit 71afed1

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

ports/stm32/mphalport.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ void mp_hal_pin_config(mp_hal_pin_obj_t pin_obj, uint32_t mode, uint32_t pull, u
114114
GPIO_TypeDef *gpio = pin_obj->gpio;
115115
uint32_t pin = pin_obj->pin;
116116
mp_hal_gpio_clock_enable(gpio);
117+
if (mode == MP_HAL_PIN_MODE_ALT || mode == MP_HAL_PIN_MODE_ALT_OPEN_DRAIN) {
118+
// To avoid any I/O glitches, make sure a valid alternate function is set in
119+
// AFR first before switching the pin mode. When switching from AF to INPUT or
120+
// OUTPUT, the AF in AFR will remain valid up until the pin mode is switched.
121+
gpio->AFR[pin >> 3] = (gpio->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7)));
122+
}
117123
gpio->MODER = (gpio->MODER & ~(3 << (2 * pin))) | ((mode & 3) << (2 * pin));
118124
#if defined(GPIO_ASCR_ASC0)
119125
// The L4 has a special analog switch to connect the GPIO to the ADC
@@ -124,7 +130,6 @@ void mp_hal_pin_config(mp_hal_pin_obj_t pin_obj, uint32_t mode, uint32_t pull, u
124130
#endif
125131
gpio->OSPEEDR = (gpio->OSPEEDR & ~(3 << (2 * pin))) | (2 << (2 * pin)); // full speed
126132
gpio->PUPDR = (gpio->PUPDR & ~(3 << (2 * pin))) | (pull << (2 * pin));
127-
gpio->AFR[pin >> 3] = (gpio->AFR[pin >> 3] & ~(15 << (4 * (pin & 7)))) | (alt << (4 * (pin & 7)));
128133
}
129134

130135
bool mp_hal_pin_config_alt(mp_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, uint8_t fn, uint8_t unit) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy