port.c 22 KB

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