]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/CodeWarrior/HCS12/port.c
Convert some ports to use xTaskIncrementTick() in place of vTaskIncrementTick().
[freertos] / FreeRTOS / Source / portable / CodeWarrior / 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 /* Scheduler includes. */\r
76 #include "FreeRTOS.h"\r
77 #include "task.h"\r
78 \r
79 \r
80 /*-----------------------------------------------------------\r
81  * Implementation of functions defined in portable.h for the HCS12 port.\r
82  *----------------------------------------------------------*/\r
83 \r
84 \r
85 /*\r
86  * Configure a timer to generate the RTOS tick at the frequency specified \r
87  * within FreeRTOSConfig.h.\r
88  */\r
89 static void prvSetupTimerInterrupt( void );\r
90 \r
91 /* Interrupt service routines have to be in non-banked memory - as does the\r
92 scheduler startup function. */\r
93 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
94 \r
95         /* Manual context switch function.  This is the SWI ISR. */\r
96         void interrupt vPortYield( void );\r
97 \r
98         /* Tick context switch function.  This is the timer ISR. */\r
99         void interrupt vPortTickInterrupt( void );\r
100         \r
101         /* Simply called by xPortStartScheduler().  xPortStartScheduler() does not\r
102         start the scheduler directly because the header file containing the \r
103         xPortStartScheduler() prototype is part of the common kernel code, and \r
104         therefore cannot use the CODE_SEG pragma. */\r
105         static portBASE_TYPE xBankedStartScheduler( void );\r
106 \r
107 #pragma CODE_SEG DEFAULT\r
108 \r
109 /* Calls to portENTER_CRITICAL() can be nested.  When they are nested the \r
110 critical section should not be left (i.e. interrupts should not be re-enabled)\r
111 until the nesting depth reaches 0.  This variable simply tracks the nesting \r
112 depth.  Each task maintains it's own critical nesting depth variable so \r
113 uxCriticalNesting is saved and restored from the task stack during a context\r
114 switch. */\r
115 volatile unsigned portBASE_TYPE uxCriticalNesting = 0xff;\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* \r
120  * See header file for description. \r
121  */\r
122 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
123 {\r
124         /* \r
125                 Place a few bytes of known values on the bottom of the stack.\r
126                 This can be uncommented to provide useful stack markers when debugging.\r
127 \r
128                 *pxTopOfStack = ( portSTACK_TYPE ) 0x11;\r
129                 pxTopOfStack--;\r
130                 *pxTopOfStack = ( portSTACK_TYPE ) 0x22;\r
131                 pxTopOfStack--;\r
132                 *pxTopOfStack = ( portSTACK_TYPE ) 0x33;\r
133                 pxTopOfStack--;\r
134         */\r
135 \r
136 \r
137 \r
138         /* Setup the initial stack of the task.  The stack is set exactly as \r
139         expected by the portRESTORE_CONTEXT() macro.  In this case the stack as\r
140         expected by the HCS12 RTI instruction. */\r
141 \r
142 \r
143         /* The address of the task function is placed in the stack byte at a time. */\r
144         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 1 );\r
145         pxTopOfStack--;\r
146         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pxCode) ) + 0 );\r
147         pxTopOfStack--;\r
148 \r
149         /* Next are all the registers that form part of the task context. */\r
150 \r
151         /* Y register */\r
152         *pxTopOfStack = ( portSTACK_TYPE ) 0xff;\r
153         pxTopOfStack--;\r
154         *pxTopOfStack = ( portSTACK_TYPE ) 0xee;\r
155         pxTopOfStack--;\r
156 \r
157         /* X register */\r
158         *pxTopOfStack = ( portSTACK_TYPE ) 0xdd;\r
159         pxTopOfStack--;\r
160         *pxTopOfStack = ( portSTACK_TYPE ) 0xcc;\r
161         pxTopOfStack--;\r
162  \r
163         /* A register contains parameter high byte. */\r
164         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 0 );\r
165         pxTopOfStack--;\r
166 \r
167         /* B register contains parameter low byte. */\r
168         *pxTopOfStack = ( portSTACK_TYPE ) *( ((portSTACK_TYPE *) (&pvParameters) ) + 1 );\r
169         pxTopOfStack--;\r
170 \r
171         /* CCR: Note that when the task starts interrupts will be enabled since\r
172         "I" bit of CCR is cleared */\r
173         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;\r
174         pxTopOfStack--;\r
175         \r
176         #ifdef BANKED_MODEL\r
177                 /* The page of the task. */\r
178                 *pxTopOfStack = ( portSTACK_TYPE ) ( ( int ) pxCode );\r
179                 pxTopOfStack--;\r
180         #endif\r
181         \r
182         /* Finally the critical nesting depth is initialised with 0 (not within\r
183         a critical section). */\r
184         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;\r
185 \r
186         return pxTopOfStack;\r
187 }\r
188 /*-----------------------------------------------------------*/\r
189 \r
190 void vPortEndScheduler( void )\r
191 {\r
192         /* It is unlikely that the HCS12 port will get stopped. */\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 static void prvSetupTimerInterrupt( void )\r
197 {\r
198         TickTimer_SetFreqHz( configTICK_RATE_HZ );\r
199         TickTimer_Enable();\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 portBASE_TYPE xPortStartScheduler( void )\r
204 {\r
205         /* xPortStartScheduler() does not start the scheduler directly because \r
206         the header file containing the xPortStartScheduler() prototype is part \r
207         of the common kernel code, and therefore cannot use the CODE_SEG pragma. \r
208         Instead it simply calls the locally defined xBankedStartScheduler() - \r
209         which does use the CODE_SEG pragma. */\r
210 \r
211         return xBankedStartScheduler();\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
216 \r
217 static portBASE_TYPE xBankedStartScheduler( void )\r
218 {\r
219         /* Configure the timer that will generate the RTOS tick.  Interrupts are\r
220         disabled when this function is called. */\r
221         prvSetupTimerInterrupt();\r
222 \r
223         /* Restore the context of the first task. */\r
224         portRESTORE_CONTEXT();\r
225 \r
226         /* Simulate the end of an interrupt to start the scheduler off. */\r
227         __asm( "rti" );\r
228 \r
229         /* Should not get here! */\r
230         return pdFALSE;\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 /*\r
235  * Context switch functions.  These are both interrupt service routines.\r
236  */\r
237 \r
238 /*\r
239  * Manual context switch forced by calling portYIELD().  This is the SWI\r
240  * handler.\r
241  */\r
242 void interrupt vPortYield( void )\r
243 {\r
244         portSAVE_CONTEXT();\r
245         vTaskSwitchContext();\r
246         portRESTORE_CONTEXT();\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 /*\r
251  * RTOS tick interrupt service routine.  If the cooperative scheduler is \r
252  * being used then this simply increments the tick count.  If the \r
253  * preemptive scheduler is being used a context switch can occur.\r
254  */\r
255 void interrupt vPortTickInterrupt( void )\r
256 {\r
257         #if configUSE_PREEMPTION == 1\r
258         {\r
259                 /* A context switch might happen so save the context. */\r
260                 portSAVE_CONTEXT();\r
261 \r
262                 /* Increment the tick ... */\r
263                 if( xTaskIncrementTick() != pdFALSE )\r
264                 {\r
265                         vTaskSwitchContext();\r
266                 }\r
267 \r
268                 TFLG1 = 1;                                                                 \r
269 \r
270                 /* Restore the context of a task - which may be a different task\r
271                 to that interrupted. */\r
272                 portRESTORE_CONTEXT();  \r
273         }\r
274         #else\r
275         {\r
276                 xTaskIncrementTick();\r
277                 TFLG1 = 1;\r
278         }\r
279         #endif\r
280 }\r
281 \r
282 #pragma CODE_SEG DEFAULT\r
283 \r
284 \r