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 "core/cortex-m4/timer.h"
#include "core/cortex-m4/clock.h"
#include "core/cortex-m4/interrupt.h"
#include "core/cortex-m4/uart.h"
#include "utils/timer_types.h"
#include <stdint.h>
Include dependency graph for timer.c:

Functions

void systick_init (uint32_t tick_us)
 Initialize the SysTick timer to generate periodic ticks.
void delay_us (uint64_t us)
 Busy-wait for the specified number of microseconds.
void delay_ms (uint32_t ms)
 Busy-wait for the specified number of milliseconds.
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.
uint32_t _get_timer_base (hal_timer_t timer)
void _enable_timer_rcc (hal_timer_t timer)
void _timer_gp1_init (hal_timer_t timer, uint32_t prescaler, uint32_t auto_reload)
void _timer_gp2_init (hal_timer_t timer, uint32_t prescaler, uint32_t auto_reload)
void _timer_adv_init (hal_timer_t timer, uint32_t prescaler, uint32_t auto_reload)
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.
uint32_t timer_get_frequency (hal_timer_t timer)
 Calculate the timer's current base frequency using PSC and ARR.
void timer_set_arr (hal_timer_t timer, uint32_t channel, uint32_t arr)
void timer_clear_interrupt_flag (hal_timer_t timer)
void TIM2_IRQHandler ()
 IRQ handler wrapper for TIM2.
void TIM3_IRQHandler ()
 IRQ handler wrapper for TIM3.
void TIM4_IRQHandler ()
 IRQ handler wrapper for TIM4.
void TIM5_IRQHandler ()
 IRQ handler wrapper for TIM5.
void TIM1BRK_TIM9_IRQHandler ()
 IRQ handler for TIM1 BRK and TIM9 shared vector.
void _set_interrupt_enable_bit (hal_timer_t timer)
void timer_enable_interrupt (hal_timer_t timer)
 Enable timer interrupts (NVIC + DIER UIE) for supported timers.
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 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_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).

Detailed Description

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

This translation unit implements:

  • SysTick initialization and a 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 is intentionally low-level (direct register access) and matches the STM32F4 register layout / behavior expected by the rest of the HAL.

Function Documentation

◆ _enable_timer_rcc()

void _enable_timer_rcc ( hal_timer_t timer)

◆ _get_timer_base()

uint32_t _get_timer_base ( hal_timer_t timer)

◆ _set_interrupt_enable_bit()

void _set_interrupt_enable_bit ( hal_timer_t timer)

◆ _timer_adv_init()

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

◆ _timer_gp1_init()

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

◆ _timer_gp2_init()

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

◆ 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.

◆ 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.

◆ TIM1BRK_TIM9_IRQHandler()

void TIM1BRK_TIM9_IRQHandler ( )

IRQ handler for TIM1 BRK and TIM9 shared vector.

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

◆ 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.

◆ 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_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_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_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.