2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /*-----------------------------------------------------------
\r
30 * Implementation of functions defined in portable.h for the RX200 port.
\r
31 *----------------------------------------------------------*/
\r
33 /* Scheduler includes. */
\r
34 #include "FreeRTOS.h"
\r
37 /* Library includes. */
\r
40 /* Hardware specifics. */
\r
41 #include "iodefine.h"
\r
43 /*-----------------------------------------------------------*/
\r
45 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
\r
46 PSW is set with U and I set, and PM and IPL clear. */
\r
47 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
\r
49 /*-----------------------------------------------------------*/
\r
51 /* The following lines are to ensure vSoftwareInterruptEntry can be referenced,
\r
52 and therefore installed in the vector table, when the FreeRTOS code is built
\r
54 extern BaseType_t vSoftwareInterruptEntry;
\r
55 const BaseType_t * p_vSoftwareInterruptEntry = &vSoftwareInterruptEntry;
\r
57 /*-----------------------------------------------------------*/
\r
60 * Function to start the first task executing - written in asm code as direct
\r
61 * access to registers is required.
\r
63 static void prvStartFirstTask( void );
\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
70 static void prvYieldHandler( void );
\r
73 * The entry point for the software interrupt handler. This is the function
\r
74 * that calls the inline asm function prvYieldHandler(). It is installed in
\r
75 * the vector table, but the code that installs it is in prvYieldHandler rather
\r
76 * than using a #pragma.
\r
78 void vSoftwareInterruptISR( void );
\r
80 /*-----------------------------------------------------------*/
\r
82 /* This is accessed by the inline assembler functions so is file scope for
\r
84 extern void *pxCurrentTCB;
\r
85 extern void vTaskSwitchContext( void );
\r
87 /*-----------------------------------------------------------*/
\r
90 * See header file for description.
\r
92 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
94 /* Offset to end up on 8 byte boundary. */
\r
97 /* R0 is not included as it is the stack pointer. */
\r
98 *pxTopOfStack = 0x00;
\r
100 *pxTopOfStack = 0x00;
\r
102 *pxTopOfStack = portINITIAL_PSW;
\r
104 *pxTopOfStack = ( StackType_t ) pxCode;
\r
106 /* When debugging it can be useful if every register is set to a known
\r
107 value. Otherwise code space can be saved by just setting the registers
\r
108 that need to be set. */
\r
109 #ifdef USE_FULL_REGISTER_INITIALISATION
\r
112 *pxTopOfStack = 0x12345678; /* r15. */
\r
114 *pxTopOfStack = 0xaaaabbbb;
\r
116 *pxTopOfStack = 0xdddddddd;
\r
118 *pxTopOfStack = 0xcccccccc;
\r
120 *pxTopOfStack = 0xbbbbbbbb;
\r
122 *pxTopOfStack = 0xaaaaaaaa;
\r
124 *pxTopOfStack = 0x99999999;
\r
126 *pxTopOfStack = 0x88888888;
\r
128 *pxTopOfStack = 0x77777777;
\r
130 *pxTopOfStack = 0x66666666;
\r
132 *pxTopOfStack = 0x55555555;
\r
134 *pxTopOfStack = 0x44444444;
\r
136 *pxTopOfStack = 0x33333333;
\r
138 *pxTopOfStack = 0x22222222;
\r
143 pxTopOfStack -= 15;
\r
147 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
\r
149 *pxTopOfStack = 0x12345678; /* Accumulator. */
\r
151 *pxTopOfStack = 0x87654321; /* Accumulator. */
\r
153 return pxTopOfStack;
\r
155 /*-----------------------------------------------------------*/
\r
157 BaseType_t xPortStartScheduler( void )
\r
159 extern void vApplicationSetupTimerInterrupt( void );
\r
161 /* Use pxCurrentTCB just so it does not get optimised away. */
\r
162 if( pxCurrentTCB != NULL )
\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
169 /* Enable the software interrupt. */
\r
170 _IEN( _ICU_SWINT ) = 1;
\r
172 /* Ensure the software interrupt is clear. */
\r
173 _IR( _ICU_SWINT ) = 0;
\r
175 /* Ensure the software interrupt is set to the kernel priority. */
\r
176 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
\r
178 /* Start the first task. */
\r
179 prvStartFirstTask();
\r
182 /* Just to make sure the function is not optimised away. */
\r
183 ( void ) vSoftwareInterruptISR();
\r
185 /* Should not get here. */
\r
188 /*-----------------------------------------------------------*/
\r
190 #pragma inline_asm prvStartFirstTask
\r
191 static void prvStartFirstTask( void )
\r
193 /* When starting the scheduler there is nothing that needs moving to the
\r
194 interrupt stack because the function is not called from an interrupt.
\r
195 Just ensure the current stack is the user stack. */
\r
198 /* Obtain the location of the stack associated with which ever task
\r
199 pxCurrentTCB is currently pointing to. */
\r
200 MOV.L #_pxCurrentTCB, R15
\r
204 /* Restore the registers from the stack of the task pointed to by
\r
207 MVTACLO R15 /* Accumulator low 32 bits. */
\r
209 MVTACHI R15 /* Accumulator high 32 bits. */
\r
210 POPM R1-R15 /* R1 to R15 - R0 is not included as it is the SP. */
\r
211 RTE /* This pops the remaining registers. */
\r
215 /*-----------------------------------------------------------*/
\r
217 #pragma interrupt ( vTickISR( vect = _VECT( configTICK_VECTOR ), enable ) )
\r
218 void vTickISR( void )
\r
220 /* Increment the tick, and perform any processing the new tick value
\r
222 set_ipl( configMAX_SYSCALL_INTERRUPT_PRIORITY );
\r
224 if( xTaskIncrementTick() != pdFALSE )
\r
229 set_ipl( configKERNEL_INTERRUPT_PRIORITY );
\r
231 /*-----------------------------------------------------------*/
\r
233 void vSoftwareInterruptISR( void )
\r
237 /*-----------------------------------------------------------*/
\r
239 #pragma inline_asm prvYieldHandler
\r
240 static void prvYieldHandler( void )
\r
242 /* Re-enable interrupts. */
\r
245 /* Move the data that was automatically pushed onto the interrupt stack when
\r
246 the interrupt occurred from the interrupt stack to the user stack.
\r
248 R15 is saved before it is clobbered. */
\r
251 /* Read the user stack pointer. */
\r
254 /* Move the address down to the data being moved. */
\r
258 /* Copy the data across. */
\r
259 MOV.L [ R0 ], [ R15 ] ; R15
\r
260 MOV.L 4[ R0 ], 4[ R15 ] ; PC
\r
261 MOV.L 8[ R0 ], 8[ R15 ] ; PSW
\r
263 /* Move the interrupt stack pointer to its new correct position. */
\r
266 /* All the rest of the registers are saved directly to the user stack. */
\r
269 /* Save the rest of the general registers (R15 has been saved already). */
\r
272 /* Save the accumulator. */
\r
275 MVFACMI R15 ; Middle order word.
\r
276 SHLL #16, R15 ; Shifted left as it is restored to the low order word.
\r
279 /* Save the stack pointer to the TCB. */
\r
280 MOV.L #_pxCurrentTCB, R15
\r
284 /* Ensure the interrupt mask is set to the syscall priority while the kernel
\r
285 structures are being accessed. */
\r
286 MVTIPL #configMAX_SYSCALL_INTERRUPT_PRIORITY
\r
288 /* Select the next task to run. */
\r
289 BSR.A _vTaskSwitchContext
\r
291 /* Reset the interrupt mask as no more data structure access is required. */
\r
292 MVTIPL #configKERNEL_INTERRUPT_PRIORITY
\r
294 /* Load the stack pointer of the task that is now selected as the Running
\r
295 state task from its TCB. */
\r
296 MOV.L #_pxCurrentTCB,R15
\r
300 /* Restore the context of the new task. The PSW (Program Status Word) and
\r
301 PC will be popped by the RTE instruction. */
\r
311 /*-----------------------------------------------------------*/
\r
313 void vPortEndScheduler( void )
\r
315 /* Not implemented in ports where there is nothing to return to.
\r
316 Artificially force an assert. */
\r
317 configASSERT( pxCurrentTCB == NULL );
\r
319 /* The following line is just to prevent the symbol getting optimised away. */
\r
320 ( void ) vTaskSwitchContext();
\r
322 /*-----------------------------------------------------------*/
\r