]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/HCS12/port.c
678cba2c82e3d4f0cc9c70905fe48bf961e95ab5
[freertos] / FreeRTOS / Source / portable / GCC / HCS12 / port.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 /* GCC/HCS12 port by Jefferson L Smith, 2005 */\r
97 \r
98 /* Scheduler includes. */\r
99 #include "FreeRTOS.h"\r
100 #include "task.h"\r
101 \r
102 /* Port includes */\r
103 #include <sys/ports_def.h>\r
104 \r
105 /*-----------------------------------------------------------\r
106  * Implementation of functions defined in portable.h for the HCS12 port.\r
107  *----------------------------------------------------------*/\r
108 \r
109 \r
110 /*\r
111  * Configure a timer to generate the RTOS tick at the frequency specified \r
112  * within FreeRTOSConfig.h.\r
113  */\r
114 static void prvSetupTimerInterrupt( void );\r
115 \r
116 /* NOTE: Interrupt service routines must be in non-banked memory - as does the\r
117 scheduler startup function. */\r
118 #define ATTR_NEAR       __attribute__((near))\r
119 \r
120 /* Manual context switch function.  This is the SWI ISR. */\r
121 // __attribute__((interrupt))\r
122 void ATTR_NEAR vPortYield( void );\r
123 \r
124 /* Tick context switch function.  This is the timer ISR. */\r
125 // __attribute__((interrupt))\r
126 void ATTR_NEAR vPortTickInterrupt( void );\r
127 \r
128 /* Function in non-banked memory which actually switches to first task. */\r
129 BaseType_t ATTR_NEAR xStartSchedulerNear( void );\r
130 \r
131 /* Calls to portENTER_CRITICAL() can be nested.  When they are nested the \r
132 critical section should not be left (i.e. interrupts should not be re-enabled)\r
133 until the nesting depth reaches 0.  This variable simply tracks the nesting \r
134 depth.  Each task maintains it's own critical nesting depth variable so \r
135 uxCriticalNesting is saved and restored from the task stack during a context\r
136 switch. */\r
137 volatile UBaseType_t uxCriticalNesting = 0x80;  // un-initialized\r
138 \r
139 /*-----------------------------------------------------------*/\r
140 \r
141 /* \r
142  * See header file for description. \r
143  */\r
144 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
145 {\r
146 \r
147 \r
148         /* Setup the initial stack of the task.  The stack is set exactly as \r
149         expected by the portRESTORE_CONTEXT() macro.  In this case the stack as\r
150         expected by the HCS12 RTI instruction. */\r
151 \r
152 \r
153         /* The address of the task function is placed in the stack byte at a time. */\r
154         *pxTopOfStack   = ( StackType_t ) *( ((StackType_t *) (&pxCode) ) + 1 );\r
155         *--pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pxCode) ) + 0 );\r
156 \r
157         /* Next are all the registers that form part of the task context. */\r
158 \r
159         /* Y register */\r
160         *--pxTopOfStack = ( StackType_t ) 0xff;\r
161         *--pxTopOfStack = ( StackType_t ) 0xee;\r
162 \r
163         /* X register */\r
164         *--pxTopOfStack = ( StackType_t ) 0xdd;\r
165         *--pxTopOfStack = ( StackType_t ) 0xcc;\r
166  \r
167         /* A register contains parameter high byte. */\r
168         *--pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 0 );\r
169 \r
170         /* B register contains parameter low byte. */\r
171         *--pxTopOfStack = ( StackType_t ) *( ((StackType_t *) (&pvParameters) ) + 1 );\r
172 \r
173         /* CCR: Note that when the task starts interrupts will be enabled since\r
174         "I" bit of CCR is cleared */\r
175         *--pxTopOfStack = ( StackType_t ) 0x80;         // keeps Stop disabled (MCU default)\r
176         \r
177         /* tmp softregs used by GCC. Values right now don't     matter. */\r
178         __asm("\n\\r
179                 movw _.frame, 2,-%0                                                     \n\\r
180                 movw _.tmp, 2,-%0                                                       \n\\r
181                 movw _.z, 2,-%0                                                         \n\\r
182                 movw _.xy, 2,-%0                                                        \n\\r
183                 ;movw _.d2, 2,-%0                                                       \n\\r
184                 ;movw _.d1, 2,-%0                                                       \n\\r
185         ": "=A"(pxTopOfStack) : "0"(pxTopOfStack) );\r
186 \r
187         #ifdef BANKED_MODEL\r
188                 /* The page of the task. */\r
189                 *--pxTopOfStack = 0x30;      // can only directly start in PPAGE 0x30\r
190         #endif\r
191         \r
192         /* The critical nesting depth is initialised with 0 (meaning not in\r
193         a critical section). */\r
194         *--pxTopOfStack = ( StackType_t ) 0x00;\r
195 \r
196 \r
197         return pxTopOfStack;\r
198 }\r
199 /*-----------------------------------------------------------*/\r
200 \r
201 void vPortEndScheduler( void )\r
202 {\r
203         /* It is unlikely that the HCS12 port will get stopped. */\r
204 }\r
205 /*-----------------------------------------------------------*/\r
206 \r
207 static void prvSetupTimerInterrupt( void )\r
208 {\r
209         /* Enable hardware RTI timer */\r
210         /* Ignores configTICK_RATE_HZ */\r
211         RTICTL = 0x50;                  // 16 MHz xtal: 976.56 Hz, 1024mS \r
212         CRGINT |= 0x80;                 // RTIE\r
213 }\r
214 /*-----------------------------------------------------------*/\r
215 \r
216 BaseType_t xPortStartScheduler( void )\r
217 {\r
218         /* xPortStartScheduler() does not start the scheduler directly because \r
219         the header file containing the xPortStartScheduler() prototype is part \r
220         of the common kernel code, and therefore cannot use the CODE_SEG pragma. \r
221         Instead it simply calls the locally defined xNearStartScheduler() - \r
222         which does use the CODE_SEG pragma. */\r
223 \r
224         int16_t register d;\r
225         __asm ("jmp  xStartSchedulerNear                ; will never return": "=d"(d));\r
226         return d;\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 BaseType_t xStartSchedulerNear( void )\r
231 {\r
232         /* Configure the timer that will generate the RTOS tick.  Interrupts are\r
233         disabled when this function is called. */\r
234         prvSetupTimerInterrupt();\r
235 \r
236         /* Restore the context of the first task. */\r
237         portRESTORE_CONTEXT();\r
238 \r
239         portISR_TAIL();\r
240 \r
241         /* Should not get here! */\r
242         return pdFALSE;\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 /*\r
247  * Context switch functions.  These are interrupt service routines.\r
248  */\r
249 \r
250 /*\r
251  * Manual context switch forced by calling portYIELD().  This is the SWI\r
252  * handler.\r
253  */\r
254 void vPortYield( void )\r
255 {\r
256         portISR_HEAD();\r
257         /* NOTE: This is the trap routine (swi) although not defined as a trap.\r
258            It will fill the stack the same way as an ISR in order to mix preemtion\r
259            and cooperative yield. */\r
260 \r
261         portSAVE_CONTEXT();\r
262         vTaskSwitchContext();\r
263         portRESTORE_CONTEXT();\r
264 \r
265         portISR_TAIL();\r
266 }\r
267 /*-----------------------------------------------------------*/\r
268 \r
269 /*\r
270  * RTOS tick interrupt service routine.  If the cooperative scheduler is \r
271  * being used then this simply increments the tick count.  If the \r
272  * preemptive scheduler is being used a context switch can occur.\r
273  */\r
274 void vPortTickInterrupt( void )\r
275 {\r
276         portISR_HEAD();\r
277 \r
278         /* Clear tick timer flag */\r
279         CRGFLG = 0x80;\r
280 \r
281         #if configUSE_PREEMPTION == 1\r
282         {\r
283                 /* A context switch might happen so save the context. */\r
284                 portSAVE_CONTEXT();\r
285 \r
286                 /* Increment the tick ... */\r
287                 if( xTaskIncrementTick() != pdFALSE )\r
288                 {\r
289                         /* A context switch is necessary. */\r
290                         vTaskSwitchContext();\r
291                 }\r
292 \r
293                 /* Restore the context of a task - which may be a different task\r
294                 to that interrupted. */\r
295                 portRESTORE_CONTEXT();\r
296         }\r
297         #else\r
298         {\r
299                 xTaskIncrementTick();\r
300         }\r
301         #endif\r
302 \r
303         portISR_TAIL();\r
304 }\r
305 \r