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