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
00044
00045
00046 #ifndef _CYCLE_COUNTER_H_
00047 #define _CYCLE_COUNTER_H_
00048
00049 #include "compiler.h"
00050
00051
00054 typedef struct
00055 {
00057 unsigned long delay_start_cycle;
00058
00060 unsigned long delay_end_cycle;
00061
00063 unsigned char timer_state;
00064 #define CPU_TIMER_STATE_STARTED 0
00065 #define CPU_TIMER_STATE_REACHED 1
00066 #define CPU_TIMER_STATE_STOPPED 2
00067 } t_cpu_time;
00068
00069
00078 #if (defined __GNUC__)
00079 __attribute__((__always_inline__))
00080 #endif
00081 extern __inline__ U32 cpu_ms_2_cy(unsigned long ms, unsigned long fcpu_hz)
00082 {
00083 return ((unsigned long long)ms * fcpu_hz + 999) / 1000;
00084 }
00085
00086
00095 #if (defined __GNUC__)
00096 __attribute__((__always_inline__))
00097 #endif
00098 extern __inline__ U32 cpu_us_2_cy(unsigned long us, unsigned long fcpu_hz)
00099 {
00100 return ((unsigned long long)us * fcpu_hz + 999999) / 1000000;
00101 }
00102
00103
00112 #if (defined __GNUC__)
00113 __attribute__((__always_inline__))
00114 #endif
00115 extern __inline__ U32 cpu_cy_2_ms(unsigned long cy, unsigned long fcpu_hz)
00116 {
00117 return ((unsigned long long)cy * 1000 + fcpu_hz-1) / fcpu_hz;
00118 }
00119
00120
00129 #if (defined __GNUC__)
00130 __attribute__((__always_inline__))
00131 #endif
00132 extern __inline__ U32 cpu_cy_2_us(unsigned long cy, unsigned long fcpu_hz)
00133 {
00134 return ((unsigned long long)cy * 1000000 + fcpu_hz-1) / fcpu_hz;
00135 }
00136
00137
00150 #if (defined __GNUC__)
00151 __attribute__((__always_inline__))
00152 #endif
00153 extern __inline__ void cpu_set_timeout(unsigned long delay, t_cpu_time *cpu_time)
00154 {
00155 cpu_time->delay_start_cycle = Get_system_register(AVR32_COUNT);
00156 cpu_time->delay_end_cycle = cpu_time->delay_start_cycle + delay;
00157 cpu_time->timer_state = CPU_TIMER_STATE_STARTED;
00158 }
00159
00160
00177 #if (defined __GNUC__)
00178 __attribute__((__always_inline__))
00179 #endif
00180 extern __inline__ unsigned long cpu_is_timeout(t_cpu_time *cpu_time)
00181 {
00182 unsigned long current_cycle_count = Get_system_register(AVR32_COUNT);
00183
00184 if( cpu_time->timer_state==CPU_TIMER_STATE_STOPPED )
00185 return FALSE;
00186
00187
00188 else if (cpu_time->timer_state == CPU_TIMER_STATE_REACHED)
00189 return TRUE;
00190
00191
00192 else if (cpu_time->delay_start_cycle > cpu_time->delay_end_cycle)
00193 {
00194 if (current_cycle_count < cpu_time->delay_start_cycle && current_cycle_count > cpu_time->delay_end_cycle)
00195 {
00196 cpu_time->timer_state = CPU_TIMER_STATE_REACHED;
00197 return TRUE;
00198 }
00199 return FALSE;
00200 }
00201 else
00202 {
00203 if (current_cycle_count < cpu_time->delay_start_cycle || current_cycle_count > cpu_time->delay_end_cycle)
00204 {
00205 cpu_time->timer_state = CPU_TIMER_STATE_REACHED;
00206 return TRUE;
00207 }
00208 return FALSE;
00209 }
00210 }
00211
00212
00224 #if (defined __GNUC__)
00225 __attribute__((__always_inline__))
00226 #endif
00227 extern __inline__ void cpu_stop_timeout(t_cpu_time *cpu_time)
00228 {
00229 cpu_time->timer_state = CPU_TIMER_STATE_STOPPED;
00230 }
00231
00232
00240 #if (defined __GNUC__)
00241 __attribute__((__always_inline__))
00242 #endif
00243 extern __inline__ unsigned long cpu_is_timer_stopped(t_cpu_time *cpu_time)
00244 {
00245
00246 if( cpu_time->timer_state==CPU_TIMER_STATE_STOPPED )
00247 return TRUE;
00248 else
00249 return FALSE;
00250 }
00251
00252
00259 #if (defined __GNUC__)
00260 __attribute__((__always_inline__))
00261 #endif
00262 extern __inline__ void cpu_delay_ms(unsigned long delay, unsigned long fcpu_hz)
00263 {
00264 t_cpu_time timer;
00265 cpu_set_timeout( cpu_ms_2_cy(delay, fcpu_hz), &timer);
00266 while( !cpu_is_timeout(&timer) );
00267 }
00268
00275 #if (defined __GNUC__)
00276 __attribute__((__always_inline__))
00277 #endif
00278 extern __inline__ void cpu_delay_us(unsigned long delay, unsigned long fcpu_hz)
00279 {
00280 t_cpu_time timer;
00281 cpu_set_timeout( cpu_us_2_cy(delay, fcpu_hz), &timer);
00282 while( !cpu_is_timeout(&timer) );
00283 }
00284
00290 #if (defined __GNUC__)
00291 __attribute__((__always_inline__))
00292 #endif
00293 extern __inline__ void cpu_delay_cy(unsigned long delay)
00294 {
00295 t_cpu_time timer;
00296 cpu_set_timeout( delay, &timer);
00297 while( !cpu_is_timeout(&timer) );
00298 }
00299
00300
00301 #define Get_sys_count() ( Get_system_register(AVR32_COUNT) )
00302 #define Set_sys_count(x) ( Set_system_register(AVR32_COUNT, (x)) )
00303 #define Get_sys_compare() ( Get_system_register(AVR32_COMPARE) )
00304 #define Set_sys_compare(x) ( Set_system_register(AVR32_COMPARE, (x)) )
00305
00306
00307 #endif // _CYCLE_COUNTER_H_