GPS Device
Loading...
Searching...
No Matches

Public functions to interact with the SysTick driver. More...

Collaboration diagram for SysTick Public API:

Functions

ERR_te systick_init (SYSTICK_CFG_ts const *systick_cfg)
 Initializes and starts the SysTick timer.
void systick_deinit (void)
 Deinitializes the SysTick timer.
void systick_get_def_cfg (SYSTICK_CFG_ts *systick_cfg_o)
 Populates a configuration structure with the default SysTick settings.
uint32_t systick_get_ms (void)
 Returns the number of milliseconds elapsed since SysTick was initialized.

Detailed Description

Public functions to interact with the SysTick driver.

Function Documentation

◆ systick_init()

ERR_te systick_init ( SYSTICK_CFG_ts const * systick_cfg)

Initializes and starts the SysTick timer.

Computes the reload value from the current AHB clock frequency to achieve a 1 ms tick period, configures the clock source and interrupt settings, clears the current value register, and enables the counter.

Parameters
[in]systick_cfgPointer to the SysTick configuration structure.
Returns
  • ERR_OK on success
  • ERR_MODULE_ALREADY_INITIALIZED if SysTick is already initialized
  • ERR_UNKNOWN if the computed reload value is zero or exceeds the 24-bit maximum (0xFFFFFF), indicating an unsupported clock frequency
Note
Typically called indirectly via init_systick.
See also
systick_init

Definition at line 41 of file arm_cortex_m4_systick.c.

41 {
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}
static struct internal_state_s internal_state
Singleton instance of the SysTick driver internal state.
@ SYST_CSR_ENABLE
@ SYST_CSR_TICKINT
@ SYST_CSR_CLKSOURCE
@ 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.
Internal state of the SysTick driver.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ systick_deinit()

void systick_deinit ( void )

Deinitializes the SysTick timer.

Disables the counter, clears the interrupt and clock source configuration, resets the reload and current value registers, and resets the internal state. Does nothing if SysTick has not been initialized.

See also
systick_deinit

Definition at line 84 of file arm_cortex_m4_systick.c.

84 {
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}

◆ systick_get_def_cfg()

void systick_get_def_cfg ( SYSTICK_CFG_ts * systick_cfg_o)

Populates a configuration structure with the default SysTick settings.

Default configuration:

  • Clock source: processor (AHB) clock
  • Interrupt: enabled
Parameters
[out]systick_cfg_oPointer to the configuration structure to populate.
See also
systick_get_def_cfg

Definition at line 102 of file arm_cortex_m4_systick.c.

102 {
104 systick_cfg_o->interrupt = SYSTICK_IT_TRUE;
105}
@ SYSTICK_CLK_SOURCE_PROCESSOR
@ SYSTICK_IT_TRUE
SYSTICK_CLK_SOURCE_te clk_source
SYSTICK_IT_te interrupt

◆ systick_get_ms()

uint32_t systick_get_ms ( void )

Returns the number of milliseconds elapsed since SysTick was initialized.

The counter is incremented by the SysTick exception handler on each 1 ms wrap. Returns 0 if SysTick has not been initialized.

Returns
Elapsed time in milliseconds since systick_init was called.
Note
The counter is a 32-bit unsigned value and will wrap after approximately 49.7 days of continuous operation.
See also
systick_get_ms

Definition at line 108 of file arm_cortex_m4_systick.c.

108 {
109 if(!internal_state.initialized) {
110 return 0;
111 }
112
113 return internal_state.elapsed_ms;
114}
Here is the caller graph for this function: