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