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