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