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