STIMER (超级定时器)

简介

ET6001的STIMER 超级定时器由 2 个 64 位的独立计数器组成,定时器可单独工作;STIMER时钟来自APB总线时钟。 计数器的时钟可通过预分频器进行预分频,支持自动重载计数周期寄存器,自动重载寄存器可通过软件进行读写,即使在计数器运行时也可执行读写操作。

STIMER 超级定时器根据配置的 4 个比较门限,产生 ACMP0~3 共 4 个比较事件,并且还可以触发 4 个比较中断,中断类型可通过寄存器查询。

功能概述

  • 64 位的递增超级定时器和16bit的预分频器;

    备注

    预分频配置值需要做+1进行计算;

  • 支持one-shot单次计数模式和Free-run循环计数模式;

    备注

    • one-shot 单次计数模式:内部计数器计到最大值后返回 0。

    • Free-run 循环计数模式:内部计数器计到最大值后返回 0,并重新加载计数最大值后继续计数。

  • 支持4个比较门限配置,用于产生ACMP0-3比较事件,输出比较事件脉冲宽度可编程;

  • 支持CPU Debug调试模式,在调试模式下,STIMER暂停计数;

    备注

    主要用于前期调试模式下对STIMER配置进行验证使用,正常使用请勿开启对应功能;

  • 支持芯片紧急故障处理,该功能主要用于到发生非掉电复位情况下的错误复位情况下对timer当前计数值进行保存;

    备注

    紧急故障触发源在XBAR模块配置,故障发生后的保留计数值在SYSC模块中读取;

    • 参数 stim_idx 指定需要对哪个STIMER进行配置,在 XBAR_STIM0XBAR_STIM1 中选择。

    • 参数 stim_es_sig 指定产生哪个故障源,共两个紧急故障源可选 XBAR_STIM_EG_FAULT_SIG0``和 ``XBAR_STIM_EG_FAULT_SIG1

    • 参数 fault_src_mask 指定产生故障事件的事件源 XBAR_FALUT_SRC_TypeDef,可以将事件进行或上配置。

    紧急故障源触发后,STIMER的计数器会锁存到SYSC模块的寄存器中,锁存值仅可通过上电复位进行复位。

软件配置流程

  1. 基础计时功能配置:

    调用 STIMER_setPreScaler() 配置时钟预分频值。

    调用 STIMER_setPeriod() 配置周期值。

    调用 STIMER_setEmulationMode() 配置时钟计数模式 STIMER_EmulationMode

    调用 STIMER_setLoadMode() 配置时钟计数值的加载模式 STIMER_LoadMode

    调用 STIMER_startTimer() 使能计数器。

  2. 在上一步的基础上增加比较事件配置:

    调用 STIMER_loadCounterCompareValue() 在使能计数器前对比较值进行配置。

    备注

    比较事件主要用于作为SARC的触发事件源。 比较事件输出为脉冲事件,输出脉冲宽度在1~256clk可配,默认为16clk,主要用于在提高触发频率的情况下可以通过减少脉冲宽度来提高触发频率。

应用示例

API Reference

Header File

Functions

void STIMER_setEmulationMode(STIM_Type *STIMx, STIMER_EmulationMode mode)

Sets the emulation mode for the STIMER.

This function sets the emulation mode for the STIMER. The emulation mode determines how the STIMER behaves in different scenarios, such as continuous counting or pausing.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • mode – The emulation mode to be set for the STIMER. This parameter should be a value from the STIMER_EmulationMode enumeration.

void STIMER_setLoadMode(STIM_Type *STIMx, STIMER_LoadMode mode)

Sets the load mode for the STIMER.

This function sets the load mode for the STIMER. The load mode determines when the timer period value is loaded into the counter register.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • mode – The load mode to be set. This parameter should be a value from the STIMER_LoadMode enumeration.

void STIMER_setPeriod(STIM_Type *STIMx, uint64_t period)

Sets the period value for the STIMER.

This function sets the period value for the STIMER. The period value determines the duration for which the timer will count. The timer will reset to zero when the count reaches the period value.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • period – The period value to be set for the STIMER. The value should be a 64-bit unsigned integer. range[1~2^64-1]

void STIMER_setPreScaler(STIM_Type *STIMx, uint16_t prescaler)

Sets the pre-scaler value for the STIMER.

