2 FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\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
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 /*-----------------------------------------------------------
\r
71 * Implementation of functions defined in portable.h for the SH2A port.
\r
72 *----------------------------------------------------------*/
\r
74 /* Scheduler includes. */
\r
75 #include "FreeRTOS.h"
\r
78 /* Library includes. */
\r
81 /* Hardware specifics. */
\r
82 #include "iodefine.h"
\r
84 /*-----------------------------------------------------------*/
\r
86 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
\r
87 PSW is set with U and I set, and PM and IPL clear. */
\r
88 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
\r
89 #define portINITIAL_FPSW ( ( StackType_t ) 0x00000100 )
\r
91 /* These macros allow a critical section to be added around the call to
\r
92 xTaskIncrementTick(), which is only ever called from interrupts at the kernel
\r
93 priority - ie a known priority. Therefore these local macros are a slight
\r
94 optimisation compared to calling the global SET/CLEAR_INTERRUPT_MASK macros,
\r
95 which would require the old IPL to be read first and stored in a local variable. */
\r
96 #define portMASK_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i"(configMAX_SYSCALL_INTERRUPT_PRIORITY) )
\r
97 #define portUNMASK_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i"(configKERNEL_INTERRUPT_PRIORITY) )
\r
99 /*-----------------------------------------------------------*/
\r
102 * Function to start the first task executing - written in asm code as direct
\r
103 * access to registers is required.
\r
105 static void prvStartFirstTask( void ) __attribute__((naked));
\r
108 * Software interrupt handler. Performs the actual context switch (saving and
\r
109 * restoring of registers). Written in asm code as direct register access is
\r
112 void vSoftwareInterruptISR( void ) __attribute__((naked));
\r
115 * The tick interrupt handler.
\r
117 void vTickISR( void ) __attribute__((interrupt));
\r
119 /*-----------------------------------------------------------*/
\r
121 extern void *pxCurrentTCB;
\r
123 /*-----------------------------------------------------------*/
\r
126 * See header file for description.
\r
128 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
130 /* R0 is not included as it is the stack pointer. */
\r
132 *pxTopOfStack = 0x00;
\r
134 *pxTopOfStack = portINITIAL_PSW;
\r
136 *pxTopOfStack = ( StackType_t ) pxCode;
\r
138 /* When debugging it can be useful if every register is set to a known
\r
139 value. Otherwise code space can be saved by just setting the registers
\r
140 that need to be set. */
\r
141 #ifdef USE_FULL_REGISTER_INITIALISATION
\r
144 *pxTopOfStack = 0xffffffff; /* r15. */
\r
146 *pxTopOfStack = 0xeeeeeeee;
\r
148 *pxTopOfStack = 0xdddddddd;
\r
150 *pxTopOfStack = 0xcccccccc;
\r
152 *pxTopOfStack = 0xbbbbbbbb;
\r
154 *pxTopOfStack = 0xaaaaaaaa;
\r
156 *pxTopOfStack = 0x99999999;
\r
158 *pxTopOfStack = 0x88888888;
\r
160 *pxTopOfStack = 0x77777777;
\r
162 *pxTopOfStack = 0x66666666;
\r
164 *pxTopOfStack = 0x55555555;
\r
166 *pxTopOfStack = 0x44444444;
\r
168 *pxTopOfStack = 0x33333333;
\r
170 *pxTopOfStack = 0x22222222;
\r
175 pxTopOfStack -= 15;
\r
179 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
\r
181 *pxTopOfStack = portINITIAL_FPSW;
\r
183 *pxTopOfStack = 0x11111111; /* Accumulator 0. */
\r
185 *pxTopOfStack = 0x22222222; /* Accumulator 0. */
\r
187 *pxTopOfStack = 0x33333333; /* Accumulator 0. */
\r
189 *pxTopOfStack = 0x44444444; /* Accumulator 1. */
\r
191 *pxTopOfStack = 0x55555555; /* Accumulator 1. */
\r
193 *pxTopOfStack = 0x66666666; /* Accumulator 1. */
\r
195 return pxTopOfStack;
\r
197 /*-----------------------------------------------------------*/
\r
199 BaseType_t xPortStartScheduler( void )
\r
201 extern void vApplicationSetupTimerInterrupt( void );
\r
203 /* Use pxCurrentTCB just so it does not get optimised away. */
\r
204 if( pxCurrentTCB != NULL )
\r
206 /* Call an application function to set up the timer that will generate the
\r
207 tick interrupt. This way the application can decide which peripheral to
\r
208 use. A demo application is provided to show a suitable example. */
\r
209 vApplicationSetupTimerInterrupt();
\r
211 /* Enable the software interrupt. */
\r
212 _IEN( _ICU_SWINT ) = 1;
\r
214 /* Ensure the software interrupt is clear. */
\r
215 _IR( _ICU_SWINT ) = 0;
\r
217 /* Ensure the software interrupt is set to the kernel priority. */
\r
218 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
\r
220 /* Start the first task. */
\r
221 prvStartFirstTask();
\r
224 /* Should not get here. */
\r
227 /*-----------------------------------------------------------*/
\r
229 void vPortEndScheduler( void )
\r
231 /* Not implemented in ports where there is nothing to return to.
\r
232 Artificially force an assert. */
\r
233 configASSERT( pxCurrentTCB == NULL );
\r
235 /*-----------------------------------------------------------*/
\r
237 static void prvStartFirstTask( void )
\r
241 /* When starting the scheduler there is nothing that needs moving to the
\r
242 interrupt stack because the function is not called from an interrupt.
\r
243 Just ensure the current stack is the user stack. */
\r
246 /* Obtain the location of the stack associated with which ever task
\r
247 pxCurrentTCB is currently pointing to. */
\r
248 "MOV.L #_pxCurrentTCB, R15 \n" \
\r
249 "MOV.L [R15], R15 \n" \
\r
250 "MOV.L [R15], R0 \n" \
\r
252 /* Restore the registers from the stack of the task pointed to by
\r
256 /* Accumulator low 32 bits. */
\r
257 "MVTACLO R15, A0 \n" \
\r
260 /* Accumulator high 32 bits. */
\r
261 "MVTACHI R15, A0 \n" \
\r
264 /* Accumulator guard. */
\r
265 "MVTACGU R15, A0 \n" \
\r
268 /* Accumulator low 32 bits. */
\r
269 "MVTACLO R15, A1 \n" \
\r
272 /* Accumulator high 32 bits. */
\r
273 "MVTACHI R15, A1 \n" \
\r
276 /* Accumulator guard. */
\r
277 "MVTACGU R15, A1 \n" \
\r
280 /* Floating point status word. */
\r
281 "MVTC R15, FPSW \n" \
\r
283 /* R1 to R15 - R0 is not included as it is the SP. */
\r
286 /* This pops the remaining registers. */
\r
292 /*-----------------------------------------------------------*/
\r
294 void vSoftwareInterruptISR( void )
\r
298 /* Re-enable interrupts. */
\r
301 /* Move the data that was automatically pushed onto the interrupt stack when
\r
302 the interrupt occurred from the interrupt stack to the user stack.
\r
304 R15 is saved before it is clobbered. */
\r
307 /* Read the user stack pointer. */
\r
308 "MVFC USP, R15 \n" \
\r
310 /* Move the address down to the data being moved. */
\r
311 "SUB #12, R15 \n" \
\r
312 "MVTC R15, USP \n" \
\r
314 /* Copy the data across, R15, then PC, then PSW. */
\r
315 "MOV.L [ R0 ], [ R15 ] \n" \
\r
316 "MOV.L 4[ R0 ], 4[ R15 ] \n" \
\r
317 "MOV.L 8[ R0 ], 8[ R15 ] \n" \
\r
319 /* Move the interrupt stack pointer to its new correct position. */
\r
322 /* All the rest of the registers are saved directly to the user stack. */
\r
325 /* Save the rest of the general registers (R15 has been saved already). */
\r
326 "PUSHM R1-R14 \n" \
\r
328 /* Save the FPSW and accumulator. */
\r
329 "MVFC FPSW, R15 \n" \
\r
331 "MVFACGU #0, A1, R15 \n" \
\r
333 "MVFACHI #0, A1, R15 \n" \
\r
335 /* Low order word. */
\r
336 "MVFACLO #0, A1, R15 \n" \
\r
338 "MVFACGU #0, A0, R15 \n" \
\r
340 "MVFACHI #0, A0, R15 \n" \
\r
342 /* Low order word. */
\r
343 "MVFACLO #0, A0, R15 \n" \
\r
346 /* Save the stack pointer to the TCB. */
\r
347 "MOV.L #_pxCurrentTCB, R15 \n" \
\r
348 "MOV.L [ R15 ], R15 \n" \
\r
349 "MOV.L R0, [ R15 ] \n" \
\r
351 /* Ensure the interrupt mask is set to the syscall priority while the kernel
\r
352 structures are being accessed. */
\r
355 /* Select the next task to run. */
\r
356 "BSR.A _vTaskSwitchContext \n" \
\r
358 /* Reset the interrupt mask as no more data structure access is required. */
\r
361 /* Load the stack pointer of the task that is now selected as the Running
\r
362 state task from its TCB. */
\r
363 "MOV.L #_pxCurrentTCB,R15 \n" \
\r
364 "MOV.L [ R15 ], R15 \n" \
\r
365 "MOV.L [ R15 ], R0 \n" \
\r
367 /* Restore the context of the new task. The PSW (Program Status Word) and
\r
368 PC will be popped by the RTE instruction. */
\r
371 /* Accumulator low 32 bits. */
\r
372 "MVTACLO R15, A0 \n" \
\r
375 /* Accumulator high 32 bits. */
\r
376 "MVTACHI R15, A0 \n" \
\r
379 /* Accumulator guard. */
\r
380 "MVTACGU R15, A0 \n" \
\r
383 /* Accumulator low 32 bits. */
\r
384 "MVTACLO R15, A1 \n" \
\r
387 /* Accumulator high 32 bits. */
\r
388 "MVTACHI R15, A1 \n" \
\r
391 /* Accumulator guard. */
\r
392 "MVTACGU R15, A1 \n" \
\r
394 "MVTC R15, FPSW \n" \
\r
399 :: "i"(configMAX_SYSCALL_INTERRUPT_PRIORITY), "i"(configKERNEL_INTERRUPT_PRIORITY)
\r
402 /*-----------------------------------------------------------*/
\r
404 void vTickISR( void )
\r
406 /* Re-enabled interrupts. */
\r
407 __asm volatile( "SETPSW I" );
\r
409 /* Increment the tick, and perform any processing the new tick value
\r
410 necessitates. Ensure IPL is at the max syscall value first. */
\r
411 portMASK_INTERRUPTS_FROM_KERNEL_ISR();
\r
413 if( xTaskIncrementTick() != pdFALSE )
\r
418 portUNMASK_INTERRUPTS_FROM_KERNEL_ISR();
\r
420 /*-----------------------------------------------------------*/
\r
422 uint32_t ulPortGetIPL( void )
\r
426 "MVFC PSW, R1 \n" \
\r
427 "SHLR #24, R1 \n" \
\r
431 /* This will never get executed, but keeps the compiler from complaining. */
\r
434 /*-----------------------------------------------------------*/
\r
436 void vPortSetIPL( uint32_t ulNewIPL )
\r
441 "MVFC PSW, R5 \n" \
\r
442 "SHLL #24, R1 \n" \
\r
443 "AND #-0F000001H, R5 \n" \
\r
445 "MVTC R5, PSW \n" \
\r