NavHAL 0.1.0
NAVRobotec's architecture-agnostic HAL for embedded systems.
Loading...
Searching...
No Matches
timer.h File Reference

Timer and SysTick register defines and API for STM32F4 (Cortex-M4). More...

#include "common/hal_types.h"
#include "utils/timer_types.h"
#include <stdint.h>
Include dependency graph for timer.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define RCC_BASE   0x40023800
#define RCC_APB1ENR   (*(__IO uint32_t *)(RCC_BASE + 0x40))
#define RCC_APB1ENR_TIM2_OFFSET   0
#define RCC_APB1ENR_TIM3_OFFSET   1
#define RCC_APB1ENR_TIM4_OFFSET   2
#define RCC_APB1ENR_TIM5_OFFSET   3
#define RCC_APB2ENR   (*(__IO uint32_t *)(RCC_BASE + 0x44))
#define RCC_APB2ENR_TIM1_OFFSET   0
#define RCC_APB2ENR_TIM9_OFFSET   16
#define RCC_APB2ENR_TIM10_OFFSET   17
#define RCC_APB2ENR_TIM11_OFFSET   18
#define TIM1_BASE   0x40010000
#define TIM2_BASE   0x40000000
#define TIM3_BASE   0x40000400
#define TIM4_BASE   0x40000800
#define TIM5_BASE   0x40000C00
#define TIM9_BASE   0x40014000
#define TIM10_BASE   0x40014400
#define TIM11_BASE   0x40014800
#define TIM_ADV_CR1_OFFSET   0x00
#define TIM_ADV_CR1_CEN_BIT   0x00
#define TIM_ADV_PSC_OFFSET    0x28
#define TIM_ADV_ARR_OFFSET    0x2C
#define TIM_ADV_EGR_OFFSET   0x14
#define TIM_ADV_EGR_UG_BIT   0x00
#define TIM_ADV_CNT_OFFSET   0X24
#define TIM_ADV_DIER_OFFSET   0X0C
#define TIM_ADV_DIER_UIE_BIT   0x00
#define TIM_GP1_CR1_OFFSET   0x00
#define TIM_GP1_CR1_CEN_BIT   0x00
#define TIM_GP1_PSC_OFFSET    0x28
#define TIM_GP1_ARR_OFFSET    0x2C
#define TIM_GP1_EGR_OFFSET   0x14
#define TIM_GP1_EGR_UG_BIT   0x00
#define TIM_GP1_CNT_OFFSET   0X24
#define TIM_GP1_DIER_OFFSET   0X0C
#define TIM_GP1_DIER_UIE_BIT   0x00
#define TIM_GP1_SR_OFFSET   0x10
#define TIM_GP1_SR_UIF_BIT   0x00
#define TIM_GP2_CR1_OFFSET   0x00
#define TIM_GP2_CR1_CEN_BIT   0x00
#define TIM_GP2_PSC_OFFSET    0x28
#define TIM_GP2_ARR_OFFSET   0x2C
#define TIM_GP2_EGR_OFFSET   0x14
#define TIM_GP2_EGR_UG_BIT   0x0
#define TIM_GP2_CNT_OFFSET   0X24
#define TIM_GP2_DIER_OFFSET   0X0C
#define TIM_GP2_DIER_UIE_BIT   0x00
#define TIM_GP2_SR_OFFSET   0x10
#define TIM_GP2_SR_UIF_BIT   0x00
#define SYST_CSR   (*(__IO uint32_t *)0xE000E010)
#define SYST_RVR   (*(__IO uint32_t *)0xE000E014)
#define SYST_CVR   (*(__IO uint32_t *)0xE000E018)
#define SYST_CALIB   (*(__IO uint32_t *)0xE000E01C)
#define SYST_CSR_EN_BIT   0
#define SYST_CSR_TICKINT_BIT   1
#define SYST_CSR_CLKSOURCE_BIT   2
#define TIMx_CCR1_OFFSET   0x34
#define TIMx_CCR2_OFFSET   0x38
#define TIMx_CCR3_OFFSET   0x3C
#define TIMx_CCR4_OFFSET   0x40
#define TIMx_CCER_OFFSET   0x20
#define TIMx_CCER_CC1E_BIT   0
#define TIMx_CCER_CC2E_BIT   4
#define TIMx_CCER_CC3E_BIT   8
#define TIMx_CCER_CC4E_BIT   12
#define TIMx_CCMR1_OFFSET   0x18
#define TIMx_CCMR2_OFFSET   0x1c
#define TIMx_CCMR1_OC1M_BIT   4
#define TIMx_CCMR1_OC1M_PWM_MODE1    0x6
#define TIMx_CCMR1_OC1M_PWM_MODE2    0x07
#define TIMx_CCMR1_OC1PE_BIT   3

