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