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