port.c 20 KB

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