]> begriffs open source - cmsis-freertos/blob - Source/portable/Renesas/RX600v2/port.c
Update cmsis_os2.c
[cmsis-freertos] / Source / portable / Renesas / RX600v2 / port.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 /*-----------------------------------------------------------
71  * Implementation of functions defined in portable.h for the RX600 port.
72  *----------------------------------------------------------*/
73
74 /* Scheduler includes. */
75 #include "FreeRTOS.h"
76 #include "task.h"
77
78 /* Library includes. */
79 #include "string.h"
80
81 /* Hardware specifics. */
82 #include "iodefine.h"
83
84 /*-----------------------------------------------------------*/
85
86 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
87 PSW is set with U and I set, and PM and IPL clear. */
88 #define portINITIAL_PSW     ( ( StackType_t ) 0x00030000 )
89 #define portINITIAL_FPSW    ( ( StackType_t ) 0x00000100 )
90
91 /*-----------------------------------------------------------*/
92
93 /* The following lines are to ensure vSoftwareInterruptEntry can be referenced,
94  and therefore installed in the vector table, when the FreeRTOS code is built
95 as a library. */
96 extern BaseType_t vSoftwareInterruptEntry;
97 const BaseType_t * p_vSoftwareInterruptEntry = &vSoftwareInterruptEntry;
98
99 /*-----------------------------------------------------------*/
100
101 /*
102  * Function to start the first task executing - written in asm code as direct
103  * access to registers is required.
104  */
105 static void prvStartFirstTask( void );
106
107 /*
108  * Software interrupt handler.  Performs the actual context switch (saving and
109  * restoring of registers).  Written in asm code as direct register access is
110  * required.
111  */
112 static void prvYieldHandler( void );
113
114 /*
115  * The entry point for the software interrupt handler.  This is the function
116  * that calls the inline asm function prvYieldHandler().  It is installed in
117  * the vector table, but the code that installs it is in prvYieldHandler rather
118  * than using a #pragma.
119  */
120 void vSoftwareInterruptISR( void );
121
122 /*-----------------------------------------------------------*/
123
124 /* This is accessed by the inline assembler functions so is file scope for
125 convenience. */
126 extern void *pxCurrentTCB;
127 extern void vTaskSwitchContext( void );
128
129 /*-----------------------------------------------------------*/
130
131 /*
132  * See header file for description.
133  */
134 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
135 {
136         /* R0 is not included as it is the stack pointer. */
137
138         *pxTopOfStack = 0x00;
139         pxTopOfStack--;
140         *pxTopOfStack = portINITIAL_PSW;
141         pxTopOfStack--;
142         *pxTopOfStack = ( StackType_t ) pxCode;
143
144         /* When debugging it can be useful if every register is set to a known
145         value.  Otherwise code space can be saved by just setting the registers
146         that need to be set. */
147         #ifdef USE_FULL_REGISTER_INITIALISATION
148         {
149                 pxTopOfStack--;
150                 *pxTopOfStack = 0xffffffff;     /* r15. */
151                 pxTopOfStack--;
152                 *pxTopOfStack = 0xeeeeeeee;
153                 pxTopOfStack--;
154                 *pxTopOfStack = 0xdddddddd;
155                 pxTopOfStack--;
156                 *pxTopOfStack = 0xcccccccc;
157                 pxTopOfStack--;
158                 *pxTopOfStack = 0xbbbbbbbb;
159                 pxTopOfStack--;
160                 *pxTopOfStack = 0xaaaaaaaa;
161                 pxTopOfStack--;
162                 *pxTopOfStack = 0x99999999;
163                 pxTopOfStack--;
164                 *pxTopOfStack = 0x88888888;
165                 pxTopOfStack--;
166                 *pxTopOfStack = 0x77777777;
167                 pxTopOfStack--;
168                 *pxTopOfStack = 0x66666666;
169                 pxTopOfStack--;
170                 *pxTopOfStack = 0x55555555;
171                 pxTopOfStack--;
172                 *pxTopOfStack = 0x44444444;
173                 pxTopOfStack--;
174                 *pxTopOfStack = 0x33333333;
175                 pxTopOfStack--;
176                 *pxTopOfStack = 0x22222222;
177                 pxTopOfStack--;
178         }
179         #else
180         {
181                 pxTopOfStack -= 15;
182         }
183         #endif
184
185         *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
186         pxTopOfStack--;
187         *pxTopOfStack = portINITIAL_FPSW;
188         pxTopOfStack--;
189         *pxTopOfStack = 0x11111111; /* Accumulator 0. */
190         pxTopOfStack--;
191         *pxTopOfStack = 0x22222222; /* Accumulator 0. */
192         pxTopOfStack--;
193         *pxTopOfStack = 0x33333333; /* Accumulator 0. */
194         pxTopOfStack--;
195         *pxTopOfStack = 0x44444444; /* Accumulator 1. */
196         pxTopOfStack--;
197         *pxTopOfStack = 0x55555555; /* Accumulator 1. */
198         pxTopOfStack--;
199         *pxTopOfStack = 0x66666666; /* Accumulator 1. */
200
201         return pxTopOfStack;
202 }
203 /*-----------------------------------------------------------*/
204
205 BaseType_t xPortStartScheduler( void )
206 {
207 extern void vApplicationSetupTimerInterrupt( void );
208
209         /* Use pxCurrentTCB just so it does not get optimised away. */
210         if( pxCurrentTCB != NULL )
211         {
212                 /* Call an application function to set up the timer that will generate the
213                 tick interrupt.  This way the application can decide which peripheral to
214                 use.  A demo application is provided to show a suitable example. */
215                 vApplicationSetupTimerInterrupt();
216
217                 /* Enable the software interrupt. */
218                 _IEN( _ICU_SWINT ) = 1;
219
220                 /* Ensure the software interrupt is clear. */
221                 _IR( _ICU_SWINT ) = 0;
222
223                 /* Ensure the software interrupt is set to the kernel priority. */
224                 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
225
226                 /* Start the first task. */
227                 prvStartFirstTask();
228         }
229
230         /* Just to make sure the function is not optimised away. */
231         ( void ) vSoftwareInterruptISR();
232
233         /* Should not get here. */
234         return pdFAIL;
235 }
236 /*-----------------------------------------------------------*/
237
238 #pragma inline_asm prvStartFirstTask
239 static void prvStartFirstTask( void )
240 {
241         /* When starting the scheduler there is nothing that needs moving to the
242         interrupt stack because the function is not called from an interrupt.
243         Just ensure the current stack is the user stack. */
244         SETPSW  U
245
246         /* Obtain the location of the stack associated with which ever task
247         pxCurrentTCB is currently pointing to. */
248         MOV.L   #_pxCurrentTCB, R15
249         MOV.L   [R15], R15
250         MOV.L   [R15], R0
251
252         /* Restore the registers from the stack of the task pointed to by
253         pxCurrentTCB. */
254     POP         R15
255     MVTACLO     R15, A0         /* Accumulator low 32 bits. */
256     POP         R15
257     MVTACHI     R15, A0         /* Accumulator high 32 bits. */
258     POP         R15
259     MVTACGU     R15, A0         /* Accumulator guard. */
260     POP         R15
261     MVTACLO     R15, A1         /* Accumulator low 32 bits. */
262     POP         R15
263     MVTACHI     R15, A1         /* Accumulator high 32 bits. */
264     POP         R15
265     MVTACGU     R15, A1         /* Accumulator guard. */
266     POP         R15
267     MVTC        R15,FPSW        /* Floating point status word. */
268     POPM        R1-R15          /* R1 to R15 - R0 is not included as it is the SP. */
269     RTE                                 /* This pops the remaining registers. */
270     NOP
271     NOP
272 }
273 /*-----------------------------------------------------------*/
274
275 #pragma interrupt ( vTickISR( vect = _VECT( configTICK_VECTOR ), enable ) )
276 void vTickISR( void )
277 {
278         /* Increment the tick, and perform any processing the new tick value
279         necessitates. */
280         set_ipl( configMAX_SYSCALL_INTERRUPT_PRIORITY );
281         {
282                 if( xTaskIncrementTick() != pdFALSE )
283                 {
284                         taskYIELD();
285                 }
286         }
287         set_ipl( configKERNEL_INTERRUPT_PRIORITY );
288 }
289 /*-----------------------------------------------------------*/
290
291 void vSoftwareInterruptISR( void )
292 {
293         prvYieldHandler();
294 }
295 /*-----------------------------------------------------------*/
296
297 #pragma inline_asm prvYieldHandler
298 static void prvYieldHandler( void )
299 {
300         /* Re-enable interrupts. */
301         SETPSW  I
302
303         /* Move the data that was automatically pushed onto the interrupt stack when
304         the interrupt occurred from the interrupt stack to the user stack.
305
306         R15 is saved before it is clobbered. */
307         PUSH.L  R15
308
309         /* Read the user stack pointer. */
310         MVFC    USP, R15
311
312         /* Move the address down to the data being moved. */
313         SUB             #12, R15
314         MVTC    R15, USP
315
316         /* Copy the data across. */
317         MOV.L   [ R0 ], [ R15 ] ; R15
318         MOV.L   4[ R0 ], 4[ R15 ]  ; PC
319         MOV.L   8[ R0 ], 8[ R15 ]  ; PSW
320
321         /* Move the interrupt stack pointer to its new correct position. */
322         ADD     #12, R0
323
324         /* All the rest of the registers are saved directly to the user stack. */
325         SETPSW  U
326
327         /* Save the rest of the general registers (R15 has been saved already). */
328         PUSHM   R1-R14
329
330         /* Save the FPSW and accumulators. */
331         MVFC    FPSW, R15
332         PUSH.L  R15
333         MVFACGU #0, A1, R15
334         PUSH.L  R15
335         MVFACHI #0, A1, R15
336         PUSH.L  R15
337         MVFACLO #0, A1, R15     ; Low order word.
338         PUSH.L  R15
339         MVFACGU #0, A0, R15
340         PUSH.L  R15
341         MVFACHI #0, A0, R15
342         PUSH.L  R15
343         MVFACLO #0, A0, R15     ; Low order word.
344         PUSH.L  R15
345
346         /* Save the stack pointer to the TCB. */
347         MOV.L   #_pxCurrentTCB, R15
348         MOV.L   [ R15 ], R15
349         MOV.L   R0, [ R15 ]
350
351         /* Ensure the interrupt mask is set to the syscall priority while the kernel
352         structures are being accessed. */
353         MVTIPL  #configMAX_SYSCALL_INTERRUPT_PRIORITY
354
355         /* Select the next task to run. */
356         BSR.A   _vTaskSwitchContext
357
358         /* Reset the interrupt mask as no more data structure access is required. */
359         MVTIPL  #configKERNEL_INTERRUPT_PRIORITY
360
361         /* Load the stack pointer of the task that is now selected as the Running
362         state task from its TCB. */
363         MOV.L   #_pxCurrentTCB,R15
364         MOV.L   [ R15 ], R15
365         MOV.L   [ R15 ], R0
366
367         /* Restore the context of the new task.  The PSW (Program Status Word) and
368         PC will be popped by the RTE instruction. */
369     POP         R15
370     MVTACLO     R15, A0         /* Accumulator low 32 bits. */
371     POP         R15
372     MVTACHI     R15, A0         /* Accumulator high 32 bits. */
373     POP         R15
374     MVTACGU     R15, A0         /* Accumulator guard. */
375     POP         R15
376     MVTACLO     R15, A1         /* Accumulator low 32 bits. */
377     POP         R15
378     MVTACHI     R15, A1         /* Accumulator high 32 bits. */
379     POP         R15
380     MVTACGU     R15, A1         /* Accumulator guard. */
381         POP             R15
382         MVTC    R15,FPSW
383         POPM    R1-R15
384         RTE
385         NOP
386         NOP
387 }
388 /*-----------------------------------------------------------*/
389
390 void vPortEndScheduler( void )
391 {
392         /* Not implemented in ports where there is nothing to return to.
393         Artificially force an assert. */
394         configASSERT( pxCurrentTCB == NULL );
395
396         /* The following line is just to prevent the symbol getting optimised away. */
397         ( void ) vTaskSwitchContext();
398 }
399 /*-----------------------------------------------------------*/
400
401
402