stm32: Add optimized stm32h7_gpio.c

Add optimized gpio functions for stm32h7 - caching the ODR register
can notably improve the performance of the gpio_out_toggle() code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2025-03-11 10:56:17 -04:00
parent 1f5783a250
commit 0d27195fd4
4 changed files with 176 additions and 5 deletions

View file

@ -13,7 +13,7 @@ config STM32_SELECT
select HAVE_GPIO_HARD_PWM if MACH_STM32F070 || MACH_STM32F072 || MACH_STM32F1 || MACH_STM32F4 || MACH_STM32F7 || MACH_STM32G0 || MACH_STM32H7
select HAVE_STRICT_TIMING
select HAVE_CHIPID
select HAVE_STEPPER_OPTIMIZED_BOTH_EDGE
select HAVE_STEPPER_OPTIMIZED_BOTH_EDGE if !MACH_STM32H7
select HAVE_BOOTLOADER_REQUEST
select HAVE_LIMITED_CODE_SIZE if FLASH_SIZE < 0x10000

View file

@ -37,7 +37,7 @@ CFLAGS_klipper.elf += -T $(OUT)src/generic/armcm_link.ld
$(OUT)klipper.elf: $(OUT)src/generic/armcm_link.ld
# Add source files
src-y += stm32/watchdog.c stm32/gpio.c stm32/clockline.c stm32/dfu_reboot.c
src-y += stm32/watchdog.c stm32/clockline.c stm32/dfu_reboot.c
src-y += generic/crc16_ccitt.c
src-y += generic/armcm_boot.c generic/armcm_irq.c generic/armcm_reset.c
src-$(CONFIG_MACH_STM32F0) += stm32/stm32f0.c ../lib/stm32f0/system_stm32f0xx.c
@ -52,8 +52,9 @@ src-$(CONFIG_MACH_STM32L4) += stm32/stm32l4.c ../lib/stm32l4/system_stm32l4xx.c
timer-src-y := generic/armcm_timer.c
timer-src-$(CONFIG_MACH_STM32F0) := generic/timer_irq.c stm32/stm32f0_timer.c
timer-src-$(CONFIG_MACH_STM32G0) := generic/timer_irq.c stm32/stm32f0_timer.c
gpio-src-y := stm32/gpioperiph.c
gpio-src-$(CONFIG_MACH_STM32F1) :=
gpio-src-y := stm32/gpio.c stm32/gpioperiph.c
gpio-src-$(CONFIG_MACH_STM32F1) := stm32/gpio.c
gpio-src-$(CONFIG_MACH_STM32H7) := stm32/stm32h7_gpio.c stm32/gpioperiph.c
src-y += $(timer-src-y) $(gpio-src-y)
adc-src-y := stm32/adc.c
adc-src-$(CONFIG_MACH_STM32F0) := stm32/stm32f0_adc.c

View file

@ -5,7 +5,10 @@
struct gpio_out {
void *regs;
union {
struct odr_cache *oc; // stm32h7 uses 'oc'; all others use 'bit'
uint32_t bit;
};
};
struct gpio_out gpio_out_setup(uint32_t pin, uint32_t val);
void gpio_out_reset(struct gpio_out g, uint32_t val);

167
src/stm32/stm32h7_gpio.c Normal file
View file

