]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/RVDS/ARM_CM0/port.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Source / portable / RVDS / ARM_CM0 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*-----------------------------------------------------------\r
29  * Implementation of functions defined in portable.h for the ARM CM0 port.\r
30  *----------------------------------------------------------*/\r
31 \r
32 /* Scheduler includes. */\r
33 #include "FreeRTOS.h"\r
34 #include "task.h"\r
35 \r
36 /* Constants required to manipulate the NVIC. */\r
37 #define portNVIC_SYSTICK_CTRL_REG                       ( * ( ( volatile uint32_t * ) 0xe000e010 ) )\r
38 #define portNVIC_SYSTICK_LOAD_REG                       ( * ( ( volatile uint32_t * ) 0xe000e014 ) )\r
39 #define portNVIC_SYSTICK_CURRENT_VALUE_REG      ( * ( ( volatile uint32_t * ) 0xe000e018 ) )\r
40 #define portNVIC_INT_CTRL_REG                           ( * ( ( volatile uint32_t * ) 0xe000ed04 ) )\r
41 #define portNVIC_SYSPRI2_REG                            ( * ( ( volatile uint32_t * ) 0xe000ed20 ) )\r
42 #define portNVIC_SYSTICK_CLK_BIT                        ( 1UL << 2UL )\r
43 #define portNVIC_SYSTICK_INT_BIT                        ( 1UL << 1UL )\r
44 #define portNVIC_SYSTICK_ENABLE_BIT                     ( 1UL << 0UL )\r
45 #define portNVIC_SYSTICK_COUNT_FLAG_BIT         ( 1UL << 16UL )\r
46 #define portNVIC_PENDSVSET_BIT                          ( 1UL << 28UL )\r
47 #define portMIN_INTERRUPT_PRIORITY                      ( 255UL )\r
48 #define portNVIC_PENDSV_PRI                                     ( portMIN_INTERRUPT_PRIORITY << 16UL )\r
49 #define portNVIC_SYSTICK_PRI                            ( portMIN_INTERRUPT_PRIORITY << 24UL )\r
50 \r
51 /* Constants required to set up the initial stack. */\r
52 #define portINITIAL_XPSR                        ( 0x01000000 )\r
53 \r
54 /* The systick is a 24-bit counter. */\r
55 #define portMAX_24_BIT_NUMBER           ( 0xffffffUL )\r
56 \r
57 /* A fiddle factor to estimate the number of SysTick counts that would have\r
58  occurred while the SysTick counter is stopped during tickless idle\r
59  calculations. */\r
60 #ifndef portMISSED_COUNTS_FACTOR\r
61         #define portMISSED_COUNTS_FACTOR        ( 45UL )\r
62 #endif\r
63 \r
64 /* Constants used with memory barrier intrinsics. */\r
65 #define portSY_FULL_READ_WRITE          ( 15 )\r
66 \r
67 /* Legacy macro for backward compatibility only.  This macro used to be used to\r
68 replace the function that configures the clock used to generate the tick\r
69 interrupt (prvSetupTimerInterrupt()), but now the function is declared weak so\r
70 the application writer can override it by simply defining a function of the\r
71 same name (vApplicationSetupTickInterrupt()). */\r
72 #ifndef configOVERRIDE_DEFAULT_TICK_CONFIGURATION\r
73         #define configOVERRIDE_DEFAULT_TICK_CONFIGURATION 0\r
74 #endif\r
75 \r
76 /* Each task maintains its own interrupt status in the critical nesting\r
77 variable. */\r
78 static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;\r
79 \r
80 /* The number of SysTick increments that make up one tick period. */\r
81 #if( configUSE_TICKLESS_IDLE == 1 )\r
82         static uint32_t ulTimerCountsForOneTick = 0;\r
83 #endif /* configUSE_TICKLESS_IDLE */\r
84 \r
85 /* The maximum number of tick periods that can be suppressed is limited by the\r
86  24 bit resolution of the SysTick timer. */\r
87 #if( configUSE_TICKLESS_IDLE == 1 )\r
88         static uint32_t xMaximumPossibleSuppressedTicks = 0;\r
89 #endif /* configUSE_TICKLESS_IDLE */\r
90 \r
91  /* Compensate for the CPU cycles that pass while the SysTick is stopped (low\r
92  power functionality only.\r
93 */\r
94 #if( configUSE_TICKLESS_IDLE == 1 )\r
95         static uint32_t ulStoppedTimerCompensation = 0;\r
96 #endif /* configUSE_TICKLESS_IDLE */\r
97 \r
98 /*\r
99  * Setup the timer to generate the tick interrupts.  The implementation in this\r
100  * file is weak to allow application writers to change the timer used to\r
101  * generate the tick interrupt.\r
102  */\r
103 void vPortSetupTimerInterrupt( void );\r
104 \r
105 /*\r
106  * Exception handlers.\r
107  */\r
108 void xPortPendSVHandler( void );\r
109 void xPortSysTickHandler( void );\r
110 void vPortSVCHandler( void );\r
111 \r
112 /*\r
113  * Start first task is a separate function so it can be tested in isolation.\r
114  */\r
115 static void prvPortStartFirstTask( void );\r
116 \r
117 /*\r
118  * Used to catch tasks that attempt to return from their implementing function.\r
119  */\r
120 static void prvTaskExitError( void );\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /*\r
125  * See header file for description.\r
126  */\r
127 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
128 {\r
129         /* Simulate the stack frame as it would be created by a context switch\r
130         interrupt. */\r
131         pxTopOfStack--; /* Offset added to account for the way the MCU uses the stack on entry/exit of interrupts. */\r
132         *pxTopOfStack = portINITIAL_XPSR;       /* xPSR */\r
133         pxTopOfStack--;\r
134         *pxTopOfStack = ( StackType_t ) pxCode; /* PC */\r
135         pxTopOfStack--;\r
136         *pxTopOfStack = ( StackType_t ) prvTaskExitError;       /* LR */\r
137         pxTopOfStack -= 5;      /* R12, R3, R2 and R1. */\r
138         *pxTopOfStack = ( StackType_t ) pvParameters;   /* R0 */\r
139         pxTopOfStack -= 8; /* R11..R4. */\r
140 \r
141         return pxTopOfStack;\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 static void prvTaskExitError( void )\r
146 {\r
147         /* A function that implements a task must not exit or attempt to return to\r
148         its caller as there is nothing to return to.  If a task wants to exit it\r
149         should instead call vTaskDelete( NULL ).\r
150 \r
151         Artificially force an assert() to be triggered if configASSERT() is\r
152         defined, then stop here so application writers can catch the error. */\r
153         configASSERT( uxCriticalNesting == ~0UL );\r
154         portDISABLE_INTERRUPTS();\r
155         for( ;; );\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 void vPortSVCHandler( void )\r
160 {\r
161         /* This function is no longer used, but retained for backward\r
162         compatibility. */\r
163 }\r
164 /*-----------------------------------------------------------*/\r
165 \r
166 __asm void prvPortStartFirstTask( void )\r
167 {\r
168         extern pxCurrentTCB;\r
169 \r
170         PRESERVE8\r
171 \r
172         /* The MSP stack is not reset as, unlike on M3/4 parts, there is no vector\r
173         table offset register that can be used to locate the initial stack value.\r
174         Not all M0 parts have the application vector table at address 0. */\r
175 \r
176         ldr     r3, =pxCurrentTCB       /* Obtain location of pxCurrentTCB. */\r
177         ldr r1, [r3]\r
178         ldr r0, [r1]                    /* The first item in pxCurrentTCB is the task top of stack. */\r
179         adds r0, #32                    /* Discard everything up to r0. */\r
180         msr psp, r0                             /* This is now the new top of stack to use in the task. */\r
181         movs r0, #2                             /* Switch to the psp stack. */\r
182         msr CONTROL, r0\r
183         isb\r
184         pop {r0-r5}                             /* Pop the registers that are saved automatically. */\r
185         mov lr, r5                              /* lr is now in r5. */\r
186         pop {r3}                                /* The return address is now in r3. */\r
187         pop {r2}                                /* Pop and discard the XPSR. */\r
188         cpsie i                                 /* The first task has its context and interrupts can be enabled. */\r
189         bx r3                                   /* Finally, jump to the user defined task code. */\r
190 \r
191         ALIGN\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 /*\r
196  * See header file for description.\r
197  */\r
198 BaseType_t xPortStartScheduler( void )\r
199 {\r
200         /* Make PendSV, CallSV and SysTick the same priority as the kernel. */\r
201         portNVIC_SYSPRI2_REG |= portNVIC_PENDSV_PRI;\r
202         portNVIC_SYSPRI2_REG |= portNVIC_SYSTICK_PRI;\r
203 \r
204         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
205         here already. */\r
206         vPortSetupTimerInterrupt();\r
207 \r
208         /* Initialise the critical nesting count ready for the first task. */\r
209         uxCriticalNesting = 0;\r
210 \r
211         /* Start the first task. */\r
212         prvPortStartFirstTask();\r
213 \r
214         /* Should not get here! */\r
215         return 0;\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 void vPortEndScheduler( void )\r
220 {\r
221         /* Not implemented in ports where there is nothing to return to.\r
222         Artificially force an assert. */\r
223         configASSERT( uxCriticalNesting == 1000UL );\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 void vPortYield( void )\r
228 {\r
229         /* Set a PendSV to request a context switch. */\r
230         portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
231 \r
232         /* Barriers are normally not required but do ensure the code is completely\r
233         within the specified behaviour for the architecture. */\r
234         __dsb( portSY_FULL_READ_WRITE );\r
235         __isb( portSY_FULL_READ_WRITE );\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 void vPortEnterCritical( void )\r
240 {\r
241     portDISABLE_INTERRUPTS();\r
242     uxCriticalNesting++;\r
243         __dsb( portSY_FULL_READ_WRITE );\r
244         __isb( portSY_FULL_READ_WRITE );\r
245 }\r
246 /*-----------------------------------------------------------*/\r
247 \r
248 void vPortExitCritical( void )\r
249 {\r
250         configASSERT( uxCriticalNesting );\r
251         uxCriticalNesting--;\r
252         if( uxCriticalNesting == 0 )\r
253         {\r
254                 portENABLE_INTERRUPTS();\r
255         }\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 __asm uint32_t ulSetInterruptMaskFromISR( void )\r
260 {\r
261         mrs r0, PRIMASK\r
262         cpsid i\r
263         bx lr\r
264 }\r
265 /*-----------------------------------------------------------*/\r
266 \r
267 __asm void vClearInterruptMaskFromISR( uint32_t ulMask )\r
268 {\r
269         msr PRIMASK, r0\r
270         bx lr\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 __asm void xPortPendSVHandler( void )\r
275 {\r
276         extern vTaskSwitchContext\r
277         extern pxCurrentTCB\r
278 \r
279         PRESERVE8\r
280 \r
281         mrs r0, psp\r
282 \r
283         ldr     r3, =pxCurrentTCB       /* Get the location of the current TCB. */\r
284         ldr     r2, [r3]\r
285 \r
286         subs r0, #32                    /* Make space for the remaining low registers. */\r
287         str r0, [r2]                    /* Save the new top of stack. */\r
288         stmia r0!, {r4-r7}              /* Store the low registers that are not saved automatically. */\r
289         mov r4, r8                              /* Store the high registers. */\r
290         mov r5, r9\r
291         mov r6, r10\r
292         mov r7, r11\r
293         stmia r0!, {r4-r7}\r
294 \r
295         push {r3, r14}\r
296         cpsid i\r
297         bl vTaskSwitchContext\r
298         cpsie i\r
299         pop {r2, r3}                    /* lr goes in r3. r2 now holds tcb pointer. */\r
300 \r
301         ldr r1, [r2]\r
302         ldr r0, [r1]                    /* The first item in pxCurrentTCB is the task top of stack. */\r
303         adds r0, #16                    /* Move to the high registers. */\r
304         ldmia r0!, {r4-r7}              /* Pop the high registers. */\r
305         mov r8, r4\r
306         mov r9, r5\r
307         mov r10, r6\r
308         mov r11, r7\r
309 \r
310         msr psp, r0                             /* Remember the new top of stack for the task. */\r
311 \r
312         subs r0, #32                    /* Go back for the low registers that are not automatically restored. */\r
313         ldmia r0!, {r4-r7}      /* Pop low registers.  */\r
314 \r
315         bx r3\r
316         ALIGN\r
317 }\r
318 /*-----------------------------------------------------------*/\r
319 \r
320 void xPortSysTickHandler( void )\r
321 {\r
322 uint32_t ulPreviousMask;\r
323 \r
324         ulPreviousMask = portSET_INTERRUPT_MASK_FROM_ISR();\r
325         {\r
326                 /* Increment the RTOS tick. */\r
327                 if( xTaskIncrementTick() != pdFALSE )\r
328                 {\r
329                         /* Pend a context switch. */\r
330                         portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;\r
331                 }\r
332         }\r
333         portCLEAR_INTERRUPT_MASK_FROM_ISR( ulPreviousMask );\r
334 }\r
335 /*-----------------------------------------------------------*/\r
336 \r
337 /*\r
338  * Setup the systick timer to generate the tick interrupts at the required\r
339  * frequency.\r
340  */\r
341 #if( configOVERRIDE_DEFAULT_TICK_CONFIGURATION == 0 )\r
342 \r
343         __weak void vPortSetupTimerInterrupt( void )\r
344         {\r
345                 /* Calculate the constants required to configure the tick interrupt. */\r
346                 #if( configUSE_TICKLESS_IDLE == 1 )\r
347                         ulTimerCountsForOneTick = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );\r
348                         xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;\r
349                         ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR;\r
350                 #endif /* configUSE_TICKLESS_IDLE */\r
351 \r
352                 /* Stop and reset the SysTick. */\r
353                 portNVIC_SYSTICK_CTRL_REG = 0UL;\r
354                 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
355 \r
356                 /* Configure SysTick to interrupt at the requested rate. */\r
357                 portNVIC_SYSTICK_LOAD_REG = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;\r
358                 portNVIC_SYSTICK_CTRL_REG = portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT;\r
359         }\r
360 \r
361 #endif /* configOVERRIDE_DEFAULT_TICK_CONFIGURATION */\r
362 /*-----------------------------------------------------------*/\r
363 \r
364 #if( configUSE_TICKLESS_IDLE == 1 )\r
365 \r
366 __weak void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )\r
367 {\r
368 uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements;\r
369 TickType_t xModifiableIdleTime;\r
370 \r
371         /* Make sure the SysTick reload value does not overflow the counter. */\r
372         if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )\r
373         {\r
374                 xExpectedIdleTime = xMaximumPossibleSuppressedTicks;\r
375         }\r
376 \r
377         /* Stop the SysTick momentarily. The time the SysTick is stopped for\r
378         is accounted for as best it can be, but using the tickless mode will\r
379         inevitably result in some tiny drift of the time maintained by the\r
380         kernel with respect to calendar time. */\r
381         portNVIC_SYSTICK_CTRL_REG &= ~portNVIC_SYSTICK_ENABLE_BIT;\r
382 \r
383         /* Calculate the reload value required to wait xExpectedIdleTime\r
384         tick periods.  -1 is used because this code will execute part way\r
385         through one of the tick periods. */\r
386         ulReloadValue = portNVIC_SYSTICK_CURRENT_VALUE_REG + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );\r
387         if( ulReloadValue > ulStoppedTimerCompensation )\r
388         {\r
389                 ulReloadValue -= ulStoppedTimerCompensation;\r
390         }\r
391 \r
392         /* Enter a critical section but don't use the taskENTER_CRITICAL()\r
393         method as that will mask interrupts that should exit sleep mode. */\r
394         __disable_irq();\r
395         __dsb( portSY_FULL_READ_WRITE );\r
396         __isb( portSY_FULL_READ_WRITE );\r
397 \r
398         /* If a context switch is pending or a task is waiting for the scheduler\r
399         to be unsuspended then abandon the low power entry. */\r
400         if( eTaskConfirmSleepModeStatus() == eAbortSleep )\r
401         {\r
402                 /* Restart from whatever is left in the count register to complete\r
403                 this tick period. */\r
404                 portNVIC_SYSTICK_LOAD_REG = portNVIC_SYSTICK_CURRENT_VALUE_REG;\r
405 \r
406                 /* Restart SysTick. */\r
407                 portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;\r
408 \r
409                 /* Reset the reload register to the value required for normal tick\r
410                 periods. */\r
411                 portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;\r
412 \r
413                 /* Re-enable interrupts - see comments above __disable_irq() call\r
414                 above. */\r
415                 __enable_irq();\r
416         }\r
417         else\r
418         {\r
419                 /* Set the new reload value. */\r
420                 portNVIC_SYSTICK_LOAD_REG = ulReloadValue;\r
421 \r
422                 /* Clear the SysTick count flag and set the count value back to\r
423                 zero. */\r
424                 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
425 \r
426                 /* Restart SysTick. */\r
427                 portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;\r
428 \r
429                 /* Sleep until something happens. configPRE_SLEEP_PROCESSING() can\r
430                 set its parameter to 0 to indicate that its implementation contains\r
431                 its own wait for interrupt or wait for event instruction, and so wfi\r
432                 should not be executed again. However, the original expected idle\r
433                 time variable must remain unmodified, so a copy is taken. */\r
434                 xModifiableIdleTime = xExpectedIdleTime;\r
435                 configPRE_SLEEP_PROCESSING( xModifiableIdleTime );\r
436                 if( xModifiableIdleTime > 0 )\r
437                 {\r
438                         __dsb( portSY_FULL_READ_WRITE );\r
439                         __wfi();\r
440                         __isb( portSY_FULL_READ_WRITE );\r
441                 }\r
442                 configPOST_SLEEP_PROCESSING( xExpectedIdleTime );\r
443 \r
444                 /* Re-enable interrupts to allow the interrupt that brought the MCU\r
445                 out of sleep mode to execute immediately. see comments above\r
446                 __disable_interrupt() call above. */\r
447                 __enable_irq();\r
448                 __dsb( portSY_FULL_READ_WRITE );\r
449                 __isb( portSY_FULL_READ_WRITE );\r
450 \r
451                 /* Disable interrupts again because the clock is about to be stopped\r
452                 and interrupts that execute while the clock is stopped will increase\r
453                 any slippage between the time maintained by the RTOS and calendar\r
454                 time. */\r
455                 __disable_irq();\r
456                 __dsb( portSY_FULL_READ_WRITE );\r
457                 __isb( portSY_FULL_READ_WRITE );\r
458 \r
459                 /* Disable the SysTick clock without reading the\r
460                 portNVIC_SYSTICK_CTRL_REG register to ensure the\r
461                 portNVIC_SYSTICK_COUNT_FLAG_BIT is not cleared if it is set.  Again,\r
462                 the time the SysTick is stopped for is accounted for as best it can\r
463                 be, but using the tickless mode will inevitably result in some tiny\r
464                 drift of the time maintained by the kernel with respect to calendar\r
465                 time*/\r
466                 portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT );\r
467 \r
468                 /* Determine if the SysTick clock has already counted to zero and\r
469                 been set back to the current reload value (the reload back being\r
470                 correct for the entire expected idle time) or if the SysTick is yet\r
471                 to count to zero (in which case an interrupt other than the SysTick\r
472                 must have brought the system out of sleep mode). */\r
473                 if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )\r
474                 {\r
475                         uint32_t ulCalculatedLoadValue;\r
476 \r
477                         /* The tick interrupt is already pending, and the SysTick count\r
478                         reloaded with ulReloadValue.  Reset the\r
479                         portNVIC_SYSTICK_LOAD with whatever remains of this tick\r
480                         period. */\r
481                         ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );\r
482 \r
483                         /* Don't allow a tiny value, or values that have somehow\r
484                         underflowed because the post sleep hook did something\r
485                         that took too long. */\r
486                         if( ( ulCalculatedLoadValue < ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick ) )\r
487                         {\r
488                                 ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL );\r
489                         }\r
490 \r
491                         portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;\r
492 \r
493                         /* As the pending tick will be processed as soon as this\r
494                         function exits, the tick value maintained by the tick is stepped\r
495                         forward by one less than the time spent waiting. */\r
496                         ulCompleteTickPeriods = xExpectedIdleTime - 1UL;\r
497                 }\r
498                 else\r
499                 {\r
500                         /* Something other than the tick interrupt ended the sleep.\r
501                         Work out how long the sleep lasted rounded to complete tick\r
502                         periods (not the ulReload value which accounted for part\r
503                         ticks). */\r
504                         ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - portNVIC_SYSTICK_CURRENT_VALUE_REG;\r
505 \r
506                         /* How many complete tick periods passed while the processor\r
507                         was waiting? */\r
508                         ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;\r
509 \r
510                         /* The reload value is set to whatever fraction of a single tick\r
511                         period remains. */\r
512                         portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1UL ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;\r
513                 }\r
514 \r
515                 /* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD\r
516                 again, then set portNVIC_SYSTICK_LOAD back to its standard\r
517                 value. */\r
518                 portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;\r
519                 portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;\r
520                 vTaskStepTick( ulCompleteTickPeriods );\r
521                 portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;\r
522 \r
523                 /* Exit with interrpts enabled. */\r
524                 __enable_irq();\r
525         }\r
526 }\r
527 \r
528 #endif /* #if configUSE_TICKLESS_IDLE */\r
529 \r
530 /*-----------------------------------------------------------*/\r