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