port.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. /*
  2. FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.
  3. All rights reserved
  4. VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
  5. This file is part of the FreeRTOS distribution.
  6. FreeRTOS is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU General Public License (version 2) as published by the
  8. Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
  9. ***************************************************************************
  10. >>! NOTE: The modification to the GPL is included to allow you to !<<
  11. >>! distribute a combined work that includes FreeRTOS without being !<<
  12. >>! obliged to provide the source code for proprietary components !<<
  13. >>! outside of the FreeRTOS kernel. !<<
  14. ***************************************************************************
  15. FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
  16. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. FOR A PARTICULAR PURPOSE. Full license text is available on the following
  18. link: http://www.freertos.org/a00114.html
  19. ***************************************************************************
  20. * *
  21. * FreeRTOS provides completely free yet professionally developed, *
  22. * robust, strictly quality controlled, supported, and cross *
  23. * platform software that is more than just the market leader, it *
  24. * is the industry's de facto standard. *
  25. * *
  26. * Help yourself get started quickly while simultaneously helping *
  27. * to support the FreeRTOS project by purchasing a FreeRTOS *
  28. * tutorial book, reference manual, or both: *
  29. * http://www.FreeRTOS.org/Documentation *
  30. * *
  31. ***************************************************************************
  32. http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
  33. the FAQ page "My application does not run, what could be wrong?". Have you
  34. defined configASSERT()?
  35. http://www.FreeRTOS.org/support - In return for receiving this top quality
  36. embedded software for free we request you assist our global community by
  37. participating in the support forum.
  38. http://www.FreeRTOS.org/training - Investing in training allows your team to
  39. be as productive as possible as early as possible. Now you can receive
  40. FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
  41. Ltd, and the world's leading authority on the world's leading RTOS.
  42. http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
  43. including FreeRTOS+Trace - an indispensable productivity tool, a DOS
  44. compatible FAT file system, and our tiny thread aware UDP/IP stack.
  45. http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
  46. Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
  47. http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
  48. Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
  49. licenses offer ticketed support, indemnification and commercial middleware.
  50. http://www.SafeRTOS.com - High Integrity Systems also provide a safety
  51. engineered and independently SIL3 certified version for use in safety and
  52. mission critical applications that require provable dependability.
  53. 1 tab == 4 spaces!
  54. */
  55. /*-----------------------------------------------------------
  56. * Implementation of functions defined in portable.h for the ARM CM4F port.
  57. *----------------------------------------------------------*/
  58. /* Compiler includes. */
  59. #include <intrinsics.h>
  60. /* Scheduler includes. */
  61. #include "FreeRTOS.h"
  62. #include "task.h"
  63. #ifndef __ARMVFP__
  64. #error This port can only be used when the project options are configured to enable hardware floating point support.
  65. #endif
  66. #if configMAX_SYSCALL_INTERRUPT_PRIORITY == 0
  67. #error configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0. See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
  68. #endif
  69. #ifndef configSYSTICK_CLOCK_HZ
  70. #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
  71. /* Ensure the SysTick is clocked at the same frequency as the core. */
  72. #define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
  73. #else
  74. /* The way the SysTick is clocked is not modified in case it is not the same
  75. as the core. */
  76. #define portNVIC_SYSTICK_CLK_BIT ( 0 )
  77. #endif
  78. /* Constants required to manipulate the core. Registers first... */
  79. #define portNVIC_SYSTICK_CTRL_REG ( * ( ( volatile uint32_t * ) 0xe000e010 ) )
  80. #define portNVIC_SYSTICK_LOAD_REG ( * ( ( volatile uint32_t * ) 0xe000e014 ) )
  81. #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( * ( ( volatile uint32_t * ) 0xe000e018 ) )
  82. #define portNVIC_SYSPRI2_REG ( * ( ( volatile uint32_t * ) 0xe000ed20 ) )
  83. /* ...then bits in the registers. */
  84. #define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
  85. #define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
  86. #define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
  87. #define portNVIC_PENDSVCLEAR_BIT ( 1UL << 27UL )
  88. #define portNVIC_PEND_SYSTICK_CLEAR_BIT ( 1UL << 25UL )
  89. /* Constants used to detect a Cortex-M7 r0p1 core, which should use the ARM_CM7
  90. r0p1 port. */
  91. #define portCPUID ( * ( ( volatile uint32_t * ) 0xE000ed00 ) )
  92. #define portCORTEX_M7_r0p1_ID ( 0x410FC271UL )
  93. #define portCORTEX_M7_r0p0_ID ( 0x410FC270UL )
  94. #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL )
  95. #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24UL )
  96. /* Constants required to check the validity of an interrupt priority. */
  97. #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
  98. #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
  99. #define portAIRCR_REG ( * ( ( volatile uint32_t * ) 0xE000ED0C ) )
  100. #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
  101. #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
  102. #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
  103. #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
  104. #define portPRIGROUP_SHIFT ( 8UL )
  105. /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */
  106. #define portVECTACTIVE_MASK ( 0xFFUL )
  107. /* Constants required to manipulate the VFP. */
  108. #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */
  109. #define portASPEN_AND_LSPEN_BITS ( 0x3UL << 30UL )
  110. /* Constants required to set up the initial stack. */
  111. #define portINITIAL_XPSR ( 0x01000000 )
  112. #define portINITIAL_EXEC_RETURN ( 0xfffffffd )
  113. /* The systick is a 24-bit counter. */
  114. #define portMAX_24_BIT_NUMBER ( 0xffffffUL )
  115. /* A fiddle factor to estimate the number of SysTick counts that would have
  116. occurred while the SysTick counter is stopped during tickless idle
  117. calculations. */
  118. #define portMISSED_COUNTS_FACTOR ( 45UL )
  119. /* Each task maintains its own interrupt status in the critical nesting
  120. variable. */
  121. static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
  122. /*
  123. * Setup the timer to generate the tick interrupts. The implementation in this
  124. * file is weak to allow application writers to change the timer used to
  125. * generate the tick interrupt.
  126. */
  127. void vPortSetupTimerInterrupt( void );
  128. /*
  129. * Exception handlers.
  130. */
  131. void xPortSysTickHandler( void );
  132. /*
  133. * Start first task is a separate function so it can be tested in isolation.
  134. */
  135. extern void vPortStartFirstTask( void );
  136. /*
  137. * Turn the VFP on.
  138. */
  139. extern void vPortEnableVFP( void );
  140. /*
  141. * Used to catch tasks that attempt to return from their implementing function.
  142. */
  143. static void prvTaskExitError( void );
  144. /*-----------------------------------------------------------*/
  145. /*
  146. * The number of SysTick increments that make up one tick period.
  147. */
  148. #if configUSE_TICKLESS_IDLE == 1
  149. static uint32_t ulTimerCountsForOneTick = 0;
  150. #endif /* configUSE_TICKLESS_IDLE */
  151. /*
  152. * The maximum number of tick periods that can be suppressed is limited by the
  153. * 24 bit resolution of the SysTick timer.
  154. */
  155. #if configUSE_TICKLESS_IDLE == 1
  156. static uint32_t xMaximumPossibleSuppressedTicks = 0;
  157. #endif /* configUSE_TICKLESS_IDLE */
  158. /*
  159. * Compensate for the CPU cycles that pass while the SysTick is stopped (low
  160. * power functionality only.
  161. */
  162. #if configUSE_TICKLESS_IDLE == 1
  163. static uint32_t ulStoppedTimerCompensation = 0;
  164. #endif /* configUSE_TICKLESS_IDLE */
  165. /*
  166. * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
  167. * FreeRTOS API functions are not called from interrupts that have been assigned
  168. * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
  169. */
  170. #if ( configASSERT_DEFINED == 1 )
  171. static uint8_t ucMaxSysCallPriority = 0;
  172. static uint32_t ulMaxPRIGROUPValue = 0;
  173. static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
  174. #endif /* configASSERT_DEFINED */
  175. /*-----------------------------------------------------------*/
  176. /*
  177. * See header file for description.
  178. */
  179. StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
  180. {
  181. /* Simulate the stack frame as it would be created by a context switch
  182. interrupt. */
  183. /* Offset added to account for the way the MCU uses the stack on entry/exit
  184. of interrupts, and to ensure alignment. */
  185. pxTopOfStack--;
  186. *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
  187. pxTopOfStack--;
  188. *pxTopOfStack = ( StackType_t ) pxCode; /* PC */
  189. pxTopOfStack--;
  190. *pxTopOfStack = ( StackType_t ) prvTaskExitError; /* LR */
  191. /* Save code space by skipping register initialisation. */
  192. pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
  193. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  194. /* A save method is being used that requires each task to maintain its
  195. own exec return value. */
  196. pxTopOfStack--;
  197. *pxTopOfStack = portINITIAL_EXEC_RETURN;
  198. pxTopOfStack -= 8; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
  199. return pxTopOfStack;
  200. }
  201. /*-----------------------------------------------------------*/
  202. static void prvTaskExitError( void )
  203. {
  204. /* A function that implements a task must not exit or attempt to return to
  205. its caller as there is nothing to return to. If a task wants to exit it
  206. should instead call vTaskDelete( NULL ).
  207. Artificially force an assert() to be triggered if configASSERT() is
  208. defined, then stop here so application writers can catch the error. */
  209. configASSERT( uxCriticalNesting == ~0UL );
  210. portDISABLE_INTERRUPTS();
  211. for( ;; );
  212. }
  213. /*-----------------------------------------------------------*/
  214. /*
  215. * See header file for description.
  216. */
  217. BaseType_t xPortStartScheduler( void )
  218. {
  219. /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.
  220. See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  221. configASSERT( configMAX_SYSCALL_INTERRUPT_PRIORITY );
  222. /* This port can be used on all revisions of the Cortex-M7 core other than
  223. the r0p1 parts. r0p1 parts should use the port from the
  224. /source/portable/GCC/ARM_CM7/r0p1 directory. */
  225. configASSERT( portCPUID != portCORTEX_M7_r0p1_ID );
  226. configASSERT( portCPUID != portCORTEX_M7_r0p0_ID );
  227. #if( configASSERT_DEFINED == 1 )
  228. {
  229. volatile uint32_t ulOriginalPriority;
  230. volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
  231. volatile uint8_t ucMaxPriorityValue;
  232. /* Determine the maximum priority from which ISR safe FreeRTOS API
  233. functions can be called. ISR safe functions are those that end in
  234. "FromISR". FreeRTOS maintains separate thread and ISR API functions to
  235. ensure interrupt entry is as fast and simple as possible.
  236. Save the interrupt priority value that is about to be clobbered. */
  237. ulOriginalPriority = *pucFirstUserPriorityRegister;
  238. /* Determine the number of priority bits available. First write to all
  239. possible bits. */
  240. *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
  241. /* Read the value back to see how many bits stuck. */
  242. ucMaxPriorityValue = *pucFirstUserPriorityRegister;
  243. /* Use the same mask on the maximum system call priority. */
  244. ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
  245. /* Calculate the maximum acceptable priority group value for the number
  246. of bits read back. */
  247. ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS;
  248. while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
  249. {
  250. ulMaxPRIGROUPValue--;
  251. ucMaxPriorityValue <<= ( uint8_t ) 0x01;
  252. }
  253. /* Shift the priority group value back to its position within the AIRCR
  254. register. */
  255. ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
  256. ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
  257. /* Restore the clobbered interrupt priority register to its original
  258. value. */
  259. *pucFirstUserPriorityRegister = ulOriginalPriority;
  260. }
  261. #endif /* conifgASSERT_DEFINED */
  262. /* Make PendSV and SysTick the lowest priority interrupts. */
  263. portNVIC_SYSPRI2_REG |= portNVIC_PENDSV_PRI;
  264. portNVIC_SYSPRI2_REG |= portNVIC_SYSTICK_PRI;
  265. /* Start the timer that generates the tick ISR. Interrupts are disabled
  266. here already. */
  267. vPortSetupTimerInterrupt();
  268. /* Initialise the critical nesting count ready for the first task. */
  269. uxCriticalNesting = 0;
  270. /* Ensure the VFP is enabled - it should be anyway. */
  271. vPortEnableVFP();
  272. /* Lazy save always. */
  273. *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
  274. /* Start the first task. */
  275. vPortStartFirstTask();
  276. /* Should not get here! */
  277. return 0;
  278. }
  279. /*-----------------------------------------------------------*/
  280. void vPortEndScheduler( void )
  281. {
  282. /* Not implemented in ports where there is nothing to return to.
  283. Artificially force an assert. */
  284. configASSERT( uxCriticalNesting == 1000UL );
  285. }
  286. /*-----------------------------------------------------------*/
  287. void vPortEnterCritical( void )
  288. {
  289. portDISABLE_INTERRUPTS();
  290. uxCriticalNesting++;
  291. /* This is not the interrupt safe version of the enter critical function so
  292. assert() if it is being called from an interrupt context. Only API
  293. functions that end in "FromISR" can be used in an interrupt. Only assert if
  294. the critical nesting count is 1 to protect against recursive calls if the
  295. assert function also uses a critical section. */
  296. if( uxCriticalNesting == 1 )
  297. {
  298. configASSERT( ( portNVIC_INT_CTRL_REG & portVECTACTIVE_MASK ) == 0 );
  299. }
  300. }
  301. /*-----------------------------------------------------------*/
  302. void vPortExitCritical( void )
  303. {
  304. configASSERT( uxCriticalNesting );
  305. uxCriticalNesting--;
  306. if( uxCriticalNesting == 0 )
  307. {
  308. portENABLE_INTERRUPTS();
  309. }
  310. }
  311. /*-----------------------------------------------------------*/
  312. void xPortSysTickHandler( void )
  313. {
  314. /* The SysTick runs at the lowest interrupt priority, so when this interrupt
  315. executes all interrupts must be unmasked. There is therefore no need to
  316. save and then restore the interrupt mask value as its value is already
  317. known. */
  318. ( void ) portSET_INTERRUPT_MASK_FROM_ISR();
  319. {
  320. /* Increment the RTOS tick. */
  321. if( xTaskIncrementTick() != pdFALSE )
  322. {
  323. /* A context switch is required. Context switching is performed in
  324. the PendSV interrupt. Pend the PendSV interrupt. */
  325. portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
  326. }
  327. }
  328. portCLEAR_INTERRUPT_MASK_FROM_ISR( 0 );
  329. }
  330. /*-----------------------------------------------------------*/
  331. #if configUSE_TICKLESS_IDLE == 1
  332. __weak void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
  333. {
  334. uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements, ulSysTickCTRL;
  335. TickType_t xModifiableIdleTime;
  336. /* Make sure the SysTick reload value does not overflow the counter. */
  337. if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
  338. {
  339. xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
  340. }
  341. /* Stop the SysTick momentarily. The time the SysTick is stopped for
  342. is accounted for as best it can be, but using the tickless mode will
  343. inevitably result in some tiny drift of the time maintained by the
  344. kernel with respect to calendar time. */
  345. portNVIC_SYSTICK_CTRL_REG &= ~portNVIC_SYSTICK_ENABLE_BIT;
  346. /* Calculate the reload value required to wait xExpectedIdleTime
  347. tick periods. -1 is used because this code will execute part way
  348. through one of the tick periods. */
  349. ulReloadValue = portNVIC_SYSTICK_CURRENT_VALUE_REG + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );
  350. if( ulReloadValue > ulStoppedTimerCompensation )
  351. {
  352. ulReloadValue -= ulStoppedTimerCompensation;
  353. }
  354. /* Enter a critical section but don't use the taskENTER_CRITICAL()
  355. method as that will mask interrupts that should exit sleep mode. */
  356. __disable_interrupt();
  357. /* If a context switch is pending or a task is waiting for the scheduler
  358. to be unsuspended then abandon the low power entry. */
  359. if( eTaskConfirmSleepModeStatus() == eAbortSleep )
  360. {
  361. /* Restart from whatever is left in the count register to complete
  362. this tick period. */
  363. portNVIC_SYSTICK_LOAD_REG = portNVIC_SYSTICK_CURRENT_VALUE_REG;
  364. /* Restart SysTick. */
  365. portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
  366. /* Reset the reload register to the value required for normal tick
  367. periods. */
  368. portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
  369. /* Re-enable interrupts - see comments above __disable_interrupt()
  370. call above. */
  371. __enable_interrupt();
  372. }
  373. else
  374. {
  375. /* Set the new reload value. */
  376. portNVIC_SYSTICK_LOAD_REG = ulReloadValue;
  377. /* Clear the SysTick count flag and set the count value back to
  378. zero. */
  379. portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
  380. /* Restart SysTick. */
  381. portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
  382. /* Sleep until something happens. configPRE_SLEEP_PROCESSING() can
  383. set its parameter to 0 to indicate that its implementation contains
  384. its own wait for interrupt or wait for event instruction, and so wfi
  385. should not be executed again. However, the original expected idle
  386. time variable must remain unmodified, so a copy is taken. */
  387. xModifiableIdleTime = xExpectedIdleTime;
  388. configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
  389. if( xModifiableIdleTime > 0 )
  390. {
  391. __DSB();
  392. __WFI();
  393. __ISB();
  394. }
  395. configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
  396. /* Stop SysTick. Again, the time the SysTick is stopped for is
  397. accounted for as best it can be, but using the tickless mode will
  398. inevitably result in some tiny drift of the time maintained by the
  399. kernel with respect to calendar time. */
  400. ulSysTickCTRL = portNVIC_SYSTICK_CTRL_REG;
  401. portNVIC_SYSTICK_CTRL_REG = ( ulSysTickCTRL & ~portNVIC_SYSTICK_ENABLE_BIT );
  402. /* Re-enable interrupts - see comments above __disable_interrupt()
  403. call above. */
  404. __enable_interrupt();
  405. if( ( ulSysTickCTRL & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )
  406. {
  407. uint32_t ulCalculatedLoadValue;
  408. /* The tick interrupt has already executed, and the SysTick
  409. count reloaded with ulReloadValue. Reset the
  410. portNVIC_SYSTICK_LOAD_REG with whatever remains of this tick
  411. period. */
  412. ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );
  413. /* Don't allow a tiny value, or values that have somehow
  414. underflowed because the post sleep hook did something
  415. that took too long. */
  416. if( ( ulCalculatedLoadValue < ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick ) )
  417. {
  418. ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL );
  419. }
  420. portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;
  421. /* The tick interrupt handler will already have pended the tick
  422. processing in the kernel. As the pending tick will be
  423. processed as soon as this function exits, the tick value
  424. maintained by the tick is stepped forward by one less than the
  425. time spent waiting. */
  426. ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
  427. }
  428. else
  429. {
  430. /* Something other than the tick interrupt ended the sleep.
  431. Work out how long the sleep lasted rounded to complete tick
  432. periods (not the ulReload value which accounted for part
  433. ticks). */
  434. ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - portNVIC_SYSTICK_CURRENT_VALUE_REG;
  435. /* How many complete tick periods passed while the processor
  436. was waiting? */
  437. ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;
  438. /* The reload value is set to whatever fraction of a single tick
  439. period remains. */
  440. portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1 ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;
  441. }
  442. /* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD_REG
  443. again, then set portNVIC_SYSTICK_LOAD_REG back to its standard
  444. value. The critical section is used to ensure the tick interrupt
  445. can only execute once in the case that the reload register is near
  446. zero. */
  447. portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
  448. portENTER_CRITICAL();
  449. {
  450. portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
  451. vTaskStepTick( ulCompleteTickPeriods );
  452. portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
  453. }
  454. portEXIT_CRITICAL();
  455. }
  456. }
  457. #endif /* #if configUSE_TICKLESS_IDLE */
  458. /*-----------------------------------------------------------*/
  459. /*
  460. * Setup the systick timer to generate the tick interrupts at the required
  461. * frequency.
  462. */
  463. __weak void vPortSetupTimerInterrupt( void )
  464. {
  465. /* Calculate the constants required to configure the tick interrupt. */
  466. #if( configUSE_TICKLESS_IDLE == 1 )
  467. {
  468. ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );
  469. xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;
  470. ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );
  471. }
  472. #endif /* configUSE_TICKLESS_IDLE */
  473. /* Configure SysTick to interrupt at the requested rate. */
  474. portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
  475. portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );
  476. }
  477. /*-----------------------------------------------------------*/
  478. #if( configASSERT_DEFINED == 1 )
  479. void vPortValidateInterruptPriority( void )
  480. {
  481. uint32_t ulCurrentInterrupt;
  482. uint8_t ucCurrentPriority;
  483. /* Obtain the number of the currently executing interrupt. */
  484. __asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) );
  485. /* Is the interrupt number a user defined interrupt? */
  486. if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
  487. {
  488. /* Look up the interrupt's priority. */
  489. ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
  490. /* The following assertion will fail if a service routine (ISR) for
  491. an interrupt that has been assigned a priority above
  492. configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
  493. function. ISR safe FreeRTOS API functions must *only* be called
  494. from interrupts that have been assigned a priority at or below
  495. configMAX_SYSCALL_INTERRUPT_PRIORITY.
  496. Numerically low interrupt priority numbers represent logically high
  497. interrupt priorities, therefore the priority of the interrupt must
  498. be set to a value equal to or numerically *higher* than
  499. configMAX_SYSCALL_INTERRUPT_PRIORITY.
  500. Interrupts that use the FreeRTOS API must not be left at their
  501. default priority of zero as that is the highest possible priority,
  502. which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
  503. and therefore also guaranteed to be invalid.
  504. FreeRTOS maintains separate thread and ISR API functions to ensure
  505. interrupt entry is as fast and simple as possible.
  506. The following links provide detailed information:
  507. http://www.freertos.org/RTOS-Cortex-M3-M4.html
  508. http://www.freertos.org/FAQHelp.html */
  509. configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
  510. }
  511. /* Priority grouping: The interrupt controller (NVIC) allows the bits
  512. that define each interrupt's priority to be split between bits that
  513. define the interrupt's pre-emption priority bits and bits that define
  514. the interrupt's sub-priority. For simplicity all bits must be defined
  515. to be pre-emption priority bits. The following assertion will fail if
  516. this is not the case (if some bits represent a sub-priority).
  517. If the application only uses CMSIS libraries for interrupt
  518. configuration then the correct setting can be achieved on all Cortex-M
  519. devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
  520. scheduler. Note however that some vendor specific peripheral libraries
  521. assume a non-zero priority group setting, in which cases using a value
  522. of zero will result in unpredicable behaviour. */
  523. configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
  524. }
  525. #endif /* configASSERT_DEFINED */