Skip to main content

7.5.16 TIMER User Guide

Hardware Support

RDK S100 currently has 6 Instances. Each Instance has 4 Channels with 32-bit counters. Each Channel is a timer that can be used for timing and other functions.

RDK S600 currently has 10 Instances. Each Instance has 4 Channels with 32-bit counters. Each Channel is a timer that can be used for timing and other functions.

Timer configuration is defined in the following macros:

// Path: mcu/McalCdd/Gpt/inc/Gpt_Lld.h

/* Number of instances */
#if ((SOC_TYPE_S100 == SOC_TYPE) || (SOC_TYPE_S100P == SOC_TYPE))
#define GPT_LLD_INSTANCE_NUM (6U)
#endif
#if ((SOC_TYPE == SOC_TYPE_S600) || (SOC_TYPE == SOC_TYPE_S300))
#define GPT_LLD_INSTANCE_NUM (10U)
#endif

/* Number of channels per instance */
#define GPT_LLD_CHANNEL_NUM_PER_INSTANCE (4U)

/* Size of each channel */
#define GPT_LLD_CHANNEL_SIZE (0x14U)

/* Size of each instance */
#define GPT_LLD_INSTANCE_SIZE (0x10000U)

/* Register base address of each instance */
#if ((SOC_TYPE_S100 == SOC_TYPE) || (SOC_TYPE_S100P == SOC_TYPE))
#define GPT_LLD_BASE_OFFSET(n) ((0X22310000U) + (GPT_LLD_INSTANCE_SIZE) * (n))
#endif
#if ((SOC_TYPE == SOC_TYPE_S600) || (SOC_TYPE == SOC_TYPE_S300))
#define GPT_LLD_BASE_OFFSET(n) ((0X23570000U) + (GPT_LLD_INSTANCE_SIZE) * (n))
#endif

/* Start address of each channel */
#define GPT_LLD_CHANNEL_OFFEST(n, m) ((GPT_LLD_BASE_OFFSET(n)) + (GPT_LLD_CHANNEL_SIZE) * (m))

Each Channel of the timer can trigger an interrupt. The interrupt numbers for each Channel are shown below:

ISR NameHardware Interrupt Vector
Gpt_Ins0Ch0Isr79
Gpt_Ins0Ch1Isr80
Gpt_Ins0Ch2Isr81
Gpt_Ins0Ch3Isr82
Gpt_Ins1Ch0Isr83
Gpt_Ins1Ch1Isr84
Gpt_Ins1Ch2Isr85
Gpt_Ins1Ch3Isr86
Gpt_Ins2Ch0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• McalCdd\Gpt\src\Gpt_Lld.c
  • McalCdd\Gpt\inc\Gpt_Lld.h
  • McalCdd\Gpt\inc\Gpt_Types.h
  • McalCdd\Gpt\inc\Gpt.h
  • McalCdd\Gpt\src\Gpt.c
  • Config\McalCdd\gen_xxx\Gpt\inc\Gpt_Cfg.h
  • Config\McalCdd\gen_xxx\Gpt\inc\Gpt_PBcfg.h
  • Config\McalCdd\gen_xxx\Gpt\src\Gpt_PBcfg.c

Application Sample

The sample program uses Timer2 Channel2 by default. The timer input clock on S100 is configured to 200 MHz by default.

The sample program uses Timer2 Channel2 by default. The timer input clock on S600 is configured to 40 MHz by default.

The timer supports down-counting only and does not support hardware compare-match functionality. It works in Continuous mode by default, meaning the count value is automatically reloaded each time. For One-Shot behavior, stop the Timer in the interrupt handler and reconfigure the count value as needed. The maximum supported count value is 2^width - 1, where width is the timer bit width. The timer supports 8-, 16-, and 32-bit widths; 32-bit is used by default.

Configuration

  1. The sample code runs on MCU1. Configure the Gpt_ConfigType structure first for module initialization:
Config/McalCdd/gen_xxx/Gpt/src/Gpt_PBcfg.c

Related structure definitions:

/* Channel configuration structure */
typedef struct
{
Gpt_ChannelType u8ChannelTotalNum; /* Total number of channels */
Gpt_PerChanneInfoType (*pChannelCfg)[]; /* Channel configuration array */
const uint8 (*pChannelId2IdxMap)[]; /* Channel ID to array index mapping (starting from 0) */
} Gpt_ConfigType;

/* Channel configuration type */
typedef struct
{
Gpt_NotificationType pfChNotification; /* Callback function pointer */
Gpt_ValueType u32ChMaxTickVal; /* Maximum tick value supported by the channel */
Gpt_ChannelModeType eChMode; /* Channel mode (CONTINUOUS / ONESHOT) */
uint8 u8HwInstance; /* Instance ID */
uint8 u8HwChannel; /* Channel ID */
} Gpt_PerChanneInfoType;

  1. The sample uses Timer2_Channel2. Add the channel configuration to the pChannelCfg array; add the Channel ID → index mapping in pChannelId2IdxMap; increment the GPT_CHANNEL_TOTAL_NUM macro by 1 to reflect the new channel count.
  2. Configure the callback for the corresponding channel. When a timer interrupt occurs, the driver automatically calls the callback registered in Gpt_PerChanneInfoType based on Instance ID and Channel ID. Configuration example:

