|
| 1 | +/**************************************************************************** |
| 2 | + * apps/testing/ostest/hrtimer.c |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 7 | + * contributor license agreements. See the NOTICE file distributed with |
| 8 | + * this work for additional information regarding copyright ownership. The |
| 9 | + * ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 10 | + * "License"); you may not use this file except in compliance with the |
| 11 | + * License. You may obtain a copy of the License at |
| 12 | + * |
| 13 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | + * |
| 15 | + * Unless required by applicable law or agreed to in writing, software |
| 16 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 18 | + * License for the specific language governing permissions and limitations |
| 19 | + * under the License. |
| 20 | + * |
| 21 | + ****************************************************************************/ |
| 22 | + |
| 23 | +/**************************************************************************** |
| 24 | + * Included Files |
| 25 | + ****************************************************************************/ |
| 26 | + |
| 27 | +#include <nuttx/config.h> |
| 28 | +#include <nuttx/hrtimer.h> |
| 29 | + |
| 30 | +#include <stdio.h> |
| 31 | +#include <sched.h> |
| 32 | + |
| 33 | +#include "ostest.h" |
| 34 | + |
| 35 | +/**************************************************************************** |
| 36 | + * Pre-processor Definitions |
| 37 | + ****************************************************************************/ |
| 38 | + |
| 39 | +#define NSEC_PER_500MS (500 * NSEC_PER_MSEC) |
| 40 | +#define NSEC_MARGIN (5) |
| 41 | + |
| 42 | +/* Simple assertion macro for HRTimer test cases */ |
| 43 | +#define HRTIMER_TEST(expr, value) \ |
| 44 | + do \ |
| 45 | + { \ |
| 46 | + ret = (expr); \ |
| 47 | + if (ret != (value)) \ |
| 48 | + { \ |
| 49 | + printf("ERROR: HRTimer test failed, line=%d ret=%d\n", \ |
| 50 | + __LINE__, ret); \ |
| 51 | + ASSERT(false); \ |
| 52 | + } \ |
| 53 | + } \ |
| 54 | + while (0) |
| 55 | + |
| 56 | +/**************************************************************************** |
| 57 | + * Private Types |
| 58 | + ****************************************************************************/ |
| 59 | + |
| 60 | +/* Structure for HRTimer test tracking */ |
| 61 | + |
| 62 | +struct hrtimer_test_s |
| 63 | +{ |
| 64 | + hrtimer_t timer; /* HRTimer instance */ |
| 65 | + uint64_t previous; /* Previous timestamp in nanoseconds */ |
| 66 | + uint64_t now; /* Current timestamp in nanoseconds */ |
| 67 | + uint32_t count; /* Number of timer expirations */ |
| 68 | + uint32_t period; /* Expected period between expirations */ |
| 69 | + bool active; /* True while the test is still running */ |
| 70 | +}; |
| 71 | + |
| 72 | +/**************************************************************************** |
| 73 | + * Private Functions |
| 74 | + ****************************************************************************/ |
| 75 | + |
| 76 | +/**************************************************************************** |
| 77 | + * Name: hrtimer_test_init |
| 78 | + * |
| 79 | + * Description: |
| 80 | + * Initialize a hrtimer_test_s structure for a new test. |
| 81 | + * |
| 82 | + * Input Parameters: |
| 83 | + * test_hrtimer - Pointer to the test structure to initialize |
| 84 | + * period - Expected timer period in nanoseconds |
| 85 | + * |
| 86 | + * Returned Value: |
| 87 | + * None |
| 88 | + * |
| 89 | + ****************************************************************************/ |
| 90 | + |
| 91 | +static void hrtimer_test_init(FAR struct hrtimer_test_s *test_hrtimer, |
| 92 | + uint32_t period) |
| 93 | +{ |
| 94 | + test_hrtimer->previous = 0; |
| 95 | + test_hrtimer->now = 0; |
| 96 | + test_hrtimer->count = 0; |
| 97 | + test_hrtimer->active = true; |
| 98 | + test_hrtimer->period = period; |
| 99 | +} |
| 100 | + |
| 101 | +/**************************************************************************** |
| 102 | + * Name: test_hrtimer_callback |
| 103 | + * |
| 104 | + * Description: |
| 105 | + * HRTimer callback function for test. |
| 106 | + * |
| 107 | + * - Verifies the timer interval is exactly 500ms (nanosecond precision) |
| 108 | + * - Stops the test after 15 expirations |
| 109 | + * - Re-arms the timer in absolute mode |
| 110 | + * |
| 111 | + * Input Parameters: |
| 112 | + * hrtimer - Pointer to the expired HRTimer instance |
| 113 | + * |
| 114 | + * Returned Value: |
| 115 | + * Timer period in nanoseconds (NSEC_PER_500MS) |
| 116 | + * |
| 117 | + ****************************************************************************/ |
| 118 | + |
| 119 | +static uint32_t test_hrtimer_callback(FAR hrtimer_t *hrtimer) |
| 120 | +{ |
| 121 | + struct timespec ts; |
| 122 | + uint32_t diff; |
| 123 | + int ret; |
| 124 | + |
| 125 | + FAR struct hrtimer_test_s *test = |
| 126 | + (FAR struct hrtimer_test_s *)hrtimer; |
| 127 | + |
| 128 | + /* Increment expiration count */ |
| 129 | + |
| 130 | + test->count++; |
| 131 | + |
| 132 | + /* Get current system time */ |
| 133 | + |
| 134 | + clock_systime_timespec(&ts); |
| 135 | + test->now = clock_time2nsec(&ts); |
| 136 | + |
| 137 | + /* Skip comparison for first two invocations */ |
| 138 | + |
| 139 | + if (test->count > 2) |
| 140 | + { |
| 141 | + /* Verify the timer interval is exactly |
| 142 | + * 500ms with nsec resolution |
| 143 | + */ |
| 144 | + |
| 145 | + diff = (uint32_t)(test->now - test->previous); |
| 146 | + |
| 147 | + HRTIMER_TEST(NSEC_PER_500MS < diff + NSEC_MARGIN, true); |
| 148 | + HRTIMER_TEST(NSEC_PER_500MS > diff - NSEC_MARGIN, true); |
| 149 | + } |
| 150 | + |
| 151 | + test->previous = test->now; |
| 152 | + |
| 153 | + /* Stop the test after 15 expirations */ |
| 154 | + |
| 155 | + if (test->count >= 15) |
| 156 | + { |
| 157 | + ret = hrtimer_cancel(hrtimer); |
| 158 | + HRTIMER_TEST(ret, 0); |
| 159 | + |
| 160 | + test->active = false; |
| 161 | + } |
| 162 | + |
| 163 | + return test->period; |
| 164 | +} |
| 165 | + |
| 166 | +/**************************************************************************** |
| 167 | + * Public Functions |
| 168 | + ****************************************************************************/ |
| 169 | + |
| 170 | +/**************************************************************************** |
| 171 | + * Name: hrtimer_test |
| 172 | + * |
| 173 | + * Description: |
| 174 | + * Entry point for high-resolution timer functional test. |
| 175 | + * |
| 176 | + * - Initializes a HRTimer |
| 177 | + * - Starts it with a 500ms relative timeout |
| 178 | + * - Verifies subsequent expirations occur at 500ms intervals |
| 179 | + * |
| 180 | + * Input Parameters: |
| 181 | + * None |
| 182 | + * |
| 183 | + * Returned Value: |
| 184 | + * None |
| 185 | + * |
| 186 | + ****************************************************************************/ |
| 187 | + |
| 188 | +void hrtimer_test(void) |
| 189 | +{ |
| 190 | + int ret; |
| 191 | + struct hrtimer_test_s test_hrtimer_500ms; |
| 192 | + |
| 193 | + /* Initialize test structure */ |
| 194 | + |
| 195 | + hrtimer_test_init(&test_hrtimer_500ms, NSEC_PER_500MS); |
| 196 | + |
| 197 | + /* Initialize the high-resolution timer */ |
| 198 | + |
| 199 | + hrtimer_init(&test_hrtimer_500ms.timer, |
| 200 | + test_hrtimer_callback, |
| 201 | + NULL); |
| 202 | + |
| 203 | + /* Start the timer with 500ms relative timeout */ |
| 204 | + |
| 205 | + ret = hrtimer_start(&test_hrtimer_500ms.timer, |
| 206 | + test_hrtimer_500ms.period, |
| 207 | + HRTIMER_MODE_REL); |
| 208 | + |
| 209 | + HRTIMER_TEST(ret, OK); |
| 210 | + |
| 211 | + /* Wait until the test completes */ |
| 212 | + |
| 213 | + while (test_hrtimer_500ms.active) |
| 214 | + { |
| 215 | + usleep(500 * USEC_PER_MSEC); |
| 216 | + } |
| 217 | +} |
0 commit comments