]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/HCS12/port.c
Update to V6.1.1
[freertos] / Source / portable / GCC / HCS12 / port.c
1 /*\r
2     FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3 \r
4     ***************************************************************************\r
5     *                                                                         *\r
6     * If you are:                                                             *\r
7     *                                                                         *\r
8     *    + New to FreeRTOS,                                                   *\r
9     *    + Wanting to learn FreeRTOS or multitasking in general quickly       *\r
10     *    + Looking for basic training,                                        *\r
11     *    + Wanting to improve your FreeRTOS skills and productivity           *\r
12     *                                                                         *\r
13     * then take a look at the FreeRTOS books - available as PDF or paperback  *\r
14     *                                                                         *\r
15     *        "Using the FreeRTOS Real Time Kernel - a Practical Guide"        *\r
16     *                  http://www.FreeRTOS.org/Documentation                  *\r
17     *                                                                         *\r
18     * A pdf reference manual is also available.  Both are usually delivered   *\r
19     * to your inbox within 20 minutes to two hours when purchased between 8am *\r
20     * and 8pm GMT (although please allow up to 24 hours in case of            *\r
21     * exceptional circumstances).  Thank you for your support!                *\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 exception to the GPL is included to allow you to distribute\r
31     a combined work that includes FreeRTOS without being obliged to provide the\r
32     source code for proprietary components outside of the FreeRTOS kernel.\r
33     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT\r
34     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
35     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     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* GCC/HCS12 port by Jefferson L Smith, 2005 */\r
55 \r
56 /* Scheduler includes. */\r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 \r
60 /* Port includes */\r
61 #include <sys/ports_def.h>\r
62 \r
63 /*-----------------------------------------------------------\r
64  * Implementation of functions defined in portable.h for the HCS12 port.\r
65  *----------------------------------------------------------*/\r
66 \r
67 \r
68 /*\r
69  * Configure a timer to generate the RTOS tick at the frequency specified \r
70  * within FreeRTOSConfig.h.\r
71  */\r
72 static void prvSetupTimerInterrupt( void );\r
73 \r
74 /* NOTE: Interrupt service routines must be in non-banked memory - as does the\r
75 scheduler startup function. */\r
76 #define ATTR_NEAR       __attribute__((near))\r
77 \r
78 /* Manual context switch function.  This is the SWI ISR. */\r
79 // __attribute__((interrupt))\r
80 void ATTR_NEAR vPortYield( void );\r
81 \r
82 /* Tick context switch function.  This is the timer ISR. */\r
83 // __attribute__((interrupt))\r
84 void ATTR_NEAR vPortTickInterrupt( void );\r
85 \r
86 /* Function in non-banked memory which actually switches to first task. */\r
87 portBASE_TYPE ATTR_NEAR xStartSchedulerNear( void );\r
88 \r
89 /* Calls to portENTER_CRITICAL() can be nested.  When they are nested the \r
90 critical section should not be left (i.e. interrupts should not be re-enabled)\r
91 until the nesting depth reaches 0.  This variable simply tracks the nesting \r
92 depth.  Each task maintains it's own critical nesting depth variable so \r
93 uxCriticalNesting is saved and restored from the task stack during a context\r
94 switch. */\r
95 volatile unsigned portBASE_TYPE uxCriticalNesting = 0x80;  // un-initialized\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 /* \r
100  * See header file for description. \r
101  */\r
102 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
103 {\r
104 \r
105 \r
106         /* Setup the initial stack of the task.  The stack is set exactly as \r
107         expected by the portRESTORE_CONTEXT() macro.  In this case the stack as\r
108         expected by the HCS12 RTI instruction. */\r
109 \r
110 \r
111         /* The address of the task function is placed in the stack byte at a time. */\r
112         *pxTopOfStack   = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 1 );\r
113         *--pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 0 );\r
114 \r
115         /* Next are all the registers that form part of the task context. */\r
116 \r
117         /* Y register */\r
118         *--pxTopOfStack = ( portSTACK_TYPE ) 0xff;\r
119         *--pxTopOfStack = ( portSTACK_TYPE ) 0xee;\r
120 \r
121         /* X register */\r
122         *--pxTopOfStack = ( portSTACK_TYPE ) 0xdd;\r
123         *--pxTopOfStack = ( portSTACK_TYPE ) 0xcc;\r
124  \r
125         /* A register contains parameter high byte. */\r
126         *--pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 0 );\r
127 \r
128         /* B register contains parameter low byte. */\r
129         *--pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 1 );\r
130 \r
131         /* CCR: Note that when the task starts interrupts will be enabled since\r
132         "I" bit of CCR is cleared */\r
133         *--pxTopOfStack = ( portSTACK_TYPE ) 0x80;              // keeps Stop disabled (MCU default)\r
134         \r
135         /* tmp softregs used by GCC. Values right now don't     matter. */\r
136         __asm("\n\\r
137                 movw _.frame, 2,-%0                                                     \n\\r
138                 movw _.tmp, 2,-%0                                                       \n\\r
139                 movw _.z, 2,-%0                                                         \n\\r
140                 movw _.xy, 2,-%0                                                        \n\\r
141                 ;movw _.d2, 2,-%0                                                       \n\\r
142                 ;movw _.d1, 2,-%0                                                       \n\\r
143         ": "=A"(pxTopOfStack) : "0"(pxTopOfStack) );\r
144 \r
145         #ifdef BANKED_MODEL\r
146                 /* The page of the task. */\r
147                 *--pxTopOfStack = 0x30;      // can only directly start in PPAGE 0x30\r
148         #endif\r
149         \r
150         /* The critical nesting depth is initialised with 0 (meaning not in\r
151         a critical section). */\r
152         *--pxTopOfStack = ( portSTACK_TYPE ) 0x00;\r
153 \r
154 \r
155         return pxTopOfStack;\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 void vPortEndScheduler( void )\r
160 {\r
161         /* It is unlikely that the HCS12 port will get stopped. */\r
162 }\r
163 /*-----------------------------------------------------------*/\r
164 \r
165 static void prvSetupTimerInterrupt( void )\r
166 {\r
167         /* Enable hardware RTI timer */\r
168         /* Ignores configTICK_RATE_HZ */\r
169         RTICTL = 0x50;                  // 16 MHz xtal: 976.56 Hz, 1024mS \r
170         CRGINT |= 0x80;                 // RTIE\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 portBASE_TYPE xPortStartScheduler( void )\r
175 {\r
176         /* xPortStartScheduler() does not start the scheduler directly because \r
177         the header file containing the xPortStartScheduler() prototype is part \r
178         of the common kernel code, and therefore cannot use the CODE_SEG pragma. \r
179         Instead it simply calls the locally defined xNearStartScheduler() - \r
180         which does use the CODE_SEG pragma. */\r
181 \r
182         short register d;\r
183         __asm ("jmp  xStartSchedulerNear                ; will never return": "=d"(d));\r
184         return d;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 portBASE_TYPE xStartSchedulerNear( void )\r
189 {\r
190         /* Configure the timer that will generate the RTOS tick.  Interrupts are\r
191         disabled when this function is called. */\r
192         prvSetupTimerInterrupt();\r
193 \r
194         /* Restore the context of the first task. */\r
195         portRESTORE_CONTEXT();\r
196 \r
197         portISR_TAIL();\r
198 \r
199         /* Should not get here! */\r
200         return pdFALSE;\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 /*\r
205  * Context switch functions.  These are interrupt service routines.\r
206  */\r
207 \r
208 /*\r
209  * Manual context switch forced by calling portYIELD().  This is the SWI\r
210  * handler.\r
211  */\r
212 void vPortYield( void )\r
213 {\r
214         portISR_HEAD();\r
215         /* NOTE: This is the trap routine (swi) although not defined as a trap.\r
216            It will fill the stack the same way as an ISR in order to mix preemtion\r
217            and cooperative yield. */\r
218 \r
219         portSAVE_CONTEXT();\r
220         vTaskSwitchContext();\r
221         portRESTORE_CONTEXT();\r
222 \r
223         portISR_TAIL();\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 /*\r
228  * RTOS tick interrupt service routine.  If the cooperative scheduler is \r
229  * being used then this simply increments the tick count.  If the \r
230  * preemptive scheduler is being used a context switch can occur.\r
231  */\r
232 void vPortTickInterrupt( void )\r
233 {\r
234         portISR_HEAD();\r
235 \r
236         /* Clear tick timer flag */\r
237         CRGFLG = 0x80;\r
238 \r
239         #if configUSE_PREEMPTION == 1\r
240         {\r
241                 /* A context switch might happen so save the context. */\r
242                 portSAVE_CONTEXT();\r
243 \r
244                 /* Increment the tick ... */\r
245                 vTaskIncrementTick();\r
246 \r
247                 /* ... then see if the new tick value has necessitated a\r
248                 context switch. */\r
249                 vTaskSwitchContext();\r
250 \r
251                 /* Restore the context of a task - which may be a different task\r
252                 to that interrupted. */\r
253                 portRESTORE_CONTEXT();\r
254         }\r
255         #else\r
256         {\r
257                 vTaskIncrementTick();\r
258         }\r
259         #endif\r
260 \r
261         portISR_TAIL();\r
262 }\r
263 \r