cycle_counter.h File Reference


Detailed Description

Cycle counter driver.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file cycle_counter.h.

#include "compiler.h"

Go to the source code of this file.

Data Structures

struct  t_cpu_time
 Structure holding private information, automatically initialized by the cpu_set_timeout() function. More...

Defines

#define CPU_TIMER_STATE_REACHED   1
#define CPU_TIMER_STATE_STARTED   0
#define CPU_TIMER_STATE_STOPPED   2
#define Get_sys_compare()   ( Get_system_register(AVR32_COMPARE) )
#define Get_sys_count()   ( Get_system_register(AVR32_COUNT) )
#define Set_sys_compare(x)   ( Set_system_register(AVR32_COMPARE, (x)) )
#define Set_sys_count(x)   ( Set_system_register(AVR32_COUNT, (x)) )

Functions

__inline__ U32 cpu_cy_2_ms (unsigned long cy, unsigned long fcpu_hz)
 Convert CPU cycles into milli-seconds.
__inline__ U32 cpu_cy_2_us (unsigned long cy, unsigned long fcpu_hz)
 Convert CPU cycles into micro-seconds.
__inline__ void cpu_delay_cy (unsigned long delay)
 Waits during at least the specified delay (in CPU cycles) before returning.
__inline__ void cpu_delay_ms (unsigned long delay, unsigned long fcpu_hz)
 Waits during at least the specified delay (in millisecond) before returning.
__inline__ void cpu_delay_us (unsigned long delay, unsigned long fcpu_hz)
 Waits during at least the specified delay (in microsecond) before returning.
__inline__ unsigned long cpu_is_timeout (t_cpu_time *cpu_time)
 Test if a timer variable reached its timeout.
__inline__ unsigned long cpu_is_timer_stopped (t_cpu_time *cpu_time)
 Test if a timer is stopped.
__inline__ U32 cpu_ms_2_cy (unsigned long ms, unsigned long fcpu_hz)
 Convert milli-seconds into CPU cycles.
__inline__ void cpu_set_timeout (unsigned long delay, t_cpu_time *cpu_time)
 Set a timer variable.
__inline__ void cpu_stop_timeout (t_cpu_time *cpu_time)
 Stop a timeout detection.
__inline__ U32 cpu_us_2_cy (unsigned long us, unsigned long fcpu_hz)
 Convert micro-seconds into CPU cycles.


Define Documentation

#define CPU_TIMER_STATE_REACHED   1

Definition at line 65 of file cycle_counter.h.

Referenced by cpu_is_timeout().

#define CPU_TIMER_STATE_STARTED   0

Definition at line 64 of file cycle_counter.h.

Referenced by cpu_set_timeout().

#define CPU_TIMER_STATE_STOPPED   2

Definition at line 66 of file cycle_counter.h.

Referenced by cpu_is_timeout(), cpu_is_timer_stopped(), and cpu_stop_timeout().

 
#define Get_sys_compare (  )     ( Get_system_register(AVR32_COMPARE) )

Definition at line 303 of file cycle_counter.h.

Referenced by main().

 
#define Get_sys_count (  )     ( Get_system_register(AVR32_COUNT) )

Definition at line 301 of file cycle_counter.h.

Referenced by main().

#define Set_sys_compare (  )     ( Set_system_register(AVR32_COMPARE, (x)) )

Definition at line 304 of file cycle_counter.h.

Referenced by compare_irq_handler(), and main().

#define Set_sys_count (  )     ( Set_system_register(AVR32_COUNT, (x)) )

Definition at line 302 of file cycle_counter.h.


Function Documentation

__inline__ U32 cpu_cy_2_ms ( unsigned long  cy,
unsigned long  fcpu_hz 
)

Convert CPU cycles into milli-seconds.

Parameters:
cy,: Number of CPU cycles.
fcpu_hz,: CPU frequency in Hz.
Returns:
the converted number of milli-second.

Definition at line 115 of file cycle_counter.h.

00116 {
00117   return ((unsigned long long)cy * 1000 + fcpu_hz-1) / fcpu_hz;
00118 }

__inline__ U32 cpu_cy_2_us ( unsigned long  cy,
unsigned long  fcpu_hz 
)

Convert CPU cycles into micro-seconds.

Parameters:
cy,: Number of CPU cycles.
fcpu_hz,: CPU frequency in Hz.
Returns:
the converted number of micro-second.

Definition at line 132 of file cycle_counter.h.

00133 {
00134   return ((unsigned long long)cy * 1000000 + fcpu_hz-1) / fcpu_hz;
00135 }

__inline__ void cpu_delay_cy ( unsigned long  delay  ) 

Waits during at least the specified delay (in CPU cycles) before returning.

Parameters:
delay,: Number of CPU cycles to wait.

Definition at line 293 of file cycle_counter.h.

References cpu_is_timeout(), and cpu_set_timeout().

00294 {
00295   t_cpu_time timer;
00296   cpu_set_timeout( delay, &timer);
00297   while( !cpu_is_timeout(&timer) );
00298 }

__inline__ void cpu_delay_ms ( unsigned long  delay,
unsigned long  fcpu_hz 
)

Waits during at least the specified delay (in millisecond) before returning.

Parameters:
delay,: Number of millisecond to wait.
fcpu_hz,: CPU frequency in Hz.

Definition at line 262 of file cycle_counter.h.

References cpu_is_timeout(), cpu_ms_2_cy(), and cpu_set_timeout().

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 }

__inline__ void cpu_delay_us ( unsigned long  delay,
unsigned long  fcpu_hz 
)

Waits during at least the specified delay (in microsecond) before returning.

Parameters:
delay,: Number of microsecond to wait.
fcpu_hz,: CPU frequency in Hz.

Definition at line 278 of file cycle_counter.h.

References cpu_is_timeout(), cpu_set_timeout(), and cpu_us_2_cy().

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 }

__inline__ unsigned long cpu_is_timeout ( t_cpu_time cpu_time  ) 

Test if a timer variable reached its timeout.

Once the timeout is reached, the function will always return TRUE, until the cpu_stop_timeout() function is called.

Ex: t_cpu_time timer; cpu_set_timeout( 10, FOSC0, &timer ); // timeout in 10 ms if( cpu_is_timeout(&timer) ) cpu_stop_timeout(&timer); ../..

Parameters:
cpu_time,: (input) internal information used by the timer API.
Returns:
TRUE if timeout occured, otherwise FALSE.

Definition at line 180 of file cycle_counter.h.

References CPU_TIMER_STATE_REACHED, CPU_TIMER_STATE_STOPPED, t_cpu_time::delay_end_cycle, t_cpu_time::delay_start_cycle, and t_cpu_time::timer_state.

Referenced by cpu_delay_cy(), cpu_delay_ms(), and cpu_delay_us().

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   // Test if the timeout as already occured.
00188   else if (cpu_time->timer_state == CPU_TIMER_STATE_REACHED)
00189     return TRUE;
00190 
00191   // If the ending cycle count of this timeout is wrapped, ...
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 }

__inline__ unsigned long cpu_is_timer_stopped ( t_cpu_time cpu_time  ) 

Test if a timer is stopped.

Parameters:
cpu_time,: (input) internal information used by the timer API.
Returns:
TRUE if timer is stopped, otherwise FALSE.

Definition at line 243 of file cycle_counter.h.

References CPU_TIMER_STATE_STOPPED, and t_cpu_time::timer_state.

00244 {
00245 
00246   if( cpu_time->timer_state==CPU_TIMER_STATE_STOPPED )
00247     return TRUE;
00248   else
00249     return FALSE;
00250 }

__inline__ U32 cpu_ms_2_cy ( unsigned long  ms,
unsigned long  fcpu_hz 
)

Convert milli-seconds into CPU cycles.

Parameters:
ms,: Number of millisecond.
fcpu_hz,: CPU frequency in Hz.
Returns:
the converted number of CPU cycles.

Definition at line 81 of file cycle_counter.h.

Referenced by cpu_delay_ms().

00082 {
00083   return ((unsigned long long)ms * fcpu_hz + 999) / 1000;
00084 }

__inline__ void cpu_set_timeout ( unsigned long  delay,
t_cpu_time cpu_time 
)

Set a timer variable.

Ex: t_cpu_time timer; cpu_set_timeout( cpu_ms_2_cy(10, FOSC0), &timer ); // timeout in 10 ms if( cpu_is_timeout(&timer) ) cpu_stop_timeout(&timer); ../..

Parameters:
delay,: (input) delay in CPU cycles before timeout.
cpu_time,: (output) internal information used by the timer API.

Definition at line 153 of file cycle_counter.h.

References CPU_TIMER_STATE_STARTED, t_cpu_time::delay_end_cycle, t_cpu_time::delay_start_cycle, and t_cpu_time::timer_state.

Referenced by cpu_delay_cy(), cpu_delay_ms(), and cpu_delay_us().

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 }

__inline__ void cpu_stop_timeout ( t_cpu_time cpu_time  ) 

Stop a timeout detection.

Ex: t_cpu_time timer; cpu_set_timeout( 10, FOSC0, &timer ); // timeout in 10 ms if( cpu_is_timeout(&timer) ) cpu_stop_timeout(&timer); ../..

Parameters:
cpu_time,: (input) internal information used by the timer API.

Definition at line 227 of file cycle_counter.h.

References CPU_TIMER_STATE_STOPPED, and t_cpu_time::timer_state.

00228 {
00229   cpu_time->timer_state = CPU_TIMER_STATE_STOPPED;
00230 }

__inline__ U32 cpu_us_2_cy ( unsigned long  us,
unsigned long  fcpu_hz 
)

Convert micro-seconds into CPU cycles.

Parameters:
us,: Number of microsecond.
fcpu_hz,: CPU frequency in Hz.
Returns:
the converted number of CPU cycles.

Definition at line 98 of file cycle_counter.h.

Referenced by cpu_delay_us().

00099 {
00100   return ((unsigned long long)us * fcpu_hz + 999999) / 1000000;
00101 }


Generated on Fri Feb 19 02:24:24 2010 for AVR32 - Cycle Counter Driver by  doxygen 1.5.5