Qemu-STM32(十二):STM32F103 框架代码添加

简介

本系列博客主要描述了STMF103的qemu模拟器实现,进行该项目的原因有两点: 作者在高铁上,想在STM32F103上验证一个软件框架时,如果此时掏出开发板,然后接一堆的线,旁边的人估计会投来异样的目光,特别是,当不太幸运坐在了靠近过道的位置,那就更麻烦了,估计没法进行代码开发了。因此,作者决定开发这个模拟器该项目,只要打开电脑,就可以随意的开发软件功能;第二个原因,作者也在设计STM32F103的PCB板卡,在硬件板卡还没焊接回来时,也可以提前进行产品原型的代码开发。

硬件板卡3d图

添加步骤

1. 创建stm32f103_soc.h文件

在qemu源码下添加include/hw/arm/stm32f103_soc.h文件,在文件中主要添加如下成员:

diff --git a/include/hw/arm/stm32f103_soc.h b/include/hw/arm/stm32f103_soc.h
new file mode 100644
index 00000000..182fe14b
--- /dev/null
+++ b/include/hw/arm/stm32f103_soc.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2025 liang yan <yanl1229@163.com>
+ *
+ * STM32F103 SoC
+ *
+ * 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.
+ */
+#ifndef STM32F103_SOC_H
+#define STM32F103_SOC_H
+
+#include "hw/or-irq.h"
+#include "hw/arm/armv7m.h"
+
+
+#define FLASH_BASE_ADDRESS  0x8000000
+#define FLASH_SIZE          (128*1024)
+#define SRAM_BASE_ADDRESS   0x20000000
+#define SRAM_SIZE           (20 * 1024)
+
+#define TYPE_STM32F103_SOC "stm32f103-soc"
+#define STM32F103_SOC(obj) \
+    OBJECT_CHECK(STM32F103State, (obj), TYPE_STM32F103_SOC)
+
+typedef struct STM32F103State {
+    /*< private >*/
+    SysBusDevice parent_obj;
+    /*< public >*/
+
+    char *kernel_filename;
+    char *cpu_type;
+    ARMv7MState armv7m;
+
+} STM32F103State;
+
+#endif

2. 创建stm32f103_soc.c文件

在qemu源码下添加hw/arm/stm32f103_soc.c文件,在文件中主要内容如下

diff --git a/hw/arm/stm32f103_soc.c b/hw/arm/stm32f103_soc.c
new file mode 100644
index 00000000..b072855e
--- /dev/null
+++ b/hw/arm/stm32f103_soc.c
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2025 liang yan <yanl1229@163.com>
+ *
+ * STM32F103 SoC
+ *
+ * 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 "qapi/error.h"
+#include "qemu-common.h"
+#include "qemu/osdep.h"
+#include "qemu/module.h"
+#include "hw/arm/boot.h"
+#include "hw/qdev-properties.h"
+#include "sysemu/sysemu.h"
+#include "migration/vmstate.h"
+#include "exec/address-spaces.h"
+#include "hw/arm/stm32f103_soc.h"
+#include "sysemu/sysemu.h"
+
+static void stm32f103_soc_initfn(Object *obj)
+{
+
+    STM32F103State *s = STM32F103_SOC(obj);
+
+    sysbus_init_child_obj(obj, "armv7m", &s->armv7m, sizeof(s->armv7m),
+                          TYPE_ARMV7M);
+}
+
+static void stm32f103_soc_realize(DeviceState *dev_soc, Error **errp)
+{
+    STM32F103State *s = STM32F103_SOC(dev_soc);
+    DeviceState *armv7m;
+    Error *err = NULL;
+
+    armv7m = DEVICE(&s->armv7m);
+    qdev_prop_set_uint32(armv7m, "num-irq", 68);
+    qdev_prop_set_string(armv7m, "cpu-type", s->cpu_type);
+    qdev_prop_set_bit(armv7m, "enable-bitband", true);
+    object_property_set_link(OBJECT(&s->armv7m), OBJECT(get_system_memory()),
+                                     "memory", &error_abort);
+    object_property_set_bool(OBJECT(&s->armv7m), true, "realized", &err);
+    if (err != NULL) {
+        error_propagate(errp, err);
+        return;
+    }
+}
+
+static Property stm32f103_soc_properties[] = {
+    DEFINE_PROP_STRING("kernel-filename", STM32F103State, kernel_filename),
+    DEFINE_PROP_STRING("cpu-type", STM32F103State, cpu_type),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void stm32f103_soc_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+
+    dc->realize = stm32f103_soc_realize;
+    dc->props = stm32f103_soc_properties;
+}
+
+static const TypeInfo stm32f103_soc_info = {
+    .name          = TYPE_STM32F103_SOC,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(STM32F103State),
+    .instance_init = stm32f103_soc_initfn,
+    .class_init    = stm32f103_soc_class_init,
+};
+
+static void stm32f103_soc_types(void)
+{
+    type_register_static(&stm32f103_soc_info);
+}
+
+type_init(stm32f103_soc_types)

