source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/hardware/arduino/avr/firmwares/wifishield/wifiHD/src/SOFTWARE_FRAMEWORK/DRIVERS/CPU/CYCLE_COUNTER/cycle_counter.h @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 8.9 KB
Line 
1/* This header file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
2
3/*This file has been prepared for Doxygen automatic documentation generation.*/
4/*! \file *********************************************************************
5 *
6 * \brief Cycle counter driver.
7 *
8 * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32
9 * - Supported devices:  All AVR32UC devices.
10 * - AppNote:
11 *
12 * \author               Atmel Corporation: http://www.atmel.com \n
13 *                       Support and FAQ: http://support.atmel.no/
14 *
15 *****************************************************************************/
16
17/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are met:
21 *
22 * 1. Redistributions of source code must retain the above copyright notice, this
23 * list of conditions and the following disclaimer.
24 *
25 * 2. Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials provided with the distribution.
28 *
29 * 3. The name of Atmel may not be used to endorse or promote products derived
30 * from this software without specific prior written permission.
31 *
32 * 4. This software may only be redistributed and used in connection with an Atmel
33 * AVR product.
34 *
35 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
38 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
39 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
41 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
43 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
45 *
46 */
47
48#ifndef _CYCLE_COUNTER_H_
49#define _CYCLE_COUNTER_H_
50
51#include "compiler.h"
52
53
54//! Structure holding private information, automatically initialized by the
55//! cpu_set_timeout() function.
56typedef struct
57{
58  //! The cycle count at the begining of the timeout.
59  unsigned long delay_start_cycle;
60
61  //! The cycle count at the end of the timeout.
62  unsigned long delay_end_cycle;
63
64  //! Enable/disable the timout detection
65  unsigned char timer_state;
66  #define CPU_TIMER_STATE_STARTED 0
67  #define CPU_TIMER_STATE_REACHED 1
68  #define CPU_TIMER_STATE_STOPPED 2
69} t_cpu_time;
70
71
72/*!
73 * \brief Convert milli-seconds into CPU cycles.
74 *
75 * \param  ms:      Number of millisecond.
76 * \param  fcpu_hz: CPU frequency in Hz.
77 *
78 * \return the converted number of CPU cycles.
79 */
80#if (defined __GNUC__)
81__attribute__((__always_inline__))
82#endif
83extern __inline__ U32 cpu_ms_2_cy(unsigned long ms, unsigned long fcpu_hz)
84{
85  return ((unsigned long long)ms * fcpu_hz + 999) / 1000;
86}
87
88
89/*!
90 * \brief Convert micro-seconds into CPU cycles.
91 *
92 * \param  us:      Number of microsecond.
93 * \param  fcpu_hz: CPU frequency in Hz.
94 *
95 * \return the converted number of CPU cycles.
96 */
97#if (defined __GNUC__)
98__attribute__((__always_inline__))
99#endif
100extern __inline__ U32 cpu_us_2_cy(unsigned long us, unsigned long fcpu_hz)
101{
102  return ((unsigned long long)us * fcpu_hz + 999999) / 1000000;
103}
104
105
106/*!
107 * \brief Convert CPU cycles into milli-seconds.
108 *
109 * \param  cy:      Number of CPU cycles.
110 * \param  fcpu_hz: CPU frequency in Hz.
111 *
112 * \return the converted number of milli-second.
113 */
114#if (defined __GNUC__)
115__attribute__((__always_inline__))
116#endif
117extern __inline__ U32 cpu_cy_2_ms(unsigned long cy, unsigned long fcpu_hz)
118{
119  return ((unsigned long long)cy * 1000 + fcpu_hz-1) / fcpu_hz;
120}
121
122
123/*!
124 * \brief Convert CPU cycles into micro-seconds.
125 *
126 * \param  cy:      Number of CPU cycles.
127 * \param  fcpu_hz: CPU frequency in Hz.
128 *
129 * \return the converted number of micro-second.
130 */
131#if (defined __GNUC__)
132__attribute__((__always_inline__))
133#endif
134extern __inline__ U32 cpu_cy_2_us(unsigned long cy, unsigned long fcpu_hz)
135{
136  return ((unsigned long long)cy * 1000000 + fcpu_hz-1) / fcpu_hz;
137}
138
139
140/*!
141 * \brief Set a timer variable.
142 *
143 * Ex:  t_cpu_time timer;
144 *      cpu_set_timeout( cpu_ms_2_cy(10, FOSC0), &timer ); // timeout in 10 ms
145 *      if( cpu_is_timeout(&timer) )
146 *         cpu_stop_timeout(&timer);
147 *         ../..
148 *
149 * \param  delay:   (input) delay in CPU cycles before timeout.
150 * \param  cpu_time: (output) internal information used by the timer API.
151 */
152#if (defined __GNUC__)
153__attribute__((__always_inline__))
154#endif
155extern __inline__ void cpu_set_timeout(unsigned long delay, t_cpu_time *cpu_time)
156{
157  cpu_time->delay_start_cycle = Get_system_register(AVR32_COUNT);
158  cpu_time->delay_end_cycle   = cpu_time->delay_start_cycle + delay;
159  cpu_time->timer_state       = CPU_TIMER_STATE_STARTED;
160}
161
162
163/*!
164 * \brief Test if a timer variable reached its timeout.
165 *
166 * Once the timeout is reached, the function will always return TRUE,
167 * until the cpu_stop_timeout() function is called.
168 *
169 * Ex:  t_cpu_time timer;
170 *      cpu_set_timeout( 10, FOSC0, &timer ); // timeout in 10 ms
171 *      if( cpu_is_timeout(&timer) )
172 *         cpu_stop_timeout(&timer);
173 *         ../..
174 *
175 * \param  cpu_time:   (input) internal information used by the timer API.
176 *
177 * \return TRUE if timeout occured, otherwise FALSE.
178 */
179#if (defined __GNUC__)
180__attribute__((__always_inline__))
181#endif
182extern __inline__ unsigned long cpu_is_timeout(t_cpu_time *cpu_time)
183{
184  unsigned long current_cycle_count = Get_system_register(AVR32_COUNT);
185
186  if( cpu_time->timer_state==CPU_TIMER_STATE_STOPPED )
187    return FALSE;
188
189  // Test if the timeout as already occured.
190  else if (cpu_time->timer_state == CPU_TIMER_STATE_REACHED)
191    return TRUE;
192
193  // If the ending cycle count of this timeout is wrapped, ...
194  else if (cpu_time->delay_start_cycle > cpu_time->delay_end_cycle)
195  {
196    if (current_cycle_count < cpu_time->delay_start_cycle && current_cycle_count > cpu_time->delay_end_cycle)
197    {
198      cpu_time->timer_state = CPU_TIMER_STATE_REACHED;
199      return TRUE;
200    }
201    return FALSE;
202  }
203  else
204  {
205    if (current_cycle_count < cpu_time->delay_start_cycle || current_cycle_count > cpu_time->delay_end_cycle)
206    {
207      cpu_time->timer_state = CPU_TIMER_STATE_REACHED;
208      return TRUE;
209    }
210    return FALSE;
211  }
212}
213
214
215/*!
216 * \brief Stop a timeout detection.
217 *
218 * Ex:  t_cpu_time timer;
219 *      cpu_set_timeout( 10, FOSC0, &timer ); // timeout in 10 ms
220 *      if( cpu_is_timeout(&timer) )
221 *         cpu_stop_timeout(&timer);
222 *         ../..
223 *
224 * \param  cpu_time:   (input) internal information used by the timer API.
225 */
226#if (defined __GNUC__)
227__attribute__((__always_inline__))
228#endif
229extern __inline__ void cpu_stop_timeout(t_cpu_time *cpu_time)
230{
231  cpu_time->timer_state = CPU_TIMER_STATE_STOPPED;
232}
233
234
235/*!
236 * \brief Test if a timer is stopped.
237 *
238 * \param  cpu_time:   (input) internal information used by the timer API.
239 *
240 * \return TRUE if timer is stopped, otherwise FALSE.
241 */
242#if (defined __GNUC__)
243__attribute__((__always_inline__))
244#endif
245extern __inline__ unsigned long cpu_is_timer_stopped(t_cpu_time *cpu_time)
246{
247
248  if( cpu_time->timer_state==CPU_TIMER_STATE_STOPPED )
249    return TRUE;
250  else
251    return FALSE;
252}
253
254
255/*!
256 * \brief Waits during at least the specified delay (in millisecond) before returning.
257 *
258 * \param  delay:   Number of millisecond to wait.
259 * \param  fcpu_hz: CPU frequency in Hz.
260 */
261#if (defined __GNUC__)
262__attribute__((__always_inline__))
263#endif
264extern __inline__ void cpu_delay_ms(unsigned long delay, unsigned long fcpu_hz)
265{
266  t_cpu_time timer;
267  cpu_set_timeout( cpu_ms_2_cy(delay, fcpu_hz), &timer);
268  while( !cpu_is_timeout(&timer) );
269}
270
271/*!
272 * \brief Waits during at least the specified delay (in microsecond) before returning.
273 *
274 * \param  delay:   Number of microsecond to wait.
275 * \param  fcpu_hz: CPU frequency in Hz.
276 */
277#if (defined __GNUC__)
278__attribute__((__always_inline__))
279#endif
280extern __inline__ void cpu_delay_us(unsigned long delay, unsigned long fcpu_hz)
281{
282  t_cpu_time timer;
283  cpu_set_timeout( cpu_us_2_cy(delay, fcpu_hz), &timer);
284  while( !cpu_is_timeout(&timer) );
285}
286
287/*!
288 * \brief Waits during at least the specified delay (in CPU cycles) before returning.
289 *
290 * \param  delay:   Number of CPU cycles to wait.
291 */
292#if (defined __GNUC__)
293__attribute__((__always_inline__))
294#endif
295extern __inline__ void cpu_delay_cy(unsigned long delay)
296{
297  t_cpu_time timer;
298  cpu_set_timeout( delay, &timer);
299  while( !cpu_is_timeout(&timer) );
300}
301
302
303#define Get_sys_count()     ( Get_system_register(AVR32_COUNT)        )
304#define Set_sys_count(x)    ( Set_system_register(AVR32_COUNT,   (x)) )
305#define Get_sys_compare()   ( Get_system_register(AVR32_COMPARE)      )
306#define Set_sys_compare(x)  ( Set_system_register(AVR32_COMPARE, (x)) )
307
308
309#endif // _CYCLE_COUNTER_H_
Note: See TracBrowser for help on using the repository browser.