1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup RTEMSTestFrameworkImpl |
---|
7 | * |
---|
8 | * @brief Implementation of T_get_one_clock_tick_busy(). |
---|
9 | */ |
---|
10 | |
---|
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 | |
---|
50 | initial = rtems_clock_get_ticks_since_boot(); |
---|
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 | |
---|
66 | initial = rtems_clock_get_ticks_since_boot(); |
---|
67 | |
---|
68 | do { |
---|
69 | now = rtems_clock_get_ticks_since_boot(); |
---|
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); |
---|
95 | now = rtems_clock_get_ticks_since_boot(); |
---|
96 | |
---|
97 | if (now != last) { |
---|
98 | break; |
---|
99 | } |
---|
100 | |
---|
101 | b *= 2; |
---|
102 | last = now; |
---|
103 | } |
---|
104 | |
---|
105 | /* Find a good value */ |
---|
106 | do { |
---|
107 | m = (a + b) / 2; |
---|
108 | |
---|
109 | last = T_wait_for_tick_change(); |
---|
110 | T_busy(m); |
---|
111 | now = rtems_clock_get_ticks_since_boot(); |
---|
112 | |
---|
113 | if (now != last) { |
---|
114 | b = m; |
---|
115 | } else { |
---|
116 | a = m; |
---|
117 | } |
---|
118 | } while (b - a > 1); |
---|
119 | |
---|
120 | return m; |
---|
121 | } |
---|