|
|
vor 1 Monat | |
|---|---|---|
| .. | ||
| README.md | vor 1 Monat | |
| clock_time_example.c | vor 1 Monat | |
This directory contains examples demonstrating the use of the unified clock_time subsystem.
Enable RT_USING_CLOCK_TIME in your configuration:
CONFIG_RT_USING_CLOCK_TIME=y
clock_time_info_example)Displays basic information about the clock_time device:
Usage:
msh> clock_time_info_example
clock_boottime_example)Shows system uptime in different formats:
Usage:
msh> clock_boottime_example
clock_conversion_example)Demonstrates conversion between counter ticks and time units (ms, us, ns).
Usage:
msh> clock_conversion_example
clock_delay_example)Tests high-precision delay functions and measures actual delays:
rt_clock_udelay)rt_clock_mdelay)Usage:
msh> clock_delay_example
clock_hrtimer_oneshot_example)Demonstrates using a high-resolution timer for one-shot timeouts.
Usage:
msh> clock_hrtimer_oneshot_example
Expected Output:
=== High-Resolution Timer (One-Shot) Example ===
Starting timer for 500ms...
Timer started successfully
High-resolution timer fired! Parameter: One-shot timer
Timer example complete
clock_hrtimer_periodic_example)Demonstrates using a high-resolution timer for periodic callbacks.
Usage:
msh> clock_hrtimer_periodic_example
Expected Output:
=== High-Resolution Timer (Periodic) Example ===
Starting periodic timer (200ms period)...
Timer started successfully
Periodic timer tick #1
Periodic timer tick #2
Periodic timer tick #3
Periodic timer tick #4
Periodic timer tick #5
Timer stopped. Total ticks: 5
clock_benchmark_example)Measures the overhead of clock_time operations:
get_counter() call overheadUsage:
msh> clock_benchmark_example
clock_time_examples_all)Runs all examples in sequence.
Usage:
msh> clock_time_examples_all
Add to your BSP's applications/SConscript:
src += Glob('../../examples/clock_time/*.c')
Copy clock_time_example.c to your BSP's applications directory and rebuild.
Some BSPs may include examples in menuconfig. Look for:
RT-Thread online packages --->
miscellaneous packages --->
[*] Enable clock_time examples
The delay examples measure actual vs. target delays. Typical results:
Factors affecting accuracy:
Typical overhead values (depending on platform):
get_counter(): 10-100 ns per callcnt_to_us(): 50-500 ns per callLower values are better. Very high values may indicate:
Enable the clock_time subsystem in Kconfig:
Device Drivers --->
[*] Using unified clock_time subsystem
No clock_time device has been registered. Options:
If one-shot or periodic timers don't fire:
RT_CLOCK_TIME_CAP_CLOCKEVENTrt_clock_hrtimer_process()If delays are consistently off:
clock_time_info_examplestatic void my_callback(void *parameter)
{
int *count = (int *)parameter;
(*count)++;
rt_kprintf("Callback executed %d times\n", *count);
}
void custom_timer_example(void)
{
struct rt_clock_hrtimer timer;
int count = 0;
rt_clock_hrtimer_init(&timer, "custom",
RT_TIMER_FLAG_PERIODIC,
my_callback,
&count);
unsigned long period = (unsigned long)rt_clock_time_ms_to_cnt(100);
rt_clock_hrtimer_start(&timer, period);
/* Let it run... */
rt_thread_mdelay(1000);
rt_clock_hrtimer_stop(&timer);
rt_clock_hrtimer_detach(&timer);
}
void measure_function_time(void)
{
unsigned long start = (unsigned long)rt_clock_time_getcnt();
/* Function to measure */
my_function();
unsigned long end = (unsigned long)rt_clock_time_getcnt();
unsigned long delta = end - start;
rt_kprintf("Function took %u us\n",
(rt_uint32_t)rt_clock_time_cnt_to_us(delta));
}