| 1 | /**************************************************************************//**
|
|---|
| 2 | * @file os_tick_ptim.c
|
|---|
| 3 | * @brief CMSIS OS Tick implementation for Private Timer
|
|---|
| 4 | * @version V1.0.2
|
|---|
| 5 | * @date 02. March 2018
|
|---|
| 6 | ******************************************************************************/
|
|---|
| 7 | /*
|
|---|
| 8 | * Copyright (c) 2017-2018 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 "RTE_Components.h"
|
|---|
| 26 | #include CMSIS_device_header
|
|---|
| 27 |
|
|---|
| 28 | #if defined(PTIM)
|
|---|
| 29 |
|
|---|
| 30 | #include "os_tick.h"
|
|---|
| 31 | #include "irq_ctrl.h"
|
|---|
| 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 | void 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 |
|
|---|
| 117 | /// Disable OS Tick.
|
|---|
| 118 | void OS_Tick_Disable (void) {
|
|---|
| 119 | uint32_t ctrl;
|
|---|
| 120 |
|
|---|
| 121 | // Stop the Private Timer
|
|---|
| 122 | ctrl = PTIM_GetControl();
|
|---|
| 123 | // Clear bit: Timer enable
|
|---|
| 124 | ctrl &= ~1U;
|
|---|
| 125 | PTIM_SetControl (ctrl);
|
|---|
| 126 |
|
|---|
| 127 | // Remember pending interrupt flag
|
|---|
| 128 | if (IRQ_GetPending(PrivTimer_IRQn) != 0) {
|
|---|
| 129 | IRQ_ClearPending (PrivTimer_IRQn);
|
|---|
| 130 | PTIM_PendIRQ = 1U;
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | // Acknowledge OS Tick IRQ.
|
|---|
| 135 | void OS_Tick_AcknowledgeIRQ (void) {
|
|---|
| 136 | PTIM_ClearEventFlag();
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | // Get OS Tick IRQ number.
|
|---|
| 140 | int32_t OS_Tick_GetIRQn (void) {
|
|---|
| 141 | return (PrivTimer_IRQn);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | // Get OS Tick clock.
|
|---|
| 145 | uint32_t OS_Tick_GetClock (void) {
|
|---|
| 146 | return (SystemCoreClock);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | // Get OS Tick interval.
|
|---|
| 150 | uint32_t OS_Tick_GetInterval (void) {
|
|---|
| 151 | return (PTIM_GetLoadValue() + 1U);
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | // Get OS Tick count value.
|
|---|
| 155 | uint32_t OS_Tick_GetCount (void) {
|
|---|
| 156 | uint32_t load = PTIM_GetLoadValue();
|
|---|
| 157 | return (load - PTIM_GetCurrentValue());
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | // Get OS Tick overflow status.
|
|---|
| 161 | uint32_t OS_Tick_GetOverflow (void) {
|
|---|
| 162 | return (PTIM->ISR & 1);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | #endif // PTIM
|
|---|