2 FreeRTOS V8.1.1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /*-----------------------------------------------------------
\r
67 * Implementation of functions defined in portable.h for the SH2A port.
\r
68 *----------------------------------------------------------*/
\r
70 /* Scheduler includes. */
\r
71 #include "FreeRTOS.h"
\r
74 /* Library includes. */
\r
77 /* Hardware specifics. */
\r
78 #include "iodefine.h"
\r
80 /*-----------------------------------------------------------*/
\r
82 /* Tasks should start with interrupts enabled and in Supervisor mode, therefore
\r
83 PSW is set with U and I set, and PM and IPL clear. */
\r
84 #define portINITIAL_PSW ( ( StackType_t ) 0x00030000 )
\r
85 #define portINITIAL_FPSW ( ( StackType_t ) 0x00000100 )
\r
87 /* These macros allow a critical section to be added around the call to
\r
88 xTaskIncrementTick(), which is only ever called from interrupts at the kernel
\r
89 priority - ie a known priority. Therefore these local macros are a slight
\r
90 optimisation compared to calling the global SET/CLEAR_INTERRUPT_MASK macros,
\r
91 which would require the old IPL to be read first and stored in a local variable. */
\r
92 #define portDISABLE_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i"(configMAX_SYSCALL_INTERRUPT_PRIORITY) )
\r
93 #define portENABLE_INTERRUPTS_FROM_KERNEL_ISR() __asm volatile ( "MVTIPL %0" ::"i"(configKERNEL_INTERRUPT_PRIORITY) )
\r
95 /*-----------------------------------------------------------*/
\r
98 * Function to start the first task executing - written in asm code as direct
\r
99 * access to registers is required.
\r
101 static void prvStartFirstTask( void ) __attribute__((naked));
\r
104 * Software interrupt handler. Performs the actual context switch (saving and
\r
105 * restoring of registers). Written in asm code as direct register access is
\r
108 void vSoftwareInterruptISR( void ) __attribute__((naked));
\r
111 * The tick interrupt handler.
\r
113 void vTickISR( void ) __attribute__((interrupt));
\r
115 /*-----------------------------------------------------------*/
\r
117 extern void *pxCurrentTCB;
\r
119 /*-----------------------------------------------------------*/
\r
122 * See header file for description.
\r
124 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
126 /* R0 is not included as it is the stack pointer. */
\r
128 *pxTopOfStack = 0x00;
\r
130 *pxTopOfStack = portINITIAL_PSW;
\r
132 *pxTopOfStack = ( StackType_t ) pxCode;
\r
134 /* When debugging it can be useful if every register is set to a known
\r
135 value. Otherwise code space can be saved by just setting the registers
\r
136 that need to be set. */
\r
137 #ifdef USE_FULL_REGISTER_INITIALISATION
\r
140 *pxTopOfStack = 0xffffffff; /* r15. */
\r
142 *pxTopOfStack = 0xeeeeeeee;
\r
144 *pxTopOfStack = 0xdddddddd;
\r
146 *pxTopOfStack = 0xcccccccc;
\r
148 *pxTopOfStack = 0xbbbbbbbb;
\r
150 *pxTopOfStack = 0xaaaaaaaa;
\r
152 *pxTopOfStack = 0x99999999;
\r
154 *pxTopOfStack = 0x88888888;
\r
156 *pxTopOfStack = 0x77777777;
\r
158 *pxTopOfStack = 0x66666666;
\r
160 *pxTopOfStack = 0x55555555;
\r
162 *pxTopOfStack = 0x44444444;
\r
164 *pxTopOfStack = 0x33333333;
\r
166 *pxTopOfStack = 0x22222222;
\r
171 pxTopOfStack -= 15;
\r
175 *pxTopOfStack = ( StackType_t ) pvParameters; /* R1 */
\r
177 *pxTopOfStack = portINITIAL_FPSW;
\r
179 *pxTopOfStack = 0x12345678; /* Accumulator. */
\r
181 *pxTopOfStack = 0x87654321; /* Accumulator. */
\r
183 return pxTopOfStack;
\r
185 /*-----------------------------------------------------------*/
\r
187 BaseType_t xPortStartScheduler( void )
\r
189 extern void vApplicationSetupTimerInterrupt( void );
\r
191 /* Use pxCurrentTCB just so it does not get optimised away. */
\r
192 if( pxCurrentTCB != NULL )
\r
194 /* Call an application function to set up the timer that will generate the
\r
195 tick interrupt. This way the application can decide which peripheral to
\r
196 use. A demo application is provided to show a suitable example. */
\r
197 vApplicationSetupTimerInterrupt();
\r
199 /* Enable the software interrupt. */
\r
200 _IEN( _ICU_SWINT ) = 1;
\r
202 /* Ensure the software interrupt is clear. */
\r
203 _IR( _ICU_SWINT ) = 0;
\r
205 /* Ensure the software interrupt is set to the kernel priority. */
\r
206 _IPR( _ICU_SWINT ) = configKERNEL_INTERRUPT_PRIORITY;
\r
208 /* Start the first task. */
\r
209 prvStartFirstTask();
\r
212 /* Should not get here. */
\r
215 /*-----------------------------------------------------------*/
\r
217 void vPortEndScheduler( void )
\r
219 /* Not implemented in ports where there is nothing to return to.
\r
220 Artificially force an assert. */
\r
221 configASSERT( pxCurrentTCB == NULL );
\r
223 /*-----------------------------------------------------------*/
\r
225 static void prvStartFirstTask( void )
\r
229 /* When starting the scheduler there is nothing that needs moving to the
\r
230 interrupt stack because the function is not called from an interrupt.
\r
231 Just ensure the current stack is the user stack. */
\r
234 /* Obtain the location of the stack associated with which ever task
\r
235 pxCurrentTCB is currently pointing to. */
\r
236 "MOV.L #_pxCurrentTCB, R15 \n" \
\r
237 "MOV.L [R15], R15 \n" \
\r
238 "MOV.L [R15], R0 \n" \
\r
240 /* Restore the registers from the stack of the task pointed to by
\r
244 /* Accumulator low 32 bits. */
\r
248 /* Accumulator high 32 bits. */
\r
252 /* Floating point status word. */
\r
253 "MVTC R15, FPSW \n" \
\r
255 /* R1 to R15 - R0 is not included as it is the SP. */
\r
258 /* This pops the remaining registers. */
\r
264 /*-----------------------------------------------------------*/
\r
266 void vSoftwareInterruptISR( void )
\r
270 /* Re-enable interrupts. */
\r
273 /* Move the data that was automatically pushed onto the interrupt stack when
\r
274 the interrupt occurred from the interrupt stack to the user stack.
\r
276 R15 is saved before it is clobbered. */
\r
279 /* Read the user stack pointer. */
\r
280 "MVFC USP, R15 \n" \
\r
282 /* Move the address down to the data being moved. */
\r
283 "SUB #12, R15 \n" \
\r
284 "MVTC R15, USP \n" \
\r
286 /* Copy the data across, R15, then PC, then PSW. */
\r
287 "MOV.L [ R0 ], [ R15 ] \n" \
\r
288 "MOV.L 4[ R0 ], 4[ R15 ] \n" \
\r
289 "MOV.L 8[ R0 ], 8[ R15 ] \n" \
\r
291 /* Move the interrupt stack pointer to its new correct position. */
\r
294 /* All the rest of the registers are saved directly to the user stack. */
\r
297 /* Save the rest of the general registers (R15 has been saved already). */
\r
298 "PUSHM R1-R14 \n" \
\r
300 /* Save the FPSW and accumulator. */
\r
301 "MVFC FPSW, R15 \n" \
\r
309 /* Shifted left as it is restored to the low order word. */
\r
310 "SHLL #16, R15 \n" \
\r
313 /* Save the stack pointer to the TCB. */
\r
314 "MOV.L #_pxCurrentTCB, R15 \n" \
\r
315 "MOV.L [ R15 ], R15 \n" \
\r
316 "MOV.L R0, [ R15 ] \n" \
\r
318 /* Ensure the interrupt mask is set to the syscall priority while the kernel
\r
319 structures are being accessed. */
\r
322 /* Select the next task to run. */
\r
323 "BSR.A _vTaskSwitchContext \n" \
\r
325 /* Reset the interrupt mask as no more data structure access is required. */
\r
328 /* Load the stack pointer of the task that is now selected as the Running
\r
329 state task from its TCB. */
\r
330 "MOV.L #_pxCurrentTCB,R15 \n" \
\r
331 "MOV.L [ R15 ], R15 \n" \
\r
332 "MOV.L [ R15 ], R0 \n" \
\r
334 /* Restore the context of the new task. The PSW (Program Status Word) and
\r
335 PC will be popped by the RTE instruction. */
\r
341 "MVTC R15, FPSW \n" \
\r
346 :: "i"(configMAX_SYSCALL_INTERRUPT_PRIORITY), "i"(configKERNEL_INTERRUPT_PRIORITY)
\r
349 /*-----------------------------------------------------------*/
\r
351 void vTickISR( void )
\r
353 /* Re-enabled interrupts. */
\r
354 __asm volatile( "SETPSW I" );
\r
356 /* Increment the tick, and perform any processing the new tick value
\r
357 necessitates. Ensure IPL is at the max syscall value first. */
\r
358 portDISABLE_INTERRUPTS_FROM_KERNEL_ISR();
\r
360 if( xTaskIncrementTick() != pdFALSE )
\r
365 portENABLE_INTERRUPTS_FROM_KERNEL_ISR();
\r
367 /*-----------------------------------------------------------*/
\r
369 uint32_t ulPortGetIPL( void )
\r
373 "MVFC PSW, R1 \n" \
\r
374 "SHLR #24, R1 \n" \
\r
378 /* This will never get executed, but keeps the compiler from complaining. */
\r
381 /*-----------------------------------------------------------*/
\r
383 void vPortSetIPL( uint32_t ulNewIPL )
\r
388 "MVFC PSW, R5 \n" \
\r
389 "SHLL #24, R1 \n" \
\r
390 "AND #-0F000001H, R5 \n" \
\r
392 "MVTC R5, PSW \n" \
\r