Skip to content

Commit 56e2ed7

Browse files
committed
testing/ostest: add hrtimer API functional tests
Add functional tests for the newly added hrtimer APIs, including hrtimer_init(), hrtimer_start(), and hrtimer_cancel(). Signed-off-by: Chengdong Wang <[email protected]>
1 parent 60f5a68 commit 56e2ed7

File tree

5 files changed

+240
-1
lines changed

5 files changed

+240
-1
lines changed

testing/ostest/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ if(CONFIG_TESTING_OSTEST)
153153
list(APPEND SRCS nxevent.c)
154154
endif()
155155

156+
if(CONFIG_HRTIMER AND CONFIG_BUILD_FLAT)
157+
list(APPEND SRCS hrtimer.c)
158+
endif()
159+
156160
if(CONFIG_BUILD_FLAT)
157161
list(APPEND SRCS wdog.c)
158162
endif()

testing/ostest/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ CSRCS += nxevent.c
150150
endif
151151
endif
152152

153+
ifeq ($(CONFIG_HRTIMER),y)
154+
ifeq ($(CONFIG_BUILD_FLAT),y)
155+
CSRCS += hrtimer.c
156+
endif
157+
endif
158+
153159
ifeq ($(CONFIG_BUILD_FLAT),y)
154160
CSRCS += wdog.c
155161
endif

testing/ostest/hrtimer.c

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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+
}

testing/ostest/ostest.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,4 +304,8 @@ int sem_nfreeholders(void);
304304
void nxevent_test(void);
305305
#endif
306306

307+
#if defined(CONFIG_SCHED_EVENTS) && defined(CONFIG_BUILD_FLAT)
308+
void hrtimer_test(void);
309+
#endif
310+
307311
#endif /* __APPS_TESTING_OSTEST_OSTEST_H */

testing/ostest/ostest_main.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,14 @@ static int user_main(int argc, char *argv[])
625625
check_test_memory_usage();
626626
#endif
627627

628+
#if defined(CONFIG_HRTIMER) && defined(CONFIG_BUILD_FLAT)
629+
/* Verify hrtimer */
630+
631+
printf("\nuser_main: hrtimer test\n");
632+
hrtimer_test();
633+
check_test_memory_usage();
634+
#endif
635+
628636
/* Compare memory usage at time ostest_main started until
629637
* user_main exits. These should not be identical, but should
630638
* be similar enough that we can detect any serious OS memory
@@ -705,7 +713,7 @@ int main(int argc, FAR char **argv)
705713
stdio_test();
706714

707715
#ifdef SDCC
708-
/* I am not yet certain why SDCC does not like the following initilizers.
716+
/* I am not yet certain why SDCC does not like the following initializers.
709717
* It involves some issues with 2- vs 3-byte pointer types.
710718
*/
711719

0 commit comments

Comments
 (0)