NavHAL 0.1.0
NAVRobotec's architecture-agnostic HAL for embedded systems.
Loading...
Searching...
No Matches
timer.h
Go to the documentation of this file.
1
10
11#ifndef CORTEX_M4_TIMER_H
12#define CORTEX_M4_TIMER_H
13#include "common/hal_types.h"
14#include "utils/timer_types.h"
15#include <stdint.h>
16
17#define RCC_BASE 0x40023800
18
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
24
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
30
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
39
40// Advance Timer 1
41// Labled as ADV
42#define TIM_ADV_CR1_OFFSET 0x00
43#define TIM_ADV_CR1_CEN_BIT 0x00
44#define TIM_ADV_PSC_OFFSET \
45 0x28 // Timer tick = ABP1_CLK/(prescaler+1), Use only lower 16 bits, upper
46 // bits are reserved
47#define TIM_ADV_ARR_OFFSET \
48 0x2C // Upper 16 bits are only available for TIM2 and TIM5, reserved for rest
49#define TIM_ADV_EGR_OFFSET 0x14 // Event generator
50#define TIM_ADV_EGR_UG_BIT 0x00 // Reinitialize the timer
51#define TIM_ADV_CNT_OFFSET 0X24
52#define TIM_ADV_DIER_OFFSET 0X0C // DMA and Interrupt register
53#define TIM_ADV_DIER_UIE_BIT 0x00 // Update Interrupt enable bit
54
55// GP Timer 2-5 on APB1
56// Labled as GP1
57#define TIM_GP1_CR1_OFFSET 0x00
58#define TIM_GP1_CR1_CEN_BIT 0x00
59#define TIM_GP1_PSC_OFFSET \
60 0x28 // Timer tick = ABP1_CLK/(prescaler+1), Use only lower 16 bits, upper
61 // bits are reserved
62#define TIM_GP1_ARR_OFFSET \
63 0x2C // Upper 16 bits are only available for TIM2 and TIM5, reserved for rest
64#define TIM_GP1_EGR_OFFSET 0x14 // Event generator
65#define TIM_GP1_EGR_UG_BIT 0x00 // Reinitialize the timer
66#define TIM_GP1_CNT_OFFSET 0X24
67#define TIM_GP1_DIER_OFFSET 0X0C // DMA and Interrupt register
68#define TIM_GP1_DIER_UIE_BIT 0x00 // Update Interrupt enable bit
69#define TIM_GP1_SR_OFFSET 0x10 // Update Interrupt enable bit
70#define TIM_GP1_SR_UIF_BIT 0x00 // Update Interrupt enable bit
71
72// GP Timer 9-11 on APB1
73// Labled as GP2
74#define TIM_GP2_CR1_OFFSET 0x00
75#define TIM_GP2_CR1_CEN_BIT 0x00
76#define TIM_GP2_PSC_OFFSET \
77 0x28 // Timer tick = ABP2_CLK/(prescaler+1), Use only lower 16 bits, upper
78 // bits are reserved
79#define TIM_GP2_ARR_OFFSET 0x2C // only lower 16 bits availabnle
80#define TIM_GP2_EGR_OFFSET 0x14 // Event generator
81#define TIM_GP2_EGR_UG_BIT 0x0 // Reinitialize the timer
82#define TIM_GP2_CNT_OFFSET 0X24
83#define TIM_GP2_DIER_OFFSET 0X0C // DMA and Interrupt register
84#define TIM_GP2_DIER_UIE_BIT 0x00 // Update Interrupt enable bit
85#define TIM_GP2_SR_OFFSET 0x10 // Update Interrupt enable bit
86#define TIM_GP2_SR_UIF_BIT 0x00 // Update Interrupt enable bit
87
88// SysTick Control and Status Register
89#define SYST_CSR (*(__IO uint32_t *)0xE000E010)
90// SysTick Reload Value Register
91#define SYST_RVR (*(__IO uint32_t *)0xE000E014)
92// SysTick Current Value Register
93#define SYST_CVR (*(__IO uint32_t *)0xE000E018)
94// SysTick Calibration Register
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
99
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
112
113#define TIMx_CCMR1_OC1M_PWM_MODE1 \
114 0x6 // In upcounting, channel 1 is active as long as TIMx_CNT<TIMx_CCR1 else
115 // inactive. In downcounting, channel 1 is inactive (OC1REF=‘0) as long as
116 // TIMx_CNT>TIMx_CCR1 else active (OC1REF=1)
117
118#define TIMx_CCMR1_OC1M_PWM_MODE2 \
119 0x07 // In upcounting, channel 1 is inactive as long as TIMx_CNT<TIMx_CCR1
120 // else active. In downcounting, channel 1 is active as long as
121 // TIMx_CNT>TIMx_CCR1 else inactive.
122#define TIMx_CCMR1_OC1PE_BIT 3
123
124// SysTick Timer Functions
125// TIM5 is used for systick
126void systick_init(uint32_t tick_us);
127void delay_ms(uint32_t ms);
128void delay_us(uint64_t us);
129uint64_t hal_get_tick(void);
130uint32_t hal_get_tick_duration_us(void);
131uint32_t hal_get_tick_reload_value(void);
132uint32_t hal_get_millis(void);
133uint32_t hal_get_micros(void);
134void SysTick_Handler(void); // ISR for systick interrupts
135void hal_systick_set_callback(void (*cb)(void));
136
137// General Purpose Timer (TIMx) Initialization & Control
138void timer_init(hal_timer_t timer, uint32_t prescaler, uint32_t auto_reload);
139void timer_start(hal_timer_t timer);
140void timer_stop(hal_timer_t timer);
141void timer_reset(hal_timer_t timer);
142uint32_t timer_get_count(hal_timer_t timer);
143
144// Timer Interrupt Management
147 hal_timer_t timer); // [TODO]Add support for all timers
149 hal_timer_t timer); // [TODO]Add support for all timers
151 hal_timer_t timer,
152 void (*callback)(void)); //[TODO] Add support for all timers
154 hal_timer_t timer); // [TODO] Add support for all timers
155
156// Timer IRQ Handlers
157void TIM2_IRQHandler(void);
158void TIM3_IRQHandler(void);
159void TIM4_IRQHandler(void);
160void TIM5_IRQHandler(void);
163
164// PWM and Output Compare (Future Stage)
165void timer_set_compare(hal_timer_t timer, uint8_t channel,
166 uint32_t compare_value);
167uint32_t timer_get_compare(hal_timer_t timer, uint32_t channel);
168uint32_t timer_get_arr(hal_timer_t timer, uint32_t channel);
169void timer_set_arr(hal_timer_t timer, uint32_t channel, uint32_t arr);
170void timer_enable_channel(hal_timer_t timer, uint32_t channel);
171
172void timer_disable_channel(hal_timer_t timer, uint32_t channel);
173
174// Utility Functions
175uint32_t timer_get_frequency(hal_timer_t timer);
176void timer_set_prescaler(hal_timer_t timer, uint32_t prescaler);
177void timer_set_auto_reload(hal_timer_t timer, uint32_t arr);
178
179#endif // !CORTEX_M4_TIMER_H
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