source: rtems/cpukit/include/rtems/counter.h @ 7e86e00

5
Last change on this file since 7e86e00 was 65f868c, checked in by Sebastian Huber <sebastian.huber@…>, on 05/23/18 at 12:17:25

Add _CPU_Counter_frequency()

Add rtems_counter_frequency() API function. Use it to initialize the
counter value converter via the new system initialization step
(RTEMS_SYSINIT_CPU_COUNTER). This decouples the counter implementation
and the counter converter. It avoids an unnecessary pull in of the
64-bit integer division from libgcc.

Update #3456.

  • Property mode set to 100644
File size: 4.5 KB
Line 
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
29extern "C" {
30#endif /* __cplusplus */
31
32/**
33 * @defgroup ClassicCounter Free-Running Counter and Busy Wait Delay
34 *
35 * @ingroup ClassicRTEMS
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 */
62typedef 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 */
69static inline uint32_t rtems_counter_frequency( void )
70{
71  return _CPU_Counter_frequency();
72}
73
74/**
75 * @brief Reads the current counter values.
76 *
77 * @return The current counter values.
78 */
79static 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 */
96static 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 Some counter ticks.
108 *
109 * @return The nanoseconds corresponding to the counter ticks.  The value is
110 * rounded up.
111 */
112uint64_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 Some nanoseconds.
120 *
121 * @return The counter ticks corresponding to the nanoseconds.  The value is
122 * rounded up.
123 */
124rtems_counter_ticks rtems_counter_nanoseconds_to_ticks(
125  uint32_t nanoseconds
126);
127
128/**
129 * @brief Initializes the counter ticks to/from nanoseconds converter functions.
130 *
131 * This function must be used to initialize the
132 * rtems_counter_ticks_to_nanoseconds() and
133 * rtems_counter_nanoseconds_to_ticks() functions.  It should be called during
134 * system initialization by the board support package.
135 *
136 * @param[in] frequency The current counter frequency in Hz.
137 */
138void rtems_counter_initialize_converter( uint32_t frequency );
139
140/**
141 * @brief Busy wait for some counter ticks.
142 *
143 * This function does not disable interrupts.  Thus task switches and
144 * interrupts can interfere with this busy wait may prolong the delay.  This
145 * function busy waits at least the specified time.  Due to some overhead the
146 * actual delay may be longer.
147 *
148 * @param[in] ticks The minimum busy wait time in counter ticks.
149 */
150void rtems_counter_delay_ticks( rtems_counter_ticks ticks );
151
152/**
153 * @brief Busy wait for some nanoseconds.
154 *
155 * This function does not disable interrupts.  Thus task switches and
156 * interrupts can interfere with this busy wait may prolong the delay.  This
157 * function busy waits at least the specified time.  Due to some overhead the
158 * actual delay may be longer.
159 *
160 * @param[in] nanoseconds The minimum busy wait time in nanoseconds.
161 */
162void rtems_counter_delay_nanoseconds( uint32_t nanoseconds );
163
164/** @} */
165
166#ifdef __cplusplus
167}
168#endif /* __cplusplus */
169
170#endif /* _RTEMS_SAPI_COUNTER_H */
Note: See TracBrowser for help on using the repository browser.