port.c 26 KB

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