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