RTEMS
t-test-busy-tick.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 
11 /*
12  * Copyright (C) 2014, 2020 embedded brains GmbH (http://www.embedded-brains.de)
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions
16  * are met:
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  * notice, this list of conditions and the following disclaimer in the
21  * documentation and/or other materials provided with the distribution.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39 
40 #include <rtems/test.h>
41 
42 #include <rtems.h>
43 
44 static uint_fast32_t
45 T_estimate_busy_loop_maximum(void)
46 {
47  uint_fast32_t initial;
48  uint_fast32_t units;
49 
51  units = 0;
52 
53  while (initial == rtems_clock_get_ticks_since_boot()) {
54  ++units;
55  }
56 
57  return units;
58 }
59 
60 static uint_fast32_t
61 T_wait_for_tick_change(void)
62 {
63  uint_fast32_t initial;
64  uint_fast32_t now;
65 
67 
68  do {
70  } while (now == initial);
71 
72  return now;
73 }
74 
75 uint_fast32_t
76 T_get_one_clock_tick_busy(void)
77 {
78  uint_fast32_t last;
79  uint_fast32_t now;
80  uint_fast32_t a;
81  uint_fast32_t b;
82  uint_fast32_t m;
83 
84  /* Choose a lower bound */
85  a = 1;
86 
87  /* Estimate an upper bound */
88 
89  T_wait_for_tick_change();
90  b = 2 * T_estimate_busy_loop_maximum();
91 
92  while (true) {
93  last = T_wait_for_tick_change();
94  T_busy(b);
96 
97  if (now != last) {
98  break;
99  }
100 
101  b *= 2;
102  }
103 
104  /* Find a good value */
105  do {
106  m = (a + b) / 2;
107 
108  last = T_wait_for_tick_change();
109  T_busy(m);
111 
112  if (now != last) {
113  b = m;
114  } else {
115  a = m;
116  }
117  } while (b - a > 1);
118 
119  return m;
120 }
This header file defines the RTEMS Classic API.
#define rtems_clock_get_ticks_since_boot()
%
Definition: clock.h:110