@ -0,0 +1,167 @@
// GPIO functions optimized for stm32h7
//
// Copyright (C) 2019-2025 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <string.h> // ffs
#include "board/irq.h" // irq_save
#include "command.h" // DECL_ENUMERATION_RANGE
#include "gpio.h" // gpio_out_setup
#include "internal.h" // gpio_peripheral
#include "sched.h" // sched_shutdown
DECL_ENUMERATION_RANGE("pin", "PA0", GPIO('A', 0), 16);
DECL_ENUMERATION_RANGE("pin", "PB0", GPIO('B', 0), 16);
DECL_ENUMERATION_RANGE("pin", "PC0", GPIO('C', 0), 16);
#ifdef GPIOD
DECL_ENUMERATION_RANGE("pin", "PD0", GPIO('D', 0), 16);
#endif
#ifdef GPIOE
DECL_ENUMERATION_RANGE("pin", "PE0", GPIO('E', 0), 16);
#endif
#ifdef GPIOF
DECL_ENUMERATION_RANGE("pin", "PF0", GPIO('F', 0), 16);
#endif
#ifdef GPIOG
DECL_ENUMERATION_RANGE("pin", "PG0", GPIO('G', 0), 16);
#endif
#ifdef GPIOH
DECL_ENUMERATION_RANGE("pin", "PH0", GPIO('H', 0), 16);
#endif
#ifdef GPIOI
DECL_ENUMERATION_RANGE("pin", "PI0", GPIO('I', 0), 16);
#endif
GPIO_TypeDef * const digital_regs[] = {
['A' - 'A'] = GPIOA, GPIOB, GPIOC,
#ifdef GPIOD
['D' - 'A'] = GPIOD,
#endif
#ifdef GPIOE
['E' - 'A'] = GPIOE,
#endif
#ifdef GPIOF
['F' - 'A'] = GPIOF,
#endif
#ifdef GPIOG
['G' - 'A'] = GPIOG,
#endif
#ifdef GPIOH
['H' - 'A'] = GPIOH,
#endif
#ifdef GPIOI
['I' - 'A'] = GPIOI,
#endif
};
// Convert a register and bit location back to an integer pin identifier
static int
regs_to_pin(GPIO_TypeDef *regs, uint32_t bit)
{
int i;
for (i=0; i<ARRAY_SIZE(digital_regs); i++)
if (digital_regs[i] == regs)
return GPIO('A' + i, ffs(bit)-1);
return 0;
}
// Verify that a gpio is a valid pin
static int
gpio_valid(uint32_t pin)
{
uint32_t port = GPIO2PORT(pin);
return port < ARRAY_SIZE(digital_regs) && digital_regs[port];
}
// The stm32h7 has very slow read access to the gpio registers. Cache
// the ODR register in memory to speed up toggle operations.
static struct odr_cache {
uint32_t bsrr;
} ODR_CACHE[ARRAY_SIZE(digital_regs) * 16] __aligned(64);
struct gpio_out
gpio_out_setup(uint32_t pin, uint32_t val)
{
if (!gpio_valid(pin))
shutdown("Not an output pin");
uint32_t port = GPIO2PORT(pin);
GPIO_TypeDef *regs = digital_regs[port];
gpio_clock_enable(regs);
struct gpio_out g = { .regs=regs, .oc=&ODR_CACHE[pin] };
gpio_out_reset(g, val);
return g;
}
void
gpio_out_reset(struct gpio_out g, uint32_t val)
{
GPIO_TypeDef *regs = g.regs;
uint32_t pin = g.oc - ODR_CACHE;
uint32_t bit_num = ((uint32_t)g.oc / sizeof(*g.oc)) % 16;
uint32_t bsrr = 1 << (val ? bit_num : bit_num + 16);
irqstatus_t flag = irq_save();
g.oc->bsrr = bsrr;
regs->BSRR = bsrr;
gpio_peripheral(pin, GPIO_OUTPUT, 0);
irq_restore(flag);
}
void
gpio_out_toggle_noirq(struct gpio_out g)
{
GPIO_TypeDef *regs = g.regs;
uint32_t bsrr = (g.oc->bsrr << 16) | (g.oc->bsrr >> 16);
g.oc->bsrr = bsrr;
regs->BSRR = bsrr;
}
void
gpio_out_toggle(struct gpio_out g)
{
irqstatus_t flag = irq_save();
gpio_out_toggle_noirq(g);
irq_restore(flag);
}
void
gpio_out_write(struct gpio_out g, uint32_t val)
{
GPIO_TypeDef *regs = g.regs;
uint32_t bit_num = ((uint32_t)g.oc / sizeof(*g.oc)) % 16;
uint32_t bsrr = 1 << (val ? bit_num : bit_num + 16);
irqstatus_t flag = irq_save();
g.oc->bsrr = bsrr;
regs->BSRR = bsrr;
irq_restore(flag);
}
struct gpio_in
gpio_in_setup(uint32_t pin, int32_t pull_up)
{
if (!gpio_valid(pin))
shutdown("Not a valid input pin");
GPIO_TypeDef *regs = digital_regs[GPIO2PORT(pin)];
struct gpio_in g = { .regs=regs, .bit=GPIO2BIT(pin) };
gpio_in_reset(g, pull_up);
return g;
}
void
gpio_in_reset(struct gpio_in g, int32_t pull_up)
{
GPIO_TypeDef *regs = g.regs;
int pin = regs_to_pin(regs, g.bit);
irqstatus_t flag = irq_save();
gpio_peripheral(pin, GPIO_INPUT, pull_up);
irq_restore(flag);
}
uint8_t
gpio_in_read(struct gpio_in g)
{
GPIO_TypeDef *regs = g.regs;
return !!(regs->IDR & g.bit);
}