port.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /*
  2. * FreeRTOS Kernel <DEVELOPMENT BRANCH>
  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 SH2A port.
  30. *----------------------------------------------------------*/
  31. /* Standard C includes. */
  32. #include "limits.h"
  33. /* Scheduler includes. */
  34. #include "FreeRTOS.h"
  35. #include "task.h"
  36. /* Library includes. */
  37. #include "string.h"
  38. /* Hardware specifics. */
  39. #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
  40. #include "platform.h"
  41. #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
  42. #include "iodefine.h"
  43. #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
  44. /*-----------------------------------------------------------*/
  45. /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
  46. * PSW is set with U and I set, and PM and IPL clear. */
  47. #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
  48. /* The peripheral clock is divided by this value before being supplying the
  49. * CMT. */
  50. #if ( configUSE_TICKLESS_IDLE == 0 )
  51. /* If tickless idle is not used then the divisor can be fixed. */
  52. #define portCLOCK_DIVISOR 8UL
  53. #elif ( configPERIPHERAL_CLOCK_HZ >= 12000000 )
  54. #define portCLOCK_DIVISOR 512UL
  55. #elif ( configPERIPHERAL_CLOCK_HZ >= 6000000 )
  56. #define portCLOCK_DIVISOR 128UL
  57. #elif ( configPERIPHERAL_CLOCK_HZ >= 1000000 )
  58. #define portCLOCK_DIVISOR 32UL
  59. #else
  60. #define portCLOCK_DIVISOR 8UL
  61. #endif
  62. /* These macros allow a critical section to be added around the call to
  63. * xTaskIncrementTick(), which is only ever called from interrupts at the kernel
  64. * priority - ie a known priority. Therefore these local macros are a slight
  65. * optimisation compared to calling the global SET/CLEAR_INTERRUPT_MASK macros,
  66. * which would require the old IPL to be read first and stored in a local variable. */
  67. #define portDISABLE_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) )
  68. #define portENABLE_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i" ( configKERNEL_INTERRUPT_PRIORITY ) )
  69. /* Keys required to lock and unlock access to certain system registers
  70. * respectively. */
  71. #define portUNLOCK_KEY 0xA50B
  72. #define portLOCK_KEY 0xA500
  73. /*-----------------------------------------------------------*/
  74. /*
  75. * Function to start the first task executing - written in asm code as direct
  76. * access to registers is required.
  77. */
  78. static void prvStartFirstTask( void ) __attribute__( ( naked ) );
  79. /*
  80. * Software interrupt handler. Performs the actual context switch (saving and
  81. * restoring of registers). Written in asm code as direct register access is
  82. * required.
  83. */
  84. #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
  85. R_BSP_PRAGMA_INTERRUPT( vPortSoftwareInterruptISR, VECT( ICU, SWINT ) )
  86. R_BSP_ATTRIB_INTERRUPT void vPortSoftwareInterruptISR( void ) __attribute__( ( naked ) );
  87. #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
  88. void vPortSoftwareInterruptISR( void ) __attribute__( ( naked ) );
  89. #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
  90. /*
  91. * The tick ISR handler. The peripheral used is configured by the application
  92. * via a hook/callback function.
  93. */
  94. #if ( configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H == 1 )
  95. R_BSP_PRAGMA_INTERRUPT( vPortTickISR, _VECT( configTICK_VECTOR ) )
  96. R_BSP_ATTRIB_INTERRUPT void vPortTickISR( void ); /* Do not add __attribute__( ( interrupt ) ). */
  97. #else /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
  98. void vPortTickISR( void ) __attribute__( ( interrupt ) );
  99. #endif /* configINCLUDE_PLATFORM_H_INSTEAD_OF_IODEFINE_H */
  100. /*
  101. * Sets up the periodic ISR used for the RTOS tick using the CMT.
  102. * The application writer can define configSETUP_TICK_INTERRUPT() (in
  103. * FreeRTOSConfig.h) such that their own tick interrupt configuration is used
  104. * in place of prvSetupTimerInterrupt().
  105. */
  106. static void prvSetupTimerInterrupt( void );
  107. #ifndef configSETUP_TICK_INTERRUPT
  108. /* The user has not provided their own tick interrupt configuration so use
  109. * the definition in this file (which uses the interval timer). */
  110. #define configSETUP_TICK_INTERRUPT() prvSetupTimerInterrupt()
  111. #endif /* configSETUP_TICK_INTERRUPT */
  112. /*
  113. * Called after the sleep mode registers have been configured, prvSleep()
  114. * executes the pre and post sleep macros, and actually calls the wait
  115. * instruction.
  116. */
  117. #if configUSE_TICKLESS_IDLE == 1
  118. static void prvSleep( TickType_t xExpectedIdleTime );
  119. #endif /* configUSE_TICKLESS_IDLE */
  120. /*-----------------------------------------------------------*/
  121. /* Used in the context save and restore code. */
  122. extern void * pxCurrentTCB;
  123. /* Calculate how many clock increments make up a single tick period. */
  124. static const uint32_t ulMatchValueForOneTick = ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );
  125. #if configUSE_TICKLESS_IDLE == 1
  126. /* Holds the maximum number of ticks that can be suppressed - which is
  127. * basically how far into the future an interrupt can be generated. Set
  128. * during initialisation. This is the maximum possible value that the
  129. * compare match register can hold divided by ulMatchValueForOneTick. */
  130. static const TickType_t xMaximumPossibleSuppressedTicks = USHRT_MAX / ( ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) / configTICK_RATE_HZ );
  131. /* Flag set from the tick interrupt to allow the sleep processing to know if
  132. * sleep mode was exited because of a tick interrupt, or an interrupt
  133. * generated by something else. */
  134. static volatile uint32_t ulTickFlag = pdFALSE;
  135. /* The CMT counter is stopped temporarily each time it is re-programmed.
  136. * The following constant offsets the CMT counter match value by the number of
  137. * CMT counts that would typically be missed while the counter was stopped to
  138. * compensate for the lost time. The large difference between the divided CMT
  139. * clock and the CPU clock means it is likely ulStoppedTimerCompensation will
  140. * equal zero - and be optimised away. */
  141. static const uint32_t ulStoppedTimerCompensation = 100UL / ( configCPU_CLOCK_HZ / ( configPERIPHERAL_CLOCK_HZ / portCLOCK_DIVISOR ) );
  142. #endif /* if configUSE_TICKLESS_IDLE == 1 */
  143. /*-----------------------------------------------------------*/
  144. /*
  145. * See header file for description.
  146. */
  147. StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
  148. TaskFunction_t pxCode,
  149. void * pvParameters )
  150. {
  151. /* Offset to end up on 8 byte boundary. */
  152. pxTopOfStack--;
  153. /* R0 is not included as it is the stack pointer. */
  154. *pxTopOfStack = 0x00;
  155. pxTopOfStack--;
  156. *pxTopOfStack = 0x00;
  157. pxTopOfStack--;
  158. *pxTopOfStack = portINITIAL_PSW;
  159. pxTopOfStack--;
  160. *pxTopOfStack = ( StackType_t ) pxCode;
  161. /* When debugging it can be useful if every register is set to a known
  162. * value. Otherwise code space can be saved by just setting the registers
  163. * that need to be set. */
  164. #ifdef USE_FULL_REGISTER_INITIALISATION
  165. {
  166. pxTopOfStack--;
  167. *pxTopOfStack = 0x12345678; /* r15. */
  168. pxTopOfStack--;
  169. *pxTopOfStack = 0xaaaabbbb;
  170. pxTopOfStack--;
  171. *pxTopOfStack = 0xdddddddd;
  172. pxTopOfStack--;
  173. *pxTopOfStack = 0xcccccccc;
  174. pxTopOfStack--;
  175. *pxTopOfStack = 0xbbbbbbbb;
  176. pxTopOfStack--;
  177. *pxTopOfStack = 0xaaaaaaaa;
  178. pxTopOfStack--;
  179. *pxTopOfStack = 0x99999999;
  180. pxTopOfStack--;
  181. *pxTopOfStack = 0x88888888;
  182. pxTopOfStack--;
  183. *pxTopOfStack = 0x77777777;
  184. pxTopOfStack--;
  185. *pxTopOfStack = 0x66666666;
  186. pxTopOfStack--;
  187. *pxTopOfStack = 0x55555555;
  188. pxTopOfStack--;
  189. *pxTopOfStack = 0x44444444;
  190. pxTopOfStack--;
  191. *pxTopOfStack = 0x33333333;
  192. pxTopOfStack--;
  193. *pxTopOfStack = 0x22222222;
  194. pxTopOfStack--;
  195. }
  196. #else /* ifdef USE_FULL_REGISTER_INITIALISATION */
  197. {
  198. /* Leave space for the registers that will get popped from the stack
  199. * when the task first starts executing. */
  200. pxTopOfStack -= 15;
  201. }
  202. #endif /* ifdef USE_FULL_REGISTER_INITIALISATION */
  203. *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
  204. pxTopOfStack--;
  205. *pxTopOfStack = 0x12345678; /* Accumulator. */
  206. pxTopOfStack--;
  207. *pxTopOfStack = 0x87654321; /* Accumulator. */
  208. return pxTopOfStack;
  209. }
  210. /*-----------------------------------------------------------*/
  211. BaseType_t xPortStartScheduler( void )
  212. {
  213. /* Use pxCurrentTCB just so it does not get optimised away. */
  214. if( pxCurrentTCB != NULL )
  215. {
  216. /* Call an application function to set up the timer that will generate
  217. * the tick interrupt. This way the application can decide which
  218. * peripheral to use. If tickless mode is used then the default
  219. * implementation defined in this file (which uses CMT0) should not be
  220. * overridden. */
  221. configSETUP_TICK_INTERRUPT();
  222. /* Enable the software interrupt. */
  223. _IEN( _ICU_SWINT ) = 1;
  224. /* Ensure the software interrupt is clear. */
  225. _IR( _ICU_SWINT ) = 0;
  226. /* Ensure the software interrupt is set to the kernel priority. */
  227. _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
  228. /* Start the first task. */
  229. prvStartFirstTask();
  230. }
  231. /* Execution should not reach here as the tasks are now running!
  232. * prvSetupTimerInterrupt() is called here to prevent the compiler outputting
  233. * a warning about a statically declared function not being referenced in the
  234. * case that the application writer has provided their own tick interrupt
  235. * configuration routine (and defined configSETUP_TICK_INTERRUPT() such that
  236. * their own routine will be called in place of prvSetupTimerInterrupt()). */
  237. prvSetupTimerInterrupt();
  238. /* Should not get here. */
  239. return pdFAIL;
  240. }
  241. /*-----------------------------------------------------------*/
  242. void vPortEndScheduler( void )
  243. {
  244. /* Not implemented in ports where there is nothing to return to.
  245. * Artificially force an assert. */
  246. configASSERT( pxCurrentTCB == NULL );
  247. }
  248. /*-----------------------------------------------------------*/
  249. static void prvStartFirstTask( void )
  250. {
  251. __asm volatile
  252. (
  253. /* When starting the scheduler there is nothing that needs moving to the
  254. * interrupt stack because the function is not called from an interrupt.
  255. * Just ensure the current stack is the user stack. */
  256. "SETPSW U \n" \
  257. /* Obtain the location of the stack associated with which ever task
  258. * pxCurrentTCB is currently pointing to. */
  259. "MOV.L #_pxCurrentTCB, R15 \n" \
  260. "MOV.L [R15], R15 \n" \
  261. "MOV.L [R15], R0 \n" \
  262. /* Restore the registers from the stack of the task pointed to by
  263. * pxCurrentTCB. */
  264. "POP R15 \n" \
  265. /* Accumulator low 32 bits. */
  266. "MVTACLO R15 \n" \
  267. "POP R15 \n" \
  268. /* Accumulator high 32 bits. */
  269. "MVTACHI R15 \n" \
  270. /* R1 to R15 - R0 is not included as it is the SP. */
  271. "POPM R1-R15 \n" \
  272. /* This pops the remaining registers. */
  273. "RTE \n" \
  274. "NOP \n" \
  275. "NOP \n"
  276. );
  277. }
  278. /*-----------------------------------------------------------*/
  279. void vPortSoftwareInterruptISR( void )
  280. {
  281. __asm volatile
  282. (
  283. /* Re-enable interrupts. */
  284. "SETPSW I \n" \
  285. /* Move the data that was automatically pushed onto the interrupt stack when
  286. * the interrupt occurred from the interrupt stack to the user stack.
  287. *
  288. * R15 is saved before it is clobbered. */
  289. "PUSH.L R15 \n" \
  290. /* Read the user stack pointer. */
  291. "MVFC USP, R15 \n" \
  292. /* Move the address down to the data being moved. */
  293. "SUB #12, R15 \n" \
  294. "MVTC R15, USP \n" \
  295. /* Copy the data across, R15, then PC, then PSW. */
  296. "MOV.L [ R0 ], [ R15 ] \n" \
  297. "MOV.L 4[ R0 ], 4[ R15 ] \n" \
  298. "MOV.L 8[ R0 ], 8[ R15 ] \n" \
  299. /* Move the interrupt stack pointer to its new correct position. */
  300. "ADD #12, R0 \n" \
  301. /* All the rest of the registers are saved directly to the user stack. */
  302. "SETPSW U \n" \
  303. /* Save the rest of the general registers (R15 has been saved already). */
  304. "PUSHM R1-R14 \n" \
  305. /* Save the accumulator. */
  306. "MVFACHI R15 \n" \
  307. "PUSH.L R15 \n" \
  308. /* Middle word. */
  309. "MVFACMI R15 \n" \
  310. /* Shifted left as it is restored to the low order word. */
  311. "SHLL #16, R15 \n" \
  312. "PUSH.L R15 \n" \
  313. /* Save the stack pointer to the TCB. */
  314. "MOV.L #_pxCurrentTCB, R15 \n" \
  315. "MOV.L [ R15 ], R15 \n" \
  316. "MOV.L R0, [ R15 ] \n" \
  317. /* Ensure the interrupt mask is set to the syscall priority while the kernel
  318. * structures are being accessed. */
  319. "MVTIPL %0 \n" \
  320. /* Select the next task to run. */
  321. "BSR.A _vTaskSwitchContext \n" \
  322. /* Reset the interrupt mask as no more data structure access is required. */
  323. "MVTIPL %1 \n" \
  324. /* Load the stack pointer of the task that is now selected as the Running
  325. * state task from its TCB. */
  326. "MOV.L #_pxCurrentTCB,R15 \n" \
  327. "MOV.L [ R15 ], R15 \n" \
  328. "MOV.L [ R15 ], R0 \n" \
  329. /* Restore the context of the new task. The PSW (Program Status Word) and
  330. * PC will be popped by the RTE instruction. */
  331. "POP R15 \n" \
  332. "MVTACLO R15 \n" \
  333. "POP R15 \n" \
  334. "MVTACHI R15 \n" \
  335. "POPM R1-R15 \n" \
  336. "RTE \n" \
  337. "NOP \n" \
  338. "NOP "
  339. ::"i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ), "i" ( configKERNEL_INTERRUPT_PRIORITY )
  340. );
  341. }
  342. /*-----------------------------------------------------------*/
  343. void vPortTickISR( void )
  344. {
  345. /* Re-enabled interrupts. */
  346. __asm volatile ( "SETPSW I" );
  347. /* Increment the tick, and perform any processing the new tick value
  348. * necessitates. Ensure IPL is at the max syscall value first. */
  349. portDISABLE_INTERRUPTS_FROM_KERNEL_ISR();
  350. {
  351. if( xTaskIncrementTick() != pdFALSE )
  352. {
  353. taskYIELD();
  354. }
  355. }
  356. portENABLE_INTERRUPTS_FROM_KERNEL_ISR();
  357. #if configUSE_TICKLESS_IDLE == 1
  358. {
  359. /* The CPU woke because of a tick. */
  360. ulTickFlag = pdTRUE;
  361. /* If this is the first tick since exiting tickless mode then the CMT
  362. * compare match value needs resetting. */
  363. CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;
  364. }
  365. #endif
  366. }
  367. /*-----------------------------------------------------------*/
  368. uint32_t ulPortGetIPL( void )
  369. {
  370. __asm volatile
  371. (
  372. "MVFC PSW, R1 \n" \
  373. "SHLR #24, R1 \n" \
  374. "RTS "
  375. );
  376. /* This will never get executed, but keeps the compiler from complaining. */
  377. return 0;
  378. }
  379. /*-----------------------------------------------------------*/
  380. void vPortSetIPL( uint32_t ulNewIPL )
  381. {
  382. __asm volatile
  383. (
  384. "PUSH R5 \n" \
  385. "MVFC PSW, R5 \n" \
  386. "SHLL #24, R1 \n" \
  387. "AND #-0F000001H, R5 \n" \
  388. "OR R1, R5 \n" \
  389. "MVTC R5, PSW \n" \
  390. "POP R5 \n" \
  391. "RTS "
  392. );
  393. }
  394. /*-----------------------------------------------------------*/
  395. static void prvSetupTimerInterrupt( void )
  396. {
  397. /* Unlock. */
  398. SYSTEM.PRCR.WORD = portUNLOCK_KEY;
  399. /* Enable CMT0. */
  400. MSTP( CMT0 ) = 0;
  401. /* Lock again. */
  402. SYSTEM.PRCR.WORD = portLOCK_KEY;
  403. /* Interrupt on compare match. */
  404. CMT0.CMCR.BIT.CMIE = 1;
  405. /* Set the compare match value. */
  406. CMT0.CMCOR = ( uint16_t ) ulMatchValueForOneTick;
  407. /* Divide the PCLK. */
  408. #if portCLOCK_DIVISOR == 512
  409. {
  410. CMT0.CMCR.BIT.CKS = 3;
  411. }
  412. #elif portCLOCK_DIVISOR == 128
  413. {
  414. CMT0.CMCR.BIT.CKS = 2;
  415. }
  416. #elif portCLOCK_DIVISOR == 32
  417. {
  418. CMT0.CMCR.BIT.CKS = 1;
  419. }
  420. #elif portCLOCK_DIVISOR == 8
  421. {
  422. CMT0.CMCR.BIT.CKS = 0;
  423. }
  424. #else /* if portCLOCK_DIVISOR == 512 */
  425. {
  426. #error Invalid portCLOCK_DIVISOR setting
  427. }
  428. #endif /* if portCLOCK_DIVISOR == 512 */
  429. /* Enable the interrupt... */
  430. _IEN( _CMT0_CMI0 ) = 1;
  431. /* ...and set its priority to the application defined kernel priority. */
  432. _IPR( _CMT0_CMI0 ) = configKERNEL_INTERRUPT_PRIORITY;
  433. /* Start the timer. */
  434. CMT.CMSTR0.BIT.STR0 = 1;
  435. }
  436. /*-----------------------------------------------------------*/
  437. #if configUSE_TICKLESS_IDLE == 1
  438. static void prvSleep( TickType_t xExpectedIdleTime )
  439. {
  440. /* Allow the application to define some pre-sleep processing. */
  441. configPRE_SLEEP_PROCESSING( xExpectedIdleTime );
  442. /* xExpectedIdleTime being set to 0 by configPRE_SLEEP_PROCESSING()
  443. * means the application defined code has already executed the WAIT
  444. * instruction. */
  445. if( xExpectedIdleTime > 0 )
  446. {
  447. __asm volatile ( "WAIT" );
  448. }
  449. /* Allow the application to define some post sleep processing. */
  450. configPOST_SLEEP_PROCESSING( xExpectedIdleTime );
  451. }
  452. #endif /* configUSE_TICKLESS_IDLE */
  453. /*-----------------------------------------------------------*/
  454. #if configUSE_TICKLESS_IDLE == 1
  455. void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime )
  456. {
  457. uint32_t ulMatchValue, ulCompleteTickPeriods, ulCurrentCount;
  458. eSleepModeStatus eSleepAction;
  459. /* THIS FUNCTION IS CALLED WITH THE SCHEDULER SUSPENDED. */
  460. /* Make sure the CMT reload value does not overflow the counter. */
  461. if( xExpectedIdleTime > xMaximumPossibleSuppressedTicks )
  462. {
  463. xExpectedIdleTime = xMaximumPossibleSuppressedTicks;
  464. }
  465. /* Calculate the reload value required to wait xExpectedIdleTime tick
  466. * periods. */
  467. ulMatchValue = ulMatchValueForOneTick * xExpectedIdleTime;
  468. if( ulMatchValue > ulStoppedTimerCompensation )
  469. {
  470. /* Compensate for the fact that the CMT is going to be stopped
  471. * momentarily. */
  472. ulMatchValue -= ulStoppedTimerCompensation;
  473. }
  474. /* Stop the CMT momentarily. The time the CMT is stopped for is
  475. * accounted for as best it can be, but using the tickless mode will
  476. * inevitably result in some tiny drift of the time maintained by the
  477. * kernel with respect to calendar time. */
  478. CMT.CMSTR0.BIT.STR0 = 0;
  479. while( CMT.CMSTR0.BIT.STR0 == 1 )
  480. {
  481. /* Nothing to do here. */
  482. }
  483. /* Critical section using the global interrupt bit as the i bit is
  484. * automatically reset by the WAIT instruction. */
  485. __asm volatile ( "CLRPSW i" );
  486. /* The tick flag is set to false before sleeping. If it is true when
  487. * sleep mode is exited then sleep mode was probably exited because the
  488. * tick was suppressed for the entire xExpectedIdleTime period. */
  489. ulTickFlag = pdFALSE;
  490. /* If a context switch is pending then abandon the low power entry as
  491. * the context switch might have been pended by an external interrupt that
  492. * requires processing. */
  493. eSleepAction = eTaskConfirmSleepModeStatus();
  494. if( eSleepAction == eAbortSleep )
  495. {
  496. /* Restart tick. */
  497. CMT.CMSTR0.BIT.STR0 = 1;
  498. __asm volatile ( "SETPSW i" );
  499. }
  500. else if( eSleepAction == eNoTasksWaitingTimeout )
  501. {
  502. /* Protection off. */
  503. SYSTEM.PRCR.WORD = portUNLOCK_KEY;
  504. /* Ready for software standby with all clocks stopped. */
  505. SYSTEM.SBYCR.BIT.SSBY = 1;
  506. /* Protection on. */
  507. SYSTEM.PRCR.WORD = portLOCK_KEY;
  508. /* Sleep until something happens. Calling prvSleep() will
  509. * automatically reset the i bit in the PSW. */
  510. prvSleep( xExpectedIdleTime );
  511. /* Restart the CMT. */
  512. CMT.CMSTR0.BIT.STR0 = 1;
  513. }
  514. else
  515. {
  516. /* Protection off. */
  517. SYSTEM.PRCR.WORD = portUNLOCK_KEY;
  518. /* Ready for deep sleep mode. */
  519. SYSTEM.MSTPCRC.BIT.DSLPE = 1;
  520. SYSTEM.MSTPCRA.BIT.MSTPA28 = 1;
  521. SYSTEM.SBYCR.BIT.SSBY = 0;
  522. /* Protection on. */
  523. SYSTEM.PRCR.WORD = portLOCK_KEY;
  524. /* Adjust the match value to take into account that the current
  525. * time slice is already partially complete. */
  526. ulMatchValue -= ( uint32_t ) CMT0.CMCNT;
  527. CMT0.CMCOR = ( uint16_t ) ulMatchValue;
  528. /* Restart the CMT to count up to the new match value. */
  529. CMT0.CMCNT = 0;
  530. CMT.CMSTR0.BIT.STR0 = 1;
  531. /* Sleep until something happens. Calling prvSleep() will
  532. * automatically reset the i bit in the PSW. */
  533. prvSleep( xExpectedIdleTime );
  534. /* Stop CMT. Again, the time the SysTick is stopped for is
  535. * accounted for as best it can be, but using the tickless mode will
  536. * inevitably result in some tiny drift of the time maintained by the
  537. * kernel with respect to calendar time. */
  538. CMT.CMSTR0.BIT.STR0 = 0;
  539. while( CMT.CMSTR0.BIT.STR0 == 1 )
  540. {
  541. /* Nothing to do here. */
  542. }
  543. ulCurrentCount = ( uint32_t ) CMT0.CMCNT;
  544. if( ulTickFlag != pdFALSE )
  545. {
  546. /* The tick interrupt has already executed, although because
  547. * this function is called with the scheduler suspended the actual
  548. * tick processing will not occur until after this function has
  549. * exited. Reset the match value with whatever remains of this
  550. * tick period. */
  551. ulMatchValue = ulMatchValueForOneTick - ulCurrentCount;
  552. CMT0.CMCOR = ( uint16_t ) ulMatchValue;
  553. /* The tick interrupt handler will already have pended the tick
  554. * processing in the kernel. As the pending tick will be
  555. * processed as soon as this function exits, the tick value
  556. * maintained by the tick is stepped forward by one less than the
  557. * time spent sleeping. The actual stepping of the tick appears
  558. * later in this function. */
  559. ulCompleteTickPeriods = xExpectedIdleTime - 1UL;
  560. }
  561. else
  562. {
  563. /* Something other than the tick interrupt ended the sleep.
  564. * How many complete tick periods passed while the processor was
  565. * sleeping? */
  566. ulCompleteTickPeriods = ulCurrentCount / ulMatchValueForOneTick;
  567. /* The match value is set to whatever fraction of a single tick
  568. * period remains. */
  569. ulMatchValue = ulCurrentCount - ( ulCompleteTickPeriods * ulMatchValueForOneTick );
  570. CMT0.CMCOR = ( uint16_t ) ulMatchValue;
  571. }
  572. /* Restart the CMT so it runs up to the match value. The match value
  573. * will get set to the value required to generate exactly one tick period
  574. * the next time the CMT interrupt executes. */
  575. CMT0.CMCNT = 0;
  576. CMT.CMSTR0.BIT.STR0 = 1;
  577. /* Wind the tick forward by the number of tick periods that the CPU
  578. * remained in a low power state. */
  579. vTaskStepTick( ulCompleteTickPeriods );
  580. }
  581. }
  582. #endif /* configUSE_TICKLESS_IDLE */