port.c 26 KB

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