| 1 |
/* |
| 2 |
FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd. |
| 3 |
All rights reserved |
| 4 |
|
| 5 |
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION. |
| 6 |
|
| 7 |
This file is part of the FreeRTOS distribution. |
| 8 |
|
| 9 |
FreeRTOS is free software; you can redistribute it and/or modify it under |
| 10 |
the terms of the GNU General Public License (version 2) as published by the |
| 11 |
Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception. |
| 12 |
|
| 13 |
*************************************************************************** |
| 14 |
>>! NOTE: The modification to the GPL is included to allow you to !<< |
| 15 |
>>! distribute a combined work that includes FreeRTOS without being !<< |
| 16 |
>>! obliged to provide the source code for proprietary components !<< |
| 17 |
>>! outside of the FreeRTOS kernel. !<< |
| 18 |
*************************************************************************** |
| 19 |
|
| 20 |
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY |
| 21 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 22 |
FOR A PARTICULAR PURPOSE. Full license text is available on the following |
| 23 |
link: http://www.freertos.org/a00114.html |
| 24 |
|
| 25 |
*************************************************************************** |
| 26 |
* * |
| 27 |
* FreeRTOS provides completely free yet professionally developed, * |
| 28 |
* robust, strictly quality controlled, supported, and cross * |
| 29 |
* platform software that is more than just the market leader, it * |
| 30 |
* is the industry's de facto standard. * |
| 31 |
* * |
| 32 |
* Help yourself get started quickly while simultaneously helping * |
| 33 |
* to support the FreeRTOS project by purchasing a FreeRTOS * |
| 34 |
* tutorial book, reference manual, or both: * |
| 35 |
* http://www.FreeRTOS.org/Documentation * |
| 36 |
* * |
| 37 |
*************************************************************************** |
| 38 |
|
| 39 |
http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading |
| 40 |
the FAQ page "My application does not run, what could be wrong?". Have you |
| 41 |
defined configASSERT()? |
| 42 |
|
| 43 |
http://www.FreeRTOS.org/support - In return for receiving this top quality |
| 44 |
embedded software for free we request you assist our global community by |
| 45 |
participating in the support forum. |
| 46 |
|
| 47 |
http://www.FreeRTOS.org/training - Investing in training allows your team to |
| 48 |
be as productive as possible as early as possible. Now you can receive |
| 49 |
FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers |
| 50 |
Ltd, and the world's leading authority on the world's leading RTOS. |
| 51 |
|
| 52 |
http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products, |
| 53 |
including FreeRTOS+Trace - an indispensable productivity tool, a DOS |
| 54 |
compatible FAT file system, and our tiny thread aware UDP/IP stack. |
| 55 |
|
| 56 |
http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate. |
| 57 |
Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS. |
| 58 |
|
| 59 |
http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High |
| 60 |
Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS |
| 61 |
licenses offer ticketed support, indemnification and commercial middleware. |
| 62 |
|
| 63 |
http://www.SafeRTOS.com - High Integrity Systems also provide a safety |
| 64 |
engineered and independently SIL3 certified version for use in safety and |
| 65 |
mission critical applications that require provable dependability. |
| 66 |
|
| 67 |
1 tab == 4 spaces! |
| 68 |
*/ |
| 69 |
|
| 70 |
/*----------------------------------------------------------- |
| 71 |
* Implementation of functions defined in portable.h for the ARM CM3 port. |
| 72 |
*----------------------------------------------------------*/ |
| 73 |
|
| 74 |
/* Scheduler includes. */ |
| 75 |
#include "FreeRTOS.h" |
| 76 |
#include "task.h" |
| 77 |
|
| 78 |
/* For backward compatibility, ensure configKERNEL_INTERRUPT_PRIORITY is |
| 79 |
defined. The value should also ensure backward compatibility. |
| 80 |
FreeRTOS.org versions prior to V4.4.0 did not include this definition. */ |
| 81 |
#ifndef configKERNEL_INTERRUPT_PRIORITY |
| 82 |
#define configKERNEL_INTERRUPT_PRIORITY 255 |
| 83 |
#endif |
| 84 |
|
| 85 |
#ifndef configSYSTICK_CLOCK_HZ |
| 86 |
#define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ |
| 87 |
/* Ensure the SysTick is clocked at the same frequency as the core. */ |
| 88 |
#define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL ) |
| 89 |
#else |
| 90 |
/* The way the SysTick is clocked is not modified in case it is not the same |
| 91 |
as the core. */ |
| 92 |
#define portNVIC_SYSTICK_CLK_BIT ( 0 ) |
| 93 |
#endif |
| 94 |
|
| 95 |
/* Constants required to manipulate the core. Registers first... */ |
| 96 |
#define portNVIC_SYSTICK_CTRL_REG ( * ( ( volatile uint32_t * ) 0xe000e010 ) ) |
| 97 |
#define portNVIC_SYSTICK_LOAD_REG ( * ( ( volatile uint32_t * ) 0xe000e014 ) ) |
| 98 |
#define portNVIC_SYSTICK_CURRENT_VALUE_REG ( * ( ( volatile uint32_t * ) 0xe000e018 ) ) |
| 99 |
#define portNVIC_SYSPRI2_REG ( * ( ( volatile uint32_t * ) 0xe000ed20 ) ) |
| 100 |
/* ...then bits in the registers. */ |
| 101 |
#define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL ) |
| 102 |
#define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL ) |
| 103 |
#define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL ) |
| 104 |
#define portNVIC_PENDSVCLEAR_BIT ( 1UL << 27UL ) |
| 105 |
#define portNVIC_PEND_SYSTICK_CLEAR_BIT ( 1UL << 25UL ) |
| 106 |
|
| 107 |
#define portNVIC_PENDSV_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL ) |
| 108 |
#define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24UL ) |
| 109 |
|
| 110 |
/* Constants required to check the validity of an interrupt priority. */ |
| 111 |
#define portFIRST_USER_INTERRUPT_NUMBER ( 16 ) |
| 112 |
#define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 ) |
| 113 |
#define portAIRCR_REG ( * ( ( volatile uint32_t * ) 0xE000ED0C ) ) |
| 114 |
#define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff ) |
| 115 |
#define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 ) |
| 116 |
#define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 ) |
| 117 |
#define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL ) |
| 118 |
#define portPRIGROUP_SHIFT ( 8UL ) |
| 119 |
|
| 120 |
/* Masks off all bits but the VECTACTIVE bits in the ICSR register. */ |
| 121 |
#define portVECTACTIVE_MASK ( 0xFFUL ) |
| 122 |
|
| 123 |
/* Constants required to set up the initial stack. */ |
| 124 |
#define portINITIAL_XPSR ( 0x01000000UL ) |
| 125 |
|
| 126 |
/* The systick is a 24-bit counter. */ |
| 127 |
#define portMAX_24_BIT_NUMBER ( 0xffffffUL ) |
| 128 |
|
| 129 |
/* A fiddle factor to estimate the number of SysTick counts that would have |
| 130 |
occurred while the SysTick counter is stopped during tickless idle |
| 131 |
calculations. */ |
| 132 |
#define portMISSED_COUNTS_FACTOR ( 45UL ) |
| 133 |
|
| 134 |
/* For strict compliance with the Cortex-M spec the task start address should |
| 135 |
have bit-0 clear, as it is loaded into the PC on exit from an ISR. */ |
| 136 |
#define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL ) |
| 137 |
|
| 138 |
/* Let the user override the pre-loading of the initial LR with the address of |
| 139 |
prvTaskExitError() in case it messes up unwinding of the stack in the |
| 140 |
debugger. */ |
| 141 |
#ifdef configTASK_RETURN_ADDRESS |
| 142 |
#define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS |
| 143 |
#else |
| 144 |
#define portTASK_RETURN_ADDRESS prvTaskExitError |
| 145 |
#endif |
| 146 |
|
| 147 |
/* Each task maintains its own interrupt status in the critical nesting |
| 148 |
variable. */ |
| 149 |
static UBaseType_t uxCriticalNesting = 0xaaaaaaaa; |
| 150 |
|
| 151 |
/* |
| 152 |
* Setup the timer to generate the tick interrupts. The implementation in this |
| 153 |
* file is weak to allow application writers to change the timer used to |
| 154 |
* generate the tick interrupt. |
| 155 |
*/ |
| 156 |
void vPortSetupTimerInterrupt( void ); |
| 157 |
|
| 158 |
/* |
| 159 |
* Exception handlers. |
| 160 |
*/ |
| 161 |
void xPortPendSVHandler( void ) __attribute__ (( naked )); |
| 162 |
void xPortSysTickHandler( void ); |
| 163 |
void vPortSVCHandler( void ) __attribute__ (( naked )); |
| 164 |
|
| 165 |
/* |
| 166 |
* Start first task is a separate function so it can be tested in isolation. |
| 167 |
*/ |
| 168 |
static void prvPortStartFirstTask( void ) __attribute__ (( naked )); |
| 169 |
|
| 170 |
/* |
| 171 |
* Used to catch tasks that attempt to return from their implementing function. |
| 172 |
*/ |
| 173 |
static void prvTaskExitError( void ); |
| 174 |
|
| 175 |
/*-----------------------------------------------------------*/ |
| 176 |
|
| 177 |
/* |
| 178 |
* The number of SysTick increments that make up one tick period. |
| 179 |
*/ |
| 180 |
#if configUSE_TICKLESS_IDLE == 1 |
| 181 |
static uint32_t ulTimerCountsForOneTick = 0; |
| 182 |
#endif /* configUSE_TICKLESS_IDLE */ |
| 183 |
|
| 184 |
/* |
| 185 |
* The maximum number of tick periods that can be suppressed is limited by the |
| 186 |
* 24 bit resolution of the SysTick timer. |
| 187 |
*/ |
| 188 |
#if configUSE_TICKLESS_IDLE == 1 |
| 189 |
static uint32_t xMaximumPossibleSuppressedTicks = 0; |
| 190 |
#endif /* configUSE_TICKLESS_IDLE */ |
| 191 |
|
| 192 |
/* |
| 193 |
* Compensate for the CPU cycles that pass while the SysTick is stopped (low |
| 194 |
* power functionality only. |
| 195 |
*/ |
| 196 |
#if configUSE_TICKLESS_IDLE == 1 |
| 197 |
static uint32_t ulStoppedTimerCompensation = 0; |
| 198 |
#endif /* configUSE_TICKLESS_IDLE */ |
| 199 |
|
| 200 |
/* |
| 201 |
* Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure |
| 202 |
* FreeRTOS API functions are not called from interrupts that have been assigned |
| 203 |
* a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY. |
| 204 |
*/ |
| 205 |
#if ( configASSERT_DEFINED == 1 ) |
| 206 |
static uint8_t ucMaxSysCallPriority = 0; |
| 207 |
static uint32_t ulMaxPRIGROUPValue = 0; |
| 208 |
static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16; |
| 209 |
#endif /* configASSERT_DEFINED */ |
| 210 |
|
| 211 |
/*-----------------------------------------------------------*/ |
| 212 |
|
| 213 |
/* |
| 214 |
* See header file for description. |
| 215 |
*/ |
| 216 |
StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters ) |
| 217 |
{ |
| 218 |
/* Simulate the stack frame as it would be created by a context switch |
| 219 |
interrupt. */ |
| 220 |
pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */ |
| 221 |
*pxTopOfStack = portINITIAL_XPSR; /* xPSR */ |
| 222 |
pxTopOfStack--; |
| 223 |
*pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC */ |
| 224 |
pxTopOfStack--; |
| 225 |
*pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* LR */ |
| 226 |
pxTopOfStack -= 5; /* R12, R3, R2 and R1. */ |
| 227 |
*pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */ |
| 228 |
pxTopOfStack -= 8; /* R11, R10, R9, R8, R7, R6, R5 and R4. */ |
| 229 |
|
| 230 |
return pxTopOfStack; |
| 231 |
} |
| 232 |
/*-----------------------------------------------------------*/ |
| 233 |
|
| 234 |
static void prvTaskExitError( void ) |
| 235 |
{ |
| 236 |
/* A function that implements a task must not exit or attempt to return to |
| 237 |
its caller as there is nothing to return to. If a task wants to exit it |
| 238 |
should instead call vTaskDelete( NULL ). |
| 239 |
|
| 240 |
Artificially force an assert() to be triggered if configASSERT() is |
| 241 |
defined, then stop here so application writers can catch the error. */ |
| 242 |
configASSERT( uxCriticalNesting == ~0UL ); |
| 243 |
portDISABLE_INTERRUPTS(); |
| 244 |
for( ;; ); |
| 245 |
} |
| 246 |
/*-----------------------------------------------------------*/ |
| 247 |
|
| 248 |
void vPortSVCHandler( void ) |
| 249 |
{ |
| 250 |
__asm volatile ( |
| 251 |
" ldr r3, pxCurrentTCBConst2 \n" /* Restore the context. */ |
| 252 |
" ldr r1, [r3] \n" /* Use pxCurrentTCBConst to get the pxCurrentTCB address. */ |
| 253 |
" ldr r0, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. */ |
| 254 |
" ldmia r0!, {r4-r11} \n" /* Pop the registers that are not automatically saved on exception entry and the critical nesting count. */ |
| 255 |
" msr psp, r0 \n" /* Restore the task stack pointer. */ |
| 256 |
" isb \n" |
| 257 |
" mov r0, #0 \n" |
| 258 |
" msr basepri, r0 \n" |
| 259 |
" orr r14, #0xd \n" |
| 260 |
" bx r14 \n" |
| 261 |
" \n" |
| 262 |
" .align 4 \n" |
| 263 |
"pxCurrentTCBConst2: .word pxCurrentTCB \n" |
| 264 |
); |
| 265 |
} |
| 266 |
/*-----------------------------------------------------------*/ |
| 267 |
|
| 268 |
static void prvPortStartFirstTask( void ) |
| 269 |
{ |
| 270 |
__asm volatile( |
| 271 |
" ldr r0, =0xE000ED08 \n" /* Use the NVIC offset register to locate the stack. */ |
| 272 |
" ldr r0, [r0] \n" |
| 273 |
" ldr r0, [r0] \n" |
| 274 |
" msr msp, r0 \n" /* Set the msp back to the start of the stack. */ |
| 275 |
" cpsie i \n" /* Globally enable interrupts. */ |
| 276 |
" cpsie f \n" |
| 277 |
" dsb \n" |
| 278 |
" isb \n" |
| 279 |
" svc 0 \n" /* System call to start first task. */ |
| 280 |
" nop \n" |
| 281 |
); |
| 282 |
} |
| 283 |
/*-----------------------------------------------------------*/ |
| 284 |
|
| 285 |
/* |
| 286 |
* See header file for description. |
| 287 |
*/ |
| 288 |
BaseType_t xPortStartScheduler( void ) |
| 289 |
{ |
| 290 |
/* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. |
| 291 |
See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */ |
| 292 |
configASSERT( configMAX_SYSCALL_INTERRUPT_PRIORITY ); |
| 293 |
|
| 294 |
#if( configASSERT_DEFINED == 1 ) |
| 295 |
{ |
| 296 |
volatile uint32_t ulOriginalPriority; |
| 297 |
volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER ); |
| 298 |
volatile uint8_t ucMaxPriorityValue; |
| 299 |
|
| 300 |
/* Determine the maximum priority from which ISR safe FreeRTOS API |
| 301 |
functions can be called. ISR safe functions are those that end in |
| 302 |
"FromISR". FreeRTOS maintains separate thread and ISR API functions to |
| 303 |
ensure interrupt entry is as fast and simple as possible. |
| 304 |
|
| 305 |
Save the interrupt priority value that is about to be clobbered. */ |
| 306 |
ulOriginalPriority = *pucFirstUserPriorityRegister; |
| 307 |
|
| 308 |
/* Determine the number of priority bits available. First write to all |
| 309 |
possible bits. */ |
| 310 |
*pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE; |
| 311 |
|
| 312 |
/* Read the value back to see how many bits stuck. */ |
| 313 |
ucMaxPriorityValue = *pucFirstUserPriorityRegister; |
| 314 |
|
| 315 |
/* Use the same mask on the maximum system call priority. */ |
| 316 |
ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue; |
| 317 |
|
| 318 |
/* Calculate the maximum acceptable priority group value for the number |
| 319 |
of bits read back. */ |
| 320 |
ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS; |
| 321 |
while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE ) |
| 322 |
{ |
| 323 |
ulMaxPRIGROUPValue--; |
| 324 |
ucMaxPriorityValue <<= ( uint8_t ) 0x01; |
| 325 |
} |
| 326 |
|
| 327 |
/* Shift the priority group value back to its position within the AIRCR |
| 328 |
register. */ |
| 329 |
ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT; |
| 330 |
ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK; |
| 331 |
|
| 332 |
/* Restore the clobbered interrupt priority register to its original |
| 333 |
value. */ |
| 334 |
*pucFirstUserPriorityRegister = ulOriginalPriority; |
| 335 |
} |
| 336 |
#endif /* conifgASSERT_DEFINED */ |
| 337 |
|
| 338 |
/* Make PendSV and SysTick the lowest priority interrupts. */ |
| 339 |
portNVIC_SYSPRI2_REG |= portNVIC_PENDSV_PRI; |
| 340 |
portNVIC_SYSPRI2_REG |= portNVIC_SYSTICK_PRI; |
| 341 |
|
| 342 |
/* Start the timer that generates the tick ISR. Interrupts are disabled |
| 343 |
here already. */ |
| 344 |
vPortSetupTimerInterrupt(); |
| 345 |
|
| 346 |
/* Initialise the critical nesting count ready for the first task. */ |
| 347 |
uxCriticalNesting = 0; |
| 348 |
|
| 349 |
/* Start the first task. */ |
| 350 |
prvPortStartFirstTask(); |
| 351 |
|
| 352 |
/* Should never get here as the tasks will now be executing! Call the task |
| 353 |
exit error function to prevent compiler warnings about a static function |
| 354 |
not being called in the case that the application writer overrides this |
| 355 |
functionality by defining configTASK_RETURN_ADDRESS. */ |
| 356 |
prvTaskExitError(); |
| 357 |
|
| 358 |
/* Should not get here! */ |
| 359 |
return 0; |
| 360 |
} |
| 361 |
/*-----------------------------------------------------------*/ |
| 362 |
|
| 363 |
void vPortEndScheduler( void ) |
| 364 |
{ |
| 365 |
/* Not implemented in ports where there is nothing to return to. |
| 366 |
Artificially force an assert. */ |
| 367 |
configASSERT( uxCriticalNesting == 1000UL ); |
| 368 |
} |
| 369 |
/*-----------------------------------------------------------*/ |
| 370 |
|
| 371 |
void vPortEnterCritical( void ) |
| 372 |
{ |
| 373 |
portDISABLE_INTERRUPTS(); |
| 374 |
uxCriticalNesting++; |
| 375 |
|
| 376 |
/* This is not the interrupt safe version of the enter critical function so |
| 377 |
assert() if it is being called from an interrupt context. Only API |
| 378 |
functions that end in "FromISR" can be used in an interrupt. Only assert if |
| 379 |
the critical nesting count is 1 to protect against recursive calls if the |
| 380 |
assert function also uses a critical section. */ |
| 381 |
if( uxCriticalNesting == 1 ) |
| 382 |
{ |
| 383 |
configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 ); |
| 384 |
} |
| 385 |
} |
| 386 |
/*-----------------------------------------------------------*/ |
| 387 |
|
| 388 |
void vPortExitCritical( void ) |
| 389 |
{ |
| 390 |
configASSERT( uxCriticalNesting ); |
| 391 |
uxCriticalNesting--; |
| 392 |
if( uxCriticalNesting == 0 ) |
| 393 |
{ |
| 394 |
portENABLE_INTERRUPTS(); |
| 395 |
} |
| 396 |
} |
| 397 |
/*-----------------------------------------------------------*/ |
| 398 |
|
| 399 |
void xPortPendSVHandler( void ) |
| 400 |
{ |
| 401 |
/* This is a naked function. */ |
| 402 |
|
| 403 |
__asm volatile |
| 404 |
( |
| 405 |
" mrs r0, psp \n" |
| 406 |
" isb \n" |
| 407 |
" \n" |
| 408 |
" ldr r3, pxCurrentTCBConst \n" /* Get the location of the current TCB. */ |
| 409 |
" ldr r2, [r3] \n" |
| 410 |
" \n" |
| 411 |
" stmdb r0!, {r4-r11} \n" /* Save the remaining registers. */ |
| 412 |
" str r0, [r2] \n" /* Save the new top of stack into the first member of the TCB. */ |
| 413 |
" \n" |
| 414 |
" stmdb sp!, {r3, r14} \n" |
| 415 |
" mov r0, %0 \n" |
| 416 |
" msr basepri, r0 \n" |
| 417 |
" bl vTaskSwitchContext \n" |
| 418 |
" mov r0, #0 \n" |
| 419 |
" msr basepri, r0 \n" |
| 420 |
" ldmia sp!, {r3, r14} \n" |
| 421 |
" \n" /* Restore the context, including the critical nesting count. */ |
| 422 |
" ldr r1, [r3] \n" |
| 423 |
" ldr r0, [r1] \n" /* The first item in pxCurrentTCB is the task top of stack. */ |
| 424 |
" ldmia r0!, {r4-r11} \n" /* Pop the registers. */ |
| 425 |
" msr psp, r0 \n" |
| 426 |
" isb \n" |
| 427 |
" bx r14 \n" |
| 428 |
" \n" |
| 429 |
" .align 4 \n" |
| 430 |
"pxCurrentTCBConst: .word pxCurrentTCB \n" |
| 431 |
::"i"(configMAX_SYSCALL_INTERRUPT_PRIORITY) |
| 432 |
); |
| 433 |
} |
| 434 |
/*-----------------------------------------------------------*/ |
| 435 |
|
| 436 |
void xPortSysTickHandler( void ) |
| 437 |
{ |
| 438 |
/* The SysTick runs at the lowest interrupt priority, so when this interrupt |
| 439 |
executes all interrupts must be unmasked. There is therefore no need to |
| 440 |
save and then restore the interrupt mask value as its value is already |
| 441 |
known. */ |
| 442 |
portDISABLE_INTERRUPTS(); |
| 443 |
{ |
| 444 |
/* Increment the RTOS tick. */ |
| 445 |
if( xTaskIncrementTick() != pdFALSE ) |
| 446 |
{ |
| 447 |
/* A context switch is required. Context switching is performed in |
| 448 |
the PendSV interrupt. Pend the PendSV interrupt. */ |
| 449 |
portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; |
| 450 |
} |
| 451 |
} |
| 452 |
portENABLE_INTERRUPTS(); |
| 453 |
} |
| 454 |
/*-----------------------------------------------------------*/ |
| 455 |
|
| 456 |
#if configUSE_TICKLESS_IDLE == 1 |
| 457 |
|
| 458 |
__attribute__((weak)) void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ) |
| 459 |
{ |
| 460 |
uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements, ulSysTickCTRL; |
| 461 |
TickType_t xModifiableIdleTime; |
| 462 |
|
| 463 |
/* Make sure the SysTick reload value does not overflow the counter. */ |
| 464 |
if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks ) |
| 465 |
{ |
| 466 |
xExpectedIdleTime = xMaximumPossibleSuppressedTicks; |
| 467 |
} |
| 468 |
|
| 469 |
/* Stop the SysTick momentarily. The time the SysTick is stopped for |
| 470 |
is accounted for as best it can be, but using the tickless mode will |
| 471 |
inevitably result in some tiny drift of the time maintained by the |
| 472 |
kernel with respect to calendar time. */ |
| 473 |
portNVIC_SYSTICK_CTRL_REG &= ~portNVIC_SYSTICK_ENABLE_BIT; |
| 474 |
|
| 475 |
/* Calculate the reload value required to wait xExpectedIdleTime |
| 476 |
tick periods. -1 is used because this code will execute part way |
| 477 |
through one of the tick periods. */ |
| 478 |
ulReloadValue = portNVIC_SYSTICK_CURRENT_VALUE_REG + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) ); |
| 479 |
if( ulReloadValue > ulStoppedTimerCompensation ) |
| 480 |
{ |
| 481 |
ulReloadValue -= ulStoppedTimerCompensation; |
| 482 |
} |
| 483 |
|
| 484 |
/* Enter a critical section but don't use the taskENTER_CRITICAL() |
| 485 |
method as that will mask interrupts that should exit sleep mode. */ |
| 486 |
__asm volatile( "cpsid i" ); |
| 487 |
__asm volatile( "dsb" ); |
| 488 |
__asm volatile( "isb" ); |
| 489 |
|
| 490 |
/* If a context switch is pending or a task is waiting for the scheduler |
| 491 |
to be unsuspended then abandon the low power entry. */ |
| 492 |
if( eTaskConfirmSleepModeStatus() == eAbortSleep ) |
| 493 |
{ |
| 494 |
/* Restart from whatever is left in the count register to complete |
| 495 |
this tick period. */ |
| 496 |
portNVIC_SYSTICK_LOAD_REG = portNVIC_SYSTICK_CURRENT_VALUE_REG; |
| 497 |
|
| 498 |
/* Restart SysTick. */ |
| 499 |
portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT; |
| 500 |
|
| 501 |
/* Reset the reload register to the value required for normal tick |
| 502 |
periods. */ |
| 503 |
portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL; |
| 504 |
|
| 505 |
/* Re-enable interrupts - see comments above the cpsid instruction() |
| 506 |
above. */ |
| 507 |
__asm volatile( "cpsie i" ); |
| 508 |
} |
| 509 |
else |
| 510 |
{ |
| 511 |
/* Set the new reload value. */ |
| 512 |
portNVIC_SYSTICK_LOAD_REG = ulReloadValue; |
| 513 |
|
| 514 |
/* Clear the SysTick count flag and set the count value back to |
| 515 |
zero. */ |
| 516 |
portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL; |
| 517 |
|
| 518 |
/* Restart SysTick. */ |
| 519 |
portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT; |
| 520 |
|
| 521 |
/* Sleep until something happens. configPRE_SLEEP_PROCESSING() can |
| 522 |
set its parameter to 0 to indicate that its implementation contains |
| 523 |
its own wait for interrupt or wait for event instruction, and so wfi |
| 524 |
should not be executed again. However, the original expected idle |
| 525 |
time variable must remain unmodified, so a copy is taken. */ |
| 526 |
xModifiableIdleTime = xExpectedIdleTime; |
| 527 |
configPRE_SLEEP_PROCESSING( xModifiableIdleTime ); |
| 528 |
if( xModifiableIdleTime > 0 ) |
| 529 |
{ |
| 530 |
__asm volatile( "dsb" ); |
| 531 |
__asm volatile( "wfi" ); |
| 532 |
__asm volatile( "isb" ); |
| 533 |
} |
| 534 |
configPOST_SLEEP_PROCESSING( xExpectedIdleTime ); |
| 535 |
|
| 536 |
/* Stop SysTick. Again, the time the SysTick is stopped for is |
| 537 |
accounted for as best it can be, but using the tickless mode will |
| 538 |
inevitably result in some tiny drift of the time maintained by the |
| 539 |
kernel with respect to calendar time. */ |
| 540 |
ulSysTickCTRL = portNVIC_SYSTICK_CTRL_REG; |
| 541 |
portNVIC_SYSTICK_CTRL_REG = ( ulSysTickCTRL & ~portNVIC_SYSTICK_ENABLE_BIT ); |
| 542 |
|
| 543 |
/* Re-enable interrupts - see comments above the cpsid instruction() |
| 544 |
above. */ |
| 545 |
__asm volatile( "cpsie i" ); |
| 546 |
|
| 547 |
if( ( ulSysTickCTRL & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 ) |
| 548 |
{ |
| 549 |
uint32_t ulCalculatedLoadValue; |
| 550 |
|
| 551 |
/* The tick interrupt has already executed, and the SysTick |
| 552 |
count reloaded with ulReloadValue. Reset the |
| 553 |
portNVIC_SYSTICK_LOAD_REG with whatever remains of this tick |
| 554 |
period. */ |
| 555 |
ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG ); |
| 556 |
|
| 557 |
/* Don't allow a tiny value, or values that have somehow |
| 558 |
underflowed because the post sleep hook did something |
| 559 |
that took too long. */ |
| 560 |
if( ( ulCalculatedLoadValue < ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick ) ) |
| 561 |
{ |
| 562 |
ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ); |
| 563 |
} |
| 564 |
|
| 565 |
portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue; |
| 566 |
|
| 567 |
/* The tick interrupt handler will already have pended the tick |
| 568 |
processing in the kernel. As the pending tick will be |
| 569 |
processed as soon as this function exits, the tick value |
| 570 |
maintained by the tick is stepped forward by one less than the |
| 571 |
time spent waiting. */ |
| 572 |
ulCompleteTickPeriods = xExpectedIdleTime - 1UL; |
| 573 |
} |
| 574 |
else |
| 575 |
{ |
| 576 |
/* Something other than the tick interrupt ended the sleep. |
| 577 |
Work out how long the sleep lasted rounded to complete tick |
| 578 |
periods (not the ulReload value which accounted for part |
| 579 |
ticks). */ |
| 580 |
ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - portNVIC_SYSTICK_CURRENT_VALUE_REG; |
| 581 |
|
| 582 |
/* How many complete tick periods passed while the processor |
| 583 |
was waiting? */ |
| 584 |
ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick; |
| 585 |
|
| 586 |
/* The reload value is set to whatever fraction of a single tick |
| 587 |
period remains. */ |
| 588 |
portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1UL ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements; |
| 589 |
} |
| 590 |
|
| 591 |
/* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD_REG |
| 592 |
again, then set portNVIC_SYSTICK_LOAD_REG back to its standard |
| 593 |
value. The critical section is used to ensure the tick interrupt |
| 594 |
can only execute once in the case that the reload register is near |
| 595 |
zero. */ |
| 596 |
portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL; |
| 597 |
portENTER_CRITICAL(); |
| 598 |
{ |
| 599 |
portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT; |
| 600 |
vTaskStepTick( ulCompleteTickPeriods ); |
| 601 |
portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL; |
| 602 |
} |
| 603 |
portEXIT_CRITICAL(); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
#endif /* #if configUSE_TICKLESS_IDLE */ |
| 608 |
/*-----------------------------------------------------------*/ |
| 609 |
|
| 610 |
/* |
| 611 |
* Setup the systick timer to generate the tick interrupts at the required |
| 612 |
* frequency. |
| 613 |
*/ |
| 614 |
__attribute__(( weak )) void vPortSetupTimerInterrupt( void ) |
| 615 |
{ |
| 616 |
/* Calculate the constants required to configure the tick interrupt. */ |
| 617 |
#if configUSE_TICKLESS_IDLE == 1 |
| 618 |
{ |
| 619 |
ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ); |
| 620 |
xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick; |
| 621 |
ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ ); |
| 622 |
} |
| 623 |
#endif /* configUSE_TICKLESS_IDLE */ |
| 624 |
|
| 625 |
/* Configure SysTick to interrupt at the requested rate. */ |
| 626 |
portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL; |
| 627 |
portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT ); |
| 628 |
} |
| 629 |
/*-----------------------------------------------------------*/ |
| 630 |
|
| 631 |
#if( configASSERT_DEFINED == 1 ) |
| 632 |
|
| 633 |
void vPortValidateInterruptPriority( void ) |
| 634 |
{ |
| 635 |
uint32_t ulCurrentInterrupt; |
| 636 |
uint8_t ucCurrentPriority; |
| 637 |
|
| 638 |
/* Obtain the number of the currently executing interrupt. */ |
| 639 |
__asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) ); |
| 640 |
|
| 641 |
/* Is the interrupt number a user defined interrupt? */ |
| 642 |
if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER ) |
| 643 |
{ |
| 644 |
/* Look up the interrupt's priority. */ |
| 645 |
ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ]; |
| 646 |
|
| 647 |
/* The following assertion will fail if a service routine (ISR) for |
| 648 |
an interrupt that has been assigned a priority above |
| 649 |
configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API |
| 650 |
function. ISR safe FreeRTOS API functions must *only* be called |
| 651 |
from interrupts that have been assigned a priority at or below |
| 652 |
configMAX_SYSCALL_INTERRUPT_PRIORITY. |
| 653 |
|
| 654 |
Numerically low interrupt priority numbers represent logically high |
| 655 |
interrupt priorities, therefore the priority of the interrupt must |
| 656 |
be set to a value equal to or numerically *higher* than |
| 657 |
configMAX_SYSCALL_INTERRUPT_PRIORITY. |
| 658 |
|
| 659 |
Interrupts that use the FreeRTOS API must not be left at their |
| 660 |
default priority of zero as that is the highest possible priority, |
| 661 |
which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY, |
| 662 |
and therefore also guaranteed to be invalid. |
| 663 |
|
| 664 |
FreeRTOS maintains separate thread and ISR API functions to ensure |
| 665 |
interrupt entry is as fast and simple as possible. |
| 666 |
|
| 667 |
The following links provide detailed information: |
| 668 |
http://www.freertos.org/RTOS-Cortex-M3-M4.html |
| 669 |
http://www.freertos.org/FAQHelp.html */ |
| 670 |
configASSERT( ucCurrentPriority >= ucMaxSysCallPriority ); |
| 671 |
} |
| 672 |
|
| 673 |
/* Priority grouping: The interrupt controller (NVIC) allows the bits |
| 674 |
that define each interrupt's priority to be split between bits that |
| 675 |
define the interrupt's pre-emption priority bits and bits that define |
| 676 |
the interrupt's sub-priority. For simplicity all bits must be defined |
| 677 |
to be pre-emption priority bits. The following assertion will fail if |
| 678 |
this is not the case (if some bits represent a sub-priority). |
| 679 |
|
| 680 |
If the application only uses CMSIS libraries for interrupt |
| 681 |
configuration then the correct setting can be achieved on all Cortex-M |
| 682 |
devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the |
| 683 |
scheduler. Note however that some vendor specific peripheral libraries |
| 684 |
assume a non-zero priority group setting, in which cases using a value |
| 685 |
of zero will result in unpredicable behaviour. */ |
| 686 |
configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue ); |
| 687 |
} |
| 688 |
|
| 689 |
#endif /* configASSERT_DEFINED */ |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|