port.c 28 KB

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