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