3. 创建开发板文件

在qemu源码下创建hw/arm/stm32f103-st-openmcu.c, 文件主要内容如下所示:

diff --git a/hw/arm/stm32f103-st-openmcu.c b/hw/arm/stm32f103-st-openmcu.c
new file mode 100644
index 00000000..d3e60021
--- /dev/null
+++ b/hw/arm/stm32f103-st-openmcu.c
@@ -0,0 +1,67 @@
+/*
+ * Copyright (c) 2025 liang yan <yanl1229@163.com>
+ *
+ * STM32F103 OpenMCU Board
+ *
+ *
+ * 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 "qapi/error.h"
+#include "hw/boards.h"
+#include "qemu/error-report.h"
+#include "exec/address-spaces.h"
+#include "hw/arm/stm32f103_soc.h"
+#include "hw/qdev-properties.h"
+#include "hw/arm/boot.h"
+
+static void openmcu_init(MachineState *machine)
+{
+    DeviceState *dev;
+    MemoryRegion *system_memory = get_system_memory();
+    MemoryRegion *sram = g_new(MemoryRegion, 1);
+    MemoryRegion *flash = g_new(MemoryRegion, 1);
+    MemoryRegion *flash_alias = g_new(MemoryRegion, 1);
+
+    memory_region_init_ram(flash, NULL, "STM32F103.flash", FLASH_SIZE,
+                           &error_fatal);
+    memory_region_init_alias(flash_alias, NULL, "STM32F103.flash.alias",
+                             flash, 0, FLASH_SIZE);
+
+    memory_region_add_subregion(system_memory, FLASH_BASE_ADDRESS, flash);
+    memory_region_add_subregion(system_memory, 0, flash_alias);
+
+    memory_region_init_ram(sram, NULL, "STM32F103.sram", SRAM_SIZE,
+                           &error_fatal);
+    memory_region_add_subregion(system_memory, SRAM_BASE_ADDRESS, sram);
+
+    dev = qdev_create(NULL, TYPE_STM32F103_SOC);
+
+    qdev_prop_set_string(dev, "cpu-type", ARM_CPU_TYPE_NAME("cortex-m3"));
+    object_property_set_bool(OBJECT(dev), true, "realized", &error_fatal);
+
+    armv7m_load_kernel(ARM_CPU(first_cpu), machine->kernel_filename,
+                       FLASH_SIZE);
+}
+
+static void openmcu_machine_init(MachineClass *mc)
+{
+    mc->desc = "Open MCU EVK Board (STM32F103 Soc)";
+    mc->init = openmcu_init;
+}
+
+DEFINE_MACHINE("stm32f103-openmcu", openmcu_machine_init)

4. 添加编译规则

a. 修改:hw/arm/Kconfig

diff --git a/hw/arm/Kconfig b/hw/arm/Kconfig
index c647cf1c..85c93cdf 100644
--- a/hw/arm/Kconfig
+++ b/hw/arm/Kconfig
@@ -129,6 +129,10 @@ config NETDUINO2boolselect STM32F205_SOC+config STM32F103_ST_OPENMCU
+    bool
+    select STM32F103_SOC
+config STM32F429_ST_DISCOVERYboolselect STM32F429_SOC
@@ -405,6 +409,11 @@ config RASPIselect PL011 # UARTselect SDHCI+config STM32F103_SOC
+    bool
+    select ARM_V7M
+    select OR_IRQ
+config STM32F205_SOCboolselect ARM_V7M

b. 修改default-configs/devices/arm-softmmu.mak:

diff --git a/default-configs/arm-softmmu.mak b/default-configs/arm-softmmu.mak
index 3a360620..1dcce679 100644
--- a/default-configs/arm-softmmu.mak
+++ b/default-configs/arm-softmmu.mak
@@ -46,6 +46,7 @@ CONFIG_STM32F429_ST_DISCOVERY=yCONFIG_STM32F407_ST_DISCOVERY=yCONFIG_STM32H743_ST_NUCLEO=yCONFIG_STM32H750_ART_PI=y
+CONFIG_STM32F103_ST_OPENMCU=yCONFIG_MINI2440=yCONFIG_SMDK2416=yCONFIG_ORANGE_PI=y

c. 修改hw/arm/Makefile.objs

diff --git a/hw/arm/Makefile.objs b/hw/arm/Makefile.objs
index a4eddba1..a7397383 100644
--- a/hw/arm/Makefile.objs
+++ b/hw/arm/Makefile.objs
@@ -36,6 +36,7 @@ obj-$(CONFIG_VERSATILEAB) += versatileab.oobj-$(CONFIG_VEXPRESS) += vexpress.o vexpress-a9.oobj-$(CONFIG_ZYNQ) += xilinx_zynq.oobj-$(CONFIG_SABRELITE) += sabrelite.o
+obj-$(CONFIG_STM32F103_ST_OPENMCU) += stm32f103-st-openmcu.oobj-$(CONFIG_STM32F429_ST_DISCOVERY) += stm32f429-st-disco.oobj-$(CONFIG_STM32F407_ST_DISCOVERY) += stm32f407-st-disco.oobj-$(CONFIG_STM32H743_ST_NUCLEO) += stm32h743-st-nucleo.o
@@ -48,6 +49,7 @@ obj-$(CONFIG_OMAP) += omap1.o omap2.oobj-$(CONFIG_STRONGARM) += strongarm.oobj-$(CONFIG_ALLWINNER_A10) += allwinner-a10.o cubieboard.oobj-$(CONFIG_RASPI) += bcm2835_peripherals.o bcm2836.o raspi.o
+obj-$(CONFIG_STM32F103_SOC) += stm32f103_soc.oobj-$(CONFIG_STM32F205_SOC) += stm32f205_soc.oobj-$(CONFIG_STM32F429_SOC) += stm32f429_soc.oobj-$(CONFIG_STM32F407_SOC) += stm32f407_soc.o

工程文件

yanl1229/qemu-5.0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/41885.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

英伟达与通用汽车深化合作,澳特证券am broker助力科技投资

在近期的GTC大会上&#xff0c;英伟达CEO黄仁勋宣布英伟达将与通用汽车深化合作&#xff0c;共同推进AI技术在自动驾驶和智能工厂的应用。此次合作标志着自动驾驶汽车时代的加速到来&#xff0c;同时也展示了英伟达在AI技术领域的最新进展。      合作内容包括&#xff1a;…

将 Markdown 表格结构转换为Excel 文件

在数据管理和文档编写过程中&#xff0c;我们经常使用 Markdown 来记录表格数据。然而&#xff0c;Markdown 格式的表格在实际应用中不如 Excel 方便&#xff0c;特别是需要进一步处理数据时。因此&#xff0c;我们开发了一个使用 wxPython 的 GUI 工具&#xff0c;将 Markdown…

HarmonyOS NEXT 关于鸿蒙的一多开发(一次开发,多端部署) 1+8+N

官方定义 定义&#xff1a;一套代码工程&#xff0c;一次开发上架&#xff0c;多端按需部署。 目标&#xff1a;支撑开发者快速高效的开发支持多种终端设备形态的应用&#xff0c;实现对不同设备兼容的同时&#xff0c;提供跨设备的流转、迁移和协同的分布式体验。 什么是18…

Nacos

简介 Nacos&#xff08;Dynamic Naming and Configuration Service&#xff09;是阿里巴巴开源的一款动态服务发现、配置管理和服务管理平台&#xff0c;旨在为微服务架构提供高可用、高性能的解决方案。其核心功能包括服务注册与发现、动态配置管理、服务健康监测、动态 DNS …

Win11系统下qq远程不能控制对方电脑(鼠标点不动)的解决方法

在被控制的电脑上&#xff0c;打开控制面板&#xff0c;点击系统和安全 点击更改用户账户控制设置 下拉用户控制设置至最低&#xff0c;从不通知&#xff0c;点击确定 返回控制面板系统与安全&#xff0c;带年纪允许远程访问 点击允许远程协助连接这台计算机 重启电脑 再次打…

猎豹移动营收连续三季增长,AI驱动的猎豹成绩单怎么分析?

3月26日&#xff0c;猎豹移动发布2024年Q4及全年财报&#xff0c;这份财报我们到底该该怎么分析呢&#xff1f; 首先&#xff0c;整体财务表现稳健&#xff0c;营收连续三季增长。从财务数据来看&#xff0c;猎豹移动整体表现稳健。2024年Q4及全年财报显示&#xff0c;总收入达…

函数:链式访问

链式访问是将函数的返回值当作回传值就是链式访问 这是原本的字符数回传代码 int main() {int len strlen("seig heil");printf("%d", len);return 0; } 运行结果&#xff1a; 这是链式访问的代码&#xff1a; int main() {printf("%d\n",s…

C++ map容器总结

map基本概念 简介&#xff1a; map中所有元素都是pair pair中第一个元素为key&#xff08;键值&#xff09;&#xff0c;起到索引作用&#xff0c;第二个元素为value&#xff08;实值&#xff09; 所有元素都会根据元素的键值自动排序 本质&#xff1a; map/multimap属于关…

23种设计模式-代理(Proxy)设计模式

代理设计模式 &#x1f6a9;什么是代理设计模式&#xff1f;&#x1f6a9;代理设计模式的特点&#x1f6a9;代理设计模式的结构&#x1f6a9;代理设计模式的优缺点&#x1f6a9;代理设计模式的Java实现&#x1f6a9;代码总结&#x1f6a9;总结 &#x1f6a9;什么是代理设计模式…

UE4学习笔记 FPS游戏制作29 更换武器时更换武器的图标

文章目录 制作物体图标UI添加获取武器图标的方法使用事件分发器&#xff0c;通知UI要换枪定义事件分发器调用事件分发器注册事件分发器 制作物体图标UI 在Fpp-UI上添加一个图片&#xff0c;改名为五weaponIcon&#xff0c;勾选SizeToContent,锚点放在右下角&#xff0c;对齐改…

Chrome 开发环境快速屏蔽 CORS 跨域限制!

Chrome 开发环境快速屏蔽 CORS 跨域限制【详细教程】 ❓ 为什么需要临时屏蔽 CORS&#xff1f; 在前后端开发过程中&#xff0c;我们经常会遇到 跨域请求被浏览器拦截 的问题。例如&#xff0c;你在 http://localhost:3000 调用 https://api.example.com 时&#xff0c;可能会…

【RAG综述系列】之 RAG 相关背景和基本原理

系列文章&#xff1a; 【RAG综述系列】之 RAG 相关背景和基本原理 【RAG综述系列】之 RAG 特点与挑战以及方法与评估 【RAG综述系列】之 RAG 先进方法与综合评估 【RAG综述系列】之 RAG 应用和未来方向 正文&#xff1a; 检索增强生成&#xff08;Retrieval-Augmented Gen…

德昂观点:如何看待MicroStrategy改名为Strategy?

2025年2月&#xff0c;纳斯达克上市公司MicroStrategy&#xff08;股票代码&#xff1a;MSTR&#xff09;宣布更名为“Strategy”&#xff0c;并同步启用全新品牌标识与橙色主视觉。这不仅是品牌形象的更新&#xff0c;更是公司战略方向的明确宣示。德昂作为MSTR中国区BI合作伙…

计算机视觉算法实战——手术导航:技术、应用与未来

✨个人主页欢迎您的访问 ✨期待您的三连 ✨ ✨个人主页欢迎您的访问 ✨期待您的三连 ✨ ✨个人主页欢迎您的访问 ✨期待您的三连✨ ​​​ ​​​​​​​​​ ​​ 1. 手术导航中的计算机视觉&#xff1a;领域介绍 计算机视觉在手术导航领域的应用代表了现代医学与人工智…

Java全栈面试宝典:内存模型与Spring设计模式深度解析

目录 一、JVM内存模型进阶篇 &#x1f525; 问题13&#xff1a;堆与栈的六大维度对比 内存结构对比图 核心差异对照表 &#x1f525; 问题14&#xff1a;三区联动内存模型解析 代码内存分配图解 三区协作流程图 二、Spring设计模式全景解析 &#x1f31f; Spring框架七…

FALL靶场通关攻略

1&#xff0c;下载好靶机后打开&#xff0c;通过kali扫描靶机ip和端口&#xff0c;得到靶机ip为192.168.50.144 2&#xff0c;扫描目录 3&#xff0c;访问靶机 4&#xff0c;访问扫描到的test.php,得到缺少GET请求参数的提示 5&#xff0c;使用FUZZ来扫出参数为file 6&#xff…

《C++11:bind绑定器与function包装器》

CSTL中提供了bind1绑定器&#xff0c;通常与函数对象一起使用。 函数对象是重载了operator&#xff08;&#xff09;函数的对象。 将二元函数对象operator&#xff08;&#xff09;的第一个参数绑定为固定的x来构造一元函数对象。返回绑定了第一个参数的函数对象。 将二元函数…

JS 防抖与节流

防抖 核心思想&#xff1a;延迟执行&#xff0c;只有在事件触发的频率降低到一定程度后才会执行&#xff0c;而且如果事件持续触发&#xff0c;之前地所有执行都会被取消。 有的操作是高频触发的&#xff0c;但是其实只需要一次触发。比如短时间内多次缩放页面resize&#xff0…

【C/C++算法】从浅到深学习--- 简单模拟算法(图文兼备 + 源码详解)

绪论&#xff1a;冲击蓝桥杯一起加油&#xff01;&#xff01; 每日激励&#xff1a;“不设限和自我肯定的心态&#xff1a;I can do all things。 — Stephen Curry” 绪论​&#xff1a; 本篇是一些简单的模拟算法&#xff0c;其中模拟的本质就是就是根据题目意思进行代码的…

​​解锁 JavaScript DOM:节点操作的核心方法与最佳实践

引言 在当今动态化的 Web 世界中&#xff0c;用户早已不满足于静态的网页展示&#xff0c;而是期待流畅的交互体验——点击按钮弹出菜单、滚动页面加载数据、实时搜索过滤内容……这些功能的背后&#xff0c;都离不开 ​JavaScript DOM&#xff08;文档对象模型&#xff09;操…