概述
本文主要描述了在Qemu平台中,如何添加STM32F407的RCC控制器模拟代码。
参考资料
STM32F4XX TRM手册,手册编号:RM0090
添加步骤
1、在hw/arm/Kconfig文件中添加STM32F4XX_RCC,如下所示:
+号部分为新增加内容
index baf5d649..78e6c820 100644
--- a/hw/arm/Kconfig
+++ b/hw/arm/Kconfig
@@ -339,6 +339,7 @@ config STM32F407_SOC
select STM32F4XX_SYSCFG
select STM32F4XX_EXTI
select STM32F4XX_USART
+ select STM32F4XX_RCC
2、在include/hw/arm/stm32f407_soc.h文件中添加
+号部分为新增加内容
index 4078db02..37a57e61 100644
--- a/include/hw/arm/stm32f407_soc.h
+++ b/include/hw/arm/stm32f407_soc.h
@@ -26,6 +26,7 @@
#include "hw/misc/stm32f4xx_syscfg.h"
#include "hw/misc/stm32f4xx_exti.h"
#include "hw/char/stm32f4xx_usart.h"
+#include "hw/misc/stm32f4xx_rcc.h"
#define TYPE_STM32F407_SOC "stm32f407-soc"
#define STM32F407_SOC(obj) \
@@ -42,6 +43,8 @@
#define STM32F407_USART3 0x40004800
#define STM32F407_USART6 0x40011400
+#define RCC_BASE_ADDR 0x40023800
+
#define FLASH_BASE_ADDRESS 0x8000000
#define FLASH_SIZE 0x100000
#define SRAM_BASE_ADDRESS 0x20000000
@@ -60,6 +63,7 @@ typedef struct STM32F407State {
STM32F4xxSyscfgState syscfg;
STM32F4xxExtiState exti;
+ STM32F4XXRccState rcc;
STM32F4XXUsartState usart[STM_NUM_USARTS];
} STM32F407State;
3、在hw/arm/stm32f407_soc.c文件中添加如下代码片段
+号部分为新增加内容
index ab555e56..185c5081 100644
--- a/hw/arm/stm32f407_soc.c
+++ b/hw/arm/stm32f407_soc.c
@@ -60,6 +60,9 @@ static void stm32f407_soc_initfn(Object *obj)
sysbus_init_child_obj(obj, "exti", &s->exti, sizeof(s->exti),
TYPE_STM32F4XX_EXTI);
+ object_initialize(&s->rcc, sizeof(s->rcc), TYPE_STM32F4XX_RCC);
+ qdev_set_parent_bus(DEVICE(&s->rcc), sysbus_get_default());
+
for (i = 0; i < STM_NUM_USARTS; i++) {
object_initialize(&s->usart[i], sizeof(s->usart[i]), TYPE_STM32F4XX_USART);
qdev_set_parent_bus(DEVICE(&s->usart[i]), sysbus_get_default());
@@ -110,6 +113,16 @@ static void stm32f407_soc_realize(DeviceState *dev_soc, Error **errp)
for (i = 0; i < 16; i++) {
qdev_connect_gpio_out(DEVICE(&s->syscfg), i, qdev_get_gpio_in(dev, i));
}
+ /* System rcc controller */
+ dev = DEVICE(&s->rcc);
+ object_property_set_bool(OBJECT(&s->rcc), true, "realized", &err);
+ if (err != NULL) {
+ error_propagate(errp, err);
+ return;
+ }
+ busdev = SYS_BUS_DEVICE(dev);
+ sysbus_mmio_map(busdev, 0, RCC_BASE_ADDR);
+
/* USART controllers */
for (i = 0; i < STM_NUM_USARTS; i++) {
dev = DEVICE(&(s->usart[i]));
4.在hw/miscKconfig中添加
index bdd77d80..6f440622 100644
--- a/hw/misc/Kconfig
+++ b/hw/misc/Kconfig
@@ -88,6 +88,9 @@ config STM32F4XX_SYSCFG
config STM32F4XX_EXTI
bool
+config STM32F4XX_RCC
+ bool
+
config MIPS_ITU
bool
5.在hw/misc/Makefile.objs中添加
index 68aae2ea..2cc860c2 100644
--- a/hw/misc/Makefile.objs
+++ b/hw/misc/Makefile.objs
@@ -66,6 +66,7 @@ common-obj-$(CONFIG_ZYNQ) += zynq-xadc.o
common-obj-$(CONFIG_STM32F2XX_SYSCFG) += stm32f2xx_syscfg.o
common-obj-$(CONFIG_STM32F4XX_SYSCFG) += stm32f4xx_syscfg.o
common-obj-$(CONFIG_STM32F4XX_EXTI) += stm32f4xx_exti.o
+common-obj-$(CONFIG_STM32F4XX_RCC) += stm32f4xx_rcc.o
obj-$(CONFIG_MIPS_CPS) += mips_cmgcr.o
obj-$(CONFIG_MIPS_CPS) += mips_cpc.o
obj-$(CONFIG_MIPS_ITU) += mips_itu.o
6.在hw/misc/创建新文件stm32f4xx_rcc.c
+/*
+ * Copyright (c) 2020 liang yan <yanl1229@163.com>
+ *
+ * STM32F4XX RCC
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, see <http://www.gnu.org/licenses/>.
+ *
+ * Contributions after 2012-01-13 are licensed under the terms of the
+ * GNU GPL, version 2 or (at your option) any later version.
+ */
+#include "qemu/osdep.h"
+#include "hw/misc/stm32f4xx_rcc.h"
+#include "qemu/log.h"
+#include "hw/irq.h"
+#include "hw/qdev-properties.h"
+#include "qe