hw/arm Add STM32F4 support and PWR device model
Subsystems (2)
v3 cover hw/arm Add STM32F4 support and PWR device model
From: Yihao Fan <[email protected]>
This patch series introduces basic support for the STM32F407 SoC and a new STM32F4spark machine in QEMU, along with a PWR device model.
In addition, I have modified the header file referenced by the RCC driver to STM32RCC in accordance with the suggestions.
This series includes:
- A new SoC model (STM32F407) with initial integration.
- A board model called STM32F4spark to instantiate and test the SoC.
- A PWR device implementation for STM32F4xx family.
---
Yihao Fan (3):
hw/arm: Add support for the STM32F407 SoC hw/arm: Add support for the STM32F407-RT-SPARk board hw/arm/stm32f407: Add PWR device to stm32f407 SoC
MAINTAINERS | 14 ++ docs/system/arm/STM32F407-RT-SPARK.rst | 41 +++++ docs/system/arm/stm32.rst | 2 + docs/system/target-arm.rst | 1 + hw/arm/Kconfig | 13 ++ hw/arm/meson.build | 2 + hw/arm/stm32f407_soc.c | 217 +++++++++++++++++++++++++ hw/arm/stm32f4spark.c | 56 +++++++ hw/misc/Kconfig | 3 + hw/misc/meson.build | 1 + hw/misc/stm32f4xx_pwr.c | 111 +++++++++++++ include/hw/arm/stm32f407_soc.h | 77 +++++++++ include/hw/misc/stm32f4xx_pwr.h | 40 +++++ 13 files changed, 578 insertions(+) create mode 100644 docs/system/arm/STM32F407-RT-SPARK.rst create mode 100644 hw/arm/stm32f407_soc.c create mode 100644 hw/arm/stm32f4spark.c create mode 100644 hw/misc/stm32f4xx_pwr.c create mode 100644 include/hw/arm/stm32f407_soc.h create mode 100644 include/hw/misc/stm32f4xx_pwr.h
Signature · 1 line
2.43.0
PATCH v3 1/3 hw/arm: Add support for the STM32F407 SoC|
From: Yihao Fan <[email protected]>
This patch introduces a new QEMU machine type for the STM32F407 SoC featuring a Cortex-M4 core. This will be used by the RT-Spark to create a machine.
Furthermore, I have updated the reusable USART, timer,
and RCC drivers to align with the existing driver implementations.
--- MAINTAINERS | 7 ++ hw/arm/Kconfig | 6 + hw/arm/meson.build | 1 + hw/arm/stm32f407_soc.c | 208 +++++++++++++++++++++++++++++++++ include/hw/arm/stm32f407_soc.h | 74 ++++++++++++ 5 files changed, 296 insertions(+) create mode 100644 hw/arm/stm32f407_soc.c create mode 100644 include/hw/arm/stm32f407_soc.h
MAINTAINERS +7 -0
@@ -1163,6 +1163,13 @@ F: hw/misc/stm32f4xx_exti.c F: hw/misc/stm32_rcc.c F: include/hw/misc/stm32_rcc.h +STM32F407 +M: yanl1229 <[email protected]> +M: Yihao Fan <[email protected]> +L: [email protected] +S: Maintained +F: hw/arm/stm32f407_soc.c + Netduino 2 M: Alistair Francis <[email protected]> M: Peter Maydell <[email protected]>
hw/arm/Kconfig +6 -0
@@ -406,6 +406,12 @@ config STM32F405_SOC select STM32F4XX_SYSCFG select STM32F4XX_EXTI +config STM32F407_SOC + bool + select ARM_V7M + select STM32F4XX_SYSCFG + select STM32F4XX_EXTI + config B_L475E_IOT01A bool default y
hw/arm/meson.build +1 -0
@@ -33,6 +33,7 @@ arm_common_ss.add(when: ['CONFIG_RASPI', 'TARGET_AARCH64'], if_true: files('bcm2 arm_common_ss.add(when: 'CONFIG_STM32F100_SOC', if_true: files('stm32f100_soc.c')) arm_common_ss.add(when: 'CONFIG_STM32F205_SOC', if_true: files('stm32f205_soc.c')) arm_common_ss.add(when: 'CONFIG_STM32F405_SOC', if_true: files('stm32f405_soc.c')) +arm_common_ss.add(when: 'CONFIG_STM32F407_SOC', if_true: files('stm32f407_soc.c')) arm_common_ss.add(when: 'CONFIG_B_L475E_IOT01A', if_true: files('b-l475e-iot01a.c')) arm_common_ss.add(when: 'CONFIG_STM32L4X5_SOC', if_true: files('stm32l4x5_soc.c')) arm_common_ss.add(when: 'CONFIG_XLNX_ZYNQMP_ARM', if_true: files('xlnx-zynqmp.c', 'xlnx-zcu102.c'))
hw/arm/stm32f407_soc.c new file +208 -0
208 lines not on this page · raw message
include/hw/arm/stm32f407_soc.h new file +74 -0
74 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v3 1/3] hw/arm: Add support for the STM32F407 SoC|
Review
Quoted 12 lines · On Sun, 26 Oct 2025 at 10:53, <[email protected]> wrote:
From: Yihao Fan <[email protected]>
This patch introduces a new QEMU machine type for the STM32F407 SoC featuring a Cortex-M4 core. This will be used by the RT-Spark to create a machine.
Furthermore, I have updated the reusable USART, timer,
and RCC drivers to align with the existing driver implementations.Signed-off-by Yihao Fan <[email protected]>(quoted diff) +2
-0+ + /* Timer 2 to 5 contoller */
"controller"
Otherwise
thanks
-- PMM
Re: [PATCH v3 1/3] hw/arm: Add support for the STM32F407 SoC|
In reply to [email protected]Quoted 9 lines · On Sun, Oct 26, 2025 at 9:02 PM <[email protected]> wrote:
From: Yihao Fan <[email protected]>
This patch introduces a new QEMU machine type for the STM32F407 SoC featuring a Cortex-M4 core. This will be used by the RT-Spark to create a machine.
Furthermore, I have updated the reusable USART, timer,
and RCC drivers to align with the existing driver implementations.Signed-off-by Yihao Fan <[email protected]>
Alistair
Quoted 355 lines
--- MAINTAINERS | 7 ++ hw/arm/Kconfig | 6 + hw/arm/meson.build | 1 + hw/arm/stm32f407_soc.c | 208 +++++++++++++++++++++++++++++++++ include/hw/arm/stm32f407_soc.h | 74 ++++++++++++ 5 files changed, 296 insertions(+) create mode 100644 hw/arm/stm32f407_soc.c create mode 100644 include/hw/arm/stm32f407_soc.hMAINTAINERS +7
-0@@ -1163,6 +1163,13 @@ F: hw/misc/stm32f4xx_exti.c F: hw/misc/stm32_rcc.c F: include/hw/misc/stm32_rcc.h +STM32F407 +M: yanl1229 <[email protected]> +M: Yihao Fan <[email protected]> +L: [email protected] +S: Maintained +F: hw/arm/stm32f407_soc.c + Netduino 2 M: Alistair Francis <[email protected]> M: Peter Maydell <[email protected]>hw/arm/Kconfig +6
-0@@ -406,6 +406,12 @@ config STM32F405_SOC select STM32F4XX_SYSCFG select STM32F4XX_EXTI +config STM32F407_SOC + bool + select ARM_V7M + select STM32F4XX_SYSCFG + select STM32F4XX_EXTI + config B_L475E_IOT01A bool default yhw/arm/meson.build +1
-0@@ -33,6 +33,7 @@ arm_common_ss.add(when: ['CONFIG_RASPI', 'TARGET_AARCH64'], if_true: files('bcm2 arm_common_ss.add(when: 'CONFIG_STM32F100_SOC', if_true: files('stm32f100_soc.c')) arm_common_ss.add(when: 'CONFIG_STM32F205_SOC', if_true: files('stm32f205_soc.c')) arm_common_ss.add(when: 'CONFIG_STM32F405_SOC', if_true: files('stm32f405_soc.c')) +arm_common_ss.add(when: 'CONFIG_STM32F407_SOC', if_true: files('stm32f407_soc.c')) arm_common_ss.add(when: 'CONFIG_B_L475E_IOT01A', if_true: files('b-l475e-iot01a.c')) arm_common_ss.add(when: 'CONFIG_STM32L4X5_SOC', if_true: files('stm32l4x5_soc.c')) arm_common_ss.add(when: 'CONFIG_XLNX_ZYNQMP_ARM', if_true: files('xlnx-zynqmp.c', 'xlnx-zcu102.c'))hw/arm/stm32f407_soc.c new file +208
-0@@ -0,0 +1,208 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* +* Copyright (c) liang yan <[email protected]> +* Copyright (c) Yihao Fan <[email protected]> +* The reference used is the STMicroElectronics RM0090 Reference manual +* https://www.st.com/en/microcontrollers-microprocessors/stm32f407-417/documentation.html +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "system/address-spaces.h" +#include "system/system.h" +#include "hw/arm/stm32f407_soc.h" +#include "hw/qdev-clock.h" +#include "hw/misc/unimp.h" +#include "hw/sd/sd.h" +#include "hw/boards.h" +#include "qom/object.h" + +static const uint32_t syscfg_addr =0x40013800 ; +static const uint32_t exti_addr =0x40013C00 ; +static const uint32_t usart_addr[STM_NUM_USARTS ] = { +STM32F407_USART1 ,STM32F407_USART2 ,STM32F407_USART3 , +STM32F407_USART6 +}; +/* At the moment only Timer 2 to 5 are modelled */ +static const uint32_t timer_addr[STM_NUM_TIMERS ] = { +STM32F407_TIM2 ,STM32F407_TIM3 ,STM32F407_TIM4 , +STM32F407_TIM5 +}; +static const int syscfg_irq =71 ; +static const int exti_irq[] = { +6 ,7 ,8 ,9 ,10 ,23 ,23 ,23 ,23 ,23 ,40 , +40 ,40 ,40 ,40 ,40 +}; +static const int usart_irq[STM_NUM_USARTS ] = { +37 ,38 ,39 ,71 +}; +static const int timer_irq[STM_NUM_TIMERS ] = { +28 ,29 ,30 ,50 +}; + +static void stm32f407_soc_initfn (Object *obj) +{ +int i; + +STM32F407State *s =STM32F407_SOC (obj); + +object_initialize_child (obj,"armv7m" , &s->armv7m,TYPE_ARMV7M ); +object_initialize_child (obj,"syscfg" , &s->syscfg,TYPE_STM32F4XX_SYSCFG ); +object_initialize_child (obj,"exti" , &s->exti,TYPE_STM32F4XX_EXTI ); +object_initialize_child (obj,"rcc" , &s->rcc,TYPE_STM32_RCC ); + +for (i =0 ; i <STM_NUM_USARTS ; i++) { +object_initialize_child (obj,"usart[*]" , &s->usart[i], +TYPE_STM32F2XX_USART ); + } + +for (i =0 ; i <STM_NUM_TIMERS ; i++) { +object_initialize_child (obj,"timer[*]" , &s->timer[i], +TYPE_STM32F2XX_TIMER ); + } + + s->sysclk =qdev_init_clock_in (DEVICE (s),"sysclk" ,NULL ,NULL ,0 ); + s->refclk =qdev_init_clock_in (DEVICE (s),"refclk" ,NULL ,NULL ,0 ); +} + +static void stm32f407_soc_realize (DeviceState *dev_soc,Error **errp) +{ +STM32F407State *s =STM32F407_SOC (dev_soc); +DeviceState *dev, *armv7m; +SysBusDevice *busdev; +DriveInfo *dinfo; +int i, j; + +MemoryRegion *system_memory =get_system_memory (); + +/* +* We use s->refclk internally and only define it with qdev_init_clock_in() +* so it is correctly parented and not leaked on an init/deinit; it is not +* intended as an externally exposed clock. +*/ +if (clock_has_source (s->refclk)) { +error_setg (errp,"refclk clock must not be wired up by the board code" ); +return ; + } + +if (!clock_has_source (s->sysclk)) { +error_setg (errp,"sysclk clock must be wired up by the board code" ); +return ; + } + +/* +* TODO: ideally we should model the SoC RCC and its ability to +* change the sysclk frequency and define different sysclk sources. +*/ + +/* The refclk always runs at frequency HCLK / 8 */ +clock_set_mul_div (s->refclk,8 ,1 ); +clock_set_source (s->refclk, s->sysclk); + +memory_region_init_rom (&s->flash,OBJECT (dev_soc),"STM32F407.flash" , +FLASH_SIZE , &error_fatal); +memory_region_init_alias (&s->flash_alias,OBJECT (dev_soc), +"STM32F407.flash.alias" , &s->flash,0 ,FLASH_SIZE ); + +memory_region_add_subregion (system_memory,FLASH_BASE_ADDRESS , &s->flash); +memory_region_add_subregion (system_memory,0 , &s->flash_alias); + +memory_region_init_ram (&s->sram,NULL ,"STM32F407.sram" ,SRAM_SIZE , + &error_fatal); +memory_region_add_subregion (system_memory,SRAM_BASE_ADDRESS , &s->sram); + +memory_region_init_ram (&s->ccm,NULL ,"STM32F407.ccm" ,CCM_SIZE , + &error_fatal); +memory_region_add_subregion (system_memory,CCM_BASE_ADDRESS , &s->ccm); + + armv7m =DEVICE (&s->armv7m); +qdev_prop_set_uint32 (armv7m,"num-irq" ,98 ); +qdev_prop_set_string (armv7m,"cpu-type" ,ARM_CPU_TYPE_NAME ("cortex-m4" )); +qdev_prop_set_bit (armv7m,"enable-bitband" , true); +qdev_connect_clock_in (armv7m,"cpuclk" , s->sysclk); +qdev_connect_clock_in (armv7m,"refclk" , s->refclk); +object_property_set_link (OBJECT (&s->armv7m),"memory" , +OBJECT (system_memory), &error_abort); +if (!sysbus_realize (SYS_BUS_DEVICE (&s->armv7m), errp)) { +return ; + } + +/* Reset and clock controller */ + dev =DEVICE (&s->rcc); +if (!sysbus_realize (SYS_BUS_DEVICE (&s->rcc), errp)) { +return ; + } + busdev =SYS_BUS_DEVICE (dev); +sysbus_mmio_map (busdev,0 ,RCC_BASE_ADDR ); + +/* System configuration controller */ + dev =DEVICE (&s->syscfg); +if (!sysbus_realize (SYS_BUS_DEVICE (&s->syscfg), errp)) { +return ; + } + busdev =SYS_BUS_DEVICE (dev); +sysbus_mmio_map (busdev,0 , syscfg_addr); +sysbus_connect_irq (busdev,0 ,qdev_get_gpio_in (armv7m, syscfg_irq)); + +/* EXTI device */ + dev =DEVICE (&s->exti); +if (!sysbus_realize (SYS_BUS_DEVICE (&s->exti), errp)) { +return ; + } + busdev =SYS_BUS_DEVICE (dev); +sysbus_mmio_map (busdev,0 , exti_addr); +for (i =0 ; i <16 ; i++) { +sysbus_connect_irq (busdev, i,qdev_get_gpio_in (armv7m, exti_irq[i])); + } +for (i =0 ; i <16 ; i++) { +qdev_connect_gpio_out (DEVICE (&s->syscfg), i,qdev_get_gpio_in (dev, i)); + } + +/* Attach UART (uses USART registers) and USART controllers */ +for (i =0 ; i <STM_NUM_USARTS ; i++) { + dev =DEVICE (&(s->usart[i])); +qdev_prop_set_chr (dev,"chardev" ,serial_hd (i)); +if (!sysbus_realize (SYS_BUS_DEVICE (&s->usart[i]), errp)) { +return ; + } + busdev =SYS_BUS_DEVICE (dev); +sysbus_mmio_map (busdev,0 , usart_addr[i]); +sysbus_connect_irq (busdev,0 ,qdev_get_gpio_in (armv7m, usart_irq[i])); + } + +/* Timer 2 to 5 contoller */ +for (i =0 ; i <STM_NUM_TIMERS ; i++) { + dev =DEVICE (&(s->timer[i])); +qdev_prop_set_uint64 (dev,"clock-frequency" ,1000000000 ); +if (!sysbus_realize (SYS_BUS_DEVICE (&s->timer[i]), errp)) { +return ; + } + busdev =SYS_BUS_DEVICE (dev); +sysbus_mmio_map (busdev,0 , timer_addr[i]); +sysbus_connect_irq (busdev,0 ,qdev_get_gpio_in (armv7m, timer_irq[i])); + } + +} + +static void stm32f407_soc_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + + dc->realize = stm32f407_soc_realize; +} + +static const TypeInfo stm32f407_soc_info = { + .name =TYPE_STM32F407_SOC , + .parent =TYPE_SYS_BUS_DEVICE , + .instance_size =sizeof (STM32F407State), + .instance_init = stm32f407_soc_initfn, + .class_init = stm32f407_soc_class_init, +}; + +static void stm32f407_soc_types (void ) +{ +type_register_static (&stm32f407_soc_info); +} + +type_init (stm32f407_soc_types)include/hw/arm/stm32f407_soc.h new file +74
-1@@ -0,0 +1,74 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* +* Copyright (c) liang yan <[email protected]> +* Copyright (c) Yihao Fan <[email protected]> +* The reference used is the STMicroElectronics RM0090 Reference manual +* https://www.st.com/en/microcontrollers-microprocessors/stm32f407-417/documentation.html +*/ + +#ifndef STM32F407_SOC_H +#define STM32F407_SOC_H + +#include "hw/or-irq.h" +#include "hw/arm/armv7m.h" +#include "hw/misc/stm32f4xx_syscfg.h" +#include "hw/misc/stm32f4xx_exti.h" +#include "hw/char/stm32f2xx_usart.h" +#include "hw/timer/stm32f2xx_timer.h" +#include "hw/misc/stm32_rcc.h" +#include "qom/object.h" + +#define TYPE_STM32F407_SOC "stm32f407-soc" +OBJECT_DECLARE_SIMPLE_TYPE(STM32F407State, STM32F407_SOC) + +#define STM_NUM_USARTS 4 +#define STM32F407_USART1 0x40011000 +#define STM32F407_USART2 0x40004400 +#define STM32F407_USART3 0x40004800 +#define STM32F407_USART6 0x40011400 + +#define STM_NUM_TIMERS 4 +#define STM32F407_TIM1 0x40010000 +#define STM32F407_TIM2 0x40000000 +#define STM32F407_TIM3 0x40000400 +#define STM32F407_TIM4 0x40000800 +#define STM32F407_TIM5 0x40000c00 + +#define RCC_BASE_ADDR 0x40023800 +#define SYSCFG_BASE_ADDRESS 0x40013800 +#define SYSCFG_IRQ 71 +#define EXIT_BASE_ADDRESS 0x40013C00 + +#define FLASH_BASE_ADDRESS 0x8000000 +#define FLASH_SIZE 0x100000 +#define SRAM_BASE_ADDRESS 0x20000000 +#define SRAM_SIZE (192 * 1024) +#define CCM_BASE_ADDRESS 0x10000000 +#define CCM_SIZE (64 * 1024) + +typedef struct STM32F407State { +/*< private >*/ +SysBusDevice parent_obj; +/*< public >*/ + +ARMv7MState armv7m; + +STM32RccState rcc; +STM32F4xxSyscfgState syscfg; +STM32F4xxExtiState exti; +STM32F2XXUsartState usart[STM_NUM_USARTS ]; +STM32F2XXTimerState timer[STM_NUM_TIMERS ]; + +MemoryRegion ccm; +MemoryRegion sram; +MemoryRegion flash; +MemoryRegion flash_alias; + +Clock *sysclk; +Clock *refclk; + + +}STM32F407State ; + +#endif --2.43.0
PATCH v3 2/3 hw/arm: Add support for the STM32F407-RT-SPARk board
In reply to [email protected]From: Yihao Fan <[email protected]>
This commit adds a new STM32F4spark machine broad using the STM32F407 SoC. The implementation is derived from the Netduino Plus 2 machine.
--- MAINTAINERS | 7 ++++ docs/system/arm/STM32F407-RT-SPARK.rst | 41 +++++++++++++++++++ docs/system/arm/stm32.rst | 1 + docs/system/target-arm.rst | 1 + hw/arm/Kconfig | 6 +++ hw/arm/meson.build | 1 + hw/arm/stm32f4spark.c | 56 ++++++++++++++++++++++++++ 7 files changed, 113 insertions(+) create mode 100644 docs/system/arm/STM32F407-RT-SPARK.rst create mode 100644 hw/arm/stm32f4spark.c
MAINTAINERS +7 -0
@@ -1056,6 +1056,13 @@ S: Maintained F: hw/arm/stm32vldiscovery.c F: docs/system/arm/stm32.rst +STM32F4SPARK +M: yanl1229 <[email protected]> +M: Yihao Fan <[email protected]> +L: [email protected] +S: Maintained +F: hw/arm/stm32f4spark.c + Versatile Express M: Peter Maydell <[email protected]> L: [email protected]
docs/system/arm/STM32F407-RT-SPARK.rst new file +41 -0
41 lines not on this page · raw message
docs/system/arm/stm32.rst +1 -0
@@ -23,6 +23,7 @@ The following machines are based on this ARM Cortex-M4F chip : - ``netduinoplus2`` Netduino Plus 2 board with STM32F405RGT6 microcontroller - ``olimex-stm32-h405`` Olimex STM32 H405 board with STM32F405RGT6 microcontroller - ``b-l475e-iot01a`` :doc:`B-L475E-IOT01A IoT Node </system/arm/b-l475e-iot01a>` board with STM32L475VG microcontroller +- ``STM32F407-RT-SPARK`` STM32F407-RT-SPARK board with STM32F407ZG microcontroller There are many other STM32 series that are currently not supported by QEMU.
docs/system/target-arm.rst +1 -0
@@ -82,6 +82,7 @@ Board-specific documentation arm/aspeed arm/bananapi_m2u.rst arm/b-l475e-iot01a.rst + arm/STM32F407-RT-SPARK.rst arm/sabrelite arm/highbank arm/digic
hw/arm/Kconfig +6 -0
@@ -239,6 +239,12 @@ config STM32VLDISCOVERY depends on TCG && ARM select STM32F100_SOC +config STM32F4SPARK + bool + default y + depends on TCG && ARM + select STM32F407_SOC + config STRONGARM bool select PXA2XX_TIMER
hw/arm/meson.build +1 -0
@@ -17,6 +17,7 @@ arm_common_ss.add(when: 'CONFIG_REALVIEW', if_true: files('realview.c')) arm_ss.add(when: 'CONFIG_SBSA_REF', if_true: files('sbsa-ref.c')) arm_common_ss.add(when: 'CONFIG_STELLARIS', if_true: files('stellaris.c')) arm_common_ss.add(when: 'CONFIG_STM32VLDISCOVERY', if_true: files('stm32vldiscovery.c')) +arm_common_ss.add(when: 'CONFIG_STM32F4SPARK', if_true: files('stm32f4spark.c')) arm_common_ss.add(when: 'CONFIG_ZYNQ', if_true: files('xilinx_zynq.c')) arm_common_ss.add(when: 'CONFIG_SABRELITE', if_true: files('sabrelite.c'))
hw/arm/stm32f4spark.c new file +56 -0
56 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH v3 2/3] hw/arm: Add support for the STM32F407-RT-SPARk board
docs/system/arm/STM32F407-RT-SPARK.rst line 2
Quoted 13 lines · On Sun, 26 Oct 2025 at 10:53, <[email protected]> wrote:
From: Yihao Fan <[email protected]>
This commit adds a new STM32F4spark machine broad using the STM32F407 SoC. The implementation is derived from the Netduino Plus 2 machine.
Signed-off-by Yihao Fan <[email protected]>---
docs/system/arm/STM32F407-RT-SPARK.rst new file +2
-0@@ -0,0 +1,41 @@ +STM32F407-RT-SPARK (``stm32_f4spark``) +============================================
I think it would be better to document this board
as part of stm32.rst, not as its own file.
Otherwise this looks OK.
thanks
-- PMM
PATCH v3 3/3 hw/arm/stm32f407: Add PWR device to stm32f407 SoC
In reply to [email protected]From: Yihao Fan <[email protected]>
Add the Power supply configuration device to the stm32f407 SoC.
--- docs/system/arm/stm32.rst | 1 + hw/arm/Kconfig | 1 + hw/arm/stm32f407_soc.c | 9 +++ hw/misc/Kconfig | 3 + hw/misc/meson.build | 1 + hw/misc/stm32f4xx_pwr.c | 111 ++++++++++++++++++++++++++++++++ include/hw/arm/stm32f407_soc.h | 3 + include/hw/misc/stm32f4xx_pwr.h | 40 ++++++++++++ 8 files changed, 169 insertions(+) create mode 100644 hw/misc/stm32f4xx_pwr.c create mode 100644 include/hw/misc/stm32f4xx_pwr.h
docs/system/arm/stm32.rst +1 -0
@@ -38,6 +38,7 @@ Supported devices * System configuration (SYSCFG) * Timer controller (TIMER) * Reset and Clock Controller (RCC) (STM32F4 only, reset and enable only) + * Power supply configuration (PWR) (STM32F4 only) Missing devices ---------------
hw/arm/Kconfig +1 -0
@@ -417,6 +417,7 @@ config STM32F407_SOC select ARM_V7M select STM32F4XX_SYSCFG select STM32F4XX_EXTI + select STM32F4XX_PWR config B_L475E_IOT01A bool
hw/arm/stm32f407_soc.c +9 -0
@@ -51,6 +51,7 @@ static void stm32f407_soc_initfn(Object *obj)object_initialize_child (obj,"syscfg" , &s->syscfg,TYPE_STM32F4XX_SYSCFG );object_initialize_child (obj,"exti" , &s->exti,TYPE_STM32F4XX_EXTI );object_initialize_child (obj,"rcc" , &s->rcc,TYPE_STM32_RCC ); +object_initialize_child (obj,"pwr" , &s->pwr,TYPE_STM32F4XX_PWR );for (i =0 ; i <STM_NUM_USARTS ; i++) { object_initialize_child(obj, "usart[*]", &s->usart[i],
@@ -183,6 +184,14 @@ static void stm32f407_soc_realize(DeviceState *dev_soc, Error **errp)sysbus_connect_irq (busdev,0 ,qdev_get_gpio_in (armv7m, timer_irq[i])); } +/* PWR controller */ + dev =DEVICE (&s->pwr); +if (!sysbus_realize (SYS_BUS_DEVICE (&s->pwr), errp)) { +return ; + } + busdev =SYS_BUS_DEVICE (dev); +sysbus_mmio_map (busdev,0 ,PWR_BASE_ADDR ); + }static void stm32f407_soc_class_init (ObjectClass *klass,void *data)
hw/misc/Kconfig +3 -0
@@ -108,6 +108,9 @@ config STM32F4XX_SYSCFG config STM32F4XX_EXTI bool +config STM32F4XX_PWR + bool + config STM32L4X5_EXTI bool
hw/misc/meson.build +1 -0
@@ -112,6 +112,7 @@ 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')) +system_ss.add(when: 'CONFIG_STM32F4XX_PWR', if_true: files('stm32f4xx_pwr.c')) system_ss.add(when: 'CONFIG_STM32L4X5_EXTI', if_true: files('stm32l4x5_exti.c')) system_ss.add(when: 'CONFIG_STM32L4X5_SYSCFG', if_true: files('stm32l4x5_syscfg.c')) system_ss.add(when: 'CONFIG_STM32L4X5_RCC', if_true: files('stm32l4x5_rcc.c'))
hw/misc/stm32f4xx_pwr.c new file +111 -0
111 lines not on this page · raw message
include/hw/arm/stm32f407_soc.h +3 -0
@@ -17,6 +17,7 @@#include "hw/char/stm32f2xx_usart.h" #include "hw/timer/stm32f2xx_timer.h" #include "hw/misc/stm32_rcc.h" +#include "hw/misc/stm32f4xx_pwr.h" #include "qom/object.h" #define TYPE_STM32F407_SOC "stm32f407-soc"
@@ -39,6 +40,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(STM32F407State, STM32F407_SOC)#define SYSCFG_BASE_ADDRESS 0x40013800#define SYSCFG_IRQ 71#define EXIT_BASE_ADDRESS 0x40013C00 +#define PWR_BASE_ADDR 0x40007000#define FLASH_BASE_ADDRESS 0x8000000#define FLASH_SIZE 0x100000
@@ -57,6 +59,7 @@ typedef struct STM32F407State {STM32RccState rcc;STM32F4xxSyscfgState syscfg;STM32F4xxExtiState exti; +STM32F4XXPwrState pwr;STM32F2XXUsartState usart[STM_NUM_USARTS ];STM32F2XXTimerState timer[STM_NUM_TIMERS ];
include/hw/misc/stm32f4xx_pwr.h new file +40 -0
@@ -0,0 +1,40 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* +* Copyright (c) liang yan <[email protected]> +* Copyright (c) Yihao Fan <[email protected]> +* The reference used is the STMicroElectronics RM0090 Reference manual +* https://www.st.com/en/microcontrollers-microprocessors/stm32f407-417/documentation.html +*/ + +#ifndef STM32F4XX_PWR_H +#define STM32F4XX_PWR_H + +#include "hw/sysbus.h" +#include "qom/object.h" + +#define PWR_CR 0x00 +#define PWR_CSR 0x04 + +#define PWR_CR_DBP (1 << 8) +#define PWR_CR_ODEN (1 << 16) +#define PWR_CR_ODSWEN (1 << 17) + +#define PWR_CSR_ODRDY (1 << 16) +#define PWR_CSR_ODSWRDY (1 << 17) + +#define TYPE_STM32F4XX_PWR "stm32f4xx-pwr" +OBJECT_DECLARE_SIMPLE_TYPE(STM32F4XXPwrState, STM32F4XX_PWR) + +struct STM32F4XXPwrState { +/* <private> */ +SysBusDevice parent_obj; + +/* <public> */ +MemoryRegion mmio; + +uint32_t pwr_cr; +uint32_t pwr_csr; +}; + +#endif
Signature · 1 line
2.43.0
Re: [PATCH v3 3/3] hw/arm/stm32f407: Add PWR device to stm32f407 SoC
Quoted 6 lines · On Sun, 26 Oct 2025 at 10:53, <[email protected]> wrote:
From: Yihao Fan <[email protected]>
Add the Power supply configuration device to the stm32f407 SoC.
Signed-off-by Yihao Fan <[email protected]>
Please put "implement new device" and "add new device to SoC model" in separate patches.
Quoted 23 lines
hw/misc/stm32f4xx_pwr.c new file +20
-0@@ -0,0 +1,111 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +/* +* Copyright (c) liang yan <[email protected]> +* Copyright (c) Yihao Fan <[email protected]> +* The reference used is the STMicroElectronics RM0090 Reference manual +* https://www.st.com/en/microcontrollers-microprocessors/stm32f407-417/documentation.html +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/log.h" +#include "hw/misc/stm32f4xx_pwr.h" +#include "hw/irq.h" +#include "hw/qdev-properties.h" +#include "qemu/module.h" +#include "migration/vmstate.h" +#include "qemu/module.h" + +#ifndef STM32F4XX_PWR_DEBUG
hw/misc/stm32f4xx_pwr.c line 30
Quoted 10 lines
hw/misc/stm32f4xx_pwr.c new file +10
-0+#define STM32F4XX_PWR_DEBUG 0 +#endif + +#define DB_PRINT_L(lvl, fmt, args...) do { \ + if (STM32F4XX_PWR_DEBUG >= lvl) { \ + qemu_log("%s: " fmt, __func__, ## args); \ + } \ +} while (0); + +#define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
No debug print macros in new code, please. Use tracepoints or qemu_log* calls as appropriate.
Quoted 15 lines
hw/misc/stm32f4xx_pwr.c new file +15
-0+ +static uint64_t stm32f4xx_pwr_read (void *opaque,hwaddr offset,unsigned size) +{ +STM32F4XXPwrState *s = opaque; + +switch (offset) { +case PWR_CR : +return s->pwr_cr; +case PWR_CSR : +return s->pwr_csr; +default : +qemu_log_mask (LOG_GUEST_ERROR ,"STM32F4XX PWR: Bad read offset 0x%lx\n" , offset); +return 0 ; + } +}
hw/misc/stm32f4xx_pwr.c line 55
Quoted 10 lines
hw/misc/stm32f4xx_pwr.c new file +10
-0+ +static void stm32f4xx_pwr_write (void *opaque,hwaddr offset,uint64_t value,unsigned size) +{ +STM32F4XXPwrState *s = opaque; + +switch (offset) { +case PWR_CR : + s->pwr_cr = value; +if (value &PWR_CR_ODEN ) + s->pwr_csr |=PWR_CSR_ODRDY ;
QEMU coding style requires braces on all if() bodies, even one-line ones.
hw/misc/stm32f4xx_pwr.c line 57
Quoted 2 lines
hw/misc/stm32f4xx_pwr.c new file +2
-0+if (value &PWR_CR_ODSWEN ) + s->pwr_csr |=PWR_CSR_ODSWRDY ;
The reference manual says that the ODEN and ODSWEN bits in PWRCR are only there for STM32F42xxx and STM32F43xxx. I thought this model was for the STM32F407xx ?
Quoted 21 lines
hw/misc/stm32f4xx_pwr.c new file +21
-0+break ; +default : +qemu_log_mask (LOG_GUEST_ERROR ,"STM32F4XX PWR: Bad write offset 0x%lx\n" , offset); +break ; + } +} + +static const MemoryRegionOps stm32f4xx_pwr_ops = { + .read = stm32f4xx_pwr_read, + .write = stm32f4xx_pwr_write, + .endianness =DEVICE_NATIVE_ENDIAN , + .valid = { + .min_access_size =4 , + .max_access_size =4 , + }, +}; + +static void stm32f4xx_pwr_init (Object *obj) +{ +STM32F4XXPwrState *s =STM32F4XX_PWR (obj); +
hw/misc/stm32f4xx_pwr.c line 88
Quoted 10 lines
hw/misc/stm32f4xx_pwr.c new file +10
-0+memory_region_init_io (&s->mmio, obj, &stm32f4xx_pwr_ops, s,TYPE_STM32F4XX_PWR ,0x400 ); +sysbus_init_mmio (SYS_BUS_DEVICE (obj), &s->mmio); +} + +static void stm32f4xx_pwr_reset (DeviceState *dev) +{ +STM32F4XXPwrState *s =STM32F4XX_PWR (dev); + + s->pwr_cr =0x0000 ; + s->pwr_csr =0x0000 ;
Just "0" will do.
The reference manual says the reset value for
PWR_CR.VOS is 1.
hw/misc/stm32f4xx_pwr.c line 95
Quoted 7 lines
hw/misc/stm32f4xx_pwr.c new file +7
-0+} + +static void stm32f4xx_pwr_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + +device_class_set_legacy_reset (dc, stm32f4xx_pwr_reset);
This is missing a VMStateInfo for migration support.
Quoted 4 lines
hw/misc/stm32f4xx_pwr.c new file +4
-0+} + +static const TypeInfo stm32f4xx_pwr_info = { + .name =TYPE_STM32F4XX_PWR ,
hw/misc/stm32f4xx_pwr.c line 109
Quoted 10 lines
hw/misc/stm32f4xx_pwr.c new file +10
-0+ .parent = TYPE_SYS_BUS_DEVICE, + .instance_size = sizeof(STM32F4XXPwrState), + .instance_init = stm32f4xx_pwr_init, + .class_init = stm32f4xx_pwr_class_init, +}; + +static void stm32f4xx_pwr_register_types (void ) +{ +type_register_static (&stm32f4xx_pwr_info); +}
thanks
-- PMM
Quoted 34 lines · On Sun, 26 Oct 2025 at 10:53, <[email protected]> wrote:
From: Yihao Fan <[email protected]>
This patch series introduces basic support for the STM32F407 SoC and a new STM32F4spark machine in QEMU, along with a PWR device model.
In addition, I have modified the header file referenced by the RCC driver to STM32RCC in accordance with the suggestions.
This series includes:
- A new SoC model (STM32F407) with initial integration.
- A board model called STM32F4spark to instantiate and test the SoC.
- A PWR device implementation for STM32F4xx family.Signed-off-by Yihao Fan <[email protected]>---
Yihao Fan (3):
hw/arm: Add support for the STM32F407 SoC hw/arm: Add support for the STM32F407-RT-SPARk board hw/arm/stm32f407: Add PWR device to stm32f407 SoCMAINTAINERS | 14 ++ docs/system/arm/STM32F407-RT-SPARK.rst | 41 +++++ docs/system/arm/stm32.rst | 2 + docs/system/target-arm.rst | 1 + hw/arm/Kconfig | 13 ++ hw/arm/meson.build | 2 + hw/arm/stm32f407_soc.c | 217 +++++++++++++++++++++++++ hw/arm/stm32f4spark.c | 56 +++++++ hw/misc/Kconfig | 3 + hw/misc/meson.build | 1 + hw/misc/stm32f4xx_pwr.c | 111 +++++++++++++ include/hw/arm/stm32f407_soc.h | 77 +++++++++
Quoted 8 lines
include/hw/misc/stm32f4xx_pwr.h | 40 +++++ 13 files changed, 578 insertions(+) create mode 100644 docs/system/arm/STM32F407-RT-SPARK.rst create mode 100644 hw/arm/stm32f407_soc.c create mode 100644 hw/arm/stm32f4spark.c create mode 100644 hw/misc/stm32f4xx_pwr.c create mode 100644 include/hw/arm/stm32f407_soc.h create mode 100644 include/hw/misc/stm32f4xx_pwr.h
Thanks for sending this; I have some review comments, but they're mostly fairly minor.
Please can you include a functional test under tests/functional/arm/ which runs the new board model with some kind of guest image to check that it can successfully run code ?
thanks
-- PMM