1 /**************************************************************************//**
3 * @brief CMSIS OS Tick implementation for Private Timer
6 ******************************************************************************/
8 * Copyright (c) 2017-2018 Arm Limited. All rights reserved.
10 * SPDX-License-Identifier: Apache-2.0
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
16 * www.apache.org/licenses/LICENSE-2.0
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.
25 #include "RTE_Components.h"
26 #include CMSIS_device_header
33 #ifndef PTIM_IRQ_PRIORITY
34 #define PTIM_IRQ_PRIORITY 0xFFU
37 static uint8_t PTIM_PendIRQ; // Timer interrupt pending flag
40 int32_t OS_Tick_Setup (uint32_t freq, IRQHandler_t handler) {
51 // Private Timer runs with the system frequency
52 load = (SystemCoreClock / freq) - 1U;
54 // Disable Private Timer and set load value
56 PTIM_SetLoadValue (load);
58 // Disable corresponding IRQ
59 IRQ_Disable (PrivTimer_IRQn);
60 IRQ_ClearPending(PrivTimer_IRQn);
62 // Determine number of implemented priority bits
63 IRQ_SetPriority (PrivTimer_IRQn, 0xFFU);
65 prio = IRQ_GetPriority (PrivTimer_IRQn);
67 // At least bits [7:4] must be implemented
68 if ((prio & 0xF0U) == 0U) {
72 for (bits = 0; bits < 4; bits++) {
73 if ((prio & 0x01) != 0) {
79 // Adjust configured priority to the number of implemented priority bits
80 prio = (PTIM_IRQ_PRIORITY << bits) & 0xFFUL;
82 // Set Private Timer interrupt priority
83 IRQ_SetPriority(PrivTimer_IRQn, prio-1U);
85 // Set edge-triggered IRQ
86 IRQ_SetMode(PrivTimer_IRQn, IRQ_MODE_TRIG_EDGE);
88 // Register tick interrupt handler function
89 IRQ_SetHandler(PrivTimer_IRQn, handler);
91 // Enable corresponding interrupt
92 IRQ_Enable (PrivTimer_IRQn);
94 // Set bits: IRQ enable and Auto reload
95 PTIM_SetControl (0x06U);
101 void OS_Tick_Enable (void) {
104 // Set pending interrupt if flag set
105 if (PTIM_PendIRQ != 0U) {
107 IRQ_SetPending (PrivTimer_IRQn);
110 // Start the Private Timer
111 ctrl = PTIM_GetControl();
112 // Set bit: Timer enable
114 PTIM_SetControl (ctrl);
118 void OS_Tick_Disable (void) {
121 // Stop the Private Timer
122 ctrl = PTIM_GetControl();
123 // Clear bit: Timer enable
125 PTIM_SetControl (ctrl);
127 // Remember pending interrupt flag
128 if (IRQ_GetPending(PrivTimer_IRQn) != 0) {
129 IRQ_ClearPending (PrivTimer_IRQn);
134 // Acknowledge OS Tick IRQ.
135 void OS_Tick_AcknowledgeIRQ (void) {
136 PTIM_ClearEventFlag();
139 // Get OS Tick IRQ number.
140 int32_t OS_Tick_GetIRQn (void) {
141 return (PrivTimer_IRQn);
144 // Get OS Tick clock.
145 uint32_t OS_Tick_GetClock (void) {
146 return (SystemCoreClock);
149 // Get OS Tick interval.
150 uint32_t OS_Tick_GetInterval (void) {
151 return (PTIM_GetLoadValue() + 1U);
154 // Get OS Tick count value.
155 uint32_t OS_Tick_GetCount (void) {
156 uint32_t load = PTIM_GetLoadValue();
157 return (load - PTIM_GetCurrentValue());
160 // Get OS Tick overflow status.
161 uint32_t OS_Tick_GetOverflow (void) {
162 return (PTIM->ISR & 1);