|
| 1 | +/* |
| 2 | +********************************************************************************************************* |
| 3 | +* uC/OS-III |
| 4 | +* The Real-Time Kernel |
| 5 | +* |
| 6 | +* Copyright 2009-2020 Silicon Laboratories Inc. www.silabs.com |
| 7 | +* |
| 8 | +* SPDX-License-Identifier: APACHE-2.0 |
| 9 | +* |
| 10 | +* This software is subject to an open source license and is distributed by |
| 11 | +* Silicon Laboratories Inc. pursuant to the terms of the Apache License, |
| 12 | +* Version 2.0 available at www.apache.org/licenses/LICENSE-2.0. |
| 13 | +* |
| 14 | +********************************************************************************************************* |
| 15 | +*/ |
| 16 | + |
| 17 | +/* |
| 18 | +********************************************************************************************************* |
| 19 | +* |
| 20 | +* uC/OS-III BOARD SUPPORT PACKAGE |
| 21 | +* Dynamic Tick BSP |
| 22 | +* |
| 23 | +* Filename : bsp_os_dt.c |
| 24 | +********************************************************************************************************* |
| 25 | +*/ |
| 26 | + |
| 27 | +/* |
| 28 | +********************************************************************************************************* |
| 29 | +* INCLUDE FILES |
| 30 | +********************************************************************************************************* |
| 31 | +*/ |
| 32 | + |
| 33 | +#include <cpu_core.h> |
| 34 | +#include <os.h> |
| 35 | + |
| 36 | + |
| 37 | +/* |
| 38 | +********************************************************************************************************* |
| 39 | +* LOCAL DEFINES |
| 40 | +********************************************************************************************************* |
| 41 | +*/ |
| 42 | + |
| 43 | +#if (OS_CFG_DYN_TICK_EN == DEF_ENABLED) |
| 44 | +#define TIMER_COUNT_HZ ($$$$) /* Frequency of the Dynamic Tick Timer. */ |
| 45 | +#define TIMER_TO_OSTICK(count) (((CPU_INT64U)(count) * OS_CFG_TICK_RATE_HZ) / TIMER_COUNT_HZ) |
| 46 | +#define OSTICK_TO_TIMER(ostick) (((CPU_INT64U)(ostick) * TIMER_COUNT_HZ) / OS_CFG_TICK_RATE_HZ) |
| 47 | + |
| 48 | + /* The max timer count should end on a 1 tick boundary. */ |
| 49 | +#define TIMER_COUNT_MAX (DEF_INT_32U_MAX_VAL - (DEF_INT_32U_MAX_VAL % OSTICK_TO_TIMER(1u))) |
| 50 | +#endif |
| 51 | + |
| 52 | + |
| 53 | +/* |
| 54 | +********************************************************************************************************* |
| 55 | +* LOCAL VARIABLES |
| 56 | +********************************************************************************************************* |
| 57 | +*/ |
| 58 | + |
| 59 | +#if (OS_CFG_DYN_TICK_EN == DEF_ENABLED) |
| 60 | +static OS_TICK TickDelta = 0u; /* Stored in OS Tick units. */ |
| 61 | +#endif |
| 62 | + |
| 63 | + |
| 64 | +/* |
| 65 | +********************************************************************************************************* |
| 66 | +* LOCAL FUNCTION PROTOTYPES |
| 67 | +********************************************************************************************************* |
| 68 | +*/ |
| 69 | + |
| 70 | +#if (OS_CFG_DYN_TICK_EN == DEF_ENABLED) |
| 71 | +static void BSP_DynTick_ISRHandler(void); |
| 72 | +#endif |
| 73 | + |
| 74 | + |
| 75 | +/* |
| 76 | +********************************************************************************************************* |
| 77 | +********************************************************************************************************* |
| 78 | +* GLOBAL FUNCTIONS |
| 79 | +********************************************************************************************************* |
| 80 | +********************************************************************************************************* |
| 81 | +*/ |
| 82 | + |
| 83 | +/* |
| 84 | +********************************************************************************************************* |
| 85 | +* BSP_OS_TickInit() |
| 86 | +* |
| 87 | +* Description : Initializes the tick interrupt for the OS. |
| 88 | +* |
| 89 | +* Argument(s) : none. |
| 90 | +* |
| 91 | +* Return(s) : none. |
| 92 | +* |
| 93 | +* Note(s) : (1) Must be called prior to OSStart() in main(). |
| 94 | +* |
| 95 | +* (2) This function ensures that the tick interrupt is disabled until BSP_OS_TickEn() is |
| 96 | +* called in the startup task. |
| 97 | +********************************************************************************************************* |
| 98 | +*/ |
| 99 | + |
| 100 | +void BSP_OS_TickInit (void) |
| 101 | +{ |
| 102 | + /* $$$$ */ /* Stop the Dynamic Tick Timer if running. */ |
| 103 | + |
| 104 | + /* $$$$ */ /* Configure the timer. */ |
| 105 | + /* $$$$ */ /* Frequency : TIMER_COUNT_HZ */ |
| 106 | + /* $$$$ */ /* Counter Match Value : TIMER_COUNT_MAX */ |
| 107 | + /* $$$$ */ /* Counter Value : 0 */ |
| 108 | + /* $$$$ */ /* Interrupt : On counter match */ |
| 109 | + |
| 110 | + /* $$$$ */ /* Install BSP_DynTick_ISRHandler as the int. handler. */ |
| 111 | + /* $$$$ */ /* Start the timer. */ |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +/* |
| 116 | +********************************************************************************************************* |
| 117 | +* BSP_OS_TickEnable() |
| 118 | +* |
| 119 | +* Description : Enable the OS tick interrupt. |
| 120 | +* |
| 121 | +* Argument(s) : none. |
| 122 | +* |
| 123 | +* Return(s) : none. |
| 124 | +* |
| 125 | +* Note(s) : none |
| 126 | +********************************************************************************************************* |
| 127 | +*/ |
| 128 | + |
| 129 | +void BSP_OS_TickEnable (void) |
| 130 | +{ |
| 131 | + /* $$$$ */ /* Enable Timer interrupt. */ |
| 132 | + /* $$$$ */ /* Start the Timer count generation. */ |
| 133 | +} |
| 134 | + |
| 135 | + |
| 136 | +/* |
| 137 | +********************************************************************************************************* |
| 138 | +* BSP_OS_TickDisable() |
| 139 | +* |
| 140 | +* Description : Disable the OS tick interrupt. |
| 141 | +* |
| 142 | +* Argument(s) : none. |
| 143 | +* |
| 144 | +* Return(s) : none. |
| 145 | +* |
| 146 | +* Note(s) : none |
| 147 | +********************************************************************************************************* |
| 148 | +*/ |
| 149 | + |
| 150 | +void BSP_OS_TickDisable (void) |
| 151 | +{ |
| 152 | + /* $$$$ */ /* Stop the Timer count generation. */ |
| 153 | + /* $$$$ */ /* Disable Timer interrupt. */ |
| 154 | +} |
| 155 | + |
| 156 | + |
| 157 | +/* |
| 158 | +********************************************************************************************************* |
| 159 | +********************************************************************************************************* |
| 160 | +** uC/OS-III DYNAMIC TICK |
| 161 | +********************************************************************************************************* |
| 162 | +********************************************************************************************************* |
| 163 | +*/ |
| 164 | + |
| 165 | +#if (OS_CFG_DYN_TICK_EN == DEF_ENABLED) |
| 166 | +/* |
| 167 | +********************************************************************************************************* |
| 168 | +* OS_DynTickGet() |
| 169 | +* |
| 170 | +* Description : Get the number of ticks which have elapsed since the last delta was set. |
| 171 | +* |
| 172 | +* Argument(s) : none. |
| 173 | +* |
| 174 | +* Return(s) : An unsigned integer between 0 and TickDelta, inclusive. |
| 175 | +* |
| 176 | +* Note(s) : 1) This function is an INTERNAL uC/OS-III function & MUST NOT be called by the |
| 177 | +* application. |
| 178 | +* |
| 179 | +* 2) This function is called with kernel-aware interrupts disabled. |
| 180 | +********************************************************************************************************* |
| 181 | +*/ |
| 182 | + |
| 183 | +OS_TICK OS_DynTickGet (void) |
| 184 | +{ |
| 185 | + CPU_INT32U tmrcnt; |
| 186 | + |
| 187 | + |
| 188 | + tmrcnt = /* $$$$ */; /* Read current timer count. */ |
| 189 | + |
| 190 | + if (/* $$$$ */) { /* Check timer interrupt flag. */ |
| 191 | + return (TickDelta); /* Counter Overflow has occured. */ |
| 192 | + } |
| 193 | + |
| 194 | + tmrcnt = TIMER_TO_OSTICK(tmrcnt); /* Otherwise, the value we read is valid. */ |
| 195 | + |
| 196 | + return ((OS_TICK)tmrcnt); |
| 197 | +} |
| 198 | + |
| 199 | + |
| 200 | +/* |
| 201 | +********************************************************************************************************* |
| 202 | +* OS_DynTickSet() |
| 203 | +* |
| 204 | +* Description : Sets the number of ticks that the kernel wants to expire before the next interrupt. |
| 205 | +* |
| 206 | +* Argument(s) : ticks number of ticks the kernel wants to delay. |
| 207 | +* 0 indicates an indefinite delay. |
| 208 | +* |
| 209 | +* Return(s) : The actual number of ticks which will elapse before the next interrupt. |
| 210 | +* (See Note #3). |
| 211 | +* |
| 212 | +* Note(s) : 1) This function is an INTERNAL uC/OS-III function & MUST NOT be called by the |
| 213 | +* application. |
| 214 | +* |
| 215 | +* 2) This function is called with kernel-aware interrupts disabled. |
| 216 | +* |
| 217 | +* 3) It is possible for the kernel to specify a delay that is too large for our |
| 218 | +* hardware timer, or an indefinite delay. In these cases, we should program the timer |
| 219 | +* to count the maximum number of ticks possible. The value we return should then |
| 220 | +* be the timer maximum converted into units of OS_TICK. |
| 221 | +********************************************************************************************************* |
| 222 | +*/ |
| 223 | + |
| 224 | +OS_TICK OS_DynTickSet (OS_TICK ticks) |
| 225 | +{ |
| 226 | + CPU_INT32U tmrcnt; |
| 227 | + |
| 228 | + |
| 229 | + tmrcnt = OSTICK_TO_TIMER(ticks); |
| 230 | + |
| 231 | + if ((tmrcnt >= TIMER_COUNT_MAX) || /* If the delay exceeds our timer's capacity, ... */ |
| 232 | + (tmrcnt == 0u)) { /* ... or the kernel wants an indefinite delay. */ |
| 233 | + tmrcnt = TIMER_COUNT_MAX; /* Count as many ticks as we are able. */ |
| 234 | + } |
| 235 | + |
| 236 | + TickDelta = TIMER_TO_OSTICK(tmrcnt); /* Convert the timer count into OS_TICK units. */ |
| 237 | + |
| 238 | + /* $$$$ */ /* Stop the timer. */ |
| 239 | + /* $$$$ */ /* Clear the timer interrupt. */ |
| 240 | + /* Set new timeout. */ |
| 241 | + /* $$$$ */ /* Counter Match Value : tmrcnt */ |
| 242 | + /* $$$$ */ /* Counter Value : 0 */ |
| 243 | + /* $$$$ */ /* Start the timer. */ |
| 244 | + |
| 245 | + return (TickDelta); /* Return the number ticks that will elapse before ... */ |
| 246 | + /* ... the next interrupt. */ |
| 247 | +} |
| 248 | + |
| 249 | + |
| 250 | +/* |
| 251 | +********************************************************************************************************* |
| 252 | +* BSP_DynTick_ISRHandler() |
| 253 | +* |
| 254 | +* Description : Dynamic Tick ISR handler. |
| 255 | +* |
| 256 | +* Argument(s) : none. |
| 257 | +* |
| 258 | +* Return(s) : none. |
| 259 | +* |
| 260 | +* Note(s) : none. |
| 261 | +********************************************************************************************************* |
| 262 | +*/ |
| 263 | + |
| 264 | +static void BSP_DynTick_ISRHandler (void) |
| 265 | +{ |
| 266 | + CPU_SR_ALLOC(); |
| 267 | + |
| 268 | + |
| 269 | + CPU_CRITICAL_ENTER(); |
| 270 | + OSIntEnter(); |
| 271 | + CPU_CRITICAL_EXIT(); |
| 272 | + |
| 273 | + OSTimeDynTick(TickDelta); /* Next delta will be set by the kernel. */ |
| 274 | + |
| 275 | + OSIntExit(); |
| 276 | +} |
| 277 | +#endif /* End of uC/OS-III Dynamic Tick module. */ |
| 278 | + |
| 279 | + |
| 280 | +/* |
| 281 | +********************************************************************************************************* |
| 282 | +* MODULE END |
| 283 | +********************************************************************************************************* |
| 284 | +*/ |
0 commit comments