High Resolution Timer

High Resolution Timer Platform interface

HR Timer Overview

High-resolution timers operate using high-frequency clock signals derived from system clocks or dedicated oscillators. The timer counts clock cycles to measure time or trigger actions, with its precision determined by the clock frequency.

Application Interface

Users can directly call the API’s though most of them will be called by device drivers.

The below table captures the functions that are to be called from the application layer.

API Functions

Function

Description

rs_hr_timer_open

To open the hr timer interface.

rs_hr_timer_start

To start the timer for the given channel

rs_hr_timer_stop

To stops the timer for the given channel

rs_hr_timer_get_count

To get count from hr timer.

rs_hr_timer_close

To close the timer unit

Error Code

  • Every API’s for the HR Timer returns some success or failure values. Please refer below section,

Documentation from the relevant header as follows:

Defines

RS_HR_TIMER_TRIGGER_MODE_CONTINUOUS

Set Trigger Mode Increment continuously

RS_HR_TIMER_TRIGGER_MODE_EXTERNAL

Increment via external event.

RS_HR_TIMER_OPERATION_MODE_INPUT_CAPTURE

Operation mode Capture input events

RS_HR_TIMER_OPERATION_MODE_OUTPUT_COMPARE

Use for output compare.

RS_HR_TIMER_OPERATION_MODE_EDGE_PWM

Generate Edge aligned PWM.

RS_HR_TIMER_OPERATION_MODE_CENTER_PWM

Generate Center aligned PWM.

RS_HR_TIMER_OPERATION_MODE_ONE_PULSE

One pulse mode.

RS_HR_TIMER_EVENT_TYPE_COUNTER_OVERFLOW

Event Type On Counter Overflow

RS_HR_TIMER_EVENT_TYPE_COUNTER_UNDERFLOW

On Counter Underflow.

RS_HR_TIMER_EVENT_TYPE_INPUT_CAPTURE

When an input event happened.

RS_HR_TIMER_WIDTH_8_BIT

Timer resolution 8 bit resolution

RS_HR_TIMER_WIDTH_16_BIT

16 bit resolution

RS_HR_TIMER_WIDTH_24_BIT

24 bit resolution

RS_HR_TIMER_WIDTH_32_BIT

32 bit resolution

RS_HR_TIMER_WIDTH_64_BIT

64 bit resolution

RS_HR_TIMER_NS_RESOLUTION

Counter Resolution Set counter resolution as nano seconds

RS_HR_TIMER_US_RESOLUTION

Set counter resolution as micro seconds.

RS_HR_TIMER_MS_RESOLUTION

Set counter resolution as milli seconds.

RS_HR_TIMER_S_RESOLUTION

Set counter resolution as seconds.

Typedefs

typedef struct tag_rs_hr_timer_config rs_hr_timer_config_t

Structure for setting up each Timer Channel configuration.

typedef struct tag_rs_hr_timer_instance rs_hr_timer_instance_t

Structure for high resolution timer instance.

Functions

rs_handle_t rs_hr_timer_open(rs_hr_timer_instance_t *ptr_hr_timer_instance, rs_hr_timer_config_t *ptr_hr_timer_config)

Function Declarations.

Configures the Timer Unit

This function configures/reconfigures the Timer Unit as per the given configuration

Parameters:
  • ptr_hr_timer_instance[in] - Pointer to the instance structure

  • ptr_hr_timer_config[in] - Pointer to the configuration structure

Returns:

0 on success or error code on failure

rs_handle_t rs_hr_timer_set_config(rs_hr_timer_config_t *ptr_hr_timer_config, uint8_t u8_unit_index, uint8_t u8_resolution, uint8_t u8_width, uint32_t u32_frequency)

Configures the high-resolution timer.

This function sets the configuration parameters for the high-resolution timer

Parameters:
  • ptr_hr_timer_config[in] - Pointer to a structure that holds the high-resolution timer configuration.

  • u8_unit_index[in] - The index of the timer unit to configure.

  • u8_resolution[in] - he resolution to set for the timer (e.g., 8-bit, 16-bit, etc.).

  • u8_width[in] - The width (size) of the timer in bits

  • u32_frequency[in] - The frequency of the timer in Hz.

Returns:

0 on success or error code on failure

rs_ret_val_t rs_hr_timer_start(rs_handle_t handle)
rs_ret_val_t rs_hr_timer_stop(rs_handle_t handle)

Stops the timer for the given channel.

This function stops the timer operation for the given channel

Parameters:

handle[in] - Handle to the Timer unit

Returns:

0 on success or error code on failure

rs_ret_val_t rs_hr_timer_get_count(rs_handle_t handle, void *ptr_value)

Get count from hr timer.

This function get count from hr timer.

Parameters:
  • handle[in] - Handle to the Timer Unit.

  • ptr_value[out] - pointer to the counter.

Returns:

None.

rs_ret_val_t rs_hr_timer_close(rs_handle_t handle)

Closes the timer unit.

This function un-initializes the Timer Unit and releases the handle

Parameters:

handle[in] - Handle to the Timer Unit

Returns:

0 on success or error code on failure

struct tag_rs_hr_timer_config
#include <rs_hr_timer.h>

Structure for setting up each Timer Channel configuration.

Public Members

uint8_t unit_index

Index to Timer unit as defined by underlying HAL layer.

uint8_t resolution

Resolution of the counter - us, ns.

uint8_t width

Width.

uint8_t reserved

Reserved.

uint32_t frequency

Timer Frequency.

struct tag_rs_hr_timer_instance
#include <rs_hr_timer.h>

Structure for high resolution timer instance.

Public Members

rs_hr_timer_config_t *ptr_hr_timer_config

Pointer to the HR timer configuration.

uint32_t u32_hr_timer_counter

HR Timer counter.