当前位置: 首页 > news >正文

Qemu-NUC980(七):Timer定时器

概述

本文描述了添加980 Timer定时器功能代码的步骤,在描述过程中,为了清晰的描述添加框架,部分代码的细节被删除,详细的代码,请参考文末的工程链接。

添加步骤

1、修改hw/arm/Kconfig文件,如下所示:

+号部分为新增加内容

--- a/hw/arm/Kconfig
+++ b/hw/arm/Kconfig
@@ -120,6 +120,7 @@ config NUC980select NUC980_SDICselect NUC980_UARTselect NUC980_AIC
+    select NUC980_TIMER

2、修改hw/arm/nuc980_soc.c,如下所示:

+号部分为新增加内容

--- a/hw/arm/nuc980_soc.c
+++ b/hw/arm/nuc980_soc.c
@@ -30,6 +30,15 @@ static struct devinfo serial_table[UART_COUNT] = {{ UART2_BASE, 38}};+static struct devinfo timer_table[TIMER_COUNT] = {
+    { TIMER0_BASE, 16},
+    { TIMER1_BASE, 17},
+    { TIMER2_BASE, 30},
+    { TIMER3_BASE, 31},
+    { TIMER4_BASE, 32},
+    { TIMER5_BASE, 34},
+};
+static void nuc980_init(Object *obj){int i;
@@ -54,6 +63,12 @@ static void nuc980_init(Object *obj)}/* aic */sysbus_init_child_obj(obj, "aic", &s->aic, sizeof(s->aic), TYPE_NUC980_AIC);
+    /* timer */
+    for (i = 0; i < TIMER_COUNT; i++) {
+        snprintf(name, NAME_SIZE, "timer%d", i + 1);
+        sysbus_init_child_obj(obj, name, &s->timer[i], sizeof(s->timer[i]),
+                              TYPE_NUC980_TIMER);
+    }}static void nuc980_realize(DeviceState *dev, Error **errp)
@@ -107,6 +122,19 @@ static void nuc980_realize(DeviceState *dev, Error **errp)qdev_get_gpio_in(DEVICE(&s->aic),serial_table[i].irq));}
+    /* timer */
+    for (i = 0; i < TIMER_COUNT; i++) {
+        qdev_prop_set_uint32(DEVICE(&s->timer[i]), "timer_id", i);
+        object_property_set_bool(OBJECT(&s->timer[i]), true, "realized", &err);
+        if (err) {
+            error_propagate(errp, err);
+            return;
+        }
+        sysbus_mmio_map(SYS_BUS_DEVICE(&s->timer[i]), 0, timer_table[i].addr);
+        sysbus_connect_irq(SYS_BUS_DEVICE(&s->timer[i]), 0,
+                           qdev_get_gpio_in(DEVICE(&s->aic),
+                            timer_table[i].irq));
+    }}static void nuc980_class_init(ObjectClass *oc, void *data)

3、修改hw/timer/Kconfig,如下所示:

+号部分为新增加内容

--- a/hw/timer/Kconfig
+++ b/hw/timer/Kconfig
@@ -34,3 +34,7 @@ config CMSDK_APB_TIMERconfig CMSDK_APB_DUALTIMERboolselect PTIMER
+
+config NUC980_TIMER
+    bool
+    select PTIMER

4、修改hw/timer/Makefile.objs,如下所示:

+号部分为新增加内容

--- a/hw/timer/Makefile.objs
+++ b/hw/timer/Makefile.objs
@@ -35,3 +35,4 @@ common-obj-$(CONFIG_CMSDK_APB_TIMER) += cmsdk-apb-timer.ocommon-obj-$(CONFIG_CMSDK_APB_DUALTIMER) += cmsdk-apb-dualtimer.ocommon-obj-$(CONFIG_MSF2) += mss-timer.ocommon-obj-$(CONFIG_RASPI) += bcm2835_systmr.o
+obj-$(CONFIG_NUC980_TIMER) += nuc980_timer.o

5、添加hw/timer/nuc980_timer.c,如下所示:

+号部分为新增加内容

