00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
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
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
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 {
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_