Skip to content

Commit da881c3

Browse files
nzmichaelhDhiru Kholia
authored andcommitted
drivers: add the ch32v00x systick driver
This commit adds the systick driver for WCH CH32V003. Signed-off-by: Michael Hope <[email protected]> Signed-off-by: Dhiru Kholia <[email protected]>
1 parent b716be9 commit da881c3

File tree

6 files changed

+110
-0
lines changed

6 files changed

+110
-0
lines changed

drivers/timer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ zephyr_library_sources_ifdef(CONFIG_ARCV2_TIMER arcv2_timer0.c)
1111
zephyr_library_sources_ifdef(CONFIG_ARM_ARCH_TIMER arm_arch_timer.c)
1212
zephyr_library_sources_ifdef(CONFIG_INTEL_ADSP_TIMER intel_adsp_timer.c)
1313
zephyr_library_sources_ifdef(CONFIG_CC13XX_CC26XX_RTC_TIMER cc13xx_cc26xx_rtc_timer.c)
14+
zephyr_library_sources_ifdef(CONFIG_CH32V00X_SYSTICK wch_systick_ch32v00x.c)
1415
zephyr_library_sources_ifdef(CONFIG_CORTEX_M_SYSTICK cortex_m_systick.c)
1516
zephyr_library_sources_ifdef(CONFIG_ESP32_SYS_TIMER esp32_sys_timer.c)
1617
zephyr_library_sources_ifdef(CONFIG_GECKO_BURTC_TIMER gecko_burtc_timer.c)

drivers/timer/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ source "drivers/timer/Kconfig.arcv2"
7070
source "drivers/timer/Kconfig.arm_arch"
7171
source "drivers/timer/Kconfig.cavs"
7272
source "drivers/timer/Kconfig.cc13xx_cc26xx_rtc"
73+
source "drivers/timer/Kconfig.wch_ch32v00x"
7374
source "drivers/timer/Kconfig.cortex_m_systick"
7475
source "drivers/timer/Kconfig.esp32"
7576
source "drivers/timer/Kconfig.gecko"

drivers/timer/Kconfig.wch_ch32v00x

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copyright (c) 2024 Michael Hope
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
config CH32V00X_SYSTICK
5+
bool "CH32V00X systick timer"
6+
depends on SOC_CH32V003
7+
default y
8+
depends on DT_HAS_WCH_SYSTICK_ENABLED
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (c) 2024 Michael Hope
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT wch_systick
8+
9+
#include <soc.h>
10+
#include <zephyr/device.h>
11+
#include <zephyr/drivers/timer/system_timer.h>
12+
#include <zephyr/irq.h>
13+
#include <zephyr/kernel.h>
14+
#include <zephyr/sys/util.h>
15+
16+
#include <ch32_systick.h>
17+
18+
#define STK_SWIE BIT(31)
19+
#define STK_STRE BIT(3)
20+
#define STK_STCLK BIT(2)
21+
#define STK_STIE BIT(1)
22+
#define STK_STE BIT(0)
23+
24+
#define STK_CNTIF BIT(0)
25+
26+
#define CYCLES_PER_SEC sys_clock_hw_cycles_per_sec()
27+
#define CYCLES_PER_TICK (CYCLES_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
28+
29+
#define SYSTICK ((SysTick_Type *)(DT_INST_REG_ADDR(0)))
30+
31+
struct ch32v00x_systick_device {
32+
};
33+
34+
static volatile uint32_t ch32v00x_systick_count;
35+
36+
static void ch32v00x_systick_irq(const struct ch32v00x_systick_device *unused)
37+
{
38+
ARG_UNUSED(unused);
39+
40+
SYSTICK->SR = 0;
41+
ch32v00x_systick_count += CYCLES_PER_TICK; /* Track cycles. */
42+
sys_clock_announce(1); /* Poke the scheduler. */
43+
}
44+
45+
uint32_t sys_clock_cycle_get_32(void)
46+
{
47+
return ch32v00x_systick_count + SYSTICK->CNT;
48+
}
49+
50+
uint32_t sys_clock_elapsed(void)
51+
{
52+
return 0;
53+
}
54+
55+
static int ch32v00x_systick_init(void)
56+
{
57+
IRQ_CONNECT(DT_INST_IRQN(0), 0, ch32v00x_systick_irq, NULL, 0);
58+
59+
SYSTICK->SR = 0;
60+
SYSTICK->CMP = CYCLES_PER_TICK;
61+
SYSTICK->CNT = 0;
62+
SYSTICK->CTLR = STK_STRE | STK_STCLK | STK_STIE | STK_STE;
63+
64+
irq_enable(DT_INST_IRQN(0));
65+
66+
return 0;
67+
}
68+
69+
SYS_INIT(ch32v00x_systick_init, PRE_KERNEL_2, CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright (c) 2024 Michael Hope
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
description: WCH CH32V00x Systick
5+
6+
compatible: "wch,systick"
7+
8+
include: base.yaml
9+
10+
properties:
11+
reg:
12+
required: true
13+
14+
interrupts:
15+
required: true

modules/hal_wch/ch32_systick.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2024 Dhiru Kholia
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef _CH32_SYSTICK_H
8+
#define _CH32_SYSTICK_H
9+
10+
#ifdef CONFIG_SOC_CH32V003
11+
#include <ch32v003fun.h>
12+
#else
13+
#error "SoC not supported!"
14+
#endif
15+
16+
#endif

0 commit comments

Comments
 (0)