Add basic GPIO and RCC functionalities to STM32F4
Subsystems (2)
PATCH 1/1 Add basic GPIO and RCC functionalities to STM32F4
Adds support for GPIO peripherals in STM32F4 targets. Main contributions are:
- Generic GPIO class for STM32 devices. It can be used for most of STM32 chips. Note that it does not implement configuration locking mechanisms.
- Generic RCC class for STM32 devices. It can be used for most of STM32 chips. Note that it only implements enable and reset capabilities.
I tested this locally with a few examples with LEDs (digital output) and buttons (digital input) and works as expected.
--- docs/system/arm/stm32.rst | 5 +- hw/arm/Kconfig | 2 + hw/arm/stm32f405_soc.c | 46 ++++- hw/gpio/Kconfig | 3 + hw/gpio/meson.build | 1 + hw/gpio/stm32_gpio.c | 357 +++++++++++++++++++++++++++++++++ hw/gpio/trace-events | 8 + hw/misc/Kconfig | 3 + hw/misc/meson.build | 1 + hw/misc/stm32_rcc.c | 154 ++++++++++++++ hw/misc/stm32f4xx_syscfg.c | 23 ++- hw/misc/trace-events | 6 + include/hw/arm/stm32.h | 41 ++++ include/hw/arm/stm32f405_soc.h | 5 + include/hw/gpio/stm32_gpio.h | 105 ++++++++++ include/hw/misc/stm32_rcc.h | 91 +++++++++ 16 files changed, 830 insertions(+), 21 deletions(-) create mode 100644 hw/gpio/stm32_gpio.c create mode 100644 hw/misc/stm32_rcc.c create mode 100644 include/hw/arm/stm32.h create mode 100644 include/hw/gpio/stm32_gpio.h create mode 100644 include/hw/misc/stm32_rcc.h
docs/system/arm/stm32.rst +3 -2
@@ -36,6 +36,8 @@ Supported devices * SPI controller * System configuration (SYSCFG) * Timer controller (TIMER) + * GPIO controller (GPIO) (STM32F4 only, no lock/unlock capability) + * Reset and Clock Controller (RCC) (STM32F4 only, reset and enable only) Missing devices ---------------
@@ -47,13 +49,12 @@ Missing devices * DMA controller * Ethernet controller * Flash Interface Unit - * GPIO controller * I2C controller * Inter-Integrated Sound (I2S) controller * Power supply configuration (PWR) * Random Number Generator (RNG) * Real-Time Clock (RTC) controller - * Reset and Clock Controller (RCC) + * Reset and Clock Controller (RCC) (other features than reset and enable) * Secure Digital Input/Output (SDIO) interface * USB OTG * Watchdog controller (IWDG, WWDG)
hw/arm/Kconfig +2 -0
@@ -466,6 +466,8 @@ config STM32F405_SOC bool select ARM_V7M select OR_IRQ + select STM32_RCC + select STM32_GPIO select STM32F4XX_SYSCFG select STM32F4XX_EXTI
hw/arm/stm32f405_soc.c +36 -10
87 lines not on this page · raw message
hw/gpio/Kconfig +3 -0
@@ -17,6 +17,9 @@ config GPIO_PWR config SIFIVE_GPIO bool +config STM32_GPIO + bool + config STM32L4X5_GPIO bool
hw/gpio/meson.build +1 -0
@@ -16,6 +16,7 @@ system_ss.add(when: 'CONFIG_RASPI', if_true: files( 'bcm2838_gpio.c' )) system_ss.add(when: 'CONFIG_STM32L4X5_SOC', if_true: files('stm32l4x5_gpio.c')) +system_ss.add(when: 'CONFIG_STM32_GPIO', if_true: files('stm32_gpio.c')) system_ss.add(when: 'CONFIG_ASPEED_SOC', if_true: files('aspeed_gpio.c')) system_ss.add(when: 'CONFIG_SIFIVE_GPIO', if_true: files('sifive_gpio.c')) system_ss.add(when: 'CONFIG_PCF8574', if_true: files('pcf8574.c'))
hw/gpio/stm32_gpio.c new file +357 -0
357 lines not on this page · raw message
hw/gpio/trace-events +8 -0
@@ -36,6 +36,14 @@ sifive_gpio_update_output_irq(int64_t line, int64_t value) "line %" PRIi64 " val aspeed_gpio_read(uint64_t offset, uint64_t value) "offset: 0x%" PRIx64 " value 0x%" PRIx64 aspeed_gpio_write(uint64_t offset, uint64_t value) "offset: 0x%" PRIx64 " value 0x%" PRIx64 +# stm32_gpio.c +stm32_gpio_read(uint64_t offset, uint64_t r) "offset 0x%" PRIx64 " value 0x%" PRIx64 +stm32_gpio_write(uint64_t offset, uint64_t value) "offset 0x%" PRIx64 " value 0x%" PRIx64 +stm32_gpio_irq_enable(int64_t line, int64_t value) "line %" PRIi64 " value %" PRIi64 +stm32_gpio_irq_reset(int64_t line, int64_t value) "line %" PRIi64 " value %" PRIi64 +stm32_gpio_irq_set(int64_t line, int64_t value) "line %" PRIi64 " value %" PRIi64 +stm32_gpio_update_output_irq(int64_t line, int64_t value) "line %" PRIi64 " value %" PRIi64 + # stm32l4x5_gpio.c stm32l4x5_gpio_read(char *gpio, uint64_t addr) "GPIO%s addr: 0x%" PRIx64 " " stm32l4x5_gpio_write(char *gpio, uint64_t addr, uint64_t data) "GPIO%s addr: 0x%" PRIx64 " val: 0x%" PRIx64 ""
hw/misc/Kconfig +3 -0
@@ -82,6 +82,9 @@ config IMX select SSI select USB_EHCI_SYSBUS +config STM32_RCC + bool + config STM32F2XX_SYSCFG bool
hw/misc/meson.build +1 -0
@@ -106,6 +106,7 @@ system_ss.add(when: 'CONFIG_XLNX_VERSAL', if_true: files( system_ss.add(when: 'CONFIG_XLNX_VERSAL_TRNG', if_true: files( 'xlnx-versal-trng.c', )) +system_ss.add(when: 'CONFIG_STM32_RCC', if_true: files('stm32_rcc.c')) system_ss.add(when: 'CONFIG_STM32F2XX_SYSCFG', if_true: files('stm32f2xx_syscfg.c')) system_ss.add(when: 'CONFIG_STM32F4XX_SYSCFG', if_true: files('stm32f4xx_syscfg.c')) system_ss.add(when: 'CONFIG_STM32F4XX_EXTI', if_true: files('stm32f4xx_exti.c'))
hw/misc/stm32_rcc.c new file +154 -0
154 lines not on this page · raw message
hw/misc/stm32f4xx_syscfg.c +14 -9
@@ -27,6 +27,7 @@#include "trace.h" #include "hw/irq.h" #include "migration/vmstate.h" +#include "hw/gpio/stm32_gpio.h" #include "hw/misc/stm32f4xx_syscfg.h" static void stm32f4xx_syscfg_reset (DeviceState *dev)
@@ -45,17 +46,21 @@ static void stm32f4xx_syscfg_reset(DeviceState *dev)static void stm32f4xx_syscfg_set_irq (void *opaque,int irq,int level) {STM32F4xxSyscfgState *s = opaque; -int icrreg = irq /4 ; -int startbit = (irq &3 ) *4 ; -uint8_t config = irq /16 ; +uint8_t pin = irq &0xF ;// first 4 bits encode the pin number (0-15) +uint8_t port = irq >>4 ;// the rest encode the port number (0-8) -trace_stm32f4xx_syscfg_set_irq (irq /16 , irq %16 , level); +g_assert (port <=STM32_GPIO_PORT_I );// stm32f4xx only has ports A-I + +int icrreg = pin /4 ; +int startbit = (pin %4 ) *4 ; + +trace_stm32f4xx_syscfg_set_irq (port, pin, level);g_assert (icrreg <SYSCFG_NUM_EXTICR ); -if (extract32 (s->syscfg_exticr[icrreg], startbit,4 ) == config) { -qemu_set_irq (s->gpio_out[irq], level); -trace_stm32f4xx_pulse_exti (irq); +if (extract32 (s->syscfg_exticr[icrreg], startbit,4 ) == port) { +qemu_set_irq (s->gpio_out[pin], level); +trace_stm32f4xx_pulse_exti (pin); } }
@@ -129,8 +134,8 @@ static void stm32f4xx_syscfg_init(Object *obj) TYPE_STM32F4XX_SYSCFG, 0x400);sysbus_init_mmio (SYS_BUS_DEVICE (obj), &s->mmio); -qdev_init_gpio_in (DEVICE (obj), stm32f4xx_syscfg_set_irq,16 *9 ); -qdev_init_gpio_out (DEVICE (obj), s->gpio_out,16 ); +qdev_init_gpio_in (DEVICE (obj), stm32f4xx_syscfg_set_irq,STM32_GPIO_NPINS * (STM32_GPIO_PORT_I +1 )); +qdev_init_gpio_out (DEVICE (obj), s->gpio_out,STM32_GPIO_NPINS ); }static const VMStateDescription vmstate_stm32f4xx_syscfg = {
hw/misc/trace-events +6 -0
@@ -156,6 +156,12 @@ npcm7xx_pwm_write(const char *id, uint64_t offset, uint32_t value) "%s offset: 0 npcm7xx_pwm_update_freq(const char *id, uint8_t index, uint32_t old_value, uint32_t new_value) "%s pwm[%u] Update Freq: old_freq: %u, new_freq: %u" npcm7xx_pwm_update_duty(const char *id, uint8_t index, uint32_t old_value, uint32_t new_value) "%s pwm[%u] Update Duty: old_duty: %u, new_duty: %u" +# stm32_rcc.c +stm32_rcc_read(uint64_t addr, uint64_t data) "reg read: addr: 0x%" PRIx64 " val: 0x%" PRIx64 "" +stm32_rcc_write(uint64_t addr, uint64_t data) "reg write: addr: 0x%" PRIx64 " val: 0x%" PRIx64 "" +stm32_rcc_pulse_enable(int line, int level) "Enable: %d to %d" +stm32_rcc_pulse_reset(int line, int level) "Reset: %d to %d" + # stm32f4xx_syscfg.c stm32f4xx_syscfg_set_irq(int gpio, int line, int level) "Interrupt: GPIO: %d, Line: %d; Level: %d" stm32f4xx_pulse_exti(int irq) "Pulse EXTI: %d"
include/hw/arm/stm32.h new file +41 -0
41 lines not on this page · raw message
include/hw/arm/stm32f405_soc.h +5 -0
@@ -25,6 +25,8 @@#ifndef HW_ARM_STM32F405_SOC_H #define HW_ARM_STM32F405_SOC_H +#include "hw/misc/stm32_rcc.h" +#include "hw/gpio/stm32_gpio.h" #include "hw/misc/stm32f4xx_syscfg.h" #include "hw/timer/stm32f2xx_timer.h" #include "hw/char/stm32f2xx_usart.h"
@@ -42,6 +44,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(STM32F405State, STM32F405_SOC)#define STM_NUM_TIMERS 4#define STM_NUM_ADCS 6#define STM_NUM_SPIS 6 +#define STM_NUM_GPIOS (STM32_GPIO_PORT_I - STM32_GPIO_PORT_A + 1)#define FLASH_BASE_ADDRESS 0x08000000#define FLASH_SIZE (1024 * 1024)
@@ -55,6 +58,7 @@ struct STM32F405State {ARMv7MState armv7m; +STM32RccState rcc;STM32F4xxSyscfgState syscfg;STM32F4xxExtiState exti;STM32F2XXUsartState usart[STM_NUM_USARTS ];
@@ -62,6 +66,7 @@ struct STM32F405State {OrIRQState adc_irqs;STM32F2XXADCState adc[STM_NUM_ADCS ];STM32F2XXSPIState spi[STM_NUM_SPIS ]; +STM32GPIOState gpio[STM_NUM_GPIOS ];MemoryRegion ccm;MemoryRegion sram;
include/hw/gpio/stm32_gpio.h new file +105 -0
105 lines not on this page · raw message
include/hw/misc/stm32_rcc.h new file +91 -0
91 lines not on this page · raw message
Signature · 1 line
2.39.3 (Apple Git-146)
Quoted 8 lines · Román Cárdenas Rodríguez
Adds support for GPIO peripherals in STM32F4 targets. Main contributions are:
- Generic GPIO class for STM32 devices. It can be used for most of STM32 chips. Note that it does not implement configuration locking mechanisms.
- Generic RCC class for STM32 devices. It can be used for most of STM32 chips. Note that it only implements enable and reset capabilities.
Quoted 4 lines
I tested this locally with a few examples with LEDs (digital output) and buttons (digital input) and works as expected.
Signed-off-by Roman Cardenas Rodriguez <[email protected]>
Hi; thanks for this patch. Please could you split it into a series of smaller patches (which each do a coherent thing and compile successfully)? A patch this size is very hard to review. An example split might be something like:
* implementation of new rcc device
* add rcc device to the board
* implementation of gpio device
* add gpio device to the board
Also, please cc the [email protected] mailing list on all patches. Our automated tooling only sees patches sent there.
A few other simple notes below for easy style stuff I saw on a quick scan; I'll save my full review for the split patchset.
Quoted 10 lines
hw/gpio/stm32_gpio.c new file +4
-0@@ -0,0 +1,357 @@ +/* +* STM32 System-on-Chip general purpose input/output register definition +* +* Copyright 2024 Román Cárdenas <[email protected]>
hw/gpio/stm32_gpio.c line 14
Quoted 10 lines
hw/gpio/stm32_gpio.c new file +10
-0+* +* Based on sifive_gpio.c: +* +* Copyright 2019 AdaCore +* +* This code is licensed under the GPL version 2 or later. See +* the COPYING file in the top-level directory. +*/ + +#include <stdio.h>
You don't need to include stdio.h, osdep.h will do that for you. (More generally, osdep.h should always be the first #include in every .c file, and never included in any .h file.)
Quoted 13 lines
hw/gpio/stm32_gpio.c new file +13
-0+ +#include "qemu/osdep.h" +#include "qemu/log.h" +#include "hw/irq.h" +#include "hw/qdev-properties.h" +#include "hw/arm/stm32.h" +#include "hw/gpio/stm32_gpio.h" +#include "migration/vmstate.h" +#include "trace.h" + +static void stm32_gpio_update_state (STM32GPIOState *s) +{ +bool prev_id, new_id, od, in, in_mask;
hw/gpio/stm32_gpio.c line 37
Quoted 10 lines
hw/gpio/stm32_gpio.c new file +10
-0+uint8_t mode, pupd; + +for (size_t i =0 ; i < s->ngpio; i++) { + prev_id =extract32 (s->idr, i,1 ); + od =extract32 (s->odr, i,1 ); + in =extract32 (s->in, i,1 ); + in_mask =extract32 (s->in_mask, i,1 ); + + mode =extract32 (s->moder, i *2 ,2 ); + pupd =extract32 (s->pupdr, i *2 ,2 );
I don't really recommend lining up assignments like this:
it looks nice in the first commit but becomes a pain when making future changes because either you have to edit all the lines to retain the alignment or else things end up not all aligned.
hw/gpio/stm32_gpio.c line 74
Quoted 6 lines
hw/gpio/stm32_gpio.c new file +6
-0+static void stm32_gpio_reset (DeviceState *dev) +{ +STM32GPIOState *s =STM32_GPIO (dev); + +// enabled is not affected by reset. It is ruled by RCC +// IDR is not directly reset. It is updated at the end by update_state
QEMU's C comment style says no '//' style comments. You can have
/* one line comments like this */
or
/*
* multiple line
* comments like this
*/
(If you haven't tried running your patches through scripts/checkpatch.pl, that will catch at least some of this kind of style issue. Note that it does also have some false positives, so you don't need to follow its suggestions 100% if they look weird.)
hw/gpio/stm32_gpio.c line 139
Quoted 9 lines
hw/gpio/stm32_gpio.c new file +9
-0+static void stm32_gpio_irq_set (void *opaque,int line,int value) +{ +STM32GPIOState *s =STM32_GPIO (opaque); + +trace_stm32_gpio_irq_set (line, value); + +assert (line >=0 && line < s->ngpio); + + s->in_mask =deposit32 (s->in_mask, line,1 , value >=0 );// <0 is unconnected/connected to load
checkpatch will also warn about overlong lines like this.
hw/gpio/stm32_gpio.c line 146
Quoted 7 lines
hw/gpio/stm32_gpio.c new file +7
-0+if (value >=0 ) { + s->in =deposit32 (s->in, line,1 , value !=0 );// 0 is low, >0 is high + } + +stm32_gpio_update_state (s); +} +
thanks
-- PMM