This function sets the pre-scaler value for the STIMER. The pre-scaler divides the input clock frequency to produce the timer’s count frequency. The pre-scaler value should be set such that the timer’s count frequency meets the desired requirements.

Attention

The pre-scaler value is set by subtracting 1 from the provided value.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • prescaler – The pre-scaler value to be set. The value should be in the range of 1 to 65536.

uint64_t STIMER_getTimerCount(STIM_Type *STIMx)

Returns the current STIMER counter value.

This function reads the current count value from the STIMER registers. It locks the current count value to the register before reading it, ensuring that the count value remains consistent during the read operation.

参数:

STIMx – Pointer to the STIMER peripheral register structure.

返回:

uint64_t The current count value of the STIMER.

void STIMER_resetTimerCount(STIM_Type *STIMx)

Resets the STIMER counter to zero.

This function clears the STIMER counter by setting the STM_TCLR_R bit in the STM_SET register. This action effectively resets the timer count to zero without stopping the timer.

Attention

This function does not stop the timer. It only resets the counter to zero.

参数:

STIMx – Pointer to the STIMER peripheral register structure.

void STIMER_startTimer(STIM_Type *STIMx)

Starts or restarts the STIMER count.

This function sets the STM_TON bit in the STM_SET register, effectively starting the STIMER count. It also clears the STM_THLT bit to ensure the count continues.

Attention

This function does not reset the timer counter.

参数:

STIMx – Pointer to the STIMER peripheral register structure.

void STIMER_pauseTimer(STIM_Type *STIMx)

Pauses the STIMER count.

This function sets the STM_THLT bit in the STM_SET register, effectively pausing the STIMER count without stopping the timer.

Attention

This function does not reset the timer counter.

参数:

STIMx – Pointer to the STIMER peripheral register structure.

void STIMER_resumeTimer(STIM_Type *STIMx)

resume the STIMER count.

This function clears the STM_THLT bit in the STM_SET register, effectively resuming the STIMER count.

Attention

This function does not reset the timer counter.

参数:

STIMx – Pointer to the STIMER peripheral register structure.

void STIMER_stopTimer(STIM_Type *STIMx)

Stops the STIMER count.

This function clears the STM_TON bit in the STM_SET register, effectively stopping the STIMER count.

Attention

This function does not reset the timer counter.

参数:

STIMx – Pointer to the STIMER peripheral register structure.

void STIMER_reloadTimerCounter(STIM_Type *STIMx)

Reloads the STIMER counter with the current period value.

This function reloads the STIMER counter with the current period value. It is useful when the timer needs to be reset to its original period without stopping the timer.

Attention

This function does not reset the timer counter. It only reloads the counter with the current period value.

参数:

STIMx – Pointer to the STIMER peripheral register structure.

void STIMER_enableCPUHalt(STIM_Type *STIMx)

Enables the STIMER to pause counting when the CPU is halted.

This function clears the STM_CPUHALT_MODE_R bit in the STM_SET register. If this bit is cleared, the STIMER will pause counting when the CPU is halted.

参数:

STIMx – Pointer to the STIMER peripheral register structure.

void STIMER_disableCPUHalt(STIM_Type *STIMx)

Disables the STIMER to pause counting when the CPU is halted.

This function clears the STM_CPUHALT_MODE_R bit in the STM_SET register. If this bit is cleared, the STIMER will continue counting when the CPU is halted.

参数:

STIMx – Pointer to the STIMER peripheral register structure.

bool STIMER_getTimerOverflowStatus(STIM_Type *STIMx)

Returns the STIMER overflow status.

This function checks the status of the STIMER_INT_COUNT_OVERFLOW interrupt. If the interrupt is triggered, indicating an overflow has occurred, the function returns true. Otherwise, it returns false.

Attention

After calling this function, all status flags are cleared.

参数:

STIMx – Pointer to the STIMER peripheral register structure.

返回:

true if an overflow has occurred, false otherwise.

void STIMER_loadCounterCompareValue(STIM_Type *STIMx, STIMER_CounterCompareModule compModule, uint64_t compCount)

Loads a compare value into the specified counter compare module of the STIMER.

This function sets the compare value for the specified counter compare module of the STIMER. The compare value is loaded into the appropriate registers, and the compare operation is enabled. The compare value is compared with the current timer count to generate an interrupt if they match.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • compModule – The counter compare module to load the compare value into.

  • compCount – The compare value to be loaded into the specified counter compare module.

