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