11#ifndef CORTEX_M4_TIMER_H
12#define CORTEX_M4_TIMER_H
17#define RCC_BASE 0x40023800
19#define RCC_APB1ENR (*(__IO uint32_t *)(RCC_BASE + 0x40))
20#define RCC_APB1ENR_TIM2_OFFSET 0
21#define RCC_APB1ENR_TIM3_OFFSET 1
22#define RCC_APB1ENR_TIM4_OFFSET 2
23#define RCC_APB1ENR_TIM5_OFFSET 3
25#define RCC_APB2ENR (*(__IO uint32_t *)(RCC_BASE + 0x44))
26#define RCC_APB2ENR_TIM1_OFFSET 0
27#define RCC_APB2ENR_TIM9_OFFSET 16
28#define RCC_APB2ENR_TIM10_OFFSET 17
29#define RCC_APB2ENR_TIM11_OFFSET 18
31#define TIM1_BASE 0x40010000
32#define TIM2_BASE 0x40000000
33#define TIM3_BASE 0x40000400
34#define TIM4_BASE 0x40000800
35#define TIM5_BASE 0x40000C00
36#define TIM9_BASE 0x40014000
37#define TIM10_BASE 0x40014400
38#define TIM11_BASE 0x40014800
42#define TIM_ADV_CR1_OFFSET 0x00
43#define TIM_ADV_CR1_CEN_BIT 0x00
44#define TIM_ADV_PSC_OFFSET \
47#define TIM_ADV_ARR_OFFSET \
49#define TIM_ADV_EGR_OFFSET 0x14
50#define TIM_ADV_EGR_UG_BIT 0x00
51#define TIM_ADV_CNT_OFFSET 0X24
52#define TIM_ADV_DIER_OFFSET 0X0C
53#define TIM_ADV_DIER_UIE_BIT 0x00
57#define TIM_GP1_CR1_OFFSET 0x00
58#define TIM_GP1_CR1_CEN_BIT 0x00
59#define TIM_GP1_PSC_OFFSET \
62#define TIM_GP1_ARR_OFFSET \
64#define TIM_GP1_EGR_OFFSET 0x14
65#define TIM_GP1_EGR_UG_BIT 0x00
66#define TIM_GP1_CNT_OFFSET 0X24
67#define TIM_GP1_DIER_OFFSET 0X0C
68#define TIM_GP1_DIER_UIE_BIT 0x00
69#define TIM_GP1_SR_OFFSET 0x10
70#define TIM_GP1_SR_UIF_BIT 0x00
74#define TIM_GP2_CR1_OFFSET 0x00
75#define TIM_GP2_CR1_CEN_BIT 0x00
76#define TIM_GP2_PSC_OFFSET \
79#define TIM_GP2_ARR_OFFSET 0x2C
80#define TIM_GP2_EGR_OFFSET 0x14
81#define TIM_GP2_EGR_UG_BIT 0x0
82#define TIM_GP2_CNT_OFFSET 0X24
83#define TIM_GP2_DIER_OFFSET 0X0C
84#define TIM_GP2_DIER_UIE_BIT 0x00
85#define TIM_GP2_SR_OFFSET 0x10
86#define TIM_GP2_SR_UIF_BIT 0x00
89#define SYST_CSR (*(__IO uint32_t *)0xE000E010)
91#define SYST_RVR (*(__IO uint32_t *)0xE000E014)
93#define SYST_CVR (*(__IO uint32_t *)0xE000E018)
95#define SYST_CALIB (*(__IO uint32_t *)0xE000E01C)
96#define SYST_CSR_EN_BIT 0
97#define SYST_CSR_TICKINT_BIT 1
98#define SYST_CSR_CLKSOURCE_BIT 2
100#define TIMx_CCR1_OFFSET 0x34
101#define TIMx_CCR2_OFFSET 0x38
102#define TIMx_CCR3_OFFSET 0x3C
103#define TIMx_CCR4_OFFSET 0x40
104#define TIMx_CCER_OFFSET 0x20
105#define TIMx_CCER_CC1E_BIT 0
106#define TIMx_CCER_CC2E_BIT 4
107#define TIMx_CCER_CC3E_BIT 8
108#define TIMx_CCER_CC4E_BIT 12
109#define TIMx_CCMR1_OFFSET 0x18
110#define TIMx_CCMR2_OFFSET 0x1c
111#define TIMx_CCMR1_OC1M_BIT 4
113#define TIMx_CCMR1_OC1M_PWM_MODE1 \
118#define TIMx_CCMR1_OC1M_PWM_MODE2 \
122#define TIMx_CCMR1_OC1PE_BIT 3
152 void (*callback)(
void));
166 uint32_t compare_value);
Hardware Abstraction Layer (HAL) common type definitions.
void delay_us(uint64_t us)
Busy-wait for the specified number of microseconds.
Definition timer.c:79
uint32_t timer_get_compare(hal_timer_t timer, uint32_t channel)
Definition timer.c:686
void timer_set_arr(hal_timer_t timer, uint32_t channel, uint32_t arr)
Definition timer.c:482
void timer_disable_channel(hal_timer_t timer, uint32_t channel)
Disable output on the specified timer channel (CCxE = 0).
Definition timer.c:740
void timer_set_auto_reload(hal_timer_t timer, uint32_t arr)
void timer_stop(hal_timer_t timer)
Stop the specified timer.
Definition timer.c:388
uint32_t timer_get_count(hal_timer_t timer)
Get the current counter value for a timer.
Definition timer.c:428
void TIM2_IRQHandler(void)
IRQ handler wrapper for TIM2.
Definition timer.c:502
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.
Definition timer.c:667
uint32_t hal_get_tick_reload_value(void)
Return the SysTick reload value (24-bit truncated).
Definition timer.c:116
void timer_attach_callback(hal_timer_t timer, void(*callback)(void))
Attach a callback to the timer's HAL interrupt table.
Definition timer.c:603
void timer_disable_interrupt(hal_timer_t timer)
void timer_start(hal_timer_t timer)
Start the specified timer.
Definition timer.c:366
void timer_enable_interrupt(hal_timer_t timer)
Enable timer interrupts (NVIC + DIER UIE) for supported timers.
Definition timer.c:571
void systick_init(uint32_t tick_us)
Initialize the SysTick timer to generate periodic ticks.
Definition timer.c:53
uint32_t timer_get_arr(hal_timer_t timer, uint32_t channel)
Definition timer.c:694
void TIM5_IRQHandler(void)
IRQ handler wrapper for TIM5.
Definition timer.c:526
void timer_clear_interrupt_flag(hal_timer_t timer)
Definition timer.c:486
uint32_t timer_get_frequency(hal_timer_t timer)
Calculate the timer's current base frequency using PSC and ARR.
Definition timer.c:449
void TIM4_IRQHandler(void)
IRQ handler wrapper for TIM4.
Definition timer.c:518
uint32_t hal_get_millis(void)
Return system uptime in milliseconds.
Definition timer.c:123
void timer_detach_callback(hal_timer_t timer)
Detach a previously attached callback for the given timer.
Definition timer.c:633
uint64_t hal_get_tick(void)
Return the current system tick count.
Definition timer.c:102
void TIM9_IRQHandler(void)
void TIM12_IRQHandler(void)
void timer_reset(hal_timer_t timer)
Reset timer counter to zero.
Definition timer.c:410
void SysTick_Handler(void)
SysTick interrupt handler increments the global tick counter.
Definition timer.c:139
void timer_enable_channel(hal_timer_t timer, uint32_t channel)
Definition timer.c:708
void delay_ms(uint32_t ms)
Busy-wait for the specified number of milliseconds.
Definition timer.c:95
uint32_t hal_get_tick_duration_us(void)
Return the configured tick duration in microseconds.
Definition timer.c:109
void hal_systick_set_callback(void(*cb)(void))
void TIM3_IRQHandler(void)
IRQ handler wrapper for TIM3.
Definition timer.c:510
void timer_set_prescaler(hal_timer_t timer, uint32_t prescaler)
uint32_t hal_get_micros(void)
Return system uptime in microseconds.
Definition timer.c:130
void timer_init(hal_timer_t timer, uint32_t prescaler, uint32_t auto_reload)
Initialize a timer based on its type (advanced / gp1 / gp2).
Definition timer.c:352
hal_timer_t
Definition timer_types.h:5