# cycle_counter.h File Reference

## Detailed Description

Cycle counter driver.

- Compiler: IAR EWAVR32 and GNU GCC for AVR32
- Supported devices: All AVR32UC devices.
- AppNote:

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() and cpu_is_timeout() functions. More...
## Defines
#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, unsigned long fcpu_hz)
| 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__ unsigned long | cpu_is_timeout (t_cpu_time *cpu_time)
| Test if a timer variable reached its timeout.
__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.

## Define Documentation

#define Get_sys_compare | ( |  | ) | ( Get_system_register(AVR32_COMPARE) )

Definition at line 216 of file cycle_counter.h.

Referenced by compare_irq_handler(), and main().

#define Get_sys_count | ( |  | ) | ( Get_system_register(AVR32_COUNT) )

Definition at line 214 of file cycle_counter.h.

Referenced by main().

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

Definition at line 217 of file cycle_counter.h.

Referenced by compare_irq_handler(), and main().

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

Definition at line 215 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 89 of file cycle_counter.h.

    00090 {
    00091   return ((unsigned long long)cy * 1000 + fcpu_hz-1) / fcpu_hz;
    00092 }

__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 106 of file cycle_counter.h.

    00107 {
    00108   return ((unsigned long long)cy * 1000000 + fcpu_hz-1) / fcpu_hz;
    00109 }

__inline__ void cpu_delay_cy | ( | unsigned long | delay,
|  | unsigned long | fcpu_hz |
| ) |  |  |

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

Parameters:

  | delay,: | Number of CPU cycles to wait.
  | fcpu_hz,: | CPU frequency in Hz.

Definition at line 206 of file cycle_counter.h.

References cpu_is_timeout(), and cpu_set_timeout().

    00207 {
    00208   t_cpu_time timer;
    00209   cpu_set_timeout( delay, &timer);
    00210   while( !cpu_is_timeout(&timer) );
    00211 }

__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 189 of file cycle_counter.h.

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

    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 }

__inline__ unsigned long cpu_is_timeout | ( | t_cpu_time * | cpu_time | ) |

Test if a timer variable reached its timeout.

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

Parameters:

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

Returns:
  TRUE if timeout occured, otherwise FALSE.

Definition at line 156 of file cycle_counter.h.

References t_cpu_time::time, and t_cpu_time::wrap.

Referenced by cpu_delay_cy(), and cpu_delay_ms().

    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 }

__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 72 of file cycle_counter.h.

Referenced by cpu_delay_ms().

    00073 {
    00074   return ((unsigned long long)ms * fcpu_hz + 999) / 1000;
    00075 }

__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) ) ../..

Parameters:

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

Definition at line 126 of file cycle_counter.h.

References t_cpu_time::time, and t_cpu_time::wrap.

Referenced by cpu_delay_cy(), and cpu_delay_ms().

    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 }