void Timer2ch2_Cbk_GptNotification(void)
{
LogNotice("Enter Timer2ch2_Cbk_GptNotification\r\n");
/* User code */
}

Gpt_PerChanneInfoType Gpt_ChannelConfig[GPT_CHANNEL_TOTAL_NUM] =
{
{
&Timer2ch2_Cbk_GptNotification,
(Gpt_ValueType)(4000000000U),
(GPT_CHANNEL_MODE_CONTINUOUS),
2,
2,
}
};

Application Programming Interface

  • Gpt_Init
/**
* @brief Initializes the GPT driver.
*
* @param[in] ConfigPtr: Pointer to a selected Gpt configuration.
* @param[out] None
*
* @retval None
*/
void Gpt_Init(const Gpt_ConfigType * ConfigPtr)
  • Gpt_StartTimer
/**
* @brief Starts a timer channel.
*
* @param[in] Channel: Numeric identifier of the GPT channel
* @param[in] Value: Target time in number of ticks
* @param[out] None
*
* @retval None
*/
void Gpt_StartTimer(Gpt_ChannelType Channel, Gpt_ValueType Value)
  • Gpt_DeInit
/**
* @brief Deinitializes the GPT driver.
*
* @param[in] None
* @param[out] None
*
* @retval None
* @design
*/
void Gpt_DeInit(void)
  • Gpt_StartTimer_S
/**
* @brief Starts a timer channel.
*
* @param[in] Channel: Numeric identifier of the GPT channel
* @param[in] Value: Target time in number of seconds
* @param[out] None
*
* @retval None
*/
void Gpt_StartTimer_S(Gpt_ChannelType Channel, Gpt_ValueType Seconds)
  • Gpt_StartTimer_MS
/**
* @brief Starts a timer channel.
*
* @param[in] Channel: Numeric identifier of the GPT channel
* @param[in] Value: Target time in number of milliseconds
* @param[out] None
*
* @retval None
*/
void Gpt_StartTimer_MS(Gpt_ChannelType Channel, Gpt_ValueType Milliseconds)
  • Gpt_StartTimer_US
/**
* @brief Starts a timer channel.
*
* @param[in] Channel: Numeric identifier of the GPT channel
* @param[in] Value: Target time in number of microseconds
* @param[out] None
*
* @retval None
*/
void Gpt_StartTimer_US(Gpt_ChannelType Channel, Gpt_ValueType Microseconds)
  • Gpt_StopTimer
/**
* @brief Stops a timer channel.
*
* @param[in] Channel: Numeric identifier of the GPT channel
* @param[out] None
*
* @retval None
*/
void Gpt_StopTimer(Gpt_ChannelType Channel)
  • Gpt_EnableNotification
/**
* @brief Enables the interrupt notification for a channel (relevant in normal mode).
*
* @param[in] Channel: Numeric identifier of the GPT channel
* @param[out] None
*
* @retval None
*/
void Gpt_EnableNotification(Gpt_ChannelType Channel)
  • Gpt_DisableNotification
/**
* @brief Disables the interrupt notification for a channel (relevant in normal mode).
*
* @param[in] Channel: Numeric identifier of the GPT channel
* @param[out] None
*
* @retval None
*/
void Gpt_DisableNotification(Gpt_ChannelType Channel)
  • Gpt_GetTimeRemaining
/**
* @brief Returns the time remaining until the target time is reached.
*
* @param[in] Channel: Numeric identifier of the GPT channel
* @param[out] None
*
* @retval None
*/
/*coverity[misra_c_2012_rule_8_7_violation:SUPPRESS] ## violation reason SYSSW_V_8.7_01*/
Gpt_ValueType Gpt_GetTimeRemaining(Gpt_ChannelType Channel)

Usage Example

Command syntax:

timer_interrupt <operation> <load/type> [irq]
  • operation: Specifies the operation to perform
    • on - Start the timer channel
    • off - Stop the timer channel
    • gettime - Read the current remaining count value
  • load: Timer reload value (initial count), valid only for the on operation

    On S100, the default clock frequency is 200 MHz. The relationship between clock frequency and load value is:

    On S600, the default clock frequency is 40 MHz. The relationship between clock frequency and load value is:

    load_value = timer_clk(Hz) × period(s)
    For example: when timer_clk = 200 MHz, a 1 s period corresponds to load_value = 200000000. In addition, load also supports time input in seconds/milliseconds/microseconds.
  • type: Read remaining time, valid only for the gettime operation
    • 0 - Read the register reload value
    • 1 - Read remaining time in seconds
    • 2 - Read remaining time in milliseconds
    • 3 - Read remaining time in microseconds
  • irq:
    • 0 - Disable timer interrupt
    • 1 - Enable timer interrupt

Example 1: Start the timer with a 1 s periodic interrupt

timer_interrupt on 1s 1

Output:

Example 2: Start the timer with a 100 us periodic interrupt

timer_interrupt on 100us 1

Output:

Example 3: Start the timer and read the current remaining count value

Set the count period to 20 seconds:

timer_interrupt on 20s 0

Read remaining time with different types:

timer_interrupt gettime 0

timer_interrupt gettime 1

timer_interrupt gettime 2

timer_interrupt gettime 3

Output: