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