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