|
| 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 | +#include <pthread.h> |
| 33 | + |
| 34 | +#include "ostest.h" |
| 35 | + |
| 36 | +/**************************************************************************** |
| 37 | + * Pre-processor Definitions |
| 38 | + ****************************************************************************/ |
| 39 | + |
| 40 | +#define NSEC_PER_500MS (500 * NSEC_PER_MSEC) |
| 41 | + |
| 42 | +/* Simple assertion macro for hrtimer test cases */ |
| 43 | + |
| 44 | +#define HRTIMER_TEST(expr, value) \ |
| 45 | + do \ |
| 46 | + { \ |
| 47 | + ret = (expr); \ |
| 48 | + if (ret != (value)) \ |
| 49 | + { \ |
| 50 | + printf("ERROR: hrtimer test failed, line=%d ret=%d\n", \ |
| 51 | + __LINE__, ret); \ |
| 52 | + ASSERT(false); \ |
| 53 | + } \ |
| 54 | + } \ |
| 55 | + while (0) |
| 56 | + |
| 57 | +/**************************************************************************** |
| 58 | + * Private Data |
| 59 | + ****************************************************************************/ |
| 60 | + |
| 61 | +/* High resolution timer instance used for 500ms periodic test */ |
| 62 | + |
| 63 | +static hrtimer_t test_hrtimer_500ms; |
| 64 | + |
| 65 | +/* Indicate whether the hrtimer test is still running */ |
| 66 | + |
| 67 | +static bool hrtimer_test_running = false; |
| 68 | + |
| 69 | +/**************************************************************************** |
| 70 | + * Private Functions |
| 71 | + ****************************************************************************/ |
| 72 | + |
| 73 | +/**************************************************************************** |
| 74 | + * Name: test_hrtimer_callback |
| 75 | + * |
| 76 | + * Description: |
| 77 | + * High resolution timer callback function. This callback verifies |
| 78 | + * that the actual expiration interval is exactly 500ms (nanosecond |
| 79 | + * precision) and re-arms the timer in absolute mode. |
| 80 | + * |
| 81 | + * The timer will be cancelled after it fires 15 times. |
| 82 | + * |
| 83 | + * Input Parameters: |
| 84 | + * hrtimer - Pointer to the expired high resolution timer instance |
| 85 | + * |
| 86 | + * Returned Value: |
| 87 | + * None |
| 88 | + * |
| 89 | + ****************************************************************************/ |
| 90 | + |
| 91 | +static uint32_t test_hrtimer_callback(FAR hrtimer_t *hrtimer) |
| 92 | +{ |
| 93 | + static uint64_t previous = 0; |
| 94 | + static uint64_t now = 0; |
| 95 | + static uint32_t count = 0; |
| 96 | + |
| 97 | + struct timespec ts; |
| 98 | + int ret; |
| 99 | + |
| 100 | + count++; |
| 101 | + |
| 102 | + /* Get current system time */ |
| 103 | + |
| 104 | + clock_systime_timespec(&ts); |
| 105 | + now = clock_time2nsec(&ts); |
| 106 | + |
| 107 | + /* Skip comparison for the first invocation */ |
| 108 | + |
| 109 | + if (previous != 0 && count > 2) |
| 110 | + { |
| 111 | + /* Verify the timer interval is exactly |
| 112 | + * 500ms with nsec resolution |
| 113 | + */ |
| 114 | + |
| 115 | + HRTIMER_TEST(now - previous, NSEC_PER_500MS); |
| 116 | + } |
| 117 | + |
| 118 | + previous = now; |
| 119 | + |
| 120 | + /* Stop the test after 15 expirations */ |
| 121 | + |
| 122 | + if (count >= 10) |
| 123 | + { |
| 124 | + ret = hrtimer_cancel(&test_hrtimer_500ms); |
| 125 | + HRTIMER_TEST(ret, OK); |
| 126 | + |
| 127 | + hrtimer_test_running = false; |
| 128 | + } |
| 129 | + |
| 130 | + return NSEC_PER_500MS; |
| 131 | +} |
| 132 | + |
| 133 | +/**************************************************************************** |
| 134 | + * Public Functions |
| 135 | + ****************************************************************************/ |
| 136 | + |
| 137 | +/**************************************************************************** |
| 138 | + * Name: hrtimer_test |
| 139 | + * |
| 140 | + * Description: |
| 141 | + * Entry point for high resolution timer functional test. |
| 142 | + * |
| 143 | + * This test initializes a high resolution timer, starts it with |
| 144 | + * a 500ms relative timeout, and verifies that subsequent expirations |
| 145 | + * occur precisely at 500ms intervals using absolute re-arming. |
| 146 | + * |
| 147 | + * Input Parameters: |
| 148 | + * None |
| 149 | + * |
| 150 | + * Returned Value: |
| 151 | + * None |
| 152 | + * |
| 153 | + ****************************************************************************/ |
| 154 | + |
| 155 | +void hrtimer_test(void) |
| 156 | +{ |
| 157 | + int ret; |
| 158 | + |
| 159 | + hrtimer_test_running = true; |
| 160 | + |
| 161 | + /* Initialize the high resolution timer */ |
| 162 | + |
| 163 | + hrtimer_init(&test_hrtimer_500ms, |
| 164 | + test_hrtimer_callback, |
| 165 | + NULL); |
| 166 | + |
| 167 | + /* Start the timer with an initial 500ms relative timeout */ |
| 168 | + |
| 169 | + ret = hrtimer_start(&test_hrtimer_500ms, |
| 170 | + NSEC_PER_500MS, |
| 171 | + HRTIMER_MODE_REL); |
| 172 | + HRTIMER_TEST(ret, OK); |
| 173 | + |
| 174 | + /* Wait until the test completes */ |
| 175 | + |
| 176 | + while (hrtimer_test_running) |
| 177 | + { |
| 178 | + usleep(500 * USEC_PER_MSEC); |
| 179 | + } |
| 180 | +} |
0 commit comments