]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/CodeWarrior/HCS12/port.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Source / portable / CodeWarrior / HCS12 / port.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \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
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\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
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \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
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /* Scheduler includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 \r
73 \r
74 /*-----------------------------------------------------------\r
75  * Implementation of functions defined in portable.h for the HCS12 port.\r
76  *----------------------------------------------------------*/\r
77 \r
78 \r
79 /*\r
80  * Configure a timer to generate the RTOS tick at the frequency specified \r
81  * within FreeRTOSConfig.h.\r
82  */\r
83 static void prvSetupTimerInterrupt( void );\r
84 \r
85 /* Interrupt service routines have to be in non-banked memory - as does the\r
86 scheduler startup function. */\r
87 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
88 \r
89         /* Manual context switch function.  This is the SWI ISR. */\r
90         void interrupt vPortYield( void );\r
91 \r
92         /* Tick context switch function.  This is the timer ISR. */\r
93         void interrupt vPortTickInterrupt( void );\r
94         \r
95         /* Simply called by xPortStartScheduler().  xPortStartScheduler() does not\r
96         start the scheduler directly because the header file containing the \r
97         xPortStartScheduler() prototype is part of the common kernel code, and \r
98         therefore cannot use the CODE_SEG pragma. */\r
99         static portBASE_TYPE xBankedStartScheduler( void );\r
100 \r
101 #pragma CODE_SEG DEFAULT\r
102 \r
103 /* Calls to portENTER_CRITICAL() can be nested.  When they are nested the \r
104 critical section should not be left (i.e. interrupts should not be re-enabled)\r
105 until the nesting depth reaches 0.  This variable simply tracks the nesting \r
106 depth.  Each task maintains it's own critical nesting depth variable so \r
107 uxCriticalNesting is saved and restored from the task stack during a context\r
108 switch. */\r
109 volatile unsigned portBASE_TYPE uxCriticalNesting = 0xff;\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 /* \r
114  * See header file for description. \r
115  */\r
116 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
117 {\r
118         /* \r
119                 Place a few bytes of known values on the bottom of the stack.\r
120                 This can be uncommented to provide useful stack markers when debugging.\r
121 \r
122                 *pxTopOfStack = ( portSTACK_TYPE ) 0x11;\r
123                 pxTopOfStack--;\r
124                 *pxTopOfStack = ( portSTACK_TYPE ) 0x22;\r
125                 pxTopOfStack--;\r
126                 *pxTopOfStack = ( portSTACK_TYPE ) 0x33;\r
127                 pxTopOfStack--;\r
128         */\r
129 \r
130 \r
131 \r
132         /* Setup the initial stack of the task.  The stack is set exactly as \r
133         expected by the portRESTORE_CONTEXT() macro.  In this case the stack as\r
134         expected by the HCS12 RTI instruction. */\r
135 \r
136 \r
137         /* The address of the task function is placed in the stack byte at a time. */\r
138         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 1 );\r
139         pxTopOfStack--;\r
140         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 0 );\r
141         pxTopOfStack--;\r
142 \r
143         /* Next are all the registers that form part of the task context. */\r
144 \r
145         /* Y register */\r
146         *pxTopOfStack = ( portSTACK_TYPE ) 0xff;\r
147         pxTopOfStack--;\r
148         *pxTopOfStack = ( portSTACK_TYPE ) 0xee;\r
149         pxTopOfStack--;\r
150 \r
151         /* X register */\r
152         *pxTopOfStack = ( portSTACK_TYPE ) 0xdd;\r
153         pxTopOfStack--;\r
154         *pxTopOfStack = ( portSTACK_TYPE ) 0xcc;\r
155         pxTopOfStack--;\r
156  \r
157         /* A register contains parameter high byte. */\r
158         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 0 );\r
159         pxTopOfStack--;\r
160 \r
161         /* B register contains parameter low byte. */\r
162         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 1 );\r
163         pxTopOfStack--;\r
164 \r
165         /* CCR: Note that when the task starts interrupts will be enabled since\r
166         "I" bit of CCR is cleared */\r
167         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;\r
168         pxTopOfStack--;\r
169         \r
170         #ifdef BANKED_MODEL\r
171                 /* The page of the task. */\r
172                 *pxTopOfStack = ( portSTACK_TYPE ) ( ( int ) pxCode );\r
173                 pxTopOfStack--;\r
174         #endif\r
175         \r
176         /* Finally the critical nesting depth is initialised with 0 (not within\r
177         a critical section). */\r
178         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;\r
179 \r
180         return pxTopOfStack;\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 void vPortEndScheduler( void )\r
185 {\r
186         /* It is unlikely that the HCS12 port will get stopped. */\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 static void prvSetupTimerInterrupt( void )\r
191 {\r
192         TickTimer_SetFreqHz( configTICK_RATE_HZ );\r
193         TickTimer_Enable();\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 portBASE_TYPE xPortStartScheduler( void )\r
198 {\r
199         /* xPortStartScheduler() does not start the scheduler directly because \r
200         the header file containing the xPortStartScheduler() prototype is part \r
201         of the common kernel code, and therefore cannot use the CODE_SEG pragma. \r
202         Instead it simply calls the locally defined xBankedStartScheduler() - \r
203         which does use the CODE_SEG pragma. */\r
204 \r
205         return xBankedStartScheduler();\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
210 \r
211 static portBASE_TYPE xBankedStartScheduler( void )\r
212 {\r
213         /* Configure the timer that will generate the RTOS tick.  Interrupts are\r
214         disabled when this function is called. */\r
215         prvSetupTimerInterrupt();\r
216 \r
217         /* Restore the context of the first task. */\r
218         portRESTORE_CONTEXT();\r
219 \r
220         /* Simulate the end of an interrupt to start the scheduler off. */\r
221         __asm( "rti" );\r
222 \r
223         /* Should not get here! */\r
224         return pdFALSE;\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 /*\r
229  * Context switch functions.  These are both interrupt service routines.\r
230  */\r
231 \r
232 /*\r
233  * Manual context switch forced by calling portYIELD().  This is the SWI\r
234  * handler.\r
235  */\r
236 void interrupt vPortYield( void )\r
237 {\r
238         portSAVE_CONTEXT();\r
239         vTaskSwitchContext();\r
240         portRESTORE_CONTEXT();\r
241 }\r
242 /*-----------------------------------------------------------*/\r
243 \r
244 /*\r
245  * RTOS tick interrupt service routine.  If the cooperative scheduler is \r
246  * being used then this simply increments the tick count.  If the \r
247  * preemptive scheduler is being used a context switch can occur.\r
248  */\r
249 void interrupt vPortTickInterrupt( void )\r
250 {\r
251         #if configUSE_PREEMPTION == 1\r
252         {\r
253                 /* A context switch might happen so save the context. */\r
254                 portSAVE_CONTEXT();\r
255 \r
256                 /* Increment the tick ... */\r
257                 vTaskIncrementTick();\r
258 \r
259                 /* ... then see if the new tick value has necessitated a\r
260                 context switch. */\r
261                 vTaskSwitchContext();\r
262 \r
263                 TFLG1 = 1;                                                                 \r
264 \r
265                 /* Restore the context of a task - which may be a different task\r
266                 to that interrupted. */\r
267                 portRESTORE_CONTEXT();  \r
268         }\r
269         #else\r
270         {\r
271                 vTaskIncrementTick();\r
272                 TFLG1 = 1;\r
273         }\r
274         #endif\r
275 }\r
276 \r
277 #pragma CODE_SEG DEFAULT\r
278 \r
279 \r