|
| 1 | +# Clock Time Examples |
| 2 | + |
| 3 | +This directory contains examples demonstrating the use of the unified `clock_time` subsystem. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +Enable `RT_USING_CLOCK_TIME` in your configuration: |
| 8 | +``` |
| 9 | +CONFIG_RT_USING_CLOCK_TIME=y |
| 10 | +``` |
| 11 | + |
| 12 | +## Available Examples |
| 13 | + |
| 14 | +### 1. Clock Time Information (`clock_time_info_example`) |
| 15 | +Displays basic information about the clock_time device: |
| 16 | +- Clock frequency |
| 17 | +- Clock resolution |
| 18 | +- Current counter value |
| 19 | +- Device capabilities |
| 20 | + |
| 21 | +**Usage:** |
| 22 | +``` |
| 23 | +msh> clock_time_info_example |
| 24 | +``` |
| 25 | + |
| 26 | +### 2. Boottime Tracking (`clock_boottime_example`) |
| 27 | +Shows system uptime in different formats: |
| 28 | +- Nanosecond precision |
| 29 | +- Microsecond precision |
| 30 | +- Second precision |
| 31 | + |
| 32 | +**Usage:** |
| 33 | +``` |
| 34 | +msh> clock_boottime_example |
| 35 | +``` |
| 36 | + |
| 37 | +### 3. Time Conversion (`clock_conversion_example`) |
| 38 | +Demonstrates conversion between counter ticks and time units (ms, us, ns). |
| 39 | + |
| 40 | +**Usage:** |
| 41 | +``` |
| 42 | +msh> clock_conversion_example |
| 43 | +``` |
| 44 | + |
| 45 | +### 4. High-Precision Delay (`clock_delay_example`) |
| 46 | +Tests high-precision delay functions and measures actual delays: |
| 47 | +- Microsecond delays (`rt_clock_udelay`) |
| 48 | +- Millisecond delays (`rt_clock_mdelay`) |
| 49 | + |
| 50 | +**Usage:** |
| 51 | +``` |
| 52 | +msh> clock_delay_example |
| 53 | +``` |
| 54 | + |
| 55 | +### 5. One-Shot Timer (`clock_hrtimer_oneshot_example`) |
| 56 | +Demonstrates using a high-resolution timer for one-shot timeouts. |
| 57 | + |
| 58 | +**Usage:** |
| 59 | +``` |
| 60 | +msh> clock_hrtimer_oneshot_example |
| 61 | +``` |
| 62 | + |
| 63 | +**Expected Output:** |
| 64 | +``` |
| 65 | +=== High-Resolution Timer (One-Shot) Example === |
| 66 | +Starting timer for 500ms... |
| 67 | +Timer started successfully |
| 68 | +High-resolution timer fired! Parameter: One-shot timer |
| 69 | +Timer example complete |
| 70 | +``` |
| 71 | + |
| 72 | +### 6. Periodic Timer (`clock_hrtimer_periodic_example`) |
| 73 | +Demonstrates using a high-resolution timer for periodic callbacks. |
| 74 | + |
| 75 | +**Usage:** |
| 76 | +``` |
| 77 | +msh> clock_hrtimer_periodic_example |
| 78 | +``` |
| 79 | + |
| 80 | +**Expected Output:** |
| 81 | +``` |
| 82 | +=== High-Resolution Timer (Periodic) Example === |
| 83 | +Starting periodic timer (200ms period)... |
| 84 | +Timer started successfully |
| 85 | +Periodic timer tick #1 |
| 86 | +Periodic timer tick #2 |
| 87 | +Periodic timer tick #3 |
| 88 | +Periodic timer tick #4 |
| 89 | +Periodic timer tick #5 |
| 90 | +Timer stopped. Total ticks: 5 |
| 91 | +``` |
| 92 | + |
| 93 | +### 7. Performance Benchmark (`clock_benchmark_example`) |
| 94 | +Measures the overhead of clock_time operations: |
| 95 | +- `get_counter()` call overhead |
| 96 | +- Time conversion overhead |
| 97 | + |
| 98 | +**Usage:** |
| 99 | +``` |
| 100 | +msh> clock_benchmark_example |
| 101 | +``` |
| 102 | + |
| 103 | +### Run All Examples (`clock_time_examples_all`) |
| 104 | +Runs all examples in sequence. |
| 105 | + |
| 106 | +**Usage:** |
| 107 | +``` |
| 108 | +msh> clock_time_examples_all |
| 109 | +``` |
| 110 | + |
| 111 | +## Building the Examples |
| 112 | + |
| 113 | +### Method 1: Include in BSP |
| 114 | + |
| 115 | +Add to your BSP's `applications/SConscript`: |
| 116 | +```python |
| 117 | +src += Glob('../../examples/clock_time/*.c') |
| 118 | +``` |
| 119 | + |
| 120 | +### Method 2: Manual Compilation |
| 121 | + |
| 122 | +Copy `clock_time_example.c` to your BSP's `applications` directory and rebuild. |
| 123 | + |
| 124 | +### Method 3: menuconfig |
| 125 | + |
| 126 | +Some BSPs may include examples in menuconfig. Look for: |
| 127 | +``` |
| 128 | +RT-Thread online packages ---> |
| 129 | + miscellaneous packages ---> |
| 130 | + [*] Enable clock_time examples |
| 131 | +``` |
| 132 | + |
| 133 | +## Interpreting Results |
| 134 | + |
| 135 | +### Delay Accuracy |
| 136 | +The delay examples measure actual vs. target delays. Typical results: |
| 137 | +- **Good**: Actual delay within 1-5% of target |
| 138 | +- **Acceptable**: Actual delay within 10% of target |
| 139 | +- **Poor**: Actual delay > 10% off target (may indicate timer issues) |
| 140 | + |
| 141 | +Factors affecting accuracy: |
| 142 | +- Timer resolution |
| 143 | +- Interrupt latency |
| 144 | +- System load |
| 145 | +- Compiler optimizations |
| 146 | + |
| 147 | +### Performance Benchmarks |
| 148 | +Typical overhead values (depending on platform): |
| 149 | +- `get_counter()`: 10-100 ns per call |
| 150 | +- `cnt_to_us()`: 50-500 ns per call |
| 151 | + |
| 152 | +Lower values are better. Very high values may indicate: |
| 153 | +- Slow hardware timer access |
| 154 | +- Cache misses |
| 155 | +- Inefficient implementation |
| 156 | + |
| 157 | +## Troubleshooting |
| 158 | + |
| 159 | +### "RT_USING_CLOCK_TIME is not enabled" |
| 160 | +Enable the clock_time subsystem in Kconfig: |
| 161 | +``` |
| 162 | +Device Drivers ---> |
| 163 | + [*] Using unified clock_time subsystem |
| 164 | +``` |
| 165 | + |
| 166 | +### "No clock_time device found" |
| 167 | +No clock_time device has been registered. Options: |
| 168 | +1. Enable an adapter (e.g., ARM Generic Timer, SysTick) |
| 169 | +2. Implement a custom adapter for your hardware |
| 170 | +3. The tick-based fallback should be available |
| 171 | + |
| 172 | +### Timer callbacks not firing |
| 173 | +If one-shot or periodic timers don't fire: |
| 174 | +1. Check that your clock_time device supports `RT_CLOCK_TIME_CAP_CLOCKEVENT` |
| 175 | +2. Verify the timer ISR calls `rt_clock_hrtimer_process()` |
| 176 | +3. Check interrupt configuration and priority |
| 177 | +4. Ensure timer interrupts are enabled |
| 178 | + |
| 179 | +### Inaccurate delays |
| 180 | +If delays are consistently off: |
| 181 | +1. Verify timer frequency with `clock_time_info_example` |
| 182 | +2. Check for interrupt latency issues |
| 183 | +3. Ensure timer counter is actually counting |
| 184 | +4. Consider using a higher frequency timer |
| 185 | + |
| 186 | +## Advanced Usage |
| 187 | + |
| 188 | +### Custom Timer Callbacks |
| 189 | + |
| 190 | +```c |
| 191 | +static void my_callback(void *parameter) |
| 192 | +{ |
| 193 | + int *count = (int *)parameter; |
| 194 | + (*count)++; |
| 195 | + rt_kprintf("Callback executed %d times\n", *count); |
| 196 | +} |
| 197 | + |
| 198 | +void custom_timer_example(void) |
| 199 | +{ |
| 200 | + struct rt_clock_hrtimer timer; |
| 201 | + int count = 0; |
| 202 | + |
| 203 | + rt_clock_hrtimer_init(&timer, "custom", |
| 204 | + RT_TIMER_FLAG_PERIODIC, |
| 205 | + my_callback, |
| 206 | + &count); |
| 207 | + |
| 208 | + unsigned long period = (unsigned long)rt_clock_time_ms_to_cnt(100); |
| 209 | + rt_clock_hrtimer_start(&timer, period); |
| 210 | + |
| 211 | + /* Let it run... */ |
| 212 | + rt_thread_mdelay(1000); |
| 213 | + |
| 214 | + rt_clock_hrtimer_stop(&timer); |
| 215 | + rt_clock_hrtimer_detach(&timer); |
| 216 | +} |
| 217 | +``` |
| 218 | +
|
| 219 | +### Precision Timing |
| 220 | +
|
| 221 | +```c |
| 222 | +void measure_function_time(void) |
| 223 | +{ |
| 224 | + unsigned long start = (unsigned long)rt_clock_time_getcnt(); |
| 225 | + |
| 226 | + /* Function to measure */ |
| 227 | + my_function(); |
| 228 | + |
| 229 | + unsigned long end = (unsigned long)rt_clock_time_getcnt(); |
| 230 | + unsigned long delta = end - start; |
| 231 | + |
| 232 | + rt_kprintf("Function took %u us\n", |
| 233 | + (rt_uint32_t)rt_clock_time_cnt_to_us(delta)); |
| 234 | +} |
| 235 | +``` |
| 236 | + |
| 237 | +## See Also |
| 238 | + |
| 239 | +- [Clock Time Documentation](../../documentation/6.components/device-driver/clock_time.md) |
| 240 | +- [Clock Time README](../../components/drivers/clock_time/README.md) |
| 241 | +- [Adapter Examples](../../components/drivers/clock_time/adapters/) |
0 commit comments