]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/HCS12/port.c
Update to V4.3.0 as described in http://www.FreeRTOS.org/History.txt
[freertos] / Source / portable / GCC / 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 /* GCC/HCS12 port by Jefferson L Smith, 2005 */\r
37 \r
38 /* Scheduler includes. */\r
39 #include "FreeRTOS.h"\r
40 #include "task.h"\r
41 \r
42 /* Port includes */\r
43 #include <sys/ports_def.h>\r
44 \r
45 /*-----------------------------------------------------------\r
46  * Implementation of functions defined in portable.h for the HCS12 port.\r
47  *----------------------------------------------------------*/\r
48 \r
49 \r
50 /*\r
51  * Configure a timer to generate the RTOS tick at the frequency specified \r
52  * within FreeRTOSConfig.h.\r
53  */\r
54 static void prvSetupTimerInterrupt( void );\r
55 \r
56 /* NOTE: Interrupt service routines must be in non-banked memory - as does the\r
57 scheduler startup function. */\r
58 #define ATTR_NEAR       __attribute__((near))\r
59 \r
60 /* Manual context switch function.  This is the SWI ISR. */\r
61 // __attribute__((interrupt))\r
62 void ATTR_NEAR vPortYield( void );\r
63 \r
64 /* Tick context switch function.  This is the timer ISR. */\r
65 // __attribute__((interrupt))\r
66 void ATTR_NEAR vPortTickInterrupt( void );\r
67 \r
68 /* Function in non-banked memory which actually switches to first task. */\r
69 portBASE_TYPE ATTR_NEAR xStartSchedulerNear( void );\r
70 \r
71 /* Calls to portENTER_CRITICAL() can be nested.  When they are nested the \r
72 critical section should not be left (i.e. interrupts should not be re-enabled)\r
73 until the nesting depth reaches 0.  This variable simply tracks the nesting \r
74 depth.  Each task maintains it's own critical nesting depth variable so \r
75 uxCriticalNesting is saved and restored from the task stack during a context\r
76 switch. */\r
77 volatile unsigned portBASE_TYPE uxCriticalNesting = 0x80;  // un-initialized\r
78 \r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /* \r
82  * See header file for description. \r
83  */\r
84 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
85 {\r
86 \r
87 \r
88         /* Setup the initial stack of the task.  The stack is set exactly as \r
89         expected by the portRESTORE_CONTEXT() macro.  In this case the stack as\r
90         expected by the HCS12 RTI instruction. */\r
91 \r
92 \r
93         /* The address of the task function is placed in the stack byte at a time. */\r
94         *pxTopOfStack   = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 1 );\r
95         *--pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 0 );\r
96 \r
97         /* Next are all the registers that form part of the task context. */\r
98 \r
99         /* Y register */\r
100         *--pxTopOfStack = ( portSTACK_TYPE ) 0xff;\r
101         *--pxTopOfStack = ( portSTACK_TYPE ) 0xee;\r
102 \r
103         /* X register */\r
104         *--pxTopOfStack = ( portSTACK_TYPE ) 0xdd;\r
105         *--pxTopOfStack = ( portSTACK_TYPE ) 0xcc;\r
106  \r
107         /* A register contains parameter high byte. */\r
108         *--pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 0 );\r
109 \r
110         /* B register contains parameter low byte. */\r
111         *--pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 1 );\r
112 \r
113         /* CCR: Note that when the task starts interrupts will be enabled since\r
114         "I" bit of CCR is cleared */\r
115         *--pxTopOfStack = ( portSTACK_TYPE ) 0x80;              // keeps Stop disabled (MCU default)\r
116         \r
117         /* tmp softregs used by GCC. Values right now don't     matter. */\r
118         __asm("\n\\r
119                 movw _.frame, 2,-%0                                                     \n\\r
120                 movw _.tmp, 2,-%0                                                       \n\\r
121                 movw _.z, 2,-%0                                                         \n\\r
122                 movw _.xy, 2,-%0                                                        \n\\r
123                 ;movw _.d2, 2,-%0                                                       \n\\r
124                 ;movw _.d1, 2,-%0                                                       \n\\r
125         ": "=A"(pxTopOfStack) : "0"(pxTopOfStack) );\r
126 \r
127         #ifdef BANKED_MODEL\r
128                 /* The page of the task. */\r
129                 *--pxTopOfStack = 0x30;      // can only directly start in PPAGE 0x30\r
130         #endif\r
131         \r
132         /* The critical nesting depth is initialised with 0 (meaning not in\r
133         a critical section). */\r
134         *--pxTopOfStack = ( portSTACK_TYPE ) 0x00;\r
135 \r
136 \r
137         return pxTopOfStack;\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 void vPortEndScheduler( void )\r
142 {\r
143         /* It is unlikely that the HCS12 port will get stopped. */\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 static void prvSetupTimerInterrupt( void )\r
148 {\r
149         /* Enable hardware RTI timer */\r
150         /* Ignores configTICK_RATE_HZ */\r
151         RTICTL = 0x50;                  // 16 MHz xtal: 976.56 Hz, 1024mS \r
152         CRGINT |= 0x80;                 // RTIE\r
153 }\r
154 /*-----------------------------------------------------------*/\r
155 \r
156 portBASE_TYPE xPortStartScheduler( void )\r
157 {\r
158         /* xPortStartScheduler() does not start the scheduler directly because \r
159         the header file containing the xPortStartScheduler() prototype is part \r
160         of the common kernel code, and therefore cannot use the CODE_SEG pragma. \r
161         Instead it simply calls the locally defined xNearStartScheduler() - \r
162         which does use the CODE_SEG pragma. */\r
163 \r
164         short register d;\r
165         __asm ("jmp  xStartSchedulerNear                ; will never return": "=d"(d));\r
166         return d;\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 portBASE_TYPE xStartSchedulerNear( void )\r
171 {\r
172         /* Configure the timer that will generate the RTOS tick.  Interrupts are\r
173         disabled when this function is called. */\r
174         prvSetupTimerInterrupt();\r
175 \r
176         /* Restore the context of the first task. */\r
177         portRESTORE_CONTEXT();\r
178 \r
179         portISR_TAIL();\r
180 \r
181         /* Should not get here! */\r
182         return pdFALSE;\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r
186 /*\r
187  * Context switch functions.  These are interrupt service routines.\r
188  */\r
189 \r
190 /*\r
191  * Manual context switch forced by calling portYIELD().  This is the SWI\r
192  * handler.\r
193  */\r
194 void vPortYield( void )\r
195 {\r
196         portISR_HEAD();\r
197         /* NOTE: This is the trap routine (swi) although not defined as a trap.\r
198            It will fill the stack the same way as an ISR in order to mix preemtion\r
199            and cooperative yield. */\r
200 \r
201         portSAVE_CONTEXT();\r
202         vTaskSwitchContext();\r
203         portRESTORE_CONTEXT();\r
204 \r
205         portISR_TAIL();\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 /*\r
210  * RTOS tick interrupt service routine.  If the cooperative scheduler is \r
211  * being used then this simply increments the tick count.  If the \r
212  * preemptive scheduler is being used a context switch can occur.\r
213  */\r
214 void vPortTickInterrupt( void )\r
215 {\r
216         portISR_HEAD();\r
217 \r
218         /* Clear tick timer flag */\r
219         CRGFLG = 0x80;\r
220 \r
221         #if configUSE_PREEMPTION == 1\r
222         {\r
223                 /* A context switch might happen so save the context. */\r
224                 portSAVE_CONTEXT();\r
225 \r
226                 /* Increment the tick ... */\r
227                 vTaskIncrementTick();\r
228 \r
229                 /* ... then see if the new tick value has necessitated a\r
230                 context switch. */\r
231                 vTaskSwitchContext();\r
232 \r
233                 /* Restore the context of a task - which may be a different task\r
234                 to that interrupted. */\r
235                 portRESTORE_CONTEXT();\r
236         }\r
237         #else\r
238         {\r
239                 vTaskIncrementTick();\r
240         }\r
241         #endif\r
242 \r
243         portISR_TAIL();\r
244 }\r
245 \r