STM32 RTOS
0.2.3
Bare-metal preemptive RTOS for STM32F446RE
Toggle main menu visibility
Loading...
Searching...
No Matches
systick.c
Go to the documentation of this file.
1
5
#include "
systick.h
"
6
11
12
DriverStatus_e
systickInit
(
SysTickRegisters_t
*systick, uint32_t core_hz, uint32_t tick_hz)
13
{
14
DriverStatus_e
status =
DRIVER_STATUS_OK
;
15
uint32_t reload = 0U;
16
17
if
((core_hz == 0U) || (tick_hz == 0U))
18
{
19
status =
DRIVER_STATUS_ERR_INVALID_PARAM
;
20
}
21
else
22
{
23
/* tick_hz > core_hz gives 0 clocks, rejected below. */
24
uint32_t period_clocks = core_hz / tick_hz;
25
26
if
(period_clocks < 2U)
27
{
28
status =
DRIVER_STATUS_ERR_INVALID_PARAM
;
29
}
30
else
31
{
32
reload = period_clocks - 1U;
33
}
34
}
35
36
if
((status ==
DRIVER_STATUS_OK
) && (reload >
SYSTICK_LOAD_RELOAD_Msk
))
37
{
38
status =
DRIVER_STATUS_ERR_INVALID_PARAM
;
39
}
40
41
if
(status ==
DRIVER_STATUS_OK
)
42
{
43
systick->
CTRL
= 0U;
44
systick->
LOAD
= reload;
45
systick->
VAL
= 0U;
46
systick->
CTRL
=
SYSTICK_CTRL_CLKSOURCE
|
SYSTICK_CTRL_TICKINT
|
SYSTICK_CTRL_ENABLE
;
47
}
48
49
return
status;
50
}
51
52
void
systickDeinit
(
SysTickRegisters_t
*systick)
53
{
54
systick->
CTRL
= 0U;
55
systick->
LOAD
= 0U;
56
systick->
VAL
= 0U;
57
}
58
DriverStatus_e
DriverStatus_e
Outcome of a driver call: a category for control flow, not a diagnosis.
Definition
driver_status.h:32
DRIVER_STATUS_OK
@ DRIVER_STATUS_OK
Succeeded.
Definition
driver_status.h:36
DRIVER_STATUS_ERR_INVALID_PARAM
@ DRIVER_STATUS_ERR_INVALID_PARAM
A parameter is outside its valid domain.
Definition
driver_status.h:38
systickInit
DRIVER_MUST_CHECK DriverStatus_e systickInit(SysTickRegisters_t *systick, uint32_t core_hz, uint32_t tick_hz)
Start SysTick at tick_hz from the processor clock, with its interrupt on.
Definition
systick.c:12
systickDeinit
void systickDeinit(SysTickRegisters_t *systick)
Stop SysTick and zero CTRL, LOAD and VAL.
Definition
systick.c:52
SYSTICK_LOAD_RELOAD_Msk
#define SYSTICK_LOAD_RELOAD_Msk
<span class="regaccess">rw</span> Reload value: the tick period is RELOAD + 1 clocks.
Definition
systick_reg.h:48
SYSTICK_CTRL_TICKINT
#define SYSTICK_CTRL_TICKINT
<span class="regaccess">rw</span> Raise the SysTick exception when the counter reaches 0.
Definition
systick_reg.h:35
SYSTICK_CTRL_ENABLE
#define SYSTICK_CTRL_ENABLE
<span class="regaccess">rw</span> Counter enable.
Definition
systick_reg.h:32
SYSTICK_CTRL_CLKSOURCE
#define SYSTICK_CTRL_CLKSOURCE
<span class="regaccess">rw</span> Clock source: 1 = processor clock (HCLK), 0 = HCLK / 8.
Definition
systick_reg.h:38
SysTickRegisters_t
SysTick register block.
Definition
systick_reg.h:19
SysTickRegisters_t::CTRL
volatile uint32_t CTRL
0x00: control and status
Definition
systick_reg.h:20
SysTickRegisters_t::LOAD
volatile uint32_t LOAD
0x04: reload value
Definition
systick_reg.h:21
SysTickRegisters_t::VAL
volatile uint32_t VAL
0x08: current value; any write clears it
Definition
systick_reg.h:22
systick.h
SysTick driver: the periodic tick timer.
drivers
src
systick.c
Generated by
1.18.0