diff --git a/hw/timer/nuc980_timer.c b/hw/timer/nuc980_timer.c
new file mode 100644
index 00000000..9d54b29a
--- /dev/null
+++ b/hw/timer/nuc980_timer.c
@@ -0,0 +1,221 @@
+/*
+ * NUC980 SOC System emulation.
+ *
+ * Copyright (c) 2023- yanl1229@163.com.
+ * Written by yanl1229
+ *
+ * This code is licensed under the GPL.
+ */
+#include "qemu/osdep.h"
+#include "hw/timer/nuc980_timer.h"
+#include "hw/misc/nuc980_clk.h"
+#include "qemu/log.h"
+#include "hw/irq.h"
+#include "hw/qdev-properties.h"
+#include "qemu/module.h"
+#include "migration/vmstate.h"
+
+
+#ifndef TIMER_ERR_DEBUG
+#define TIMER_ERR_DEBUG 0
+#endif
+
+#define DB_PRINT_L(lvl, fmt, args...) do { \
+    if (TIMER_ERR_DEBUG >= lvl) { \
+        qemu_log("%s: " fmt, __func__, ## args); \
+    } \
+} while (0);
+
+#define DB_PRINT(fmt, args...) DB_PRINT_L(1, fmt, ## args)
+
+static void nuc980_timer_reset(DeviceState *dev)
+{
+    NUC980TimerState *s = NUC980TIMER(dev);
+
+    s->timer_ctl    = 0x00000000;
+    s->timer_precnt = 0x00000000;
+    s->timer_cmp    = 0x00000000;
+    s->timer_inten  = 0x00000000;
+    s->timer_intsts = 0x00000000;
+    s->timer_cnt    = 0x00000000;
+    s->timer_cap    = 0x00000000;
+    s->timer_ectl   = 0x00000000;
+}+static uint64_t nuc980_timer_read(void *opaque, hwaddr offset,
+                           unsigned size)
+{
+    ......
+    return 0;
+}
+
+static void nuc980_timer_write(void *opaque, hwaddr offset,
+                        uint64_t val64, unsigned size)
+{......
+}
+
+static const MemoryRegionOps nuc980_timer_ops = {
+    .read = nuc980_timer_read,
+    .write = nuc980_timer_write,
+    .endianness = DEVICE_NATIVE_ENDIAN,
+};
+
+static const VMStateDescription vmstate_nuc980_timer = {
+    .name = TYPE_NUC980_TIMER,
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT32(timer_ctl, NUC980TimerState),
+        VMSTATE_UINT32(timer_precnt, NUC980TimerState),
+        VMSTATE_UINT32(timer_cmp, NUC980TimerState),
+        VMSTATE_UINT32(timer_inten, NUC980TimerState),
+        VMSTATE_UINT32(timer_intsts, NUC980TimerState),
+        VMSTATE_UINT32(timer_cnt, NUC980TimerState),
+        VMSTATE_UINT32(timer_cap, NUC980TimerState),
+        VMSTATE_UINT32(timer_ectl, NUC980TimerState),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static void nuc980_timer_tick(void *opaque)
+{
+    NUC980TimerState *s = (NUC980TimerState *)opaque;
+
+    DB_PRINT("%s update", __func__);
+    s->timer_intsts = 1;
+    nuc980_timer_update(s);
+}
+
+static void nuc980_timer_realize(DeviceState *dev, Error **errp)
+{
+    NUC980TimerState *s = NUC980TIMER(dev);
+    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
+
+    sysbus_init_irq(sbd, &s->irq);
+    s->ptimer = ptimer_init(nuc980_timer_tick, s, PTIMER_POLICY_DEFAULT);
+    memory_region_init_io(&s->iomem, OBJECT(s), &nuc980_timer_ops, s,
+                          "nuc980_timer", 0x100);
+    sysbus_init_mmio(sbd, &s->iomem);
+}
+
+static Property nuc980_properties[] = {
+    DEFINE_PROP_UINT32("timer_id", NUC980TimerState, timer_id, 0),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void nuc980_timer_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    dc->realize = nuc980_timer_realize;
+    dc->reset = nuc980_timer_reset;
+    dc->props = nuc980_properties;
+    dc->vmsd = &vmstate_nuc980_timer;
+}
+
+static const TypeInfo nuc980_timer_info = {
+    .name          = TYPE_NUC980_TIMER,
+    .parent        = TYPE_SYS_BUS_DEVICE,
+    .instance_size = sizeof(NUC980TimerState),
+    .class_init    = nuc980_timer_class_init,
+};
+
+static void nuc980_timer_register_types(void)
+{
+    type_register_static(&nuc980_timer_info);
+}
+
+type_init(nuc980_timer_register_types)

6、修改include/hw/arm/nuc980.h,如下所示:

+号部分为新增加内容

--- a/include/hw/arm/nuc980.h
+++ b/include/hw/arm/nuc980.h
@@ -18,6 +18,7 @@#include "hw/misc/nuc980_sdic.h"#include "hw/intc/nuc980_aic.h"#include "hw/char/nuc980_uart.h"
+#include "hw/timer/nuc980_timer.h"#define SDRAM_BASE      0x0000000#define SDRAM_SIZE      (64 *1024 * 1024)
@@ -35,6 +36,14 @@#define UART2_BASE      0xb0072000#define UART_COUNT      3+#define TIMER0_BASE     0xb0050000
+#define TIMER1_BASE     0xb0050100
+#define TIMER2_BASE     0xb0051000
+#define TIMER3_BASE     0xb0051100
+#define TIMER4_BASE     0xb0052000
+#define TIMER5_BASE     0xb0052100
+#define TIMER_COUNT     6
+#define AIC_BASE                0xb0042000#define TYPE_NUC980 "nuc980"
@@ -54,6 +63,7 @@ typedef struct NUC980State {NUC980SDICState sdic;NUC980UartState uart[UART_COUNT];NUC980AICState aic;
+    NUC980TimerState timer[TIMER_COUNT];} NUC980State;

7、添加include/hw/timer/nuc980_timer.h,如下所示:

+号部分为新增加内容

new file mode 100644
index 00000000..ff90e96e
--- /dev/null
+++ b/include/hw/timer/nuc980_timer.h
@@ -0,0 +1,59 @@
+/*
+ * NUC980 SOC System emulation.
+ *
+ * Copyright (c) 2023- yanl1229@163.com.
+ * Written by yanl1229
+ *
+ * This code is licensed under the GPL.
+ */
+#ifndef NUC980_TIMER_H
+#define NUC980_TIMER_H
+
+#include "hw/sysbus.h"
+#include "qemu/timer.h"
+#include "sysemu/sysemu.h"
+#include "hw/ptimer.h"
+
+#define TIMER_CTL       0x00
+#define TIMER_PRECNT    0x04
+#define TIMER_CMP       0x08
+#define TIMER_INTEN     0x0c
+#define TIMER_INTSTS    0x10
+#define TIMER_CNT       0x14
+#define TIMER_CAP       0x18
+#define TIMER_ECTL      0x20
+
+#define TIMER_CTL_CNTEN  (0x1 << 0)
+
+#define TIMER_INTEN_CNTIEN (0x1 << 0)
+
+#define TIMER_CNT_RSTACT (0x1 << 31)
+
+#define TYPE_NUC980_TIMER "nuc980-timer"
+#define NUC980TIMER(obj) OBJECT_CHECK(NUC980TimerState, \
+                            (obj), TYPE_NUC980_TIMER)
+
+typedef struct NUC980TimerState {
+    /* <private> */
+    SysBusDevice parent_obj;
+
+    /* <public> */
+    MemoryRegion iomem;
+
+    uint32_t timer_ctl;
+    uint32_t timer_precnt;
+    uint32_t timer_cmp;
+    uint32_t timer_inten;
+    uint32_t timer_intsts;
+    uint32_t timer_cnt;
+    uint32_t timer_cap;
+    uint32_t timer_ectl;
+
+    uint32_t timer_rate;
+    qemu_irq irq;
+    ptimer_state *ptimer;
+    uint32_t timer_id;
+
+} NUC980TimerState;
+
+#endif

总结

工程链接

https://gitee.com/yanl1229/qemu.git

http://www.dtcms.com/a/464921.html

相关文章:

  • 20251009
  • CanFestival 主站-NMT初始化
  • Transformer基础之注意力机制
  • 模板式网站价格网页设置快捷键
  • 重要通知:spring-ai-hunyuan 已兼容 Spring AI 稳定版!
  • 惊艳的网站工作室网页模板
  • 如何在 Spring Boot 应用中配置多个 Spring AI 的 LLM 客户端
  • 【实时Linux实战系列】实时系统的可观测性:Prometheus 与 Grafana 集成
  • HTML 元素:构建网页的基础
  • HTML应用指南:利用GET请求获取全国中国建设银行网点位置信息
  • AI编程 | 基于飞书知识库+多模态大模型,打造B站视频AI笔记自动生成系统
  • 专门做预售的网站做app需要学什么编程
  • [VoiceRAG] RAG工具集 | attach_rag_tools | _search_tool | _report_grounding_tool
  • ppo笔记2
  • 小九源码-springboot082-java旅游攻略平台
  • 从 Kotlin 编译器 API 的变化开始: 2.2.2X -> 2.3.0-Beta1
  • go中调用合约
  • 用Python可视化国庆期间旅游概况与消费趋势
  • InitLWIP() 初始化
  • Python爬虫实战:获取新浪旅游热门景点排行榜及数据分析
  • C++设计模式之行为型模式:中介者模式(Mediator)
  • 为什么苏州网络进不了网站ps设计网站
  • [C# starter-kit] Domain Entities | `AuditableEntity`基类 | 跟踪变化 | 软删除
  • 深度复盘+完整源码:我把 libuv 的高性能内存池,用现代 C++ 给你扒了个底朝天
  • GUI 自动化与接口自动化:概念、差异与协同落地
  • 网站建设公司是怎么找客户idc网站模板源码下载
  • kafka的数据消费通过flinksql 入数到Doris的报错(Connection timed out)
  • 【汽车篇】AI深度学习在汽车零部件外观检测——石墨电极板的应用
  • 花型图案设计网站做网站自己能做百度推广吗
  • Java求职面试实战:从Spring Boot到微服务架构的技术探讨