1 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
2 // ==== OS Tick API ====
4 \addtogroup CMSIS_RTOS_TickAPI OS Tick API
6 \brief Provides a low level API between an device agnostic RTOS implementation and specific periodic timer capabilities.
7 \details The CMSIS OS Tick API may be used by an arbitrary RTOS implementation to be easily potable across a wide range
10 Cortex-M devices share a common System Tick Timer to be used for RTOS timing purposes. Cortex-A devices do not have a
11 common System Tick Timer but various vendor specific solution. In order to make it easier to enable an RTOS, such as RTX5,
12 to support a wide range of Cortex Microcontrollers the OS Tick API is used to encapsulate the device specific timer
15 A default implementation for Cortex-M System Tick Timer can be found in \ref os_systick.c.
17 \note The default implementation is defined \c weak thus it can easily be overwritten by an alternative user implementation.
19 \include "../../RTOS2/Source/os_systick.c"
24 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
26 \fn int32_t OS_Tick_Setup (uint32_t freq, IRQHandler_t handler)
27 \details Setup the a hardware time to be used for generating periodic tick interrupts to the RTOS.
29 The timer should be configured to generate interrupts at the given frequency.
30 The given callback should be used as the interrupt handler.
32 The timer should only be initialized and configured accordingly. It must not be started nor
33 creating interrupts before \ref OS_Tick_Enable is called.
35 For a simple Cortex-M device using the built in SystemTick timer the default implementation looks
36 like the following example:
39 #ifndef SYSTICK_IRQ_PRIORITY
40 #define SYSTICK_IRQ_PRIORITY 0xFFU
43 static uint8_t PendST;
45 int32_t OS_Tick_Setup (uint32_t freq, IRQHandler_t handler) {
53 load = (SystemCoreClock / freq) - 1U;
54 if (load > 0x00FFFFFFU) {
58 NVIC_SetPriority(SysTick_IRQn, SYSTICK_IRQ_PRIORITY);
60 SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk;
71 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
73 \fn int32_t OS_Tick_Enable (void)
74 \details Start the timer to count and enable generation of periodic interrupts.
76 For a simple Cortex-M device using the built in SystemTick timer the default implementation looks
77 like the following example:
80 int32_t OS_Tick_Enable (void) {
84 SCB->ICSR = SCB_ICSR_PENDSTSET_Msk;
87 SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
94 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
96 \fn int32_t OS_Tick_Disable (void)
97 \details Stop the timer from counting and disable generation of periodic interrupts.
99 After a call to this function the timer must not generate any further interrupt.
101 For a simple Cortex-M device using the built in SystemTick timer the default implementation looks
102 like the following example:
105 int32_t OS_Tick_Disable (void) {
107 SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
109 if ((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) != 0U) {
110 SCB->ICSR = SCB_ICSR_PENDSTCLR_Msk;
119 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
121 \fn int32_t OS_Tick_AcknowledgeIRQ (void)
122 \details Acknowledge the pending tick interrupt, i.e. by clear the pending flag.
124 For a simple Cortex-M device using the built in SystemTick timer the default implementation looks
125 like the following example:
128 int32_t OS_Tick_AcknowledgeIRQ (void) {
135 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
137 \fn int32_t OS_Tick_GetIRQn (void)
138 \details Return the actual numeric value to identify the interrupt used by the timer.
140 For a simple Cortex-M device using the built in SystemTick timer the default implementation looks
141 like the following example:
144 int32_t OS_Tick_GetIRQn (void) {
145 return (SysTick_IRQn);
150 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
152 \fn uint32_t OS_Tick_GetClock (void)
153 \details Return the clock frequency the timer operates at, i.e. giving the rate the internal counter value is incremented at.
155 For a simple Cortex-M device using the built in SystemTick timer the default implementation looks
156 like the following example:
159 uint32_t OS_Tick_GetClock (void) {
160 return (SystemCoreClock);
165 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
167 \fn uint32_t OS_Tick_GetInterval (void)
168 \details Return the actual counting interval used for the internal counter value between to consecutive tick interrupts.
170 For a simple Cortex-M device using the built in SystemTick timer the default implementation looks
171 like the following example:
174 uint32_t OS_Tick_GetInterval (void) {
175 return (SysTick->LOAD + 1U);
180 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
182 \fn uint32_t OS_Tick_GetCount (void)
183 \details Return the current value of the internal counter between 0 to \ref OS_Tick_GetInterval() - 1.
185 This value is used to calculate subticks, i.e. OS_Tick_GetCount() / OS_Tick_GetInterval(), if a higher time resolution is needed.
187 \note If the hardware is a down-counter (such as the Cortex-M System Tick Timer) one has to calculate the corresponding up-counter value.
189 For a simple Cortex-M device using the built in SystemTick timer the default implementation looks
190 like the following example:
193 uint32_t OS_Tick_GetCount (void) {
194 uint32_t load = SysTick->LOAD;
195 return (load - SysTick->VAL);
200 /*=======0=========1=========2=========3=========4=========5=========6=========7=========8=========9=========0=========1====*/
202 \fn OS_Tick_GetOverflow (void)
203 \details Return the current state of the overflow signal, i.e. the timers interrupt pending bit.
205 This information can be used to calculate an intermediate (but correct) tick value while the tick
206 interrupt is pending but blocked.
208 For a simple Cortex-M device using the built in SystemTick timer the default implementation looks
209 like the following example:
212 uint32_t OS_Tick_GetOverflow (void) {
213 return ((SysTick->CTRL >> 16) & 1U);
218 /** @} */ /* group CMSIS_RTOS_TickAPI */