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