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