port.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 CM4F port.
  30. *----------------------------------------------------------*/
  31. /* Scheduler includes. */
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. #ifndef __TI_VFP_SUPPORT__
  35. #error This port can only be used when the project options are configured to enable hardware floating point support.
  36. #endif
  37. #if ( configMAX_SYSCALL_INTERRUPT_PRIORITY == 0 )
  38. #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. See http: /*www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  39. #endif
  40. #ifndef configSYSTICK_CLOCK_HZ
  41. #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
  42. /* Ensure the SysTick is clocked at the same frequency as the core. */
  43. #define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
  44. #else
  45. /* The way the SysTick is clocked is not modified in case it is not the same
  46. * as the core. */
  47. #define portNVIC_SYSTICK_CLK_BIT ( 0 )
  48. #endif
  49. /* Constants required to manipulate the core. Registers first... */
  50. #define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
  51. #define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
  52. #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
  53. #define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
  54. /* ...then bits in the registers. */
  55. #define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
  56. #define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
  57. #define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
  58. #define portNVIC_PENDSVCLEAR_BIT ( 1UL << 27UL )
  59. #define portNVIC_PEND_SYSTICK_CLEAR_BIT ( 1UL << 25UL )
  60. #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL )
  61. #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24UL )
  62. /* Constants required to check the validity of an interrupt priority. */
  63. #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
  64. #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
  65. #define portAIRCR_REG ( *( ( volatile uint32_t * ) 0xE000ED0C ) )
  66. #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
  67. #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
  68. #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
  69. #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
  70. #define portPRIGROUP_SHIFT ( 8UL )
  71. /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */
  72. #define portVECTACTIVE_MASK ( 0xFFUL )
  73. /* Constants required to manipulate the VFP. */
  74. #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */
  75. #define portASPEN_AND_LSPEN_BITS ( 0x3UL << 30UL )
  76. /* Constants required to set up the initial stack. */
  77. #define portINITIAL_XPSR ( 0x01000000 )
  78. #define portINITIAL_EXC_RETURN ( 0xfffffffd )
  79. /* The systick is a 24-bit counter. */
  80. #define portMAX_24_BIT_NUMBER ( 0xffffffUL )
  81. /* A fiddle factor to estimate the number of SysTick counts that would have
  82. * occurred while the SysTick counter is stopped during tickless idle
  83. * calculations. */
  84. #define portMISSED_COUNTS_FACTOR ( 45UL )
  85. /* For strict compliance with the Cortex-M spec the task start address should
  86. * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
  87. #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
  88. /*
  89. * Setup the timer to generate the tick interrupts. The implementation in this
  90. * file is weak to allow application writers to change the timer used to
  91. * generate the tick interrupt.
  92. */
  93. void vPortSetupTimerInterrupt( void );
  94. /*
  95. * Exception handlers.
  96. */
  97. void xPortSysTickHandler( void );
  98. /*
  99. * Start first task is a separate function so it can be tested in isolation.
  100. */
  101. extern void vPortStartFirstTask( void );
  102. /*
  103. * Turn the VFP on.
  104. */
  105. extern void vPortEnableVFP( void );
  106. /*
  107. * Used to catch tasks that attempt to return from their implementing function.
  108. */
  109. static void prvTaskExitError( void );
  110. /*-----------------------------------------------------------*/
  111. /* Required to allow portasm.asm access the configMAX_SYSCALL_INTERRUPT_PRIORITY
  112. * setting. */
  113. const uint32_t ulMaxSyscallInterruptPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY;
  114. /* Each task maintains its own interrupt status in the critical nesting
  115. * variable. */
  116. static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
  117. /*
  118. * The number of SysTick increments that make up one tick period.
  119. */
  120. #if ( configUSE_TICKLESS_IDLE == 1 )
  121. static uint32_t ulTimerCountsForOneTick = 0;
  122. #endif /* configUSE_TICKLESS_IDLE */
  123. /*
  124. * The maximum number of tick periods that can be suppressed is limited by the
  125. * 24 bit resolution of the SysTick timer.
  126. */
  127. #if ( configUSE_TICKLESS_IDLE == 1 )
  128. static uint32_t xMaximumPossibleSuppressedTicks = 0;
  129. #endif /* configUSE_TICKLESS_IDLE */
  130. /*
  131. * Compensate for the CPU cycles that pass while the SysTick is stopped (low
  132. * power functionality only.
  133. */
  134. #if ( configUSE_TICKLESS_IDLE == 1 )
  135. static uint32_t ulStoppedTimerCompensation = 0;
  136. #endif /* configUSE_TICKLESS_IDLE */
  137. /*
  138. * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
  139. * FreeRTOS API functions are not called from interrupts that have been assigned
  140. * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
  141. */
  142. #if ( configASSERT_DEFINED == 1 )
  143. static uint8_t ucMaxSysCallPriority = 0;
  144. static uint32_t ulMaxPRIGROUPValue = 0;
  145. static const volatile uint8_t * const pcInterruptPriorityRegisters = ( uint8_t * ) portNVIC_IP_REGISTERS_OFFSET_16;
  146. #endif /* configASSERT_DEFINED */
  147. /*-----------------------------------------------------------*/
  148. /*
  149. * See header file for description.
  150. */
  151. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  152. TaskFunction_t pxCode,
  153. void * pvParameters )
  154. {
  155. /* Simulate the stack frame as it would be created by a context switch
  156. * interrupt. */
  157. /* Offset added to account for the way the MCU uses the stack on entry/exit
  158. * of interrupts, and to ensure alignment. */
  159. pxTopOfStack--;
  160. *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
  161. pxTopOfStack--;
  162. *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC */
  163. pxTopOfStack--;
  164. *pxTopOfStack = ( StackType_t ) prvTaskExitError; /* LR */
  165. /* Save code space by skipping register initialisation. */
  166. pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
  167. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  168. /* A save method is being used that requires each task to maintain its
  169. * own exec return value. */
  170. pxTopOfStack--;
  171. *pxTopOfStack = portINITIAL_EXC_RETURN;
  172. pxTopOfStack -= 8; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
  173. return pxTopOfStack;
  174. }
  175. /*-----------------------------------------------------------*/
  176. static void prvTaskExitError( void )
  177. {
  178. /* A function that implements a task must not exit or attempt to return to
  179. * its caller as there is nothing to return to. If a task wants to exit it
  180. * should instead call vTaskDelete( NULL ).
  181. *
  182. * Artificially force an assert() to be triggered if configASSERT() is
  183. * defined, then stop here so application writers can catch the error. */
  184. configASSERT( uxCriticalNesting == ~0UL );
  185. portDISABLE_INTERRUPTS();
  186. for( ; ; )
  187. {
  188. }
  189. }
  190. /*-----------------------------------------------------------*/
  191. /*
  192. * See header file for description.
  193. */
  194. BaseType_t xPortStartScheduler( void )
  195. {
  196. #if ( configASSERT_DEFINED == 1 )
  197. {
  198. volatile uint32_t ulOriginalPriority;
  199. volatile uint8_t * const pucFirstUserPriorityRegister = ( uint8_t * ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
  200. volatile uint8_t ucMaxPriorityValue;
  201. /* Determine the maximum priority from which ISR safe FreeRTOS API
  202. * functions can be called. ISR safe functions are those that end in
  203. * "FromISR". FreeRTOS maintains separate thread and ISR API functions to
  204. * ensure interrupt entry is as fast and simple as possible.
  205. *
  206. * Save the interrupt priority value that is about to be clobbered. */
  207. ulOriginalPriority = *pucFirstUserPriorityRegister;
  208. /* Determine the number of priority bits available. First write to all
  209. * possible bits. */
  210. *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
  211. /* Read the value back to see how many bits stuck. */
  212. ucMaxPriorityValue = *pucFirstUserPriorityRegister;
  213. /* Use the same mask on the maximum system call priority. */
  214. ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
  215. /* Calculate the maximum acceptable priority group value for the number
  216. * of bits read back. */
  217. ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS;
  218. while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
  219. {
  220. ulMaxPRIGROUPValue--;
  221. ucMaxPriorityValue <<= ( uint8_t ) 0x01;
  222. }
  223. #ifdef __NVIC_PRIO_BITS
  224. {
  225. /* Check the CMSIS configuration that defines the number of
  226. * priority bits matches the number of priority bits actually queried
  227. * from the hardware. */
  228. configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == __NVIC_PRIO_BITS );
  229. }
  230. #endif
  231. #ifdef configPRIO_BITS
  232. {
  233. /* Check the FreeRTOS configuration that defines the number of
  234. * priority bits matches the number of priority bits actually queried
  235. * from the hardware. */
  236. configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == configPRIO_BITS );
  237. }
  238. #endif
  239. /* Shift the priority group value back to its position within the AIRCR
  240. * register. */
  241. ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
  242. ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
  243. /* Restore the clobbered interrupt priority register to its original
  244. * value. */
  245. *pucFirstUserPriorityRegister = ulOriginalPriority;
  246. }
  247. #endif /* configASSERT_DEFINED */
  248. /* Make PendSV and SysTick the lowest priority interrupts. */
  249. portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
  250. portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
  251. /* Start the timer that generates the tick ISR. Interrupts are disabled
  252. * here already. */
  253. vPortSetupTimerInterrupt();
  254. /* Initialise the critical nesting count ready for the first task. */
  255. uxCriticalNesting = 0;
  256. /* Ensure the VFP is enabled - it should be anyway. */
  257. vPortEnableVFP();
  258. /* Lazy save always. */
  259. *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
  260. /* Start the first task. */
  261. vPortStartFirstTask();
  262. /* Should not get here! */
  263. return 0;
  264. }
  265. /*-----------------------------------------------------------*/
  266. void vPortEndScheduler( void )
  267. {
  268. /* Not implemented in ports where there is nothing to return to.
  269. * Artificially force an assert. */
  270. configASSERT( uxCriticalNesting == 1000UL );
  271. }
  272. /*-----------------------------------------------------------*/
  273. void vPortEnterCritical( void )
  274. {
  275. portDISABLE_INTERRUPTS();
  276. uxCriticalNesting++;
  277. /* This is not the interrupt safe version of the enter critical function so
  278. * assert() if it is being called from an interrupt context. Only API
  279. * functions that end in "FromISR" can be used in an interrupt. Only assert if
  280. * the critical nesting count is 1 to protect against recursive calls if the
  281. * assert function also uses a critical section. */
  282. if( uxCriticalNesting == 1 )
  283. {
  284. configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
  285. }
  286. }
  287. /*-----------------------------------------------------------*/
  288. void vPortExitCritical( void )
  289. {
  290. configASSERT( uxCriticalNesting );
  291. uxCriticalNesting--;
  292. if( uxCriticalNesting == 0 )
  293. {
  294. portENABLE_INTERRUPTS();
  295. }
  296. }
  297. /*-----------------------------------------------------------*/
  298. void xPortSysTickHandler( void )
  299. {
  300. /* The SysTick runs at the lowest interrupt priority, so when this interrupt
  301. * executes all interrupts must be unmasked. There is therefore no need to
  302. * save and then restore the interrupt mask value as its value is already
  303. * known. */
  304. ( void ) portSET_INTERRUPT_MASK_FROM_ISR();
  305. {
  306. /* Increment the RTOS tick. */
  307. if( xTaskIncrementTick() != pdFALSE )
  308. {
  309. /* A context switch is required. Context switching is performed in
  310. * the PendSV interrupt. Pend the PendSV interrupt. */
  311. portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
  312. }
  313. }
  314. portCLEAR_INTERRUPT_MASK_FROM_ISR( 0 );
  315. }
  316. /*-----------------------------------------------------------*/
  317. #if ( configUSE_TICKLESS_IDLE == 1 )
  318. #pragma WEAK( vPortSuppressTicksAndSleep )
  319. void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
  320. {
  321. uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements;
  322. TickType_t xModifiableIdleTime;
  323. /* Make sure the SysTick reload value does not overflow the counter. */
  324. if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
  325. {
  326. xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
  327. }
  328. /* Stop the SysTick momentarily. The time the SysTick is stopped for
  329. * is accounted for as best it can be, but using the tickless mode will
  330. * inevitably result in some tiny drift of the time maintained by the
  331. * kernel with respect to calendar time. */
  332. portNVIC_SYSTICK_CTRL_REG &= ~portNVIC_SYSTICK_ENABLE_BIT;
  333. /* Calculate the reload value required to wait xExpectedIdleTime
  334. * tick periods. -1 is used because this code will execute part way
  335. * through one of the tick periods. */
  336. ulReloadValue = portNVIC_SYSTICK_CURRENT_VALUE_REG + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );
  337. if( ulReloadValue > ulStoppedTimerCompensation )
  338. {
  339. ulReloadValue -= ulStoppedTimerCompensation;
  340. }
  341. /* Enter a critical section but don't use the taskENTER_CRITICAL()
  342. * method as that will mask interrupts that should exit sleep mode. */
  343. __asm( " cpsid i");
  344. __asm( " dsb");
  345. __asm( " isb");
  346. /* If a context switch is pending or a task is waiting for the scheduler
  347. * to be unsuspended then abandon the low power entry. */
  348. if( eTaskConfirmSleepModeStatus() == eAbortSleep )
  349. {
  350. /* Restart from whatever is left in the count register to complete
  351. * this tick period. */
  352. portNVIC_SYSTICK_LOAD_REG = portNVIC_SYSTICK_CURRENT_VALUE_REG;
  353. /* Restart SysTick. */
  354. portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
  355. /* Reset the reload register to the value required for normal tick
  356. * periods. */
  357. portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
  358. /* Re-enable interrupts - see comments above __disable_interrupt()
  359. * call above. */
  360. __asm( " cpsie i");
  361. }
  362. else
  363. {
  364. /* Set the new reload value. */
  365. portNVIC_SYSTICK_LOAD_REG = ulReloadValue;
  366. /* Clear the SysTick count flag and set the count value back to
  367. * zero. */
  368. portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
  369. /* Restart SysTick. */
  370. portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
  371. /* Sleep until something happens. configPRE_SLEEP_PROCESSING() can
  372. * set its parameter to 0 to indicate that its implementation contains
  373. * its own wait for interrupt or wait for event instruction, and so wfi
  374. * should not be executed again. However, the original expected idle
  375. * time variable must remain unmodified, so a copy is taken. */
  376. xModifiableIdleTime = xExpectedIdleTime;
  377. configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
  378. if( xModifiableIdleTime > 0 )
  379. {
  380. __asm( " dsb");
  381. __asm( " wfi");
  382. __asm( " isb");
  383. }
  384. configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
  385. /* Re-enable interrupts to allow the interrupt that brought the MCU
  386. * out of sleep mode to execute immediately. see comments above
  387. * __disable_interrupt() call above. */
  388. __asm( " cpsie i");
  389. __asm( " dsb");
  390. __asm( " isb");
  391. /* Disable interrupts again because the clock is about to be stopped
  392. * and interrupts that execute while the clock is stopped will increase
  393. * any slippage between the time maintained by the RTOS and calendar
  394. * time. */
  395. __asm( " cpsid i");
  396. __asm( " dsb");
  397. __asm( " isb");
  398. /* Disable the SysTick clock without reading the
  399. * portNVIC_SYSTICK_CTRL_REG register to ensure the
  400. * portNVIC_SYSTICK_COUNT_FLAG_BIT is not cleared if it is set. Again,
  401. * the time the SysTick is stopped for is accounted for as best it can
  402. * be, but using the tickless mode will inevitably result in some tiny
  403. * drift of the time maintained by the kernel with respect to calendar
  404. * time*/
  405. portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT );
  406. /* Determine if the SysTick clock has already counted to zero and
  407. * been set back to the current reload value (the reload back being
  408. * correct for the entire expected idle time) or if the SysTick is yet
  409. * to count to zero (in which case an interrupt other than the SysTick
  410. * must have brought the system out of sleep mode). */
  411. if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )
  412. {
  413. uint32_t ulCalculatedLoadValue;
  414. /* The tick interrupt is already pending, and the SysTick count
  415. * reloaded with ulReloadValue. Reset the
  416. * portNVIC_SYSTICK_LOAD_REG with whatever remains of this tick
  417. * period. */
  418. ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );
  419. /* Don't allow a tiny value, or values that have somehow
  420. * underflowed because the post sleep hook did something
  421. * that took too long. */
  422. if( ( ulCalculatedLoadValue < ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick ) )
  423. {
  424. ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL );
  425. }
  426. portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;
  427. /* As the pending tick will be processed as soon as this
  428. * function exits, the tick value maintained by the tick is stepped
  429. * forward by one less than the time spent waiting. */
  430. ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
  431. }
  432. else
  433. {
  434. /* Something other than the tick interrupt ended the sleep.
  435. * Work out how long the sleep lasted rounded to complete tick
  436. * periods (not the ulReload value which accounted for part
  437. * ticks). */
  438. ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - portNVIC_SYSTICK_CURRENT_VALUE_REG;
  439. /* How many complete tick periods passed while the processor
  440. * was waiting? */
  441. ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;
  442. /* The reload value is set to whatever fraction of a single tick
  443. * period remains. */
  444. portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1UL ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;
  445. }
  446. /* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD_REG
  447. * again, then set portNVIC_SYSTICK_LOAD_REG back to its standard
  448. * value. */
  449. portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
  450. portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
  451. vTaskStepTick( ulCompleteTickPeriods );
  452. portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
  453. /* Exit with interrupts enabled. */
  454. __asm( " cpsie i");
  455. }
  456. }
  457. #endif /* configUSE_TICKLESS_IDLE */
  458. /*-----------------------------------------------------------*/
  459. /*
  460. * Setup the systick timer to generate the tick interrupts at the required
  461. * frequency.
  462. */
  463. #pragma WEAK( vPortSetupTimerInterrupt )
  464. void vPortSetupTimerInterrupt( void )
  465. {
  466. /* Calculate the constants required to configure the tick interrupt. */
  467. #if ( configUSE_TICKLESS_IDLE == 1 )
  468. {
  469. ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );
  470. xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;
  471. ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );
  472. }
  473. #endif /* configUSE_TICKLESS_IDLE */
  474. /* Stop and clear the SysTick. */
  475. portNVIC_SYSTICK_CTRL_REG = 0UL;
  476. portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
  477. /* Configure SysTick to interrupt at the requested rate. */
  478. portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
  479. portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );
  480. }
  481. /*-----------------------------------------------------------*/
  482. #if ( configASSERT_DEFINED == 1 )
  483. void vPortValidateInterruptPriority( void )
  484. {
  485. extern uint32_t ulPortGetIPSR( void );
  486. uint32_t ulCurrentInterrupt;
  487. uint8_t ucCurrentPriority;
  488. ulCurrentInterrupt = ulPortGetIPSR();
  489. /* Is the interrupt number a user defined interrupt? */
  490. if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
  491. {
  492. /* Look up the interrupt's priority. */
  493. ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
  494. /* The following assertion will fail if a service routine (ISR) for
  495. * an interrupt that has been assigned a priority above
  496. * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
  497. * function. ISR safe FreeRTOS API functions must *only* be called
  498. * from interrupts that have been assigned a priority at or below
  499. * configMAX_SYSCALL_INTERRUPT_PRIORITY.
  500. *
  501. * Numerically low interrupt priority numbers represent logically high
  502. * interrupt priorities, therefore the priority of the interrupt must
  503. * be set to a value equal to or numerically *higher* than
  504. * configMAX_SYSCALL_INTERRUPT_PRIORITY.
  505. *
  506. * Interrupts that use the FreeRTOS API must not be left at their
  507. * default priority of zero as that is the highest possible priority,
  508. * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
  509. * and therefore also guaranteed to be invalid.
  510. *
  511. * FreeRTOS maintains separate thread and ISR API functions to ensure
  512. * interrupt entry is as fast and simple as possible.
  513. *
  514. * The following links provide detailed information:
  515. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
  516. * https://www.FreeRTOS.org/FAQHelp.html */
  517. configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
  518. }
  519. /* Priority grouping: The interrupt controller (NVIC) allows the bits
  520. * that define each interrupt's priority to be split between bits that
  521. * define the interrupt's pre-emption priority bits and bits that define
  522. * the interrupt's sub-priority. For simplicity all bits must be defined
  523. * to be pre-emption priority bits. The following assertion will fail if
  524. * this is not the case (if some bits represent a sub-priority).
  525. *
  526. * If the application only uses CMSIS libraries for interrupt
  527. * configuration then the correct setting can be achieved on all Cortex-M
  528. * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
  529. * scheduler. Note however that some vendor specific peripheral libraries
  530. * assume a non-zero priority group setting, in which cases using a value
  531. * of zero will result in unpredictable behaviour. */
  532. configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
  533. }
  534. #endif /* configASSERT_DEFINED */