bool STIMER_getStatus(STIM_Type *STIMx, uint32_t stsFlags)

Returns the status of a specified STIMER interrupt flag.

This function checks the status of a specified interrupt flag in the STIMER’s status register. If the specified interrupt flag is set, indicating an interrupt has occurred, the function returns true. Otherwise, it returns false.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • stsFlags – The interrupt flag to check. This parameter should be a value from the STIMER_STS_XXX enumeration.

返回:

true if the specified interrupt flag is set, indicating an interrupt has occurred.

返回:

false if the specified interrupt flag is not set.

void STIMER_enableInterrupt(STIM_Type *STIMx, uint32_t intFlags)

Enables the specified interrupt flags in the STIMER peripheral.

This function sets the specified interrupt flags in the STM_INT_EN register. By enabling these interrupt flags, the corresponding interrupt requests will be generated.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • intFlags – The interrupt flags to be enabled. This parameter should be a value from the STIMER_INT_XXX enumeration.

    • STIMER_INT_COMPARE_EVT0

    • STIMER_INT_COMPARE_EVT1

    • STIMER_INT_COMPARE_EVT2

    • STIMER_INT_COMPARE_EVT3

    • STIMER_INT_COUNT_OVERFLOW

    • STIMER_INT_FLAGS_ALL

void STIMER_disableInterrupt(STIM_Type *STIMx, uint32_t intFlags)

Disables the specified interrupt flags in the STIMER peripheral.

This function clears the specified interrupt flags in the STM_INT_EN register, effectively disabling the corresponding interrupts.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • intFlags – The interrupt flags to be disabled. This parameter should be a value from the STIMER_INT_XXX enumeration.

void STIMER_setInterruptMask(STIM_Type *STIMx, uint32_t intFlags)

Sets the interrupt mask for the specified STIMER peripheral.

This function unmasks the specified interrupt flags in the STM_INT_MASK register. By unmasking these interrupt flags, the corresponding interrupts will be enabled.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • intFlags – The interrupt flags to be unmasked. This parameter should be a value from the STIMER_INT_XXX enumeration.

void STIMER_clearInterruptMask(STIM_Type *STIMx, uint32_t intFlags)

Clears the interrupt mask for the specified STIMER peripheral.

This function unmasks the interrupt mask for the specified interrupt flags. By unmasking the interrupt mask, the corresponding interrupt requests will be processed by the STIMER.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • intFlags – The interrupt flags for which the interrupt mask needs to be cleared. This parameter should be a value from the STIMER_INT_XXX enumeration.

bool STIMER_getInterruptStatus(STIM_Type *STIMx, uint32_t intFlags)

Returns the interrupt status for the specified interrupt flags.

This function checks the interrupt status for the specified interrupt flags in the STIMER’s interrupt status register. If the interrupt status for the specified flags is set, indicating an interrupt has occurred, the function returns true. Otherwise, it returns false.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • intFlags – The interrupt flags to check. This parameter should be a value from the STIMER_INT_XXX enumeration.

返回:

bool true: if the interrupt status for the specified flags is set, indicating an interrupt has occurred. false: if the interrupt status for the specified flags is not set.

uint32_t STIMER_getAllInterruptStatus(STIM_Type *STIMx)

Returns the combined interrupt status for all STIMER interrupts.

This function reads the interrupt status from the STM_INT_STATUS_RPT register and returns the combined status of all STIMER interrupts. The status is masked to only include the first 5 bits, as per the STIMER’s documentation.

Attention

The function does not clear the interrupt status flags.

参数:

STIMx – Pointer to the STIMER peripheral register structure.

返回:

uint32_t The combined interrupt status for all STIMER interrupts. The returned value is masked to only include the first 5 bits.

bool STIMER_getInterruptStatusRaw(STIM_Type *STIMx, uint32_t intFlags)

Returns the raw interrupt status for the specified interrupt flags in the STIMER peripheral.

This function checks the raw interrupt status for the specified interrupt flags in the STM_INT_RAW_RPT register. If the interrupt status for the specified flags is set, indicating an interrupt has occurred, the function returns true. Otherwise, it returns false.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • intFlags – The interrupt flags for which the interrupt status needs to be checked. This parameter should be a value from the STIMER_INT_XXX enumeration.

