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