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