返回:

true if the interrupt status for the specified flags is set, indicating an interrupt has occurred.

返回:

false if the interrupt status for the specified flags is not set.

void STIMER_clearInterruptStatus(STIM_Type *STIMx, uint32_t intFlags)

Clears the interrupt status for the specified interrupt flags in the STIMER peripheral.

This function clears the interrupt status for the specified interrupt flags in the STM_INT_CLR register. By clearing these interrupt status flags, the corresponding interrupt requests are no longer pending.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • intFlags – The interrupt flags for which the interrupt status needs to be cleared. This parameter should be a value from the STIMER_INT_XXX enumeration.

void STIMER_forceInterrupt(STIM_Type *STIMx, uint32_t intFlags)

Forces the specified interrupt flags in the STIMER peripheral.

This function sets the specified interrupt flags in the STM_INT_FORCE_IND register. By forcing these interrupt flags, the corresponding interrupt requests are generated, regardless of the interrupt status and mask settings.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • intFlags – The interrupt flags to be forced. This parameter should be a value from the STIMER_INT_XXX enumeration.

void STIMER_selectDMARequest(STIM_Type *STIMx, uint32_t request)

Selects the DMA request for the specified STIMER peripheral.

This function sets the DMA request for the specified STIMER peripheral. The DMA request is selected using the provided request parameter.

参数:
  • STIMx – Pointer to the STIMER peripheral register structure.

  • request – The DMA request to be selected. This parameter should be a value from the STIMER_DMA_REQ_XXX enumeration.

Macros

STIMER_INT_COMPARE_EVT0

备注

compare event 0

STIMER_INT_COMPARE_EVT1

compare event 1

STIMER_INT_COMPARE_EVT2

compare event 2

STIMER_INT_COMPARE_EVT3

compare event 3

STIMER_INT_COUNT_OVERFLOW

overflow event

STIMER_INT_FLAGS_ALL

all interrupt flags

STIMER_STS_COMPARE_EVT0_VAL_ERROR

compare event 0 value < current counter

STIMER_STS_COMPARE_EVT1_VAL_ERROR

compare event 1 value < current counter

STIMER_STS_COMPARE_EVT2_VAL_ERROR

compare event 2 value < current counter

STIMER_STS_COMPARE_EVT3_VAL_ERROR

compare event 3 value < current counter

STIMER_STS_COUNT_OVERFLOW

overflow event

STIMER_STS_COMPARE_EVT0_OVERFLOW

compare event 0 overflow

STIMER_STS_COMPARE_EVT1_OVERFLOW

compare event 1 overflow

STIMER_STS_COMPARE_EVT2_OVERFLOW

compare event 2 overflow

STIMER_STS_COMPARE_EVT3_OVERFLOW

compare event 3 overflow

STIMER_STS_ALL

all status

STIMER_DMA_REQ_COUNT_OVERFLOW

DMA request source overflow event.

STIMER_DMA_REQ_COMPARE_EVT0

DMA request source compare event 0.

STIMER_DMA_REQ_COMPARE_EVT1

DMA request source compare event 1.

STIMER_DMA_REQ_COMPARE_EVT2

DMA request source compare event 2.

STIMER_DMA_REQ_COMPARE_EVT3

DMA request source compare event 3.

Enumerations

enum STIMER_EmulationMode

Values that can be passed to STIMER_setEmulationMode() as the mode parameter.

Values:

enumerator STIMER_EMULATIONMODE_STOPATZERO

Denotes that the timer will stop when it reaches zero.

enumerator STIMER_EMULATIONMODE_RUNFREE

Denotes that the timer will run free.

enum STIMER_LoadMode

Values that can be passed to STIMER_setLoadMode() as the mode parameter.

Values:

enumerator STIMER_LOADMODE_IMMEDIATELY
enumerator STIMER_LOADMODE_SHADOW
enum STIMER_CounterCompareModule

Values that can be passed to the STIMER_getCounterCompareStatus() as the compModule parameter.

Values:

enumerator STIMER_COUNTER_COMPARE_0

Counter compare 0.

enumerator STIMER_COUNTER_COMPARE_1

Counter compare 1.

enumerator STIMER_COUNTER_COMPARE_2

Counter compare 2.

enumerator STIMER_COUNTER_COMPARE_3

Counter compare 3.