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