]> begriffs open source - freertos/blob - Source/portable/CodeWarrior/HCS12/port.c
Prepare for V5.3.0 release.
[freertos] / Source / portable / CodeWarrior / HCS12 / port.c
1 /*\r
2         FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /* Scheduler includes. */\r
53 #include "FreeRTOS.h"\r
54 #include "task.h"\r
55 \r
56 \r
57 /*-----------------------------------------------------------\r
58  * Implementation of functions defined in portable.h for the HCS12 port.\r
59  *----------------------------------------------------------*/\r
60 \r
61 \r
62 /*\r
63  * Configure a timer to generate the RTOS tick at the frequency specified \r
64  * within FreeRTOSConfig.h.\r
65  */\r
66 static void prvSetupTimerInterrupt( void );\r
67 \r
68 /* Interrupt service routines have to be in non-banked memory - as does the\r
69 scheduler startup function. */\r
70 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
71 \r
72         /* Manual context switch function.  This is the SWI ISR. */\r
73         void interrupt vPortYield( void );\r
74 \r
75         /* Tick context switch function.  This is the timer ISR. */\r
76         void interrupt vPortTickInterrupt( void );\r
77         \r
78         /* Simply called by xPortStartScheduler().  xPortStartScheduler() does not\r
79         start the scheduler directly because the header file containing the \r
80         xPortStartScheduler() prototype is part of the common kernel code, and \r
81         therefore cannot use the CODE_SEG pragma. */\r
82         static portBASE_TYPE xBankedStartScheduler( void );\r
83 \r
84 #pragma CODE_SEG DEFAULT\r
85 \r
86 /* Calls to portENTER_CRITICAL() can be nested.  When they are nested the \r
87 critical section should not be left (i.e. interrupts should not be re-enabled)\r
88 until the nesting depth reaches 0.  This variable simply tracks the nesting \r
89 depth.  Each task maintains it's own critical nesting depth variable so \r
90 uxCriticalNesting is saved and restored from the task stack during a context\r
91 switch. */\r
92 volatile unsigned portBASE_TYPE uxCriticalNesting = 0xff;\r
93 \r
94 /*-----------------------------------------------------------*/\r
95 \r
96 /* \r
97  * See header file for description. \r
98  */\r
99 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
100 {\r
101         /* \r
102                 Place a few bytes of known values on the bottom of the stack.\r
103                 This can be uncommented to provide useful stack markers when debugging.\r
104 \r
105                 *pxTopOfStack = ( portSTACK_TYPE ) 0x11;\r
106                 pxTopOfStack--;\r
107                 *pxTopOfStack = ( portSTACK_TYPE ) 0x22;\r
108                 pxTopOfStack--;\r
109                 *pxTopOfStack = ( portSTACK_TYPE ) 0x33;\r
110                 pxTopOfStack--;\r
111         */\r
112 \r
113 \r
114 \r
115         /* Setup the initial stack of the task.  The stack is set exactly as \r
116         expected by the portRESTORE_CONTEXT() macro.  In this case the stack as\r
117         expected by the HCS12 RTI instruction. */\r
118 \r
119 \r
120         /* The address of the task function is placed in the stack byte at a time. */\r
121         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 1 );\r
122         pxTopOfStack--;\r
123         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 0 );\r
124         pxTopOfStack--;\r
125 \r
126         /* Next are all the registers that form part of the task context. */\r
127 \r
128         /* Y register */\r
129         *pxTopOfStack = ( portSTACK_TYPE ) 0xff;\r
130         pxTopOfStack--;\r
131         *pxTopOfStack = ( portSTACK_TYPE ) 0xee;\r
132         pxTopOfStack--;\r
133 \r
134         /* X register */\r
135         *pxTopOfStack = ( portSTACK_TYPE ) 0xdd;\r
136         pxTopOfStack--;\r
137         *pxTopOfStack = ( portSTACK_TYPE ) 0xcc;\r
138         pxTopOfStack--;\r
139  \r
140         /* A register contains parameter high byte. */\r
141         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 0 );\r
142         pxTopOfStack--;\r
143 \r
144         /* B register contains parameter low byte. */\r
145         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 1 );\r
146         pxTopOfStack--;\r
147 \r
148         /* CCR: Note that when the task starts interrupts will be enabled since\r
149         "I" bit of CCR is cleared */\r
150         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;\r
151         pxTopOfStack--;\r
152         \r
153         #ifdef BANKED_MODEL\r
154                 /* The page of the task. */\r
155                 *pxTopOfStack = ( portSTACK_TYPE ) ( ( int ) pxCode );\r
156                 pxTopOfStack--;\r
157         #endif\r
158         \r
159         /* Finally the critical nesting depth is initialised with 0 (not within\r
160         a critical section). */\r
161         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;\r
162 \r
163         return pxTopOfStack;\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 void vPortEndScheduler( void )\r
168 {\r
169         /* It is unlikely that the HCS12 port will get stopped. */\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 static void prvSetupTimerInterrupt( void )\r
174 {\r
175         TickTimer_SetFreqHz( configTICK_RATE_HZ );\r
176         TickTimer_Enable();\r
177 }\r
178 /*-----------------------------------------------------------*/\r
179 \r
180 portBASE_TYPE xPortStartScheduler( void )\r
181 {\r
182         /* xPortStartScheduler() does not start the scheduler directly because \r
183         the header file containing the xPortStartScheduler() prototype is part \r
184         of the common kernel code, and therefore cannot use the CODE_SEG pragma. \r
185         Instead it simply calls the locally defined xBankedStartScheduler() - \r
186         which does use the CODE_SEG pragma. */\r
187 \r
188         return xBankedStartScheduler();\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
193 \r
194 static portBASE_TYPE xBankedStartScheduler( void )\r
195 {\r
196         /* Configure the timer that will generate the RTOS tick.  Interrupts are\r
197         disabled when this function is called. */\r
198         prvSetupTimerInterrupt();\r
199 \r
200         /* Restore the context of the first task. */\r
201         portRESTORE_CONTEXT();\r
202 \r
203         /* Simulate the end of an interrupt to start the scheduler off. */\r
204         __asm( "rti" );\r
205 \r
206         /* Should not get here! */\r
207         return pdFALSE;\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 /*\r
212  * Context switch functions.  These are both interrupt service routines.\r
213  */\r
214 \r
215 /*\r
216  * Manual context switch forced by calling portYIELD().  This is the SWI\r
217  * handler.\r
218  */\r
219 void interrupt vPortYield( void )\r
220 {\r
221         portSAVE_CONTEXT();\r
222         vTaskSwitchContext();\r
223         portRESTORE_CONTEXT();\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 /*\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
231  */\r
232 void interrupt vPortTickInterrupt( void )\r
233 {\r
234         #if configUSE_PREEMPTION == 1\r
235         {\r
236                 /* A context switch might happen so save the context. */\r
237                 portSAVE_CONTEXT();\r
238 \r
239                 /* Increment the tick ... */\r
240                 vTaskIncrementTick();\r
241 \r
242                 /* ... then see if the new tick value has necessitated a\r
243                 context switch. */\r
244                 vTaskSwitchContext();\r
245 \r
246                 TFLG1 = 1;                                                                 \r
247 \r
248                 /* Restore the context of a task - which may be a different task\r
249                 to that interrupted. */\r
250                 portRESTORE_CONTEXT();  \r
251         }\r
252         #else\r
253         {\r
254                 vTaskIncrementTick();\r
255                 TFLG1 = 1;\r
256         }\r
257         #endif\r
258 }\r
259 \r
260 #pragma CODE_SEG DEFAULT\r
261 \r
262 \r