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