|
| 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); |
0 commit comments