Functions

void systick_init (uint32_t tick_us)
 Initialize the SysTick timer to generate periodic ticks.
void delay_ms (uint32_t ms)
 Busy-wait for the specified number of milliseconds.
void delay_us (uint64_t us)
 Busy-wait for the specified number of microseconds.
uint64_t hal_get_tick (void)
 Return the current system tick count.
uint32_t hal_get_tick_duration_us (void)
 Return the configured tick duration in microseconds.
uint32_t hal_get_tick_reload_value (void)
 Return the SysTick reload value (24-bit truncated).
uint32_t hal_get_millis (void)
 Return system uptime in milliseconds.
uint32_t hal_get_micros (void)
 Return system uptime in microseconds.
void SysTick_Handler (void)
 SysTick interrupt handler increments the global tick counter.
void hal_systick_set_callback (void(*cb)(void))
void timer_init (hal_timer_t timer, uint32_t prescaler, uint32_t auto_reload)
 Initialize a timer based on its type (advanced / gp1 / gp2).
void timer_start (hal_timer_t timer)
 Start the specified timer.
void timer_stop (hal_timer_t timer)
 Stop the specified timer.
void timer_reset (hal_timer_t timer)
 Reset timer counter to zero.
uint32_t timer_get_count (hal_timer_t timer)
 Get the current counter value for a timer.
void timer_enable_interrupt (hal_timer_t timer)
 Enable timer interrupts (NVIC + DIER UIE) for supported timers.
void timer_disable_interrupt (hal_timer_t timer)
void timer_clear_interrupt_flag (hal_timer_t timer)
void timer_attach_callback (hal_timer_t timer, void(*callback)(void))
 Attach a callback to the timer's HAL interrupt table.
void timer_detach_callback (hal_timer_t timer)
 Detach a previously attached callback for the given timer.
void TIM2_IRQHandler (void)
 IRQ handler wrapper for TIM2.
void TIM3_IRQHandler (void)
 IRQ handler wrapper for TIM3.
void TIM4_IRQHandler (void)
 IRQ handler wrapper for TIM4.
void TIM5_IRQHandler (void)
 IRQ handler wrapper for TIM5.
void TIM9_IRQHandler (void)
void TIM12_IRQHandler (void)
void timer_set_compare (hal_timer_t timer, uint8_t channel, uint32_t compare_value)
 Set the compare (CCR) register for a timer channel and configure CCMR.
uint32_t timer_get_compare (hal_timer_t timer, uint32_t channel)
uint32_t timer_get_arr (hal_timer_t timer, uint32_t channel)
void timer_set_arr (hal_timer_t timer, uint32_t channel, uint32_t arr)
void timer_enable_channel (hal_timer_t timer, uint32_t channel)
void timer_disable_channel (hal_timer_t timer, uint32_t channel)
 Disable output on the specified timer channel (CCxE = 0).
uint32_t timer_get_frequency (hal_timer_t timer)
 Calculate the timer's current base frequency using PSC and ARR.
void timer_set_prescaler (hal_timer_t timer, uint32_t prescaler)
void timer_set_auto_reload (hal_timer_t timer, uint32_t arr)

Detailed Description

Timer and SysTick register defines and API for STM32F4 (Cortex-M4).

This header contains register offsets, bit definitions, SysTick control, timer base addresses, and the public timer/SysTick API used by the HAL. All defines and function prototypes are intended for the STM32F4 series on a Cortex-M4 core.

Macro Definition Documentation

◆ RCC_APB1ENR

#define RCC_APB1ENR   (*(__IO uint32_t *)(RCC_BASE + 0x40))

◆ RCC_APB1ENR_TIM2_OFFSET

#define RCC_APB1ENR_TIM2_OFFSET   0

◆ RCC_APB1ENR_TIM3_OFFSET

#define RCC_APB1ENR_TIM3_OFFSET   1

◆ RCC_APB1ENR_TIM4_OFFSET

#define RCC_APB1ENR_TIM4_OFFSET   2

◆ RCC_APB1ENR_TIM5_OFFSET

#define RCC_APB1ENR_TIM5_OFFSET   3

◆ RCC_APB2ENR

