This directory contains reference adapter implementations for various hardware timers.
These adapters demonstrate how to integrate hardware timers with the unified clock_time subsystem.
clock_time_arm_gtimer.c)Target Platforms:
Features:
Usage:
// In your BSP board.c or timer initialization
rt_clock_time_arm_gtimer_init();
// In your timer ISR
void ARM_GTIMER_IRQHandler(void)
{
rt_interrupt_enter();
rt_clock_time_arm_gtimer_isr();
rt_interrupt_leave();
}
Registers Used:
clock_time_systick.c)Target Platforms:
Features:
Usage:
// DWT is automatically enabled during initialization
// No ISR needed - this is clocksource only
// Optional: Set CPU frequency if SystemCoreClock is not accurate
rt_clock_time_systick_set_freq(168000000); // 168 MHz
Notes:
To create an adapter for your hardware timer:
static rt_uint64_t my_timer_get_freq(void)
{
return TIMER_FREQUENCY_HZ;
}
static rt_uint64_t my_timer_get_counter(void)
{
return MY_TIMER->COUNT; /* Read hardware counter */
}
static rt_err_t my_timer_set_timeout(rt_uint64_t delta)
{
if (delta == 0)
{
/* Cancel timeout */
MY_TIMER->CTRL &= ~TIMER_ENABLE;
return RT_EOK;
}
/* Set compare value for interrupt */
MY_TIMER->COMPARE = MY_TIMER->COUNT + delta;
MY_TIMER->CTRL |= TIMER_ENABLE | TIMER_INT_ENABLE;
return RT_EOK;
}
static const struct rt_clock_time_ops my_timer_ops =
{
.get_freq = my_timer_get_freq,
.get_counter = my_timer_get_counter,
.set_timeout = my_timer_set_timeout,
};
int my_timer_init(void)
{
static struct rt_clock_time_device my_device;
/* Initialize hardware */
// ... hardware setup code ...
my_device.ops = &my_timer_ops;
/* Register with appropriate capabilities */
return rt_clock_time_device_register(&my_device, "my_timer",
RT_CLOCK_TIME_CAP_CLOCKSOURCE | RT_CLOCK_TIME_CAP_CLOCKEVENT);
}
INIT_DEVICE_EXPORT(my_timer_init);
void MY_TIMER_IRQHandler(void)
{
rt_interrupt_enter();
/* Clear hardware interrupt flag */
MY_TIMER->STATUS = TIMER_INT_FLAG;
/* Process high-resolution timer timeouts */
rt_clock_hrtimer_process();
rt_interrupt_leave();
}
Use when:
Use when:
Use when:
rt_clock_hrtimer_process()Verify frequency: Print actual frequency during initialization
rt_kprintf("Timer freq: %d Hz\n", (rt_uint32_t)rt_clock_time_getfreq());
Test counter increment: Verify counter is actually counting
rt_uint64_t cnt1 = rt_clock_time_getcnt();
rt_thread_mdelay(1);
rt_uint64_t cnt2 = rt_clock_time_getcnt();
rt_kprintf("Counter delta: %d (expected ~%d)\n",
(rt_uint32_t)(cnt2 - cnt1),
(rt_uint32_t)(rt_clock_time_getfreq() / 1000));
Check interrupt firing: Add debug output in ISR
static int isr_count = 0;
void timer_isr(void) {
isr_count++;
if (isr_count % 1000 == 0) {
rt_kprintf("Timer ISR count: %d\n", isr_count);
}
}
Look for these files in various BSPs for real-world examples:
bsp/qemu-virt64-aarch64/drivers/drv_timer.c - ARM Generic Timerbsp/stm32/libraries/HAL_Drivers/drv_hwtimer.c - STM32 hardware timersbsp/rockchip/*/driver/hwtimer/ - Rockchip timer driversWhen contributing a new adapter: