- Main Page
- Related Pages
- Data Structures
- Files

# cycle_counter.h

Go to the documentation of this file.

    00001 /*This file has been prepared for Doxygen automatic documentation generation.*/
    00015 /* Copyright (C) 2006-2008, Atmel Corporation All rights reserved.
    00016  *
    00017  * Redistribution and use in source and binary forms, with or without
    00018  * modification, are permitted provided that the following conditions are met:
    00019  *
    00020  * 1. Redistributions of source code must retain the above copyright notice,
    00021  * this list of conditions and the following disclaimer.
    00022  *
    00023  * 2. Redistributions in binary form must reproduce the above copyright notice,
    00024  * this list of conditions and the following disclaimer in the documentation
    00025  * and/or other materials provided with the distribution.
    00026  *
    00027  * 3. The name of ATMEL may not be used to endorse or promote products derived
    00028  * from this software without specific prior written permission.
    00029  *
    00030  * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
    00031  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00032  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
    00033  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
    00034  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    00035  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    00036  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00037  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00038  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    00039  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00040  */
    00041
    00042
    00043 #ifndef _CYCLE_COUNTER_H_
    00044 #define _CYCLE_COUNTER_H_
    00045
    00046 #include "compiler.h"
    00047
    00048
    00051 typedef struct
    00052 {
    00054   unsigned long time;
    00055
    00057   Bool wrap;
    00058 } t_cpu_time;
    00059
    00060
    00069 #if __GNUC__
    00070 __attribute__((__always_inline__))
    00071 #endif
    00072 extern __inline__ U32 cpu_ms_2_cy(unsigned long ms, unsigned long fcpu_hz)
    00073 {
    00074   return ((unsigned long long)ms * fcpu_hz + 999) / 1000;
    00075 }
    00076
    00077
    00086 #if __GNUC__
    00087 __attribute__((__always_inline__))
    00088 #endif
    00089 extern __inline__ U32 cpu_cy_2_ms(unsigned long cy, unsigned long fcpu_hz)
    00090 {
    00091   return ((unsigned long long)cy * 1000 + fcpu_hz-1) / fcpu_hz;
    00092 }
    00093
    00094
    00103 #if __GNUC__
    00104 __attribute__((__always_inline__))
    00105 #endif
    00106 extern __inline__ U32 cpu_cy_2_us(unsigned long cy, unsigned long fcpu_hz)
    00107 {
    00108   return ((unsigned long long)cy * 1000000 + fcpu_hz-1) / fcpu_hz;
    00109 }
    00110
    00111
    00123 #if __GNUC__
    00124 __attribute__((__always_inline__))
    00125 #endif
    00126 extern __inline__ void cpu_set_timeout(unsigned long delay, t_cpu_time *cpu_time)
    00127 {
    00128   // Use the CPU cycle counter.
    00129   unsigned long delay_start_cycle = Get_system_register(AVR32_COUNT);
    00130   unsigned long delay_end_cycle   = delay_start_cycle + delay;
    00131
    00132   if (delay_start_cycle <= delay_end_cycle)
    00133     cpu_time->wrap=FALSE;
    00134   else
    00135     cpu_time->wrap=TRUE;
    00136
    00137   cpu_time->time= delay_end_cycle;
    00138 }
    00139
    00140
    00153 #if __GNUC__
    00154 __attribute__((__always_inline__))
    00155 #endif
    00156 extern __inline__ unsigned long cpu_is_timeout(t_cpu_time *cpu_time)
    00157 {
    00158   unsigned long delay_end_cycle = cpu_time->time;
    00159   // Use the CPU cycle counter.
    00160   if (cpu_time->wrap==FALSE)
    00161   {
    00162     if( (unsigned long)Get_system_register(AVR32_COUNT) < delay_end_cycle )
    00163       return FALSE;
    00164     else
    00165       return TRUE;
    00166   }
    00167   else
    00168   {
    00169     if( (unsigned long)Get_system_register(AVR32_COUNT) > delay_end_cycle )
    00170       return FALSE;
    00171     else
    00172     { // AVR32 counter has wrapped.
    00173       cpu_time->wrap=FALSE;
    00174       return FALSE;
    00175     }
    00176   }
    00177 }
    00178
    00179
    00186 #if __GNUC__
    00187 __attribute__((__always_inline__))
    00188 #endif
    00189 extern __inline__ void cpu_delay_ms(unsigned long delay, unsigned long fcpu_hz)
    00190 {
    00191   t_cpu_time timer;
    00192   cpu_set_timeout( cpu_ms_2_cy(delay, fcpu_hz), &timer);
    00193   while( !cpu_is_timeout(&timer) );
    00194 }
    00195
    00196
    00203 #if __GNUC__
    00204 __attribute__((__always_inline__))
    00205 #endif
    00206 extern __inline__ void cpu_delay_cy(unsigned long delay, unsigned long fcpu_hz)
    00207 {
    00208   t_cpu_time timer;
    00209   cpu_set_timeout( delay, &timer);
    00210   while( !cpu_is_timeout(&timer) );
    00211 }
    00212
    00213
    00214 #define Get_sys_count()     ( Get_system_register(AVR32_COUNT)        )
    00215 #define Set_sys_count(x)    ( Set_system_register(AVR32_COUNT,   (x)) )
    00216 #define Get_sys_compare()   ( Get_system_register(AVR32_COMPARE)      )
    00217 #define Set_sys_compare(x)  ( Set_system_register(AVR32_COMPARE, (x)) )
    00218
    00219
    00220 #endif // _CYCLE_COUNTER_H_