概述
本文主要描述了在Qemu平台中,如何添加STM32F407的GPIO控制器模拟代码。
参考资料
STM32F4XX TRM手册,手册编号:RM0090
添加步骤
1、在hw/arm/Kconfig文件中添加STM32F4XX_GPIO,如下所示:
+号部分为新增加内容
--- a/hw/arm/Kconfig
+++ b/hw/arm/Kconfig
@@ -342,6 +342,7 @@ config STM32F407_SOC
select STM32F4XX_RCC
select STM32F4XX_PWR
select STM32F4XX_TIMER
+ select STM32F4XX_GPIO
2、在include/hw/arm/stm32f407_soc.h文件中添加
+号部分为新增加内容
--- a/include/hw/arm/stm32f407_soc.h
+++ b/include/hw/arm/stm32f407_soc.h
@@ -29,6 +29,7 @@
#include "hw/misc/stm32f4xx_rcc.h"
#include "hw/misc/stm32f4xx_pwr.h"
#include "hw/timer/stm32f4xx_timer.h"
+#include "hw/gpio/stm32f4xx_gpio.h"
#define TYPE_STM32F407_SOC "stm32f407-soc"
#define STM32F407_SOC(obj) \
@@ -62,6 +63,20 @@
#define RCC_BASE_ADDR 0x40023800
#define POWER_BASE_ADDR 0x40007000
+/* PortA ~ PortK */
+#define STM_NUM_GPIOS 11
+#define STM_GPIO_PORTA 0x40020000
+#define STM_GPIO_PORTB 0x40020400
+#define STM_GPIO_PORTC 0x40020800
+#define STM_GPIO_PORTD 0x40020c00
+#define STM_GPIO_PORTE 0x40021000
+#define STM_GPIO_PORTF 0x40021400
+#define STM_GPIO_PORTG 0x40021800
+#define STM_GPIO_PORTH 0x40021c00
+#define STM_GPIO_PORTI 0x40022000
+#define STM_GPIO_PORTJ 0x40022400
+#define STM_GPIO_PORTK 0x40022800
+
#define FLASH_BASE_ADDRESS 0x8000000
#define FLASH_SIZE 0x100000
#define SRAM_BASE_ADDRESS 0x20000000
@@ -84,6 +99,8 @@ typedef struct STM32F407State {
STM32F4XXPowerState power;
STM32F4XXUsartState usart[STM_NUM_USARTS];
STM32F4XXTimerState timer[STM_NUM_TIMERS];
+ STM32F4XXGPIOState gpio[STM_NUM_GPIOS];
+
} STM32F407State;
#endif
\ No newline at end of file
3、在hw/arm/stm32f407_soc.c文件中添加如下代码片段
+号部分为新增加内容
--- a/hw/arm/stm32f407_soc.c
+++ b/hw/arm/stm32f407_soc.c
@@ -42,6 +42,12 @@ static const uint32_t usart_addr[STM_NUM_USARTS] = {
STM32F407_USART6
};
+static const uint32_t gpio_addr[STM_NUM_GPIOS] = {
+ STM_GPIO_PORTA, STM_GPIO_PORTB, STM_GPIO_PORTC, STM_GPIO_PORTD,
+ STM_GPIO_PORTE, STM_GPIO_PORTF, STM_GPIO_PORTG, STM_GPIO_PORTH,
+ STM_GPIO_PORTI, STM_GPIO_PORTJ, STM_GPIO_PORTK
+};
+
static const int usart_irq[STM_NUM_USARTS] = {
37, 38, 39, 71
};
@@ -84,6 +90,12 @@ static void stm32f407_soc_initfn(Object *obj)
object_initialize(&s->timer[i], sizeof(s->timer[i]), TYPE_STM32F4XX_TIMER);
qdev_set_parent_bus(DEVICE(&s->timer[i]), sysbus_get_default());
}
+
+ for (i = 0; i < STM_NUM_GPIOS; i++) {
+ object_initialize(&s->gpio[i], sizeof(s->gpio[i]),
+ TYPE_STM32F4XX_GPIO);
+ qdev_set_parent_bus(DEVICE(&s->gpio[i]), sysbus_get_default());
+ }
}
static void stm32f407_soc_realize(DeviceState *dev_soc, Error **errp)
@@ -174,6 +186,17 @@ static void stm32f407_soc_realize(DeviceState *dev_soc, Error **errp)
sysbus_mmio_map(busdev, 0, timer_addr[i]);
sysbus_connect_irq(busdev, 0, qdev_get_gpio_in(armv7m, timer_irq[i]));
}
+ /* GPIO A to K */
+ for (i = 0; i < STM_NUM_GPIOS; i++) {
+ dev = DEVICE(&(s->gpio[i]));
+ object_property_set_bool(OBJECT(&s->gpio[i]), true, "realized", &err);
+ if (err != NULL) {
+ error_propagate(errp, err);
+ return;
+ }
+ busdev = SYS_BUS_DEVICE(dev);
+ sysbus_mmio_map(busdev, 0, gpio_addr[i]);
+ }
}
4.在hw/gpio/Kconfig中添加
--- a/hw/gpio/Kconfig
+++ b/hw/gpio/Kconfig
@@ -7,3 +7,6 @@ config PL061
config GPIO_KEY
bool
+
+config STM32F4XX_GPIO
+ bool
\ No newline at end of file
5.在hw/gpio/Makefile.objs中添加
--- a/hw/gpio/Makefile.objs
+++ b/hw/gpio/Makefile.objs
@@ -10,3 +10,4 @@ common-obj-$(CONFIG_IMX) += imx_gpio.o
common-obj-$(CONFIG_RASPI) += bcm2835_gpio.o
common-obj-$(CONFIG_NRF51_SOC) += nrf51_gpio.o
common-obj-$(CONFIG_ASPEED_SOC) += aspeed_gpio.o
+common-obj-$(CONFIG_STM32F4XX_GPIO) += stm32f4xx_gpio.o
\ No newline at end of file
6.在hw/gpio/创建新文件stm32f4xx_gpio.c
new file mode 100644
index 00000000..b72e5c5c
--- /dev/null
+++ b/hw/gpio/stm32f4xx_gpio.c
@@ -0,0 +1,206 @@
+/*
+ * Copyright (c) 2020 liang yan <yanl1229@163.com>
+ *
+ * STM32F4XX GPIO
+ *
+ * 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/log.h"
+#include "hw/gpio/stm32f4xx_gpio.h"
+#include "hw/irq.h"
+#include "hw/qdev-properties.h"
+#include "migration/vmstate.h"
+#include "qemu/module.h"
+
+#ifndef STM_GPIO_ERR_DEBUG
+#define STM_GPIO_ERR_DEBUG 0
+#endif
+
+#define DB_PRINT_L(lvl, fmt, args...) do { \
+ if (STM_GPIO_ERR_DEBUG >= lvl) { \
+ qemu_log("%s: " fmt, __func__, ## args); \
+ } \
+} while (0);
+
+#define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
+
+static void stm32f4xx_gpio_reset(DeviceState *dev)
+{
+ STM32F4XXGPIOState *s = STM32F4XX_GPIO(dev);
+ s->gpio_mode = 0x00000000;
+ s->gpio_otyper = 0x00000000;
+ s->gpio_ospeedr = 0x00000000;
+ s->gpio_otyper = 0x00000000;
+ s->gpio_pupdr = 0x00000000;
+ s->gpio_idr = 0x00000000;
+ s->gpio_odr = 0x00000000;
+ s->gpio_bsrr = 0x00000000;
+ s->gpio_lckr = 0x00000000;
+ s->gpio_afrl = 0x00000000;
+ s->gpio_afrh = 0x00000000;
+}
+
+static uint64_t stm32f4xx_gpio_read(void *opaque, hwaddr addr,
+ unsigned int size)
+{
+ STM32F4XXGPIOState *s = opaque;
+ uint64_t retvalue = 0;
+
+ DB_PRINT("Address: 0x%" HWADDR_PRIx "\n", addr);
+ switch(addr) {
+ case STM_GPIO_MODE:
+ retvalue = s->gpio_mode;
+ break;
+ case STM_GPIO_OTYPER:
+ retvalue = s->gpio_otyper;
+ break;
+ case STM_GPIO_OSPEEDR:
+ retvalue = s->gpio_ospeedr;
+ break;
+ case STM_GPIO_PUPDR:
+ retvalue = s->gpio_pupdr;
+ break;
+ case STM_GPIO_IDR:
+ retvalue = s->gpio_idr;
+ break;
+ case STM_GPIO_ODR:
+ retvalue = s->gpio_odr;
+ break;
+ case STM_GPIO_BSRR:
+ retvalue = s->gpio_bsrr;
+ break;
+ case STM_GPIO_LCKR:
+ retvalue = s->gpio_lckr;
+ break;
+ case STM_GPIO_AFRL:
+ retvalue = s->gpio_afrl;
+ break;
+ case STM_GPIO_AFRH:
+ retvalue = s->gpio_afrh;
+ break;
+ default:
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
+ __func__, addr);
+ retvalue = 0;
+ break;
+ }
+ return retvalue;
+}
+
+static void stm32f4xx_gpio_write(void *opaque, hwaddr addr,
+ uint64_t val64, unsigned int size)
+{
+ STM32F4XXGPIOState *s = opaque;
+ uint32_t value = val64;
+
+ DB_PRINT("Address: 0x%" HWADDR_PRIx ", Value: 0x%x\n", addr, value);
+ switch(addr) {
+ case STM_GPIO_MODE:
+ s->gpio_mode = value;
+ break;
+ case STM_GPIO_OTYPER:
+ s->gpio_otyper = value;
+ break;
+ case STM_GPIO_OSPEEDR:
+ s->gpio_ospeedr = value;
+ break;
+ case STM_GPIO_PUPDR:
+ s->gpio_pupdr = value;
+ break;
+ case STM_GPIO_IDR:
+ s->gpio_idr = value;
+ break;
+ case STM_GPIO_ODR:
+ s->gpio_odr = value;
+ break;
+ case STM_GPIO_BSRR:
+ s->gpio_bsrr = value;
+ break;
+ case STM_GPIO_LCKR:
+ s->gpio_lckr = value;
+ break;
+ case STM_GPIO_AFRL:
+ s->gpio_afrl = value;
+ break;
+ case STM_GPIO_AFRH:
+ s->gpio_afrh = value;
+ break;
+ default:
+ qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
+ __func__, addr);
+ }
+}
+
+static const MemoryRegionOps stm32f4xx_gpio_ops = {
+ .read = stm32f4xx_gpio_read,
+ .write = stm32f4xx_gpio_write,
+ .endianness = DEVICE_NATIVE_ENDIAN,
+};
+
+static const VMStateDescription vmstate_stm32f4xx_gpio = {
+ .name = TYPE_STM32F4XX_GPIO,
+ .version_id = 1,
+ .minimum_version_id = 1,
+ .fields = (VMStateField[]) {
+ VMSTATE_UINT32(gpio_mode, STM32F4XXGPIOState),
+ VMSTATE_UINT32(gpio_otyper, STM32F4XXGPIOState),
+ VMSTATE_UINT32(gpio_ospeedr, STM32F4XXGPIOState),
+ VMSTATE_UINT32(gpio_pupdr, STM32F4XXGPIOState),
+ VMSTATE_UINT32(gpio_idr, STM32F4XXGPIOState),
+ VMSTATE_UINT32(gpio_odr, STM32F4XXGPIOState),
+ VMSTATE_UINT32(gpio_bsrr, STM32F4XXGPIOState),
+ VMSTATE_UINT32(gpio_lckr, STM32F4XXGPIOState),
+ VMSTATE_UINT32(gpio_afrl, STM32F4XXGPIOState),
+ VMSTATE_UINT32(gpio_afrh, STM32F4XXGPIOState),
+ VMSTATE_END_OF_LIST()
+ }
+};
+
+static void stm32f4xx_gpio_init(Object *obj)
+{
+ STM32F4XXGPIOState *s = STM32F4XX_GPIO(obj);
+
+ memory_region_init_io(&s->mmio, obj, &stm32f4xx_gpio_ops, s,
+ TYPE_STM32F4XX_GPIO, 0x400);
+ sysbus_init_mmio(SYS_BUS_DEVICE(obj), &s->mmio);
+
+ sysbus_init_irq(SYS_BUS_DEVICE(obj), &s->irq);
+}
+
+static void stm32f4xx_gpio_class_init(ObjectClass *klass, void *data)
+{
+ DeviceClass *dc = DEVICE_CLASS(klass);
+
+ dc->reset = stm32f4xx_gpio_reset;
+ dc->vmsd = &vmstate_stm32f4xx_gpio;
+}
+
+static const TypeInfo stm32f4xx_gpio_info = {
+ .name = TYPE_STM32F4XX_GPIO,
+ .parent = TYPE_SYS_BUS_DEVICE,
+ .instance_size = sizeof(STM32F4XXGPIOState),
+ .instance_init = stm32f4xx_gpio_init,
+ .class_init = stm32f4xx_gpio_class_init,
+};
+
+static void stm32f4xx_gpio_register_types(void)
+{
+ type_register_static(&stm32f4xx_gpio_info);
+}
+
+type_init(stm32f4xx_gpio_register_types)
7.在include/hw/gpio/创建stm32f4xx_gpio.h文件
new file mode 100644
index 00000000..7b9c41e9
--- /dev/null
+++ b/include/hw/gpio/stm32f4xx_gpio.h
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2020 liang yan <yanl1229@163.com>
+ *
+ * STM32F4XX GPIO
+ *
+ * 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 STM32F4XX_GPIO_H
+#define STM32F4XX_GPIO_H
+
+#include "hw/sysbus.h"
+
+#define STM_GPIO_MODE 0x00
+#define STM_GPIO_OTYPER 0x04
+#define STM_GPIO_OSPEEDR 0x08
+#define STM_GPIO_PUPDR 0x0C
+#define STM_GPIO_IDR 0x10
+#define STM_GPIO_ODR 0x14
+#define STM_GPIO_BSRR 0x18
+#define STM_GPIO_LCKR 0x1c
+#define STM_GPIO_AFRL 0x20
+#define STM_GPIO_AFRH 0x24
+
+#define TYPE_STM32F4XX_GPIO "stm32f4xx-gpio"
+#define STM32F4XX_GPIO(obj) \
+ OBJECT_CHECK(STM32F4XXGPIOState, (obj), TYPE_STM32F4XX_GPIO)
+
+typedef struct {
+ /* <private> */
+ SysBusDevice parent_obj;
+
+ /* <public> */
+ MemoryRegion mmio;
+
+ uint32_t gpio_mode;
+ uint32_t gpio_otyper;
+ uint32_t gpio_ospeedr;
+ uint32_t gpio_pupdr;
+ uint32_t gpio_idr;
+ uint32_t gpio_odr;
+ uint32_t gpio_bsrr;
+ uint32_t gpio_lckr;
+ uint32_t gpio_afrl;
+ uint32_t gpio_afrh;
+
+ qemu_irq irq;
+} STM32F4XXGPIOState;
+
+#endif
\ No newline at end of file
总结
1、本文描述了如何在qemu中添加stm32f407平台上GPIO控制器实现;
2、完成的提交记录,请查看代码库链接;
链接:
1、qemu代码库链接