#define RCC_APB2ENR   (*(__IO uint32_t *)(RCC_BASE + 0x44))

◆ RCC_APB2ENR_TIM10_OFFSET

#define RCC_APB2ENR_TIM10_OFFSET   17

◆ RCC_APB2ENR_TIM11_OFFSET

#define RCC_APB2ENR_TIM11_OFFSET   18

◆ RCC_APB2ENR_TIM1_OFFSET

#define RCC_APB2ENR_TIM1_OFFSET   0

◆ RCC_APB2ENR_TIM9_OFFSET

#define RCC_APB2ENR_TIM9_OFFSET   16

◆ RCC_BASE

#define RCC_BASE   0x40023800

◆ SYST_CALIB

#define SYST_CALIB   (*(__IO uint32_t *)0xE000E01C)

◆ SYST_CSR

#define SYST_CSR   (*(__IO uint32_t *)0xE000E010)

◆ SYST_CSR_CLKSOURCE_BIT

#define SYST_CSR_CLKSOURCE_BIT   2

◆ SYST_CSR_EN_BIT

#define SYST_CSR_EN_BIT   0

◆ SYST_CSR_TICKINT_BIT

#define SYST_CSR_TICKINT_BIT   1

◆ SYST_CVR

#define SYST_CVR   (*(__IO uint32_t *)0xE000E018)

◆ SYST_RVR

#define SYST_RVR   (*(__IO uint32_t *)0xE000E014)

◆ TIM10_BASE

#define TIM10_BASE   0x40014400

◆ TIM11_BASE

#define TIM11_BASE   0x40014800

◆ TIM1_BASE

#define TIM1_BASE   0x40010000

◆ TIM2_BASE

#define TIM2_BASE   0x40000000

◆ TIM3_BASE

#define TIM3_BASE   0x40000400

◆ TIM4_BASE

#define TIM4_BASE   0x40000800

◆ TIM5_BASE

#define TIM5_BASE   0x40000C00

◆ TIM9_BASE

#define TIM9_BASE   0x40014000

◆ TIM_ADV_ARR_OFFSET

#define TIM_ADV_ARR_OFFSET    0x2C

◆ TIM_ADV_CNT_OFFSET

#define TIM_ADV_CNT_OFFSET   0X24

◆ TIM_ADV_CR1_CEN_BIT

#define TIM_ADV_CR1_CEN_BIT   0x00

◆ TIM_ADV_CR1_OFFSET

#define TIM_ADV_CR1_OFFSET   0x00

◆ TIM_ADV_DIER_OFFSET

#define TIM_ADV_DIER_OFFSET   0X0C

◆ TIM_ADV_DIER_UIE_BIT

#define TIM_ADV_DIER_UIE_BIT   0x00

◆ TIM_ADV_EGR_OFFSET

#define TIM_ADV_EGR_OFFSET   0x14

◆ TIM_ADV_EGR_UG_BIT

#define TIM_ADV_EGR_UG_BIT   0x00

◆ TIM_ADV_PSC_OFFSET

#define TIM_ADV_PSC_OFFSET    0x28

◆ TIM_GP1_ARR_OFFSET

#define TIM_GP1_ARR_OFFSET    0x2C

◆ TIM_GP1_CNT_OFFSET

#define TIM_GP1_CNT_OFFSET   0X24

◆ TIM_GP1_CR1_CEN_BIT

#define TIM_GP1_CR1_CEN_BIT   0x00

◆ TIM_GP1_CR1_OFFSET

#define TIM_GP1_CR1_OFFSET   0x00

◆ TIM_GP1_DIER_OFFSET

#define TIM_GP1_DIER_OFFSET   0X0C

◆ TIM_GP1_DIER_UIE_BIT

#define TIM_GP1_DIER_UIE_BIT   0x00

◆ TIM_GP1_EGR_OFFSET

#define TIM_GP1_EGR_OFFSET   0x14

◆ TIM_GP1_EGR_UG_BIT

#define TIM_GP1_EGR_UG_BIT   0x00

◆ TIM_GP1_PSC_OFFSET

#define TIM_GP1_PSC_OFFSET    0x28

◆ TIM_GP1_SR_OFFSET

#define TIM_GP1_SR_OFFSET   0x10

◆ TIM_GP1_SR_UIF_BIT

#define TIM_GP1_SR_UIF_BIT   0x00

◆ TIM_GP2_ARR_OFFSET

#define TIM_GP2_ARR_OFFSET   0x2C

◆ TIM_GP2_CNT_OFFSET

#define TIM_GP2_CNT_OFFSET   0X24

◆ TIM_GP2_CR1_CEN_BIT

#define TIM_GP2_CR1_CEN_BIT   0x00

◆ TIM_GP2_CR1_OFFSET

#define TIM_GP2_CR1_OFFSET   0x00

◆ TIM_GP2_DIER_OFFSET

#define TIM_GP2_DIER_OFFSET   0X0C

◆ TIM_GP2_DIER_UIE_BIT

#define TIM_GP2_DIER_UIE_BIT   0x00

◆ TIM_GP2_EGR_OFFSET

#define TIM_GP2_EGR_OFFSET   0x14

◆ TIM_GP2_EGR_UG_BIT

#define TIM_GP2_EGR_UG_BIT   0x0

◆ TIM_GP2_PSC_OFFSET

#define TIM_GP2_PSC_OFFSET    0x28

◆ TIM_GP2_SR_OFFSET

#define TIM_GP2_SR_OFFSET   0x10

◆ TIM_GP2_SR_UIF_BIT

#define TIM_GP2_SR_UIF_BIT   0x00

◆ TIMx_CCER_CC1E_BIT

#define TIMx_CCER_CC1E_BIT   0

◆ TIMx_CCER_CC2E_BIT

#define TIMx_CCER_CC2E_BIT   4

◆ TIMx_CCER_CC3E_BIT

#define TIMx_CCER_CC3E_BIT   8

◆ TIMx_CCER_CC4E_BIT

#define TIMx_CCER_CC4E_BIT   12

◆ TIMx_CCER_OFFSET

#define TIMx_CCER_OFFSET   0x20

◆ TIMx_CCMR1_OC1M_BIT

#define TIMx_CCMR1_OC1M_BIT   4

◆ TIMx_CCMR1_OC1M_PWM_MODE1

#define TIMx_CCMR1_OC1M_PWM_MODE1    0x6

◆ TIMx_CCMR1_OC1M_PWM_MODE2

#define TIMx_CCMR1_OC1M_PWM_MODE2    0x07

◆ TIMx_CCMR1_OC1PE_BIT

#define TIMx_CCMR1_OC1PE_BIT   3

◆ TIMx_CCMR1_OFFSET

#define TIMx_CCMR1_OFFSET   0x18

◆ TIMx_CCMR2_OFFSET

#define TIMx_CCMR2_OFFSET   0x1c

◆ TIMx_CCR1_OFFSET

#define TIMx_CCR1_OFFSET   0x34

◆ TIMx_CCR2_OFFSET

#define TIMx_CCR2_OFFSET   0x38

◆ TIMx_CCR3_OFFSET

#define TIMx_CCR3_OFFSET   0x3C

◆ TIMx_CCR4_OFFSET

#define TIMx_CCR4_OFFSET   0x40

Function Documentation

◆ delay_ms()

void delay_ms ( uint32_t ms)

Busy-wait for the specified number of milliseconds.

Parameters
msNumber of milliseconds to delay.

◆ delay_us()

void delay_us ( uint64_t us)

Busy-wait for the specified number of microseconds.

Parameters
usNumber of microseconds to delay.
Note
This is a blocking busy-wait that uses hal_get_tick() and the configured tick duration. It will wait at least one tick if the requested delay is smaller than the tick duration.

◆ hal_get_micros()

uint32_t hal_get_micros ( void )

Return system uptime in microseconds.

Returns
Microseconds since tick counter started.

◆ hal_get_millis()

uint32_t hal_get_millis ( void )

Return system uptime in milliseconds.

Returns
Milliseconds since tick counter started.

◆ hal_get_tick()

uint64_t hal_get_tick ( void )

Return the current system tick count.

Returns
Current tick count.

◆ hal_get_tick_duration_us()

uint32_t hal_get_tick_duration_us ( void )

Return the configured tick duration in microseconds.

Returns
Tick duration (us).

◆ hal_get_tick_reload_value()

uint32_t hal_get_tick_reload_value ( void )

Return the SysTick reload value (24-bit truncated).

Returns
Reload value currently configured in SYST_RVR.

◆ hal_systick_set_callback()

void hal_systick_set_callback ( void(* cb )(void))

◆ SysTick_Handler()

void SysTick_Handler ( void )

SysTick interrupt handler increments the global tick counter.

Note
This handler is intended to be wired into the vector table.

◆ systick_init()

void systick_init ( uint32_t tick_us)

Initialize the SysTick timer to generate periodic ticks.

Parameters
tick_usTick period in microseconds.
Note
The SysTick reload is limited to 24 bits; this function clips the computed reload value to 24 bits. The function configures SysTick to use the selected clock source, enables the SysTick interrupt and starts the timer.

◆ TIM12_IRQHandler()

void TIM12_IRQHandler ( void )

◆ TIM2_IRQHandler()

void TIM2_IRQHandler ( void )

IRQ handler wrapper for TIM2.

Clears the flag and dispatches to the HAL interrupt handler table.

◆ TIM3_IRQHandler()

void TIM3_IRQHandler ( void )

IRQ handler wrapper for TIM3.

◆ TIM4_IRQHandler()

void TIM4_IRQHandler ( void )

IRQ handler wrapper for TIM4.

◆ TIM5_IRQHandler()

void TIM5_IRQHandler ( void )

IRQ handler wrapper for TIM5.

◆ TIM9_IRQHandler()

void TIM9_IRQHandler ( void )

◆ timer_attach_callback()

void timer_attach_callback ( hal_timer_t timer,
void(* callback )(void) )

Attach a callback to the timer's HAL interrupt table.

Parameters
timerTimer identifier.
callbackFunction pointer to call when the timer IRQ fires.

◆ timer_clear_interrupt_flag()

void timer_clear_interrupt_flag ( hal_timer_t timer)

◆ timer_detach_callback()

void timer_detach_callback ( hal_timer_t timer)

Detach a previously attached callback for the given timer.

Parameters
timerTimer identifier.

◆ timer_disable_channel()

void timer_disable_channel ( hal_timer_t timer,
uint32_t channel )

Disable output on the specified timer channel (CCxE = 0).

Parameters
timerTimer identifier.
channelChannel number (1-4).

◆ timer_disable_interrupt()

void timer_disable_interrupt ( hal_timer_t timer)

◆ timer_enable_channel()

void timer_enable_channel ( hal_timer_t timer,
uint32_t channel )

◆ timer_enable_interrupt()

void timer_enable_interrupt ( hal_timer_t timer)

Enable timer interrupts (NVIC + DIER UIE) for supported timers.

Parameters
timerTimer identifier.
Note
For TIM1 more complex options exist and are left TODO in the original.

◆ timer_get_arr()

uint32_t timer_get_arr ( hal_timer_t timer,
uint32_t channel )

◆ timer_get_compare()

uint32_t timer_get_compare ( hal_timer_t timer,
uint32_t channel )

◆ timer_get_count()

uint32_t timer_get_count ( hal_timer_t timer)

Get the current counter value for a timer.

Parameters
timerTimer identifier.
Returns
Current counter value or 0 if invalid timer.

◆ timer_get_frequency()

uint32_t timer_get_frequency ( hal_timer_t timer)

Calculate the timer's current base frequency using PSC and ARR.

Parameters
timerTimer identifier.
Returns
Timer frequency in Hz or 0 if invalid timer.
Note
This reads PSC and ARR directly from timer registers and uses the appropriate APB clock for the timer.

◆ timer_init()

void timer_init ( hal_timer_t timer,
uint32_t prescaler,
uint32_t auto_reload )

Initialize a timer based on its type (advanced / gp1 / gp2).

Parameters
timerTimer identifier.
prescalerPrescaler value.
auto_reloadAuto-reload value.

◆ timer_reset()

void timer_reset ( hal_timer_t timer)

Reset timer counter to zero.

Parameters
timerTimer identifier.

◆ timer_set_arr()

void timer_set_arr ( hal_timer_t timer,
uint32_t channel,
uint32_t arr )

◆ timer_set_auto_reload()

void timer_set_auto_reload ( hal_timer_t timer,
uint32_t arr )

◆ timer_set_compare()

void timer_set_compare ( hal_timer_t timer,
uint8_t channel,
uint32_t compare_value )

Set the compare (CCR) register for a timer channel and configure CCMR.

Parameters
timerTimer identifier.
channelChannel number (1-4).
compare_valueValue to write into CCRx.
Note
This function sets PWM mode 1 and enables the channel after writing CCR.

◆ timer_set_prescaler()

void timer_set_prescaler ( hal_timer_t timer,
uint32_t prescaler )

◆ timer_start()

void timer_start ( hal_timer_t timer)

Start the specified timer.

Parameters
timerTimer identifier.

◆ timer_stop()

void timer_stop ( hal_timer_t timer)

Stop the specified timer.

Parameters
timerTimer identifier.