GPS Device
Loading...
Searching...
No Matches
arm_cortex_m4_systick.c
Go to the documentation of this file.
1
11
13#include "arm_cortex_m4.h"
14#include "err.h"
15#include "stm32f401re_rcc.h"
16
26 uint32_t volatile elapsed_ms;
27
30};
31
34
39
41ERR_te systick_init(SYSTICK_CFG_ts const *systick_cfg) {
42 if(internal_state.initialized) {
44 }
45
46 // Disable SysTick before reconfiguring
47 SYSTICK->SYST_CSR &= ~(1U << SYST_CSR_ENABLE);
48
49 // Clear internal state
50 internal_state = (struct internal_state_s){ 0 };
51
52 // Configure interrupt enable
53 SYSTICK->SYST_CSR &= ~(1U << SYST_CSR_TICKINT);
54 SYSTICK->SYST_CSR |= (systick_cfg->interrupt << SYST_CSR_TICKINT);
55
56 // Configure clock source
57 SYSTICK->SYST_CSR &= ~(1U << SYST_CSR_CLKSOURCE);
58 SYSTICK->SYST_CSR |= (systick_cfg->clk_source << SYST_CSR_CLKSOURCE);
59
60 // Clear current value
61 SYSTICK->SYST_CVR = 0;
62
63 // Compute reload value for a 1 ms tick period from the AHB clock
64 uint32_t cpu_clk = rcc_get_ahb_clk();
65 uint32_t one_ms_clk_cycle = cpu_clk / 1000;
66 uint32_t reload_value = one_ms_clk_cycle + 1;
67 if (reload_value == 0 || reload_value > 0x01000000U) {
68 return ERR_UNKNOWN;
69 }
70
71 // Set reload value (24-bit register)
72 SYSTICK->SYST_RVR &= ~(0xFFFFFF);
73 SYSTICK->SYST_RVR |= (reload_value - 1);
74
75 // Enable SysTick
76 SYSTICK->SYST_CSR |= (1U << SYST_CSR_ENABLE);
77
78 internal_state.initialized = true;
79
80 return ERR_OK;
81}
82
84void systick_deinit(void) {
85 if(!internal_state.initialized) {
86 return;
87 }
88
89 SYSTICK->SYST_CSR &= ~(1U << SYST_CSR_ENABLE);
90
91 internal_state = (struct internal_state_s){ 0 };
92
93 SYSTICK->SYST_CSR &= ~(1U << SYST_CSR_TICKINT);
94 SYSTICK->SYST_CSR &= ~(1U << SYST_CSR_CLKSOURCE);
95
96 SYSTICK->SYST_CVR = 0;
97
98 SYSTICK->SYST_RVR &= ~(0xFFFFFF);
99}
100
104 systick_cfg_o->interrupt = SYSTICK_IT_TRUE;
105}
106
108uint32_t systick_get_ms(void) {
109 if(!internal_state.initialized) {
110 return 0;
111 }
112
113 return internal_state.elapsed_ms;
114}
115
123void SysTick_Handler(void) {
124 internal_state.elapsed_ms++;
125}
126
Register definitions and bit position enumerations for the Arm Cortex-M4 core peripherals.
static struct internal_state_s internal_state
Singleton instance of the SysTick driver internal state.
Arm Cortex-M4 SysTick driver public API.
System-wide error code definitions.
void systick_get_def_cfg(SYSTICK_CFG_ts *systick_cfg_o)
Populates a configuration structure with the default SysTick settings.
void systick_deinit(void)
Deinitializes the SysTick timer.
void SysTick_Handler(void)
SysTick exception handler. Increments the millisecond counter on each 1 ms wrap.
ERR_te systick_init(SYSTICK_CFG_ts const *systick_cfg)
Initializes and starts the SysTick timer.
uint32_t systick_get_ms(void)
Returns the number of milliseconds elapsed since SysTick was initialized.
@ SYST_CSR_ENABLE
@ SYST_CSR_TICKINT
@ SYST_CSR_CLKSOURCE
@ SYSTICK_CLK_SOURCE_PROCESSOR
@ SYSTICK_IT_TRUE
ERR_te
Standard return type used by all public API functions.
Definition err.h:35
@ ERR_UNKNOWN
Definition err.h:37
@ ERR_OK
Definition err.h:36
@ ERR_MODULE_ALREADY_INITIALIZED
Definition err.h:54
uint32_t rcc_get_ahb_clk(void)
Returns the current AHB bus clock frequency in Hz.
STM32F401RE RCC driver public API.
Configuration structure for initializing the SysTick timer.
SYSTICK_CLK_SOURCE_te clk_source
SYSTICK_IT_te interrupt
Internal state of the SysTick driver.
uint32_t volatile elapsed_ms