2 FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 /* GCC/HCS12 port by Jefferson L Smith, 2005 */
\r
77 /* Scheduler includes. */
\r
78 #include "FreeRTOS.h"
\r
82 #include <sys/ports_def.h>
\r
84 /*-----------------------------------------------------------
\r
85 * Implementation of functions defined in portable.h for the HCS12 port.
\r
86 *----------------------------------------------------------*/
\r
90 * Configure a timer to generate the RTOS tick at the frequency specified
\r
91 * within FreeRTOSConfig.h.
\r
93 static void prvSetupTimerInterrupt( void );
\r
95 /* NOTE: Interrupt service routines must be in non-banked memory - as does the
\r
96 scheduler startup function. */
\r
97 #define ATTR_NEAR __attribute__((near))
\r
99 /* Manual context switch function. This is the SWI ISR. */
\r
100 // __attribute__((interrupt))
\r
101 void ATTR_NEAR vPortYield( void );
\r
103 /* Tick context switch function. This is the timer ISR. */
\r
104 // __attribute__((interrupt))
\r
105 void ATTR_NEAR vPortTickInterrupt( void );
\r
107 /* Function in non-banked memory which actually switches to first task. */
\r
108 portBASE_TYPE ATTR_NEAR xStartSchedulerNear( void );
\r
110 /* Calls to portENTER_CRITICAL() can be nested. When they are nested the
\r
111 critical section should not be left (i.e. interrupts should not be re-enabled)
\r
112 until the nesting depth reaches 0. This variable simply tracks the nesting
\r
113 depth. Each task maintains it's own critical nesting depth variable so
\r
114 uxCriticalNesting is saved and restored from the task stack during a context
\r
116 volatile unsigned portBASE_TYPE uxCriticalNesting = 0x80; // un-initialized
\r
118 /*-----------------------------------------------------------*/
\r
121 * See header file for description.
\r
123 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )
\r
127 /* Setup the initial stack of the task. The stack is set exactly as
\r
128 expected by the portRESTORE_CONTEXT() macro. In this case the stack as
\r
129 expected by the HCS12 RTI instruction. */
\r
132 /* The address of the task function is placed in the stack byte at a time. */
\r
133 *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 1 );
\r
134 *--pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 0 );
\r
136 /* Next are all the registers that form part of the task context. */
\r
139 *--pxTopOfStack = ( portSTACK_TYPE ) 0xff;
\r
140 *--pxTopOfStack = ( portSTACK_TYPE ) 0xee;
\r
143 *--pxTopOfStack = ( portSTACK_TYPE ) 0xdd;
\r
144 *--pxTopOfStack = ( portSTACK_TYPE ) 0xcc;
\r
146 /* A register contains parameter high byte. */
\r
147 *--pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 0 );
\r
149 /* B register contains parameter low byte. */
\r
150 *--pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 1 );
\r
152 /* CCR: Note that when the task starts interrupts will be enabled since
\r
153 "I" bit of CCR is cleared */
\r
154 *--pxTopOfStack = ( portSTACK_TYPE ) 0x80; // keeps Stop disabled (MCU default)
\r
156 /* tmp softregs used by GCC. Values right now don't matter. */
\r
158 movw _.frame, 2,-%0 \n\
\r
159 movw _.tmp, 2,-%0 \n\
\r
160 movw _.z, 2,-%0 \n\
\r
161 movw _.xy, 2,-%0 \n\
\r
162 ;movw _.d2, 2,-%0 \n\
\r
163 ;movw _.d1, 2,-%0 \n\
\r
164 ": "=A"(pxTopOfStack) : "0"(pxTopOfStack) );
\r
166 #ifdef BANKED_MODEL
\r
167 /* The page of the task. */
\r
168 *--pxTopOfStack = 0x30; // can only directly start in PPAGE 0x30
\r
171 /* The critical nesting depth is initialised with 0 (meaning not in
\r
172 a critical section). */
\r
173 *--pxTopOfStack = ( portSTACK_TYPE ) 0x00;
\r
176 return pxTopOfStack;
\r
178 /*-----------------------------------------------------------*/
\r
180 void vPortEndScheduler( void )
\r
182 /* It is unlikely that the HCS12 port will get stopped. */
\r
184 /*-----------------------------------------------------------*/
\r
186 static void prvSetupTimerInterrupt( void )
\r
188 /* Enable hardware RTI timer */
\r
189 /* Ignores configTICK_RATE_HZ */
\r
190 RTICTL = 0x50; // 16 MHz xtal: 976.56 Hz, 1024mS
\r
191 CRGINT |= 0x80; // RTIE
\r
193 /*-----------------------------------------------------------*/
\r
195 portBASE_TYPE xPortStartScheduler( void )
\r
197 /* xPortStartScheduler() does not start the scheduler directly because
\r
198 the header file containing the xPortStartScheduler() prototype is part
\r
199 of the common kernel code, and therefore cannot use the CODE_SEG pragma.
\r
200 Instead it simply calls the locally defined xNearStartScheduler() -
\r
201 which does use the CODE_SEG pragma. */
\r
204 __asm ("jmp xStartSchedulerNear ; will never return": "=d"(d));
\r
207 /*-----------------------------------------------------------*/
\r
209 portBASE_TYPE xStartSchedulerNear( void )
\r
211 /* Configure the timer that will generate the RTOS tick. Interrupts are
\r
212 disabled when this function is called. */
\r
213 prvSetupTimerInterrupt();
\r
215 /* Restore the context of the first task. */
\r
216 portRESTORE_CONTEXT();
\r
220 /* Should not get here! */
\r
223 /*-----------------------------------------------------------*/
\r
226 * Context switch functions. These are interrupt service routines.
\r
230 * Manual context switch forced by calling portYIELD(). This is the SWI
\r
233 void vPortYield( void )
\r
236 /* NOTE: This is the trap routine (swi) although not defined as a trap.
\r
237 It will fill the stack the same way as an ISR in order to mix preemtion
\r
238 and cooperative yield. */
\r
240 portSAVE_CONTEXT();
\r
241 vTaskSwitchContext();
\r
242 portRESTORE_CONTEXT();
\r
246 /*-----------------------------------------------------------*/
\r
249 * RTOS tick interrupt service routine. If the cooperative scheduler is
\r
250 * being used then this simply increments the tick count. If the
\r
251 * preemptive scheduler is being used a context switch can occur.
\r
253 void vPortTickInterrupt( void )
\r
257 /* Clear tick timer flag */
\r
260 #if configUSE_PREEMPTION == 1
\r
262 /* A context switch might happen so save the context. */
\r
263 portSAVE_CONTEXT();
\r
265 /* Increment the tick ... */
\r
266 vTaskIncrementTick();
\r
268 /* ... then see if the new tick value has necessitated a
\r
270 vTaskSwitchContext();
\r
272 /* Restore the context of a task - which may be a different task
\r
273 to that interrupted. */
\r
274 portRESTORE_CONTEXT();
\r
278 vTaskIncrementTick();
\r