PX4 Firmware
PX4 Autopilot Software http://px4.io
test_time.c
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file test_time.c
36  * Tests clocks/timekeeping.
37  */
38 
39 #include <px4_platform_common/px4_config.h>
40 #include <px4_platform_common/defines.h>
41 #include <px4_platform_common/posix.h>
42 #include <sys/types.h>
43 
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 
50 #include <arch/board/board.h>
51 
52 #include "tests_main.h"
53 
54 #include <math.h>
55 #include <float.h>
56 #include <drivers/drv_hrt.h>
57 
58 
59 static hrt_abstime
60 cycletime(void)
61 {
62  /* emulate hrt_absolute_time using the cycle counter */
63  static uint64_t basetime;
64  static uint32_t lasttime;
65  uint32_t cycles;
66 
67  cycles = *(unsigned long *)0xe0001004;
68 
69  if (cycles < lasttime) {
70  basetime += 0x100000000ULL;
71  }
72 
73  lasttime = cycles;
74 
75  return (basetime + cycles) / 168; /* XXX magic number */
76 }
77 
78 int test_time(int argc, char *argv[])
79 {
80  hrt_abstime h, c;
81  int64_t lowdelta, maxdelta = 0;
82  int64_t delta, deltadelta;
83 
84  /* enable the cycle counter */
85  (*(unsigned long *)0xe000edfc) |= (1 << 24); /* DEMCR |= DEMCR_TRCENA */
86  (*(unsigned long *)0xe0001000) |= 1; /* DWT_CTRL |= DWT_CYCCNT_ENA */
87 
88  /* get an average delta between the two clocks - this should stay roughly the same */
89  delta = 0;
90 
91  for (unsigned i = 0; i < 100; i++) {
92  uint32_t flags = px4_enter_critical_section();
93 
94  h = hrt_absolute_time();
95  c = cycletime();
96 
97  px4_leave_critical_section(flags);
98 
99  delta += h - c;
100  }
101 
102  lowdelta = abs(delta / 100);
103 
104  /* loop checking the time */
105  for (unsigned i = 0; i < 100; i++) {
106 
107  usleep(rand() % SHRT_MAX);
108 
109  uint32_t flags = px4_enter_critical_section();
110 
111  c = cycletime();
112  h = hrt_absolute_time();
113 
114  px4_leave_critical_section(flags);
115 
116  delta = abs(h - c);
117  deltadelta = abs(delta - lowdelta);
118 
119  if (deltadelta > maxdelta) {
120  maxdelta = deltadelta;
121  }
122 
123  if (deltadelta > 1000) {
124  fprintf(stderr, "h %" PRIu64 " c %" PRIu64 " d %" PRId64 "\n", h, c, delta - lowdelta);
125  }
126  }
127 
128  printf("Maximum jitter %" PRId64 "us\n", maxdelta);
129 
130  return 0;
131 }
int test_time(int argc, char *argv[])
Definition: test_time.c:78
static hrt_abstime cycletime(void)
Definition: test_time.c:60
High-resolution timer with callouts and timekeeping.
Tests declaration file.
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
Dual< Scalar, N > abs(const Dual< Scalar, N > &a)
Definition: Dual.hpp:196
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).