Skip to content

Commit fd72386

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 fd72386

File tree

5 files changed

+203
-1
lines changed

5 files changed

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

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)