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