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