port.c 24 KB

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