]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/CodeWarrior/HCS12/port.c
Add FreeRTOS-Plus directory.
[freertos] / FreeRTOS / Source / portable / CodeWarrior / HCS12 / port.c
1 /*\r
2     FreeRTOS V7.1.1 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43     \r
44     ***************************************************************************\r
45      *                                                                       *\r
46      *    Having a problem?  Start by reading the FAQ "My application does   *\r
47      *    not run, what could be wrong?                                      *\r
48      *                                                                       *\r
49      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
50      *                                                                       *\r
51     ***************************************************************************\r
52 \r
53     \r
54     http://www.FreeRTOS.org - Documentation, training, latest information, \r
55     license and contact details.\r
56     \r
57     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
58     including FreeRTOS+Trace - an indispensable productivity tool.\r
59 \r
60     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
61     the code with commercial support, indemnification, and middleware, under \r
62     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
63     provide a safety engineered and independently SIL3 certified version under \r
64     the SafeRTOS brand: http://www.SafeRTOS.com.\r
65 */\r
66 \r
67 /* Scheduler includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 \r
71 \r
72 /*-----------------------------------------------------------\r
73  * Implementation of functions defined in portable.h for the HCS12 port.\r
74  *----------------------------------------------------------*/\r
75 \r
76 \r
77 /*\r
78  * Configure a timer to generate the RTOS tick at the frequency specified \r
79  * within FreeRTOSConfig.h.\r
80  */\r
81 static void prvSetupTimerInterrupt( void );\r
82 \r
83 /* Interrupt service routines have to be in non-banked memory - as does the\r
84 scheduler startup function. */\r
85 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
86 \r
87         /* Manual context switch function.  This is the SWI ISR. */\r
88         void interrupt vPortYield( void );\r
89 \r
90         /* Tick context switch function.  This is the timer ISR. */\r
91         void interrupt vPortTickInterrupt( void );\r
92         \r
93         /* Simply called by xPortStartScheduler().  xPortStartScheduler() does not\r
94         start the scheduler directly because the header file containing the \r
95         xPortStartScheduler() prototype is part of the common kernel code, and \r
96         therefore cannot use the CODE_SEG pragma. */\r
97         static portBASE_TYPE xBankedStartScheduler( void );\r
98 \r
99 #pragma CODE_SEG DEFAULT\r
100 \r
101 /* Calls to portENTER_CRITICAL() can be nested.  When they are nested the \r
102 critical section should not be left (i.e. interrupts should not be re-enabled)\r
103 until the nesting depth reaches 0.  This variable simply tracks the nesting \r
104 depth.  Each task maintains it's own critical nesting depth variable so \r
105 uxCriticalNesting is saved and restored from the task stack during a context\r
106 switch. */\r
107 volatile unsigned portBASE_TYPE uxCriticalNesting = 0xff;\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /* \r
112  * See header file for description. \r
113  */\r
114 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
115 {\r
116         /* \r
117                 Place a few bytes of known values on the bottom of the stack.\r
118                 This can be uncommented to provide useful stack markers when debugging.\r
119 \r
120                 *pxTopOfStack = ( portSTACK_TYPE ) 0x11;\r
121                 pxTopOfStack--;\r
122                 *pxTopOfStack = ( portSTACK_TYPE ) 0x22;\r
123                 pxTopOfStack--;\r
124                 *pxTopOfStack = ( portSTACK_TYPE ) 0x33;\r
125                 pxTopOfStack--;\r
126         */\r
127 \r
128 \r
129 \r
130         /* Setup the initial stack of the task.  The stack is set exactly as \r
131         expected by the portRESTORE_CONTEXT() macro.  In this case the stack as\r
132         expected by the HCS12 RTI instruction. */\r
133 \r
134 \r
135         /* The address of the task function is placed in the stack byte at a time. */\r
136         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 1 );\r
137         pxTopOfStack--;\r
138         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 0 );\r
139         pxTopOfStack--;\r
140 \r
141         /* Next are all the registers that form part of the task context. */\r
142 \r
143         /* Y register */\r
144         *pxTopOfStack = ( portSTACK_TYPE ) 0xff;\r
145         pxTopOfStack--;\r
146         *pxTopOfStack = ( portSTACK_TYPE ) 0xee;\r
147         pxTopOfStack--;\r
148 \r
149         /* X register */\r
150         *pxTopOfStack = ( portSTACK_TYPE ) 0xdd;\r
151         pxTopOfStack--;\r
152         *pxTopOfStack = ( portSTACK_TYPE ) 0xcc;\r
153         pxTopOfStack--;\r
154  \r
155         /* A register contains parameter high byte. */\r
156         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 0 );\r
157         pxTopOfStack--;\r
158 \r
159         /* B register contains parameter low byte. */\r
160         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 1 );\r
161         pxTopOfStack--;\r
162 \r
163         /* CCR: Note that when the task starts interrupts will be enabled since\r
164         "I" bit of CCR is cleared */\r
165         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;\r
166         pxTopOfStack--;\r
167         \r
168         #ifdef BANKED_MODEL\r
169                 /* The page of the task. */\r
170                 *pxTopOfStack = ( portSTACK_TYPE ) ( ( int ) pxCode );\r
171                 pxTopOfStack--;\r
172         #endif\r
173         \r
174         /* Finally the critical nesting depth is initialised with 0 (not within\r
175         a critical section). */\r
176         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;\r
177 \r
178         return pxTopOfStack;\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 void vPortEndScheduler( void )\r
183 {\r
184         /* It is unlikely that the HCS12 port will get stopped. */\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 static void prvSetupTimerInterrupt( void )\r
189 {\r
190         TickTimer_SetFreqHz( configTICK_RATE_HZ );\r
191         TickTimer_Enable();\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 portBASE_TYPE xPortStartScheduler( void )\r
196 {\r
197         /* xPortStartScheduler() does not start the scheduler directly because \r
198         the header file containing the xPortStartScheduler() prototype is part \r
199         of the common kernel code, and therefore cannot use the CODE_SEG pragma. \r
200         Instead it simply calls the locally defined xBankedStartScheduler() - \r
201         which does use the CODE_SEG pragma. */\r
202 \r
203         return xBankedStartScheduler();\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
208 \r
209 static portBASE_TYPE xBankedStartScheduler( void )\r
210 {\r
211         /* Configure the timer that will generate the RTOS tick.  Interrupts are\r
212         disabled when this function is called. */\r
213         prvSetupTimerInterrupt();\r
214 \r
215         /* Restore the context of the first task. */\r
216         portRESTORE_CONTEXT();\r
217 \r
218         /* Simulate the end of an interrupt to start the scheduler off. */\r
219         __asm( "rti" );\r
220 \r
221         /* Should not get here! */\r
222         return pdFALSE;\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 /*\r
227  * Context switch functions.  These are both interrupt service routines.\r
228  */\r
229 \r
230 /*\r
231  * Manual context switch forced by calling portYIELD().  This is the SWI\r
232  * handler.\r
233  */\r
234 void interrupt vPortYield( void )\r
235 {\r
236         portSAVE_CONTEXT();\r
237         vTaskSwitchContext();\r
238         portRESTORE_CONTEXT();\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 /*\r
243  * RTOS tick interrupt service routine.  If the cooperative scheduler is \r
244  * being used then this simply increments the tick count.  If the \r
245  * preemptive scheduler is being used a context switch can occur.\r
246  */\r
247 void interrupt vPortTickInterrupt( void )\r
248 {\r
249         #if configUSE_PREEMPTION == 1\r
250         {\r
251                 /* A context switch might happen so save the context. */\r
252                 portSAVE_CONTEXT();\r
253 \r
254                 /* Increment the tick ... */\r
255                 vTaskIncrementTick();\r
256 \r
257                 /* ... then see if the new tick value has necessitated a\r
258                 context switch. */\r
259                 vTaskSwitchContext();\r
260 \r
261                 TFLG1 = 1;                                                                 \r
262 \r
263                 /* Restore the context of a task - which may be a different task\r
264                 to that interrupted. */\r
265                 portRESTORE_CONTEXT();  \r
266         }\r
267         #else\r
268         {\r
269                 vTaskIncrementTick();\r
270                 TFLG1 = 1;\r
271         }\r
272         #endif\r
273 }\r
274 \r
275 #pragma CODE_SEG DEFAULT\r
276 \r
277 \r