1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @ingroup ClassicCounter |
---|
5 | * |
---|
6 | * @brief Free-Running Counter and Busy Wait Delay API |
---|
7 | */ |
---|
8 | |
---|
9 | /* |
---|
10 | * Copyright (c) 2014, 2018 embedded brains GmbH. All rights reserved. |
---|
11 | * |
---|
12 | * embedded brains GmbH |
---|
13 | * Dornierstr. 4 |
---|
14 | * 82178 Puchheim |
---|
15 | * Germany |
---|
16 | * <rtems@embedded-brains.de> |
---|
17 | * |
---|
18 | * The license and distribution terms for this file may be |
---|
19 | * found in the file LICENSE in this distribution or at |
---|
20 | * http://www.rtems.org/license/LICENSE. |
---|
21 | */ |
---|
22 | |
---|
23 | #ifndef _RTEMS_SAPI_COUNTER_H |
---|
24 | #define _RTEMS_SAPI_COUNTER_H |
---|
25 | |
---|
26 | #include <rtems/score/cpu.h> |
---|
27 | |
---|
28 | #ifdef __cplusplus |
---|
29 | extern "C" { |
---|
30 | #endif /* __cplusplus */ |
---|
31 | |
---|
32 | /** |
---|
33 | * @defgroup ClassicCounter Free-Running Counter and Busy Wait Delay |
---|
34 | * |
---|
35 | * @ingroup RTEMSAPIClassic |
---|
36 | * |
---|
37 | * @brief Free-running counter and busy wait delay functions. |
---|
38 | * |
---|
39 | * The RTEMS counter is some free-running counter. It ticks usually with a |
---|
40 | * frequency close to the CPU or system bus clock. |
---|
41 | * |
---|
42 | * The counter can be used in case the overhead of the |
---|
43 | * rtems_clock_get_uptime_nanoseconds() is too high. The step from counter |
---|
44 | * ticks to/from nanoseconds is explicit in this API unlike to |
---|
45 | * rtems_clock_get_uptime_nanoseconds() which performs the conversion on each |
---|
46 | * invocation. |
---|
47 | * |
---|
48 | * This counter works without a clock driver and during system initialization. |
---|
49 | * |
---|
50 | * The counter can be used to profile low-level operations like SMP locks or |
---|
51 | * interrupt disabled critical sections. The counter can act also as an |
---|
52 | * entropy source for a random number generator. |
---|
53 | * |
---|
54 | * The period of the counter depends on the actual hardware. |
---|
55 | * |
---|
56 | * @{ |
---|
57 | */ |
---|
58 | |
---|
59 | /** |
---|
60 | * @brief Unsigned integer type for counter values. |
---|
61 | */ |
---|
62 | typedef CPU_Counter_ticks rtems_counter_ticks; |
---|
63 | |
---|
64 | /** |
---|
65 | * @brief Returns the current counter frequency in Hz. |
---|
66 | * |
---|
67 | * @return The current counter frequency in Hz. |
---|
68 | */ |
---|
69 | static inline uint32_t rtems_counter_frequency( void ) |
---|
70 | { |
---|
71 | return _CPU_Counter_frequency(); |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * @brief Reads the current counter value. |
---|
76 | * |
---|
77 | * @return The current counter value. |
---|
78 | */ |
---|
79 | static inline rtems_counter_ticks rtems_counter_read( void ) |
---|
80 | { |
---|
81 | return _CPU_Counter_read(); |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * @brief Returns the difference between the second and first CPU counter |
---|
86 | * value. |
---|
87 | * |
---|
88 | * This operation may be carried out as a modulo operation depending on the |
---|
89 | * range of the CPU counter device. |
---|
90 | * |
---|
91 | * @param[in] second The second CPU counter value. |
---|
92 | * @param[in] first The first CPU counter value. |
---|
93 | * |
---|
94 | * @return Returns second minus first modulo counter period. |
---|
95 | */ |
---|
96 | static inline rtems_counter_ticks rtems_counter_difference( |
---|
97 | rtems_counter_ticks second, |
---|
98 | rtems_counter_ticks first |
---|
99 | ) |
---|
100 | { |
---|
101 | return _CPU_Counter_difference( second, first ); |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * @brief Converts counter ticks into nanoseconds. |
---|
106 | * |
---|
107 | * @param[in] ticks The counter ticks value to convert. |
---|
108 | * |
---|
109 | * @return The nanoseconds value corresponding to the counter ticks. The value |
---|
110 | * is rounded up. |
---|
111 | */ |
---|
112 | uint64_t rtems_counter_ticks_to_nanoseconds( |
---|
113 | rtems_counter_ticks ticks |
---|
114 | ); |
---|
115 | |
---|
116 | /** |
---|
117 | * @brief Converts nanoseconds into counter ticks. |
---|
118 | * |
---|
119 | * @param[in] nanoseconds The nanoseconds value to convert. |
---|
120 | * |
---|
121 | * @return The counter ticks value corresponding to the nanoseconds value. The |
---|
122 | * value is rounded up. |
---|
123 | */ |
---|
124 | rtems_counter_ticks rtems_counter_nanoseconds_to_ticks( |
---|
125 | uint32_t nanoseconds |
---|
126 | ); |
---|
127 | |
---|
128 | /** |
---|
129 | * @brief Converts counter ticks into signed binary time (sbintime_t). |
---|
130 | * |
---|
131 | * @param[in] ticks The counter ticks value to convert. |
---|
132 | * |
---|
133 | * @return The signed binary time value corresponding to the counter ticks |
---|
134 | * value. The value is rounded up. |
---|
135 | */ |
---|
136 | int64_t rtems_counter_ticks_to_sbintime( rtems_counter_ticks ticks ); |
---|
137 | |
---|
138 | /** |
---|
139 | * @brief Converts signed binary time (sbintime_t) into counter ticks. |
---|
140 | * |
---|
141 | * @param[in] sbt The signed binary time value to convert. |
---|
142 | * |
---|
143 | * @return The counter ticks value corresponding to the nanoseconds value. The |
---|
144 | * value is rounded up. |
---|
145 | */ |
---|
146 | rtems_counter_ticks rtems_counter_sbintime_to_ticks( int64_t sbt ); |
---|
147 | |
---|
148 | /** |
---|
149 | * @brief Initializes the counter ticks to/from nanoseconds converter functions. |
---|
150 | * |
---|
151 | * This function must be used to initialize the |
---|
152 | * rtems_counter_ticks_to_nanoseconds() and |
---|
153 | * rtems_counter_nanoseconds_to_ticks() functions. It should be called during |
---|
154 | * system initialization by the board support package. |
---|
155 | * |
---|
156 | * @param[in] frequency The current counter frequency in Hz. |
---|
157 | */ |
---|
158 | void rtems_counter_initialize_converter( uint32_t frequency ); |
---|
159 | |
---|
160 | /** |
---|
161 | * @brief Busy wait for some counter ticks. |
---|
162 | * |
---|
163 | * This function does not disable interrupts. Thus task switches and |
---|
164 | * interrupts can interfere with this busy wait may prolong the delay. This |
---|
165 | * function busy waits at least the specified time. Due to some overhead the |
---|
166 | * actual delay may be longer. |
---|
167 | * |
---|
168 | * @param[in] ticks The minimum busy wait time in counter ticks. |
---|
169 | */ |
---|
170 | void rtems_counter_delay_ticks( rtems_counter_ticks ticks ); |
---|
171 | |
---|
172 | /** |
---|
173 | * @brief Busy wait for some nanoseconds. |
---|
174 | * |
---|
175 | * This function does not disable interrupts. Thus task switches and |
---|
176 | * interrupts can interfere with this busy wait may prolong the delay. This |
---|
177 | * function busy waits at least the specified time. Due to some overhead the |
---|
178 | * actual delay may be longer. |
---|
179 | * |
---|
180 | * @param[in] nanoseconds The minimum busy wait time in nanoseconds. |
---|
181 | */ |
---|
182 | void rtems_counter_delay_nanoseconds( uint32_t nanoseconds ); |
---|
183 | |
---|
184 | /** @} */ |
---|
185 | |
---|
186 | #ifdef __cplusplus |
---|
187 | } |
---|
188 | #endif /* __cplusplus */ |
---|
189 | |
---|
190 | #endif /* _RTEMS_SAPI_COUNTER_H */ |
---|