port.c 21 KB

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