CPU Time

The MyNewt cputime module provides high resolution time and timer support.

Description

The cputime API provides high resolution time and timer support. The module must be initialized, using the os_cputime_init() function, with the clock frequency to use. The module uses the hal_timer API, defined in hal/hal_timer.h, to access the hardware timers. It uses the hardware timer number specified by the OS_CPUTIME_TIMER_NUM system configuration setting.

API

int os_cputime_init(uint32_t clock_freq)

Initialize the cputime module.

This must be called after os_init is called and before any other timer API are used. This should be called only once and should be called before the hardware timer is used.

Return

int 0 on success; -1 on error.

Parameters
  • clock_freq: The desired cputime frequency, in hertz (Hz).

uint32_t os_cputime_get32(void)

Returns the low 32 bits of cputime.

Return

uint32_t The lower 32 bits of cputime

uint32_t os_cputime_nsecs_to_ticks(uint32_t nsecs)

Converts the given number of nanoseconds into cputime ticks.

Not defined if OS_CPUTIME_FREQ_PWR2 is defined.

Return

uint32_t The number of ticks corresponding to ‘nsecs’

Parameters
  • usecs: The number of nanoseconds to convert to ticks

uint32_t os_cputime_ticks_to_nsecs(uint32_t ticks)

Convert the given number of ticks into nanoseconds.

Not defined if OS_CPUTIME_FREQ_PWR2 is defined.

Return

uint32_t The number of nanoseconds corresponding to ‘ticks’

Parameters
  • ticks: The number of ticks to convert to nanoseconds.

void os_cputime_delay_nsecs(uint32_t nsecs)

Wait until ‘nsecs’ nanoseconds has elapsed.

This is a blocking delay. Not defined if OS_CPUTIME_FREQ_PWR2 is defined.

Parameters
  • nsecs: The number of nanoseconds to wait.

uint32_t os_cputime_usecs_to_ticks(uint32_t usecs)

Converts the given number of microseconds into cputime ticks.

Return

uint32_t The number of ticks corresponding to ‘usecs’

Parameters
  • usecs: The number of microseconds to convert to ticks

uint32_t os_cputime_ticks_to_usecs(uint32_t ticks)

Convert the given number of ticks into microseconds.

Return

uint32_t The number of microseconds corresponding to ‘ticks’

Parameters
  • ticks: The number of ticks to convert to microseconds.

void os_cputime_delay_ticks(uint32_t ticks)

Wait until the number of ticks has elapsed.

This is a blocking delay.

Parameters
  • ticks: The number of ticks to wait.

void os_cputime_delay_usecs(uint32_t usecs)

Wait until ‘usecs’ microseconds has elapsed.

This is a blocking delay.

Parameters
  • usecs: The number of usecs to wait.

void os_cputime_timer_init(struct hal_timer *timer, hal_timer_cb fp, void *arg)

Initialize a CPU timer, using the given HAL timer.

Parameters
  • timer: The timer to initialize. Cannot be NULL.

  • fp: The timer callback function. Cannot be NULL.

  • arg: Pointer to data object to pass to timer.

int os_cputime_timer_start(struct hal_timer *timer, uint32_t cputime)

Start a cputimer that will expire at ‘cputime’.

If cputime has already passed, the timer callback will still be called (at interrupt context).

NOTE: This must be called when the timer is stopped.

Return

int 0 on success; EINVAL if timer already started or timer struct invalid

Parameters
  • timer: Pointer to timer to start. Cannot be NULL.

  • cputime: The cputime at which the timer should expire.

int os_cputime_timer_relative(struct hal_timer *timer, uint32_t usecs)

Sets a cpu timer that will expire ‘usecs’ microseconds from the current cputime.

NOTE: This must be called when the timer is stopped.

Return

int 0 on success; EINVAL if timer already started or timer struct invalid

Parameters
  • timer: Pointer to timer. Cannot be NULL.

  • usecs: The number of usecs from now at which the timer will expire.

void os_cputime_timer_stop(struct hal_timer *timer)

Stops a cputimer from running.

The timer is removed from the timer queue and interrupts are disabled if no timers are left on the queue. Can be called even if timer is not running.

Parameters
  • timer: Pointer to cputimer to stop. Cannot be NULL.

CPUTIME_LT(__t1, __t2)

evaluates to true if t1 is before t2 in time

CPUTIME_GT(__t1, __t2)

evaluates to true if t1 is after t2 in time

CPUTIME_GEQ(__t1, __t2)

evaluates to true if t1 is after t2 in time

CPUTIME_LEQ(__t1, __t2)

evaluates to true if t1 is on or after t2 in time