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

Timer, SysTick and timer-peripheral helpers for STM32F4 (Cortex-M4) More...

Include dependency graph for timer.c:

Functions

<<<<<<< HEAD static volatile uint64_t systick_ticks=0;static volatile uint32_t tick_duration_us=1;static volatile uint32_t tick_reload_value=0;=======static volatile uint64_t systick_ticks=0;static volatile uint32_t tick_duration_us=1;static volatile uint32_t tick_reload_value=0;> > > > > > void systick_init (uint32_t tick_us)
 Initialize the SysTick timer.
void delay_us (uint64_t us)
 Busy-wait delay in microseconds.
void delay_ms (uint32_t ms)
 Busy-wait delay in milliseconds.
uint64_t hal_get_tick (void)
 Get current system tick count.
uint32_t hal_get_tick_duration_us (void)
 Get configured tick duration.
uint32_t hal_get_tick_reload_value (void)
 Get SysTick reload value.
uint32_t hal_get_millis (void)
 Get system uptime in milliseconds.
uint32_t hal_get_micros (void)
 Get system uptime in microseconds.
void SysTick_Handler (void)
 SysTick interrupt handler.
void timer_init (hal_timer_t timer, uint32_t prescaler, uint32_t auto_reload)
 Initialize timer based on type.

Detailed Description

Timer, SysTick and timer-peripheral helpers for STM32F4 (Cortex-M4)

This translation unit implements:

  • SysTick initialization and microsecond tick counter
  • Busy-wait delay helpers (delay_us, delay_ms)
  • Timer peripheral base lookup, RCC enable, init/start/stop/reset
  • Timer interrupt control, attach/detach callbacks and IRQ handlers
  • Channel compare and PWM setup helpers

All logic uses direct register access matching the STM32F4 register layout.

Function Documentation

◆ delay_ms()

void delay_ms ( uint32_t ms)

Busy-wait delay in milliseconds.

Parameters
msNumber of milliseconds to delay

◆ delay_us()

void delay_us ( uint64_t us)

Busy-wait delay in microseconds.

Parameters
usNumber of microseconds to delay
Note
Minimum delay is one tick period

◆ hal_get_micros()

uint32_t hal_get_micros ( void )

Get system uptime in microseconds.

Returns
Microseconds since startup

◆ hal_get_millis()

uint32_t hal_get_millis ( void )

Get system uptime in milliseconds.

Returns
Milliseconds since startup

◆ hal_get_tick()

uint64_t hal_get_tick ( void )

Get current system tick count.

Returns
Current tick count

◆ hal_get_tick_duration_us()

uint32_t hal_get_tick_duration_us ( void )

Get configured tick duration.

Returns
Tick duration in microseconds

◆ hal_get_tick_reload_value()

uint32_t hal_get_tick_reload_value ( void )

Get SysTick reload value.

Returns
Current reload value (24-bit)

◆ SysTick_Handler()

void SysTick_Handler ( void )

SysTick interrupt handler.

Increments the global tick counter

◆ systick_init()

<<<<<<< HEAD static volatile uint64_t systick_ticks=0; static volatile uint32_t tick_duration_us=1; static volatile uint32_t tick_reload_value=0;======= static volatile uint64_t systick_ticks=0; static volatile uint32_t tick_duration_us=1; static volatile uint32_t tick_reload_value=0; > > > > > > void systick_init ( uint32_t tick_us)

Initialize the SysTick timer.

Parameters
tick_usTick period in microseconds

Configures SysTick to:

  • Use processor clock (AHB)
  • Generate interrupts at specified interval
  • Start counting immediately
    Note
    Reload value is limited to 24 bits

◆ timer_init()

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

Initialize timer based on type.

Parameters
timerTimer identifier
prescalerPrescaler value
auto_reloadAuto-reload value

Start timer counter

Parameters
timerTimer identifier

Stop timer counter

Parameters
timerTimer identifier

Set timer compare value for PWM

Parameters
timerTimer identifier
channelChannel number (1-4)
compare_valueCCR register value

Configures PWM mode 1 and enables the channel output

Enable timer channel output

Parameters
timerTimer identifier
channelChannel number (1-4)

Reset timer counter to zero.

Parameters
timerTimer identifier.

Get the current counter value for a timer.

Parameters
timerTimer identifier.
Returns
Current counter value or 0 if invalid 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.

IRQ handler wrapper for TIM2.

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

IRQ handler wrapper for TIM3.

IRQ handler wrapper for TIM4.

IRQ handler wrapper for TIM5.

IRQ handler for TIM1 BRK and TIM9 shared vector.

Note
This function clears TIM9's flag and dispatches using the shared IRQn.

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.

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

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

Detach a previously attached callback for the given timer.

Parameters
timerTimer identifier.

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.

Disable timer channel output

Parameters
timerTimer identifier
channelChannel number (1-4)