Microchip® Advanced Software Framework

cycle_counter.h File Reference

ARM functions for busy-wait delay loops.

Copyright (c) 2013-2018 Microchip Technology Inc. and its subsidiaries.

#include <compiler.h>
#include <clock.h>

Macros

#define F_CPU   system_gclk_gen_get_hz(0)
 MCU Clock Frequency (Hertz) More...
 

Convenience functions for busy-wait delay loops

#define cpu_ms_2_cy(ms, f_cpu)   (((uint64_t)(ms) * (f_cpu) + (uint64_t)(7e3-1ul)) / (uint64_t)7e3)
 Convert milli-seconds into CPU cycles. More...
 
#define cpu_us_2_cy(us, f_cpu)   (((uint64_t)(us) * (f_cpu) + (uint64_t)(7e6-1ul)) / (uint64_t)7e6)
 Convert micro-seconds into CPU cycles. More...
 
#define delay_cycles   portable_delay_cycles
 Delay program execution for a specified number of CPU cycles. More...
 
#define cpu_delay_s(delay)   delay_cycles(cpu_ms_2_cy(1000 * delay, F_CPU))
 
#define cpu_delay_ms(delay)   delay_cycles(cpu_ms_2_cy(delay, F_CPU))
 Delay program execution for a specified number of milliseconds. More...
 
#define cpu_delay_us(delay)   delay_cycles(cpu_us_2_cy(delay, F_CPU))
 Delay program execution for a specified number of microseconds. More...
 
void portable_delay_cycles (unsigned long n)
 Delay loop to delay n number of cycles. More...
 

#define cpu_delay_ms (   delay)    delay_cycles(cpu_ms_2_cy(delay, F_CPU))

Delay program execution for a specified number of milliseconds.

Parameters
delaynumber of milliseconds to wait
f_cpuCPU frequency in Hertz
#define cpu_delay_s (   delay)    delay_cycles(cpu_ms_2_cy(1000 * delay, F_CPU))
#define cpu_delay_us (   delay)    delay_cycles(cpu_us_2_cy(delay, F_CPU))

Delay program execution for a specified number of microseconds.

Parameters
delaynumber of microseconds to wait
f_cpuCPU frequency in Hertz
#define cpu_ms_2_cy (   ms,
  f_cpu 
)    (((uint64_t)(ms) * (f_cpu) + (uint64_t)(7e3-1ul)) / (uint64_t)7e3)

Convert milli-seconds into CPU cycles.

Parameters
msnumber of milliseconds
f_cpuCPU frequency in Hertz
Returns
the converted number of CPU cycles
#define cpu_us_2_cy (   us,
  f_cpu 
)    (((uint64_t)(us) * (f_cpu) + (uint64_t)(7e6-1ul)) / (uint64_t)7e6)

Convert micro-seconds into CPU cycles.

Parameters
msnumber of microseconds
f_cpuCPU frequency in Hertz
Returns
the converted number of CPU cycles
#define delay_cycles   portable_delay_cycles

Delay program execution for a specified number of CPU cycles.

Parameters
nnumber of CPU cycles to wait
#define F_CPU   system_gclk_gen_get_hz(0)

MCU Clock Frequency (Hertz)

void portable_delay_cycles ( unsigned long  n)

Delay loop to delay n number of cycles.

Note
The function runs in internal RAM so that flash wait states will not affect the delay time.
Parameters
nNumber of cycles

References UNUSED.