port.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /*
  2. * FreeRTOS Kernel V10.4.6
  3. * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
  4. *
  5. * SPDX-License-Identifier: MIT
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. * this software and associated documentation files (the "Software"), to deal in
  9. * the Software without restriction, including without limitation the rights to
  10. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11. * the Software, and to permit persons to whom the Software is furnished to do so,
  12. * subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * https://www.FreeRTOS.org
  25. * https://github.com/FreeRTOS
  26. *
  27. */
  28. /*-----------------------------------------------------------
  29. * Implementation of functions defined in portable.h for the ARM CM4F port.
  30. *----------------------------------------------------------*/
  31. /* Scheduler includes. */
  32. #include "FreeRTOS.h"
  33. #include "task.h"
  34. #ifndef __VFP_FP__
  35. #error This port can only be used when the project options are configured to enable hardware floating point support.
  36. #endif
  37. #ifndef configSYSTICK_CLOCK_HZ
  38. #define configSYSTICK_CLOCK_HZ configCPU_CLOCK_HZ
  39. /* Ensure the SysTick is clocked at the same frequency as the core. */
  40. #define portNVIC_SYSTICK_CLK_BIT ( 1UL << 2UL )
  41. #else
  42. /* The way the SysTick is clocked is not modified in case it is not the same
  43. * as the core. */
  44. #define portNVIC_SYSTICK_CLK_BIT ( 0 )
  45. #endif
  46. /* Constants required to manipulate the core. Registers first... */
  47. #define portNVIC_SYSTICK_CTRL_REG ( *( ( volatile uint32_t * ) 0xe000e010 ) )
  48. #define portNVIC_SYSTICK_LOAD_REG ( *( ( volatile uint32_t * ) 0xe000e014 ) )
  49. #define portNVIC_SYSTICK_CURRENT_VALUE_REG ( *( ( volatile uint32_t * ) 0xe000e018 ) )
  50. #define portNVIC_SHPR3_REG ( *( ( volatile uint32_t * ) 0xe000ed20 ) )
  51. /* ...then bits in the registers. */
  52. #define portNVIC_SYSTICK_INT_BIT ( 1UL << 1UL )
  53. #define portNVIC_SYSTICK_ENABLE_BIT ( 1UL << 0UL )
  54. #define portNVIC_SYSTICK_COUNT_FLAG_BIT ( 1UL << 16UL )
  55. #define portNVIC_PENDSVCLEAR_BIT ( 1UL << 27UL )
  56. #define portNVIC_PEND_SYSTICK_CLEAR_BIT ( 1UL << 25UL )
  57. /* Constants used to detect a Cortex-M7 r0p1 core, which should use the ARM_CM7
  58. * r0p1 port. */
  59. #define portCPUID ( *( ( volatile uint32_t * ) 0xE000ed00 ) )
  60. #define portCORTEX_M7_r0p1_ID ( 0x410FC271UL )
  61. #define portCORTEX_M7_r0p0_ID ( 0x410FC270UL )
  62. #define portNVIC_PENDSV_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 16UL )
  63. #define portNVIC_SYSTICK_PRI ( ( ( uint32_t ) configKERNEL_INTERRUPT_PRIORITY ) << 24UL )
  64. /* Constants required to check the validity of an interrupt priority. */
  65. #define portFIRST_USER_INTERRUPT_NUMBER ( 16 )
  66. #define portNVIC_IP_REGISTERS_OFFSET_16 ( 0xE000E3F0 )
  67. #define portAIRCR_REG ( *( ( volatile uint32_t * ) 0xE000ED0C ) )
  68. #define portMAX_8_BIT_VALUE ( ( uint8_t ) 0xff )
  69. #define portTOP_BIT_OF_BYTE ( ( uint8_t ) 0x80 )
  70. #define portMAX_PRIGROUP_BITS ( ( uint8_t ) 7 )
  71. #define portPRIORITY_GROUP_MASK ( 0x07UL << 8UL )
  72. #define portPRIGROUP_SHIFT ( 8UL )
  73. /* Masks off all bits but the VECTACTIVE bits in the ICSR register. */
  74. #define portVECTACTIVE_MASK ( 0xFFUL )
  75. /* Constants required to manipulate the VFP. */
  76. #define portFPCCR ( ( volatile uint32_t * ) 0xe000ef34 ) /* Floating point context control register. */
  77. #define portASPEN_AND_LSPEN_BITS ( 0x3UL << 30UL )
  78. /* Constants required to set up the initial stack. */
  79. #define portINITIAL_XPSR ( 0x01000000 )
  80. #define portINITIAL_EXC_RETURN ( 0xfffffffd )
  81. /* The systick is a 24-bit counter. */
  82. #define portMAX_24_BIT_NUMBER ( 0xffffffUL )
  83. /* For strict compliance with the Cortex-M spec the task start address should
  84. * have bit-0 clear, as it is loaded into the PC on exit from an ISR. */
  85. #define portSTART_ADDRESS_MASK ( ( StackType_t ) 0xfffffffeUL )
  86. /* A fiddle factor to estimate the number of SysTick counts that would have
  87. * occurred while the SysTick counter is stopped during tickless idle
  88. * calculations. */
  89. #define portMISSED_COUNTS_FACTOR ( 45UL )
  90. /* Let the user override the pre-loading of the initial LR with the address of
  91. * prvTaskExitError() in case it messes up unwinding of the stack in the
  92. * debugger. */
  93. #ifdef configTASK_RETURN_ADDRESS
  94. #define portTASK_RETURN_ADDRESS configTASK_RETURN_ADDRESS
  95. #else
  96. #define portTASK_RETURN_ADDRESS prvTaskExitError
  97. #endif
  98. /*
  99. * Setup the timer to generate the tick interrupts. The implementation in this
  100. * file is weak to allow application writers to change the timer used to
  101. * generate the tick interrupt.
  102. */
  103. void vPortSetupTimerInterrupt( void );
  104. /*
  105. * Exception handlers.
  106. */
  107. void xPortPendSVHandler( void ) __attribute__( ( naked ) );
  108. void xPortSysTickHandler( void );
  109. void vPortSVCHandler( void ) __attribute__( ( naked ) );
  110. /*
  111. * Start first task is a separate function so it can be tested in isolation.
  112. */
  113. static void prvPortStartFirstTask( void ) __attribute__( ( naked ) );
  114. /*
  115. * Function to enable the VFP.
  116. */
  117. static void vPortEnableVFP( void ) __attribute__( ( naked ) );
  118. /*
  119. * Used to catch tasks that attempt to return from their implementing function.
  120. */
  121. static void prvTaskExitError( void );
  122. /*-----------------------------------------------------------*/
  123. /* Each task maintains its own interrupt status in the critical nesting
  124. * variable. */
  125. static UBaseType_t uxCriticalNesting = 0xaaaaaaaa;
  126. /*
  127. * The number of SysTick increments that make up one tick period.
  128. */
  129. #if ( configUSE_TICKLESS_IDLE == 1 )
  130. static uint32_t ulTimerCountsForOneTick = 0;
  131. #endif /* configUSE_TICKLESS_IDLE */
  132. /*
  133. * The maximum number of tick periods that can be suppressed is limited by the
  134. * 24 bit resolution of the SysTick timer.
  135. */
  136. #if ( configUSE_TICKLESS_IDLE == 1 )
  137. static uint32_t xMaximumPossibleSuppressedTicks = 0;
  138. #endif /* configUSE_TICKLESS_IDLE */
  139. /*
  140. * Compensate for the CPU cycles that pass while the SysTick is stopped (low
  141. * power functionality only.
  142. */
  143. #if ( configUSE_TICKLESS_IDLE == 1 )
  144. static uint32_t ulStoppedTimerCompensation = 0;
  145. #endif /* configUSE_TICKLESS_IDLE */
  146. /*
  147. * Used by the portASSERT_IF_INTERRUPT_PRIORITY_INVALID() macro to ensure
  148. * FreeRTOS API functions are not called from interrupts that have been assigned
  149. * a priority above configMAX_SYSCALL_INTERRUPT_PRIORITY.
  150. */
  151. #if ( configASSERT_DEFINED == 1 )
  152. static uint8_t ucMaxSysCallPriority = 0;
  153. static uint32_t ulMaxPRIGROUPValue = 0;
  154. static const volatile uint8_t * const pcInterruptPriorityRegisters = ( const volatile uint8_t * const ) portNVIC_IP_REGISTERS_OFFSET_16;
  155. #endif /* configASSERT_DEFINED */
  156. /*-----------------------------------------------------------*/
  157. /*
  158. * See header file for description.
  159. */
  160. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  161. TaskFunction_t pxCode,
  162. void * pvParameters )
  163. {
  164. /* Simulate the stack frame as it would be created by a context switch
  165. * interrupt. */
  166. /* Offset added to account for the way the MCU uses the stack on entry/exit
  167. * of interrupts, and to ensure alignment. */
  168. pxTopOfStack--;
  169. *pxTopOfStack = portINITIAL_XPSR; /* xPSR */
  170. pxTopOfStack--;
  171. *pxTopOfStack = ( ( StackType_t ) pxCode ) & portSTART_ADDRESS_MASK; /* PC */
  172. pxTopOfStack--;
  173. *pxTopOfStack = ( StackType_t ) portTASK_RETURN_ADDRESS; /* LR */
  174. /* Save code space by skipping register initialisation. */
  175. pxTopOfStack -= 5; /* R12, R3, R2 and R1. */
  176. *pxTopOfStack = ( StackType_t ) pvParameters; /* R0 */
  177. /* A save method is being used that requires each task to maintain its
  178. * own exec return value. */
  179. pxTopOfStack--;
  180. *pxTopOfStack = portINITIAL_EXC_RETURN;
  181. pxTopOfStack -= 8; /* R11, R10, R9, R8, R7, R6, R5 and R4. */
  182. return pxTopOfStack;
  183. }
  184. /*-----------------------------------------------------------*/
  185. static void prvTaskExitError( void )
  186. {
  187. volatile uint32_t ulDummy = 0;
  188. /* A function that implements a task must not exit or attempt to return to
  189. * its caller as there is nothing to return to. If a task wants to exit it
  190. * should instead call vTaskDelete( NULL ).
  191. *
  192. * Artificially force an assert() to be triggered if configASSERT() is
  193. * defined, then stop here so application writers can catch the error. */
  194. configASSERT( uxCriticalNesting == ~0UL );
  195. portDISABLE_INTERRUPTS();
  196. while( ulDummy == 0 )
  197. {
  198. /* This file calls prvTaskExitError() after the scheduler has been
  199. * started to remove a compiler warning about the function being defined
  200. * but never called. ulDummy is used purely to quieten other warnings
  201. * about code appearing after this function is called - making ulDummy
  202. * volatile makes the compiler think the function could return and
  203. * therefore not output an 'unreachable code' warning for code that appears
  204. * after it. */
  205. }
  206. }
  207. /*-----------------------------------------------------------*/
  208. void vPortSVCHandler( void )
  209. {
  210. __asm volatile (
  211. " ldr r3, pxCurrentTCBConst2 \n"/* Restore the context. */
  212. " ldr r1, [r3] \n"/* Use pxCurrentTCBConst to get the pxCurrentTCB address. */
  213. " ldr r0, [r1] \n"/* The first item in pxCurrentTCB is the task top of stack. */
  214. " ldmia r0!, {r4-r11, r14} \n"/* Pop the registers that are not automatically saved on exception entry and the critical nesting count. */
  215. " msr psp, r0 \n"/* Restore the task stack pointer. */
  216. " isb \n"
  217. " mov r0, #0 \n"
  218. " msr basepri, r0 \n"
  219. " bx r14 \n"
  220. " \n"
  221. " .align 4 \n"
  222. "pxCurrentTCBConst2: .word pxCurrentTCB \n"
  223. );
  224. }
  225. /*-----------------------------------------------------------*/
  226. static void prvPortStartFirstTask( void )
  227. {
  228. /* Start the first task. This also clears the bit that indicates the FPU is
  229. * in use in case the FPU was used before the scheduler was started - which
  230. * would otherwise result in the unnecessary leaving of space in the SVC stack
  231. * for lazy saving of FPU registers. */
  232. __asm volatile (
  233. " ldr r0, =0xE000ED08 \n"/* Use the NVIC offset register to locate the stack. */
  234. " ldr r0, [r0] \n"
  235. " ldr r0, [r0] \n"
  236. " msr msp, r0 \n"/* Set the msp back to the start of the stack. */
  237. " mov r0, #0 \n"/* Clear the bit that indicates the FPU is in use, see comment above. */
  238. " msr control, r0 \n"
  239. " cpsie i \n"/* Globally enable interrupts. */
  240. " cpsie f \n"
  241. " dsb \n"
  242. " isb \n"
  243. " svc 0 \n"/* System call to start first task. */
  244. " nop \n"
  245. " .ltorg \n"
  246. );
  247. }
  248. /*-----------------------------------------------------------*/
  249. /*
  250. * See header file for description.
  251. */
  252. BaseType_t xPortStartScheduler( void )
  253. {
  254. /* configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to 0.
  255. * See https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
  256. configASSERT( configMAX_SYSCALL_INTERRUPT_PRIORITY );
  257. /* This port can be used on all revisions of the Cortex-M7 core other than
  258. * the r0p1 parts. r0p1 parts should use the port from the
  259. * /source/portable/GCC/ARM_CM7/r0p1 directory. */
  260. configASSERT( portCPUID != portCORTEX_M7_r0p1_ID );
  261. configASSERT( portCPUID != portCORTEX_M7_r0p0_ID );
  262. #if ( configASSERT_DEFINED == 1 )
  263. {
  264. volatile uint32_t ulOriginalPriority;
  265. volatile uint8_t * const pucFirstUserPriorityRegister = ( volatile uint8_t * const ) ( portNVIC_IP_REGISTERS_OFFSET_16 + portFIRST_USER_INTERRUPT_NUMBER );
  266. volatile uint8_t ucMaxPriorityValue;
  267. /* Determine the maximum priority from which ISR safe FreeRTOS API
  268. * functions can be called. ISR safe functions are those that end in
  269. * "FromISR". FreeRTOS maintains separate thread and ISR API functions to
  270. * ensure interrupt entry is as fast and simple as possible.
  271. *
  272. * Save the interrupt priority value that is about to be clobbered. */
  273. ulOriginalPriority = *pucFirstUserPriorityRegister;
  274. /* Determine the number of priority bits available. First write to all
  275. * possible bits. */
  276. *pucFirstUserPriorityRegister = portMAX_8_BIT_VALUE;
  277. /* Read the value back to see how many bits stuck. */
  278. ucMaxPriorityValue = *pucFirstUserPriorityRegister;
  279. /* Use the same mask on the maximum system call priority. */
  280. ucMaxSysCallPriority = configMAX_SYSCALL_INTERRUPT_PRIORITY & ucMaxPriorityValue;
  281. /* Calculate the maximum acceptable priority group value for the number
  282. * of bits read back. */
  283. ulMaxPRIGROUPValue = portMAX_PRIGROUP_BITS;
  284. while( ( ucMaxPriorityValue & portTOP_BIT_OF_BYTE ) == portTOP_BIT_OF_BYTE )
  285. {
  286. ulMaxPRIGROUPValue--;
  287. ucMaxPriorityValue <<= ( uint8_t ) 0x01;
  288. }
  289. #ifdef __NVIC_PRIO_BITS
  290. {
  291. /* Check the CMSIS configuration that defines the number of
  292. * priority bits matches the number of priority bits actually queried
  293. * from the hardware. */
  294. configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == __NVIC_PRIO_BITS );
  295. }
  296. #endif
  297. #ifdef configPRIO_BITS
  298. {
  299. /* Check the FreeRTOS configuration that defines the number of
  300. * priority bits matches the number of priority bits actually queried
  301. * from the hardware. */
  302. configASSERT( ( portMAX_PRIGROUP_BITS - ulMaxPRIGROUPValue ) == configPRIO_BITS );
  303. }
  304. #endif
  305. /* Shift the priority group value back to its position within the AIRCR
  306. * register. */
  307. ulMaxPRIGROUPValue <<= portPRIGROUP_SHIFT;
  308. ulMaxPRIGROUPValue &= portPRIORITY_GROUP_MASK;
  309. /* Restore the clobbered interrupt priority register to its original
  310. * value. */
  311. *pucFirstUserPriorityRegister = ulOriginalPriority;
  312. }
  313. #endif /* configASSERT_DEFINED */
  314. /* Make PendSV and SysTick the lowest priority interrupts. */
  315. portNVIC_SHPR3_REG |= portNVIC_PENDSV_PRI;
  316. portNVIC_SHPR3_REG |= portNVIC_SYSTICK_PRI;
  317. /* Start the timer that generates the tick ISR. Interrupts are disabled
  318. * here already. */
  319. vPortSetupTimerInterrupt();
  320. /* Initialise the critical nesting count ready for the first task. */
  321. uxCriticalNesting = 0;
  322. /* Ensure the VFP is enabled - it should be anyway. */
  323. vPortEnableVFP();
  324. /* Lazy save always. */
  325. *( portFPCCR ) |= portASPEN_AND_LSPEN_BITS;
  326. /* Start the first task. */
  327. prvPortStartFirstTask();
  328. /* Should never get here as the tasks will now be executing! Call the task
  329. * exit error function to prevent compiler warnings about a static function
  330. * not being called in the case that the application writer overrides this
  331. * functionality by defining configTASK_RETURN_ADDRESS. Call
  332. * vTaskSwitchContext() so link time optimisation does not remove the
  333. * symbol. */
  334. vTaskSwitchContext();
  335. prvTaskExitError();
  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. void xPortPendSVHandler( void )
  373. {
  374. /* This is a naked function. */
  375. __asm volatile
  376. (
  377. " mrs r0, psp \n"
  378. " isb \n"
  379. " \n"
  380. " ldr r3, pxCurrentTCBConst \n"/* Get the location of the current TCB. */
  381. " ldr r2, [r3] \n"
  382. " \n"
  383. " tst r14, #0x10 \n"/* Is the task using the FPU context? If so, push high vfp registers. */
  384. " it eq \n"
  385. " vstmdbeq r0!, {s16-s31} \n"
  386. " \n"
  387. " stmdb r0!, {r4-r11, r14} \n"/* Save the core registers. */
  388. " str r0, [r2] \n"/* Save the new top of stack into the first member of the TCB. */
  389. " \n"
  390. " stmdb sp!, {r0, r3} \n"
  391. " mov r0, %0 \n"
  392. " msr basepri, r0 \n"
  393. " dsb \n"
  394. " isb \n"
  395. " bl vTaskSwitchContext \n"
  396. " mov r0, #0 \n"
  397. " msr basepri, r0 \n"
  398. " ldmia sp!, {r0, r3} \n"
  399. " \n"
  400. " ldr r1, [r3] \n"/* The first item in pxCurrentTCB is the task top of stack. */
  401. " ldr r0, [r1] \n"
  402. " \n"
  403. " ldmia r0!, {r4-r11, r14} \n"/* Pop the core registers. */
  404. " \n"
  405. " tst r14, #0x10 \n"/* Is the task using the FPU context? If so, pop the high vfp registers too. */
  406. " it eq \n"
  407. " vldmiaeq r0!, {s16-s31} \n"
  408. " \n"
  409. " msr psp, r0 \n"
  410. " isb \n"
  411. " \n"
  412. #ifdef WORKAROUND_PMU_CM001 /* XMC4000 specific errata workaround. */
  413. #if WORKAROUND_PMU_CM001 == 1
  414. " push { r14 } \n"
  415. " pop { pc } \n"
  416. #endif
  417. #endif
  418. " \n"
  419. " bx r14 \n"
  420. " \n"
  421. " .align 4 \n"
  422. "pxCurrentTCBConst: .word pxCurrentTCB \n"
  423. ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY )
  424. );
  425. }
  426. /*-----------------------------------------------------------*/
  427. void xPortSysTickHandler( void )
  428. {
  429. /* The SysTick runs at the lowest interrupt priority, so when this interrupt
  430. * executes all interrupts must be unmasked. There is therefore no need to
  431. * save and then restore the interrupt mask value as its value is already
  432. * known. */
  433. portDISABLE_INTERRUPTS();
  434. {
  435. /* Increment the RTOS tick. */
  436. if( xTaskIncrementTick() != pdFALSE )
  437. {
  438. /* A context switch is required. Context switching is performed in
  439. * the PendSV interrupt. Pend the PendSV interrupt. */
  440. portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT;
  441. }
  442. }
  443. portENABLE_INTERRUPTS();
  444. }
  445. /*-----------------------------------------------------------*/
  446. #if ( configUSE_TICKLESS_IDLE == 1 )
  447. __attribute__( ( weak ) ) void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
  448. {
  449. uint32_t ulReloadValue, ulCompleteTickPeriods, ulCompletedSysTickDecrements;
  450. TickType_t xModifiableIdleTime;
  451. /* Make sure the SysTick reload value does not overflow the counter. */
  452. if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
  453. {
  454. xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
  455. }
  456. /* Stop the SysTick momentarily. The time the SysTick is stopped for
  457. * is accounted for as best it can be, but using the tickless mode will
  458. * inevitably result in some tiny drift of the time maintained by the
  459. * kernel with respect to calendar time. */
  460. portNVIC_SYSTICK_CTRL_REG &= ~portNVIC_SYSTICK_ENABLE_BIT;
  461. /* Calculate the reload value required to wait xExpectedIdleTime
  462. * tick periods. -1 is used because this code will execute part way
  463. * through one of the tick periods. */
  464. ulReloadValue = portNVIC_SYSTICK_CURRENT_VALUE_REG + ( ulTimerCountsForOneTick * ( xExpectedIdleTime - 1UL ) );
  465. if( ulReloadValue > ulStoppedTimerCompensation )
  466. {
  467. ulReloadValue -= ulStoppedTimerCompensation;
  468. }
  469. /* Enter a critical section but don't use the taskENTER_CRITICAL()
  470. * method as that will mask interrupts that should exit sleep mode. */
  471. __asm volatile ( "cpsid i" ::: "memory" );
  472. __asm volatile ( "dsb" );
  473. __asm volatile ( "isb" );
  474. /* If a context switch is pending or a task is waiting for the scheduler
  475. * to be unsuspended then abandon the low power entry. */
  476. if( eTaskConfirmSleepModeStatus() == eAbortSleep )
  477. {
  478. /* Restart from whatever is left in the count register to complete
  479. * this tick period. */
  480. portNVIC_SYSTICK_LOAD_REG = portNVIC_SYSTICK_CURRENT_VALUE_REG;
  481. /* Restart SysTick. */
  482. portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
  483. /* Reset the reload register to the value required for normal tick
  484. * periods. */
  485. portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
  486. /* Re-enable interrupts - see comments above the cpsid instruction()
  487. * above. */
  488. __asm volatile ( "cpsie i" ::: "memory" );
  489. }
  490. else
  491. {
  492. /* Set the new reload value. */
  493. portNVIC_SYSTICK_LOAD_REG = ulReloadValue;
  494. /* Clear the SysTick count flag and set the count value back to
  495. * zero. */
  496. portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
  497. /* Restart SysTick. */
  498. portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
  499. /* Sleep until something happens. configPRE_SLEEP_PROCESSING() can
  500. * set its parameter to 0 to indicate that its implementation contains
  501. * its own wait for interrupt or wait for event instruction, and so wfi
  502. * should not be executed again. However, the original expected idle
  503. * time variable must remain unmodified, so a copy is taken. */
  504. xModifiableIdleTime = xExpectedIdleTime;
  505. configPRE_SLEEP_PROCESSING( xModifiableIdleTime );
  506. if( xModifiableIdleTime > 0 )
  507. {
  508. __asm volatile ( "dsb" ::: "memory" );
  509. __asm volatile ( "wfi" );
  510. __asm volatile ( "isb" );
  511. }
  512. configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
  513. /* Re-enable interrupts to allow the interrupt that brought the MCU
  514. * out of sleep mode to execute immediately. see comments above
  515. * __disable_interrupt() call above. */
  516. __asm volatile ( "cpsie i" ::: "memory" );
  517. __asm volatile ( "dsb" );
  518. __asm volatile ( "isb" );
  519. /* Disable interrupts again because the clock is about to be stopped
  520. * and interrupts that execute while the clock is stopped will increase
  521. * any slippage between the time maintained by the RTOS and calendar
  522. * time. */
  523. __asm volatile ( "cpsid i" ::: "memory" );
  524. __asm volatile ( "dsb" );
  525. __asm volatile ( "isb" );
  526. /* Disable the SysTick clock without reading the
  527. * portNVIC_SYSTICK_CTRL_REG register to ensure the
  528. * portNVIC_SYSTICK_COUNT_FLAG_BIT is not cleared if it is set. Again,
  529. * the time the SysTick is stopped for is accounted for as best it can
  530. * be, but using the tickless mode will inevitably result in some tiny
  531. * drift of the time maintained by the kernel with respect to calendar
  532. * time*/
  533. portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT );
  534. /* Determine if the SysTick clock has already counted to zero and
  535. * been set back to the current reload value (the reload back being
  536. * correct for the entire expected idle time) or if the SysTick is yet
  537. * to count to zero (in which case an interrupt other than the SysTick
  538. * must have brought the system out of sleep mode). */
  539. if( ( portNVIC_SYSTICK_CTRL_REG & portNVIC_SYSTICK_COUNT_FLAG_BIT ) != 0 )
  540. {
  541. uint32_t ulCalculatedLoadValue;
  542. /* The tick interrupt is already pending, and the SysTick count
  543. * reloaded with ulReloadValue. Reset the
  544. * portNVIC_SYSTICK_LOAD_REG with whatever remains of this tick
  545. * period. */
  546. ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL ) - ( ulReloadValue - portNVIC_SYSTICK_CURRENT_VALUE_REG );
  547. /* Don't allow a tiny value, or values that have somehow
  548. * underflowed because the post sleep hook did something
  549. * that took too long. */
  550. if( ( ulCalculatedLoadValue < ulStoppedTimerCompensation ) || ( ulCalculatedLoadValue > ulTimerCountsForOneTick ) )
  551. {
  552. ulCalculatedLoadValue = ( ulTimerCountsForOneTick - 1UL );
  553. }
  554. portNVIC_SYSTICK_LOAD_REG = ulCalculatedLoadValue;
  555. /* As the pending tick will be processed as soon as this
  556. * function exits, the tick value maintained by the tick is stepped
  557. * forward by one less than the time spent waiting. */
  558. ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
  559. }
  560. else
  561. {
  562. /* Something other than the tick interrupt ended the sleep.
  563. * Work out how long the sleep lasted rounded to complete tick
  564. * periods (not the ulReload value which accounted for part
  565. * ticks). */
  566. ulCompletedSysTickDecrements = ( xExpectedIdleTime * ulTimerCountsForOneTick ) - portNVIC_SYSTICK_CURRENT_VALUE_REG;
  567. /* How many complete tick periods passed while the processor
  568. * was waiting? */
  569. ulCompleteTickPeriods = ulCompletedSysTickDecrements / ulTimerCountsForOneTick;
  570. /* The reload value is set to whatever fraction of a single tick
  571. * period remains. */
  572. portNVIC_SYSTICK_LOAD_REG = ( ( ulCompleteTickPeriods + 1UL ) * ulTimerCountsForOneTick ) - ulCompletedSysTickDecrements;
  573. }
  574. /* Restart SysTick so it runs from portNVIC_SYSTICK_LOAD_REG
  575. * again, then set portNVIC_SYSTICK_LOAD_REG back to its standard
  576. * value. */
  577. portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
  578. portNVIC_SYSTICK_CTRL_REG |= portNVIC_SYSTICK_ENABLE_BIT;
  579. vTaskStepTick( ulCompleteTickPeriods );
  580. portNVIC_SYSTICK_LOAD_REG = ulTimerCountsForOneTick - 1UL;
  581. /* Exit with interrupts enabled. */
  582. __asm volatile ( "cpsie i" ::: "memory" );
  583. }
  584. }
  585. #endif /* #if configUSE_TICKLESS_IDLE */
  586. /*-----------------------------------------------------------*/
  587. /*
  588. * Setup the systick timer to generate the tick interrupts at the required
  589. * frequency.
  590. */
  591. __attribute__( ( weak ) ) void vPortSetupTimerInterrupt( void )
  592. {
  593. /* Calculate the constants required to configure the tick interrupt. */
  594. #if ( configUSE_TICKLESS_IDLE == 1 )
  595. {
  596. ulTimerCountsForOneTick = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ );
  597. xMaximumPossibleSuppressedTicks = portMAX_24_BIT_NUMBER / ulTimerCountsForOneTick;
  598. ulStoppedTimerCompensation = portMISSED_COUNTS_FACTOR / ( configCPU_CLOCK_HZ / configSYSTICK_CLOCK_HZ );
  599. }
  600. #endif /* configUSE_TICKLESS_IDLE */
  601. /* Stop and clear the SysTick. */
  602. portNVIC_SYSTICK_CTRL_REG = 0UL;
  603. portNVIC_SYSTICK_CURRENT_VALUE_REG = 0UL;
  604. /* Configure SysTick to interrupt at the requested rate. */
  605. portNVIC_SYSTICK_LOAD_REG = ( configSYSTICK_CLOCK_HZ / configTICK_RATE_HZ ) - 1UL;
  606. portNVIC_SYSTICK_CTRL_REG = ( portNVIC_SYSTICK_CLK_BIT | portNVIC_SYSTICK_INT_BIT | portNVIC_SYSTICK_ENABLE_BIT );
  607. }
  608. /*-----------------------------------------------------------*/
  609. /* This is a naked function. */
  610. static void vPortEnableVFP( void )
  611. {
  612. __asm volatile
  613. (
  614. " ldr.w r0, =0xE000ED88 \n"/* The FPU enable bits are in the CPACR. */
  615. " ldr r1, [r0] \n"
  616. " \n"
  617. " orr r1, r1, #( 0xf << 20 ) \n"/* Enable CP10 and CP11 coprocessors, then save back. */
  618. " str r1, [r0] \n"
  619. " bx r14 \n"
  620. " .ltorg \n"
  621. );
  622. }
  623. /*-----------------------------------------------------------*/
  624. #if ( configASSERT_DEFINED == 1 )
  625. void vPortValidateInterruptPriority( void )
  626. {
  627. uint32_t ulCurrentInterrupt;
  628. uint8_t ucCurrentPriority;
  629. /* Obtain the number of the currently executing interrupt. */
  630. __asm volatile ( "mrs %0, ipsr" : "=r" ( ulCurrentInterrupt )::"memory" );
  631. /* Is the interrupt number a user defined interrupt? */
  632. if( ulCurrentInterrupt >= portFIRST_USER_INTERRUPT_NUMBER )
  633. {
  634. /* Look up the interrupt's priority. */
  635. ucCurrentPriority = pcInterruptPriorityRegisters[ ulCurrentInterrupt ];
  636. /* The following assertion will fail if a service routine (ISR) for
  637. * an interrupt that has been assigned a priority above
  638. * configMAX_SYSCALL_INTERRUPT_PRIORITY calls an ISR safe FreeRTOS API
  639. * function. ISR safe FreeRTOS API functions must *only* be called
  640. * from interrupts that have been assigned a priority at or below
  641. * configMAX_SYSCALL_INTERRUPT_PRIORITY.
  642. *
  643. * Numerically low interrupt priority numbers represent logically high
  644. * interrupt priorities, therefore the priority of the interrupt must
  645. * be set to a value equal to or numerically *higher* than
  646. * configMAX_SYSCALL_INTERRUPT_PRIORITY.
  647. *
  648. * Interrupts that use the FreeRTOS API must not be left at their
  649. * default priority of zero as that is the highest possible priority,
  650. * which is guaranteed to be above configMAX_SYSCALL_INTERRUPT_PRIORITY,
  651. * and therefore also guaranteed to be invalid.
  652. *
  653. * FreeRTOS maintains separate thread and ISR API functions to ensure
  654. * interrupt entry is as fast and simple as possible.
  655. *
  656. * The following links provide detailed information:
  657. * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html
  658. * https://www.FreeRTOS.org/FAQHelp.html */
  659. configASSERT( ucCurrentPriority >= ucMaxSysCallPriority );
  660. }
  661. /* Priority grouping: The interrupt controller (NVIC) allows the bits
  662. * that define each interrupt's priority to be split between bits that
  663. * define the interrupt's pre-emption priority bits and bits that define
  664. * the interrupt's sub-priority. For simplicity all bits must be defined
  665. * to be pre-emption priority bits. The following assertion will fail if
  666. * this is not the case (if some bits represent a sub-priority).
  667. *
  668. * If the application only uses CMSIS libraries for interrupt
  669. * configuration then the correct setting can be achieved on all Cortex-M
  670. * devices by calling NVIC_SetPriorityGrouping( 0 ); before starting the
  671. * scheduler. Note however that some vendor specific peripheral libraries
  672. * assume a non-zero priority group setting, in which cases using a value
  673. * of zero will result in unpredictable behaviour. */
  674. configASSERT( ( portAIRCR_REG & portPRIORITY_GROUP_MASK ) <= ulMaxPRIGROUPValue );
  675. }
  676. #endif /* configASSERT_DEFINED */