2 FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
4 ***************************************************************************
\r
8 * + New to FreeRTOS, *
\r
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
\r
10 * + Looking for basic training, *
\r
11 * + Wanting to improve your FreeRTOS skills and productivity *
\r
13 * then take a look at the FreeRTOS books - available as PDF or paperback *
\r
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
\r
16 * http://www.FreeRTOS.org/Documentation *
\r
18 * A pdf reference manual is also available. Both are usually delivered *
\r
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
\r
20 * and 8pm GMT (although please allow up to 24 hours in case of *
\r
21 * exceptional circumstances). Thank you for your support! *
\r
23 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
\r
31 a combined work that includes FreeRTOS without being obliged to provide the
\r
32 source code for proprietary components outside of the FreeRTOS kernel.
\r
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* GCC/HCS12 port by Jefferson L Smith, 2005 */
\r
56 /* Scheduler includes. */
\r
57 #include "FreeRTOS.h"
\r
61 #include <sys/ports_def.h>
\r
63 /*-----------------------------------------------------------
\r
64 * Implementation of functions defined in portable.h for the HCS12 port.
\r
65 *----------------------------------------------------------*/
\r
69 * Configure a timer to generate the RTOS tick at the frequency specified
\r
70 * within FreeRTOSConfig.h.
\r
72 static void prvSetupTimerInterrupt( void );
\r
74 /* NOTE: Interrupt service routines must be in non-banked memory - as does the
\r
75 scheduler startup function. */
\r
76 #define ATTR_NEAR __attribute__((near))
\r
78 /* Manual context switch function. This is the SWI ISR. */
\r
79 // __attribute__((interrupt))
\r
80 void ATTR_NEAR vPortYield( void );
\r
82 /* Tick context switch function. This is the timer ISR. */
\r
83 // __attribute__((interrupt))
\r
84 void ATTR_NEAR vPortTickInterrupt( void );
\r
86 /* Function in non-banked memory which actually switches to first task. */
\r
87 portBASE_TYPE ATTR_NEAR xStartSchedulerNear( void );
\r
89 /* Calls to portENTER_CRITICAL() can be nested. When they are nested the
\r
90 critical section should not be left (i.e. interrupts should not be re-enabled)
\r
91 until the nesting depth reaches 0. This variable simply tracks the nesting
\r
92 depth. Each task maintains it's own critical nesting depth variable so
\r
93 uxCriticalNesting is saved and restored from the task stack during a context
\r
95 volatile unsigned portBASE_TYPE uxCriticalNesting = 0x80; // un-initialized
\r
97 /*-----------------------------------------------------------*/
\r
100 * See header file for description.
\r
102 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
106 /* Setup the initial stack of the task. The stack is set exactly as
\r
107 expected by the portRESTORE_CONTEXT() macro. In this case the stack as
\r
108 expected by the HCS12 RTI instruction. */
\r
111 /* The address of the task function is placed in the stack byte at a time. */
\r
112 *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 1 );
\r
113 *--pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 0 );
\r
115 /* Next are all the registers that form part of the task context. */
\r
118 *--pxTopOfStack = ( portSTACK_TYPE ) 0xff;
\r
119 *--pxTopOfStack = ( portSTACK_TYPE ) 0xee;
\r
122 *--pxTopOfStack = ( portSTACK_TYPE ) 0xdd;
\r
123 *--pxTopOfStack = ( portSTACK_TYPE ) 0xcc;
\r
125 /* A register contains parameter high byte. */
\r
126 *--pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 0 );
\r
128 /* B register contains parameter low byte. */
\r
129 *--pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 1 );
\r
131 /* CCR: Note that when the task starts interrupts will be enabled since
\r
132 "I" bit of CCR is cleared */
\r
133 *--pxTopOfStack = ( portSTACK_TYPE ) 0x80; // keeps Stop disabled (MCU default)
\r
135 /* tmp softregs used by GCC. Values right now don't matter. */
\r
137 movw _.frame, 2,-%0 \n\
\r
138 movw _.tmp, 2,-%0 \n\
\r
139 movw _.z, 2,-%0 \n\
\r
140 movw _.xy, 2,-%0 \n\
\r
141 ;movw _.d2, 2,-%0 \n\
\r
142 ;movw _.d1, 2,-%0 \n\
\r
143 ": "=A"(pxTopOfStack) : "0"(pxTopOfStack) );
\r
145 #ifdef BANKED_MODEL
\r
146 /* The page of the task. */
\r
147 *--pxTopOfStack = 0x30; // can only directly start in PPAGE 0x30
\r
150 /* The critical nesting depth is initialised with 0 (meaning not in
\r
151 a critical section). */
\r
152 *--pxTopOfStack = ( portSTACK_TYPE ) 0x00;
\r
155 return pxTopOfStack;
\r
157 /*-----------------------------------------------------------*/
\r
159 void vPortEndScheduler( void )
\r
161 /* It is unlikely that the HCS12 port will get stopped. */
\r
163 /*-----------------------------------------------------------*/
\r
165 static void prvSetupTimerInterrupt( void )
\r
167 /* Enable hardware RTI timer */
\r
168 /* Ignores configTICK_RATE_HZ */
\r
169 RTICTL = 0x50; // 16 MHz xtal: 976.56 Hz, 1024mS
\r
170 CRGINT |= 0x80; // RTIE
\r
172 /*-----------------------------------------------------------*/
\r
174 portBASE_TYPE xPortStartScheduler( void )
\r
176 /* xPortStartScheduler() does not start the scheduler directly because
\r
177 the header file containing the xPortStartScheduler() prototype is part
\r
178 of the common kernel code, and therefore cannot use the CODE_SEG pragma.
\r
179 Instead it simply calls the locally defined xNearStartScheduler() -
\r
180 which does use the CODE_SEG pragma. */
\r
183 __asm ("jmp xStartSchedulerNear ; will never return": "=d"(d));
\r
186 /*-----------------------------------------------------------*/
\r
188 portBASE_TYPE xStartSchedulerNear( void )
\r
190 /* Configure the timer that will generate the RTOS tick. Interrupts are
\r
191 disabled when this function is called. */
\r
192 prvSetupTimerInterrupt();
\r
194 /* Restore the context of the first task. */
\r
195 portRESTORE_CONTEXT();
\r
199 /* Should not get here! */
\r
202 /*-----------------------------------------------------------*/
\r
205 * Context switch functions. These are interrupt service routines.
\r
209 * Manual context switch forced by calling portYIELD(). This is the SWI
\r
212 void vPortYield( void )
\r
215 /* NOTE: This is the trap routine (swi) although not defined as a trap.
\r
216 It will fill the stack the same way as an ISR in order to mix preemtion
\r
217 and cooperative yield. */
\r
219 portSAVE_CONTEXT();
\r
220 vTaskSwitchContext();
\r
221 portRESTORE_CONTEXT();
\r
225 /*-----------------------------------------------------------*/
\r
228 * RTOS tick interrupt service routine. If the cooperative scheduler is
\r
229 * being used then this simply increments the tick count. If the
\r
230 * preemptive scheduler is being used a context switch can occur.
\r
232 void vPortTickInterrupt( void )
\r
236 /* Clear tick timer flag */
\r
239 #if configUSE_PREEMPTION == 1
\r
241 /* A context switch might happen so save the context. */
\r
242 portSAVE_CONTEXT();
\r
244 /* Increment the tick ... */
\r
245 vTaskIncrementTick();
\r
247 /* ... then see if the new tick value has necessitated a
\r
249 vTaskSwitchContext();
\r
251 /* Restore the context of a task - which may be a different task
\r
252 to that interrupted. */
\r
253 portRESTORE_CONTEXT();
\r
257 vTaskIncrementTick();
\r