Add gpio_set_af to set alternative function of a pin

This commit is contained in:
Alexander Heldt
2025-01-01 12:23:18 +01:00
parent 3f95f00852
commit 992b3c5b97
10 changed files with 549 additions and 335 deletions

View File

@@ -2020,10 +2020,11 @@ typedef enum {
} GPIO_MODE;
void gpio_set_mode(uint16_t pin, GPIO_MODE mode);
void gpio_set_af(uint16_t pin, uint8_t af);
void gpio_write(uint16_t pin,
# 41 "src/gpio.h" 3 4
# 42 "src/gpio.h" 3 4
_Bool
# 41 "src/gpio.h"
# 42 "src/gpio.h"
val);
# 5 "src/gpio.c" 2
@@ -2034,10 +2035,22 @@ void gpio_set_mode(uint16_t pin, GPIO_MODE mode) {
gpio->MODER |= (mode & 0b11) << (pn * 2);
}
void gpio_set_af(uint16_t pin, uint8_t af) {
struct gpio *gpio = ((struct gpio*)(uintptr_t)((0x40020000U) + ((0x400U) * (pin >> 8))));
int pn = (pin & 0b1111);
if (pn < 8) {
gpio->AFRL &= ~(0b1111 << (pn * 4));
gpio->AFRL |= (af & 0b1111) << (pn * 4);
} else {
gpio->AFRH &= ~(0b1111 << (pn * 4));
gpio->AFRH |= (af & 0b1111) << (pn * 4);
}
}
void gpio_write(uint16_t pin,
# 13 "src/gpio.c" 3 4
# 25 "src/gpio.c" 3 4
_Bool
# 13 "src/gpio.c"
# 25 "src/gpio.c"
val) {
struct gpio *gpio = ((struct gpio*)(uintptr_t)((0x40020000U) + ((0x400U) * (pin >> 8))));
gpio->BSRR = (0b0011 << (pin & 0b1111)) << (val ? 0 : 16);