]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/RX600v2/port.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Source / portable / GCC / RX600v2 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\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  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.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 /* Scheduler includes. */\r
33 #include "FreeRTOS.h"\r
34 #include "task.h"\r
35 \r
36 /* Library includes. */\r
37 #include "string.h"\r
38 \r
39 /* Hardware specifics. */\r
40 #include "iodefine.h"\r
41 \r
42 /*-----------------------------------------------------------*/\r
43 \r
44 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore\r
45 PSW is set with U and I set, and PM and IPL clear. */\r
46 #define portINITIAL_PSW     ( ( StackType_t ) 0x00030000 )\r
47 #define portINITIAL_FPSW    ( ( StackType_t ) 0x00000100 )\r
48 \r
49 /* These macros allow a critical section to be added around the call to\r
50 xTaskIncrementTick(), which is only ever called from interrupts at the kernel\r
51 priority - ie a known priority.  Therefore these local macros are a slight\r
52 optimisation compared to calling the global SET/CLEAR_INTERRUPT_MASK macros,\r
53 which would require the old IPL to be read first and stored in a local variable. */\r
54 #define portMASK_INTERRUPTS_FROM_KERNEL_ISR()   __asm volatile ( "MVTIPL        %0" ::"i"(configMAX_SYSCALL_INTERRUPT_PRIORITY) )\r
55 #define portUNMASK_INTERRUPTS_FROM_KERNEL_ISR()         __asm volatile ( "MVTIPL        %0" ::"i"(configKERNEL_INTERRUPT_PRIORITY) )\r
56 \r
57 /*-----------------------------------------------------------*/\r
58 \r
59 /*\r
60  * Function to start the first task executing - written in asm code as direct\r
61  * access to registers is required.\r
62  */\r
63 static void prvStartFirstTask( void ) __attribute__((naked));\r
64 \r
65 /*\r
66  * Software interrupt handler.  Performs the actual context switch (saving and\r
67  * restoring of registers).  Written in asm code as direct register access is\r
68  * required.\r
69  */\r
70 void vSoftwareInterruptISR( void ) __attribute__((naked));\r
71 \r
72 /*\r
73  * The tick interrupt handler.\r
74  */\r
75 void vTickISR( void ) __attribute__((interrupt));\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 extern void *pxCurrentTCB;\r
80 \r
81 /*-----------------------------------------------------------*/\r
82 \r
83 /*\r
84  * See header file for description.\r
85  */\r
86 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
87 {\r
88         /* R0 is not included as it is the stack pointer. */\r
89 \r
90         *pxTopOfStack = 0x00;\r
91         pxTopOfStack--;\r
92         *pxTopOfStack = portINITIAL_PSW;\r
93         pxTopOfStack--;\r
94         *pxTopOfStack = ( StackType_t ) pxCode;\r
95 \r
96         /* When debugging it can be useful if every register is set to a known\r
97         value.  Otherwise code space can be saved by just setting the registers\r
98         that need to be set. */\r
99         #ifdef USE_FULL_REGISTER_INITIALISATION\r
100         {\r
101                 pxTopOfStack--;\r
102                 *pxTopOfStack = 0xffffffff;     /* r15. */\r
103                 pxTopOfStack--;\r
104                 *pxTopOfStack = 0xeeeeeeee;\r
105                 pxTopOfStack--;\r
106                 *pxTopOfStack = 0xdddddddd;\r
107                 pxTopOfStack--;\r
108                 *pxTopOfStack = 0xcccccccc;\r
109                 pxTopOfStack--;\r
110                 *pxTopOfStack = 0xbbbbbbbb;\r
111                 pxTopOfStack--;\r
112                 *pxTopOfStack = 0xaaaaaaaa;\r
113                 pxTopOfStack--;\r
114                 *pxTopOfStack = 0x99999999;\r
115                 pxTopOfStack--;\r
116                 *pxTopOfStack = 0x88888888;\r
117                 pxTopOfStack--;\r
118                 *pxTopOfStack = 0x77777777;\r
119                 pxTopOfStack--;\r
120                 *pxTopOfStack = 0x66666666;\r
121                 pxTopOfStack--;\r
122                 *pxTopOfStack = 0x55555555;\r
123                 pxTopOfStack--;\r
124                 *pxTopOfStack = 0x44444444;\r
125                 pxTopOfStack--;\r
126                 *pxTopOfStack = 0x33333333;\r
127                 pxTopOfStack--;\r
128                 *pxTopOfStack = 0x22222222;\r
129                 pxTopOfStack--;\r
130         }\r
131         #else\r
132         {\r
133                 pxTopOfStack -= 15;\r
134         }\r
135         #endif\r
136 \r
137         *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */\r
138         pxTopOfStack--;\r
139         *pxTopOfStack = portINITIAL_FPSW;\r
140         pxTopOfStack--;\r
141         *pxTopOfStack = 0x11111111; /* Accumulator 0. */\r
142         pxTopOfStack--;\r
143         *pxTopOfStack = 0x22222222; /* Accumulator 0. */\r
144         pxTopOfStack--;\r
145         *pxTopOfStack = 0x33333333; /* Accumulator 0. */\r
146         pxTopOfStack--;\r
147         *pxTopOfStack = 0x44444444; /* Accumulator 1. */\r
148         pxTopOfStack--;\r
149         *pxTopOfStack = 0x55555555; /* Accumulator 1. */\r
150         pxTopOfStack--;\r
151         *pxTopOfStack = 0x66666666; /* Accumulator 1. */\r
152 \r
153         return pxTopOfStack;\r
154 }\r
155 /*-----------------------------------------------------------*/\r
156 \r
157 BaseType_t xPortStartScheduler( void )\r
158 {\r
159 extern void vApplicationSetupTimerInterrupt( void );\r
160 \r
161         /* Use pxCurrentTCB just so it does not get optimised away. */\r
162         if( pxCurrentTCB != NULL )\r
163         {\r
164                 /* Call an application function to set up the timer that will generate the\r
165                 tick interrupt.  This way the application can decide which peripheral to\r
166                 use.  A demo application is provided to show a suitable example. */\r
167                 vApplicationSetupTimerInterrupt();\r
168 \r
169                 /* Enable the software interrupt. */\r
170                 _IEN( _ICU_SWINT ) = 1;\r
171 \r
172                 /* Ensure the software interrupt is clear. */\r
173                 _IR( _ICU_SWINT ) = 0;\r
174 \r
175                 /* Ensure the software interrupt is set to the kernel priority. */\r
176                 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;\r
177 \r
178                 /* Start the first task. */\r
179                 prvStartFirstTask();\r
180         }\r
181 \r
182         /* Should not get here. */\r
183         return pdFAIL;\r
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 void vPortEndScheduler( void )\r
188 {\r
189         /* Not implemented in ports where there is nothing to return to.\r
190         Artificially force an assert. */\r
191         configASSERT( pxCurrentTCB == NULL );\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static void prvStartFirstTask( void )\r
196 {\r
197         __asm volatile\r
198         (\r
199                 /* When starting the scheduler there is nothing that needs moving to the\r
200                 interrupt stack because the function is not called from an interrupt.\r
201                 Just ensure the current stack is the user stack. */\r
202                 "SETPSW         U                                               \n" \\r
203 \r
204                 /* Obtain the location of the stack associated with which ever task\r
205                 pxCurrentTCB is currently pointing to. */\r
206                 "MOV.L          #_pxCurrentTCB, R15             \n" \\r
207                 "MOV.L          [R15], R15                              \n" \\r
208                 "MOV.L          [R15], R0                               \n" \\r
209 \r
210                 /* Restore the registers from the stack of the task pointed to by\r
211                 pxCurrentTCB. */\r
212             "POP                R15                                             \n" \\r
213 \r
214                 /* Accumulator low 32 bits. */\r
215             "MVTACLO    R15, A0                                 \n" \\r
216             "POP                R15                                             \n" \\r
217 \r
218             /* Accumulator high 32 bits. */\r
219             "MVTACHI    R15, A0                                 \n" \\r
220             "POP                R15                                             \n" \\r
221 \r
222             /* Accumulator guard. */\r
223             "MVTACGU    R15, A0                                 \n" \\r
224             "POP                R15                                             \n" \\r
225 \r
226             /* Accumulator low 32 bits. */\r
227             "MVTACLO    R15, A1                                 \n" \\r
228             "POP                R15                                             \n" \\r
229 \r
230             /* Accumulator high 32 bits. */\r
231             "MVTACHI    R15, A1                                 \n" \\r
232             "POP                R15                                             \n" \\r
233 \r
234             /* Accumulator guard. */\r
235             "MVTACGU    R15, A1                                 \n" \\r
236             "POP                R15                                             \n" \\r
237 \r
238                 /* Floating point status word. */\r
239             "MVTC               R15, FPSW                               \n" \\r
240 \r
241                 /* R1 to R15 - R0 is not included as it is the SP. */\r
242             "POPM               R1-R15                                  \n" \\r
243 \r
244                 /* This pops the remaining registers. */\r
245             "RTE                                                                \n" \\r
246             "NOP                                                                \n" \\r
247             "NOP                                                                \n"\r
248         );\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 void vSoftwareInterruptISR( void )\r
253 {\r
254         __asm volatile\r
255         (\r
256                 /* Re-enable interrupts. */\r
257                 "SETPSW         I                                                       \n" \\r
258 \r
259                 /* Move the data that was automatically pushed onto the interrupt stack when\r
260                 the interrupt occurred from the interrupt stack to the user stack.\r
261 \r
262                 R15 is saved before it is clobbered. */\r
263                 "PUSH.L         R15                                                     \n" \\r
264 \r
265                 /* Read the user stack pointer. */\r
266                 "MVFC           USP, R15                                        \n" \\r
267 \r
268                 /* Move the address down to the data being moved. */\r
269                 "SUB            #12, R15                                        \n" \\r
270                 "MVTC           R15, USP                                        \n" \\r
271 \r
272                 /* Copy the data across, R15, then PC, then PSW. */\r
273                 "MOV.L          [ R0 ], [ R15 ]                         \n" \\r
274                 "MOV.L          4[ R0 ], 4[ R15 ]                       \n" \\r
275                 "MOV.L          8[ R0 ], 8[ R15 ]                       \n" \\r
276 \r
277                 /* Move the interrupt stack pointer to its new correct position. */\r
278                 "ADD            #12, R0                                         \n" \\r
279 \r
280                 /* All the rest of the registers are saved directly to the user stack. */\r
281                 "SETPSW         U                                                       \n" \\r
282 \r
283                 /* Save the rest of the general registers (R15 has been saved already). */\r
284                 "PUSHM          R1-R14                                          \n" \\r
285 \r
286                 /* Save the FPSW and accumulator. */\r
287                 "MVFC           FPSW, R15                                       \n" \\r
288                 "PUSH.L         R15                                                     \n" \\r
289                 "MVFACGU        #0, A1, R15                                     \n" \\r
290                 "PUSH.L         R15                                                     \n" \\r
291                 "MVFACHI        #0, A1, R15                                     \n" \\r
292                 "PUSH.L         R15                                                     \n" \\r
293                 /* Low order word. */\r
294                 "MVFACLO        #0, A1, R15                                     \n" \\r
295                 "PUSH.L         R15                                                     \n" \\r
296                 "MVFACGU        #0, A0, R15                                     \n" \\r
297                 "PUSH.L         R15                                                     \n" \\r
298                 "MVFACHI        #0, A0, R15                                     \n" \\r
299                 "PUSH.L         R15                                                     \n" \\r
300                 /* Low order word. */\r
301                 "MVFACLO        #0, A0, R15                                     \n" \\r
302                 "PUSH.L         R15                                                     \n" \\r
303 \r
304                 /* Save the stack pointer to the TCB. */\r
305                 "MOV.L          #_pxCurrentTCB, R15                     \n" \\r
306                 "MOV.L          [ R15 ], R15                            \n" \\r
307                 "MOV.L          R0, [ R15 ]                                     \n" \\r
308 \r
309                 /* Ensure the interrupt mask is set to the syscall priority while the kernel\r
310                 structures are being accessed. */\r
311                 "MVTIPL         %0                                                      \n" \\r
312 \r
313                 /* Select the next task to run. */\r
314                 "BSR.A          _vTaskSwitchContext                     \n" \\r
315 \r
316                 /* Reset the interrupt mask as no more data structure access is required. */\r
317                 "MVTIPL         %1                                                      \n" \\r
318 \r
319                 /* Load the stack pointer of the task that is now selected as the Running\r
320                 state task from its TCB. */\r
321                 "MOV.L          #_pxCurrentTCB,R15                      \n" \\r
322                 "MOV.L          [ R15 ], R15                            \n" \\r
323                 "MOV.L          [ R15 ], R0                                     \n" \\r
324 \r
325                 /* Restore the context of the new task.  The PSW (Program Status Word) and\r
326                 PC will be popped by the RTE instruction. */\r
327             "POP                R15                                                     \n" \\r
328 \r
329             /* Accumulator low 32 bits. */\r
330             "MVTACLO    R15, A0                                         \n" \\r
331             "POP                R15                                                     \n" \\r
332 \r
333             /* Accumulator high 32 bits. */\r
334             "MVTACHI    R15, A0                                         \n" \\r
335             "POP                R15                                                     \n" \\r
336 \r
337             /* Accumulator guard. */\r
338             "MVTACGU    R15, A0                                         \n" \\r
339             "POP                R15                                                     \n" \\r
340 \r
341             /* Accumulator low 32 bits. */\r
342             "MVTACLO    R15, A1                                         \n" \\r
343             "POP                R15                                                     \n" \\r
344 \r
345             /* Accumulator high 32 bits. */\r
346             "MVTACHI    R15, A1                                         \n" \\r
347             "POP                R15                                                     \n" \\r
348 \r
349             /* Accumulator guard. */\r
350             "MVTACGU    R15, A1                                         \n" \\r
351                 "POP            R15                                                     \n" \\r
352                 "MVTC           R15, FPSW                                       \n" \\r
353                 "POPM           R1-R15                                          \n" \\r
354                 "RTE                                                                    \n" \\r
355                 "NOP                                                                    \n" \\r
356                 "NOP                                                                      "\r
357                 :: "i"(configMAX_SYSCALL_INTERRUPT_PRIORITY), "i"(configKERNEL_INTERRUPT_PRIORITY)\r
358         );\r
359 }\r
360 /*-----------------------------------------------------------*/\r
361 \r
362 void vTickISR( void )\r
363 {\r
364         /* Re-enabled interrupts. */\r
365         __asm volatile( "SETPSW I" );\r
366 \r
367         /* Increment the tick, and perform any processing the new tick value\r
368         necessitates.  Ensure IPL is at the max syscall value first. */\r
369         portMASK_INTERRUPTS_FROM_KERNEL_ISR();\r
370         {\r
371                 if( xTaskIncrementTick() != pdFALSE )\r
372                 {\r
373                         taskYIELD();\r
374                 }\r
375         }\r
376         portUNMASK_INTERRUPTS_FROM_KERNEL_ISR();\r
377 }\r
378 /*-----------------------------------------------------------*/\r
379 \r
380 uint32_t ulPortGetIPL( void )\r
381 {\r
382         __asm volatile\r
383         (\r
384                 "MVFC   PSW, R1                 \n"     \\r
385                 "SHLR   #24, R1                 \n"     \\r
386                 "RTS                                      "\r
387         );\r
388 \r
389         /* This will never get executed, but keeps the compiler from complaining. */\r
390         return 0;\r
391 }\r
392 /*-----------------------------------------------------------*/\r
393 \r
394 void vPortSetIPL( uint32_t ulNewIPL )\r
395 {\r
396         __asm volatile\r
397         (\r
398                 "PUSH   R5                              \n" \\r
399                 "MVFC   PSW, R5                 \n"     \\r
400                 "SHLL   #24, R1                 \n" \\r
401                 "AND    #-0F000001H, R5 \n" \\r
402                 "OR             R1, R5                  \n" \\r
403                 "MVTC   R5, PSW                 \n" \\r
404                 "POP    R5                              \n" \\r
405                 "RTS                                      "\r
406          );\r
407 }\r