]> begriffs open source - cmsis/blob - CMSIS/RTOS2/Source/os_systick.c
CMSIS Driver: Updated NAND API V2.3.0 (extended ARM_NAND_ECC_INFO structure)
[cmsis] / CMSIS / RTOS2 / Source / os_systick.c
1 /**************************************************************************//**
2  * @file     os_systick.c
3  * @brief    CMSIS OS Tick SysTick implementation
4  * @version  V1.0.0
5  * @date     05. June 2017
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2017-2017 ARM Limited. All rights reserved.
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  *
12  * Licensed under the Apache License, Version 2.0 (the License); you may
13  * not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */
24
25 #include "os_tick.h"
26
27 #include "RTE_Components.h"
28 #include CMSIS_device_header
29
30 #ifdef  SysTick
31
32 #ifndef SYSTICK_IRQ_PRIORITY
33 #define SYSTICK_IRQ_PRIORITY    0xFFU
34 #endif
35
36 static uint8_t PendST;
37
38 // Setup OS Tick.
39 __WEAK int32_t  OS_Tick_Setup (uint32_t freq, IRQHandler_t handler) {
40   uint32_t load;
41   (void)handler;
42
43   if (freq == 0U) {
44     return (-1);
45   }
46
47   load = (SystemCoreClock / freq) - 1U;
48   if (load > 0x00FFFFFFU) {
49     return (-1);
50   }
51
52   // Set SysTick Interrupt Priority
53 #if   ((defined(__ARM_ARCH_8M_MAIN__) && (__ARM_ARCH_8M_MAIN__ == 1U)) || \
54        (defined(__CORTEX_M)           && (__CORTEX_M           == 7U)))
55   SCB->SHPR[11] =  SYSTICK_IRQ_PRIORITY;
56 #elif  (defined(__ARM_ARCH_8M_BASE__) && (__ARM_ARCH_8M_BASE__ == 1U))
57   SCB->SHPR[1] |= (SYSTICK_IRQ_PRIORITY << 24);
58 #elif ((defined(__ARM_ARCH_7M__)      && (__ARM_ARCH_7M__      == 1U)) || \
59        (defined(__ARM_ARCH_7EM__)     && (__ARM_ARCH_7EM__     == 1U)))
60   SCB->SHP[11]  =  SYSTICK_IRQ_PRIORITY;
61 #elif  (defined(__ARM_ARCH_6M__)      && (__ARM_ARCH_6M__      == 1U))
62   SCB->SHP[1]  |= (SYSTICK_IRQ_PRIORITY << 24);
63 #else
64 #error "Unknown ARM Core!"
65 #endif
66
67   SysTick->CTRL =  SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk;
68   SysTick->LOAD =  load;
69   SysTick->VAL  =  0U;
70
71   PendST = 0U;
72
73   return (0);
74 }
75
76 /// Enable OS Tick.
77 __WEAK int32_t  OS_Tick_Enable (void) {
78
79   if (PendST != 0U) {
80     PendST = 0U;
81     SCB->ICSR = SCB_ICSR_PENDSTSET_Msk;
82   }
83
84   SysTick->CTRL |=  SysTick_CTRL_ENABLE_Msk;
85
86   return (0);
87 }
88
89 /// Disable OS Tick.
90 __WEAK int32_t  OS_Tick_Disable (void) {
91
92   SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
93
94   if ((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk) != 0U) {
95     SCB->ICSR = SCB_ICSR_PENDSTCLR_Msk;
96     PendST = 1U;
97   }
98
99   return (0);
100 }
101
102 // Acknowledge OS Tick IRQ.
103 __WEAK int32_t  OS_Tick_AcknowledgeIRQ (void) {
104   (void)SysTick->CTRL;
105   return (0);
106 }
107
108 // Get OS Tick IRQ number.
109 __WEAK int32_t  OS_Tick_GetIRQn (void) {
110   return (SysTick_IRQn);
111 }
112
113 // Get OS Tick clock.
114 __WEAK uint32_t OS_Tick_GetClock (void) {
115   return (SystemCoreClock);
116 }
117
118 // Get OS Tick interval.
119 __WEAK uint32_t OS_Tick_GetInterval (void) {
120   return (SysTick->LOAD + 1U);
121 }
122
123 // Get OS Tick count value.
124 __WEAK uint32_t OS_Tick_GetCount (void) {
125   uint32_t load = SysTick->LOAD;
126   return  (load - SysTick->VAL);
127 }
128
129 // Get OS Tick overflow status.
130 __WEAK uint32_t OS_Tick_GetOverflow (void) {
131   return ((SysTick->CTRL >> 16) & 1U);
132 }
133
134 #endif  // SysTick