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