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