Timer Interface

Timer Platform interface

Timer Overview

A timer is a critical component in microcontrollers and embedded systems, used for timekeeping, event scheduling, and generating precise delays or waveforms. Timers operate by counting clock cycles from the microcontroller’s clock source, allowing developers to execute tasks or generate signals at specified intervals.

Timer Operation Modes

The supportted operation modes, trigger sources as supported by the underlying timer.

Timer Operation Modes

Mode

Description

RS_TIMER_OPERATION_MODE_INPUT_CAPTURE

Timer is configured for Input Capture operation

RS_TIMER_OPERATION_MODE_OUTPUT_COMPARE

Timer is configured for Output compare operation

RS_TIMER_OPERATION_MODE_EDGE_PWM

Timer is used to generate edge aligned PWM

RS_TIMER_OPERATION_MODE_CENTER_PWM

Timer is used to generate center aligned PWM

RS_TIMER_OPERATION_MODE_ONE_PULSE

Timer is used to generate single pulse

Timer Trigger Modes

Mode

Description

RS_TIMER_TRIGGER_MODE_CONTINUOUS

Increment continuously

RS_TIMER_TRIGGER_MODE_EXTERNAL

Increment via external event

Timer Event Type

Mode

Description

RS_TIMER_EVENT_TYPE_COUNTER_OVERFLOW

On counter overflow

RS_TIMER_EVENT_TYPE_COUNTER_UNDERFLOW

On counter underflow

RS_TIMER_EVENT_TYPE_INPUT_CAPTURE

Input event happened

Application Interface

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

API Functions

Function

Description

rs_timer_init

To open timer unit

rs_timer_configure_channel

To configures the timer channel

rs_timer_start_channel

To starts the timer for the given channel

rs_timer_stop_channel

To stops the timer for the given channel

rs_timer_register_event_callback

To registers the given callback function for event

rs_timer_deinit

To close the timer unit

Error Code

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

Implementation Guide

  • Initialize the timer unit

  • Configure the timer channel under the unit

  • Register the callback function

  • Start the timer channel

  • Callback gets called.

  • Stop the timer once work is done

  • Un-Initialize the timer unit

Documentation from the relevant header as follows:

Defines

RS_TIMER_TRIGGER_MODE_CONTINUOUS

Increment continously

RS_TIMER_TRIGGER_MODE_EXTERNAL

Increment via external event

RS_TIMER_OPERATION_MODE_INPUT_CAPTURE

Capture input events

RS_TIMER_OPERATION_MODE_OUTPUT_COMPARE

Use for output compare

RS_TIMER_OPERATION_MODE_EDGE_PWM

Generate Edge aligned PWM

RS_TIMER_OPERATION_MODE_CENTER_PWM

Generate Center aligned PWM

RS_TIMER_OPERATION_MODE_ONE_PULSE

One pulse mode

RS_TIMER_EVENT_TYPE_COUNTER_OVERFLOW

On Counter Overflow

RS_TIMER_EVENT_TYPE_COUNTER_UNDERFLOW

On Counter Underflow

RS_TIMER_EVENT_TYPE_INPUT_CAPTURE

When an input event happened

RS_TIMER_WIDTH_8_BIT

8 bit resolution

RS_TIMER_WIDTH_16_BIT

16 bit resolution

RS_TIMER_WIDTH_24_BIT

24 bit resolution

RS_TIMER_WIDTH_32_BIT

32 bit resolution

RS_TIMER_WIDTH_64_BIT

64 bit resolution

Typedefs

typedef struct __rs_timer_channel_config rs_timer_channel_config_t

Structure for setting up each Timer Channel configuration

typedef struct __rs_timer_config rs_timer_config_t

Structure for setting up each Timer Unit configuration

typedef void (*rs_timer_callback_event)(rs_handle_t handle, rs_ret_val_t event, uint32_t u32_channel, void *ptr_arg)

Callback function format.

This callback function is called on an event

Param handle:

[in] - Handle to the Timer unit

Param event:

[in] - Event that initiated the callback or error code

Param u32_channel:

[in] - Channel for which the event is for

Param ptr_arg:

[in] - Argument as provided during register callback

Return:

None

Functions

rs_handle_t rs_timer_init(rs_timer_config_t *ptr_timer_config)
rs_ret_val_t rs_timer_configure_channel(rs_handle_t handle, rs_timer_channel_config_t *ptr_config)

Configures the Timer channel.

This function configures/reconfigures the channel as per the given configuration

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

  • ptr_config[in] - Pointer to the configuration structure

Returns:

0 on success or error code on failure

rs_ret_val_t rs_timer_start_channel(rs_handle_t handle, uint32_t u32_channel)

Starts the timer for the given channel.

This function intitiates the timer operation for the given Timer channel

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

  • u32_channel[in] - Channel number

Returns:

0 on success or error code on failure

rs_ret_val_t rs_timer_stop_channel(rs_handle_t handle, uint32_t u32_channel)

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

  • u32_channel[in] - Channel number

Returns:

0 on success or error code on failure

rs_ret_val_t rs_timer_register_event_callback(rs_handle_t handle, rs_timer_callback_event ptr_func, void *ptr_arg)
rs_ret_val_t rs_timer_deinit(rs_handle_t handle)

Uninitializes 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 __rs_timer_channel_config
#include <rs_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 channel

Channel number inside the unit, Starting from 0 index

uint8_t group

Grouping of channel, if supported

uint8_t trigger

Trigger source

uint8_t oper_mode

Operation mode - One-shot, continuous etc

uint8_t ext_event_id

Source of external event as defined by HAL

uint8_t event_type

Event up on which call back to be called

uint8_t u8_counter

8 bit Counter value

uint16_t u16_counter

16 bit Counter value

uint32_t u32_counter

32 bit Counter value

uint64_t u64_counter

32 bit Counter value

union __rs_timer_channel_config count_val
struct __rs_timer_config
#include <rs_timer.h>

Structure for setting up each Timer Unit configuration

Public Members

uint8_t unit

Index to Timer Unit

uint8_t oper_mode

Operation mode - One-shot, continuous etc

uint8_t width

Counter width in bits

uint8_t clock_source

Data alignement

uint32_t set_freq

Frequency set to

uint32_t actual_freq

Frequency actually working