port.c 23 KB

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