]> begriffs open source - cmsis/blob - CMSIS/RTOS2/Source/os_tick_ptim.c
CMSIS-Core(A):
[cmsis] / CMSIS / RTOS2 / Source / os_tick_ptim.c
1 /**************************************************************************//**
2  * @file     os_tick_ptim.c
3  * @brief    CMSIS OS Tick implementation for Private Timer
4  * @version  V1.0.0
5  * @date     29. June 2017
6  ******************************************************************************/
7 /*
8  * Copyright (c) 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 #include "irq_ctrl.h"
27
28 #include "RTE_Components.h"
29 #include CMSIS_device_header
30
31 #if defined(PTIM)
32
33 #ifndef PTIM_IRQ_PRIORITY
34 #define PTIM_IRQ_PRIORITY           0xFFU
35 #endif
36
37 static uint8_t PTIM_PendIRQ;        // Timer interrupt pending flag
38
39 // Setup OS Tick.
40 int32_t  OS_Tick_Setup (uint32_t freq, IRQHandler_t handler) {
41   uint32_t load;
42   uint32_t prio;
43   uint32_t bits;
44
45   if (freq == 0U) {
46     return (-1);
47   }
48
49   PTIM_PendIRQ = 0U;
50
51   // Private Timer runs with the system frequency
52   load = (SystemCoreClock / freq) - 1U;
53
54   // Disable Private Timer and set load value
55   PTIM_SetControl   (0U);
56   PTIM_SetLoadValue (load);
57
58   // Disable corresponding IRQ
59   IRQ_Disable     (PrivTimer_IRQn);
60   IRQ_ClearPending(PrivTimer_IRQn);
61
62   // Determine number of implemented priority bits
63   IRQ_SetPriority (PrivTimer_IRQn, 0xFFU);
64
65   prio = IRQ_GetPriority (PrivTimer_IRQn);
66
67   // At least bits [7:4] must be implemented
68   if ((prio & 0xF0U) == 0U) {
69     return (-1);
70   }
71
72   for (bits = 0; bits < 4; bits++) {
73     if ((prio & 0x01) != 0) {
74       break;
75     }
76     prio >>= 1;
77   }
78   
79   // Adjust configured priority to the number of implemented priority bits
80   prio = (PTIM_IRQ_PRIORITY << bits) & 0xFFUL;
81
82   // Set Private Timer interrupt priority
83   IRQ_SetPriority(PrivTimer_IRQn, prio-1U);
84
85   // Set edge-triggered IRQ
86   IRQ_SetMode(PrivTimer_IRQn, IRQ_MODE_TRIG_EDGE);
87
88   // Register tick interrupt handler function
89   IRQ_SetHandler(PrivTimer_IRQn, handler);
90
91   // Enable corresponding interrupt
92   IRQ_Enable (PrivTimer_IRQn);
93
94   // Set bits: IRQ enable and Auto reload
95   PTIM_SetControl (0x06U);
96
97   return (0);
98 }
99
100 /// Enable OS Tick.
101 int32_t  OS_Tick_Enable (void) {
102   uint32_t ctrl;
103
104   // Set pending interrupt if flag set
105   if (PTIM_PendIRQ != 0U) {
106     PTIM_PendIRQ = 0U;
107     IRQ_SetPending (PrivTimer_IRQn);
108   }
109
110   // Start the Private Timer
111   ctrl  = PTIM_GetControl();
112   // Set bit: Timer enable
113   ctrl |= 1U;
114   PTIM_SetControl (ctrl);
115
116   return (0);
117 }
118
119 /// Disable OS Tick.
120 int32_t  OS_Tick_Disable (void) {
121   uint32_t ctrl;
122   
123   // Stop the Private Timer
124   ctrl  = PTIM_GetControl();
125   // Clear bit: Timer enable
126   ctrl &= ~1U;
127   PTIM_SetControl (ctrl);
128
129   // Remember pending interrupt flag
130   if (IRQ_GetPending(PrivTimer_IRQn) != 0) {
131     IRQ_ClearPending (PrivTimer_IRQn);
132     PTIM_PendIRQ = 1U;
133   }
134
135   return (0);
136 }
137
138 // Acknowledge OS Tick IRQ.
139 int32_t  OS_Tick_AcknowledgeIRQ (void) {
140   PTIM_ClearEventFlag();
141   return (0);
142 }
143
144 // Get OS Tick IRQ number.
145 int32_t  OS_Tick_GetIRQn (void) {
146   return (PrivTimer_IRQn);
147 }
148
149 // Get OS Tick clock.
150 uint32_t OS_Tick_GetClock (void) {
151   return (SystemCoreClock);
152 }
153
154 // Get OS Tick interval.
155 uint32_t OS_Tick_GetInterval (void) {
156   return (PTIM_GetLoadValue() + 1U);
157 }
158
159 // Get OS Tick count value.
160 uint32_t OS_Tick_GetCount (void) {
161   uint32_t load = PTIM_GetLoadValue();
162   return  (load - PTIM_GetCurrentValue());
163 }
164
165 // Get OS Tick overflow status.
166 uint32_t OS_Tick_GetOverflow (void) {
167   return (PTIM->ISR & 1);
168 }
169
170 #endif  // PTIM