]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/H8S2329/port.c
Prepare for V7.3.0 release.
[freertos] / FreeRTOS / Source / portable / GCC / H8S2329 / port.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 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     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /* Scheduler includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 \r
73 \r
74 /*-----------------------------------------------------------\r
75  * Implementation of functions defined in portable.h for the H8S port.\r
76  *----------------------------------------------------------*/\r
77 \r
78 \r
79 /*-----------------------------------------------------------*/\r
80 \r
81 /* When the task starts interrupts should be enabled. */\r
82 #define portINITIAL_CCR                 ( ( portSTACK_TYPE ) 0x00 )\r
83 \r
84 /* Hardware specific constants used to generate the RTOS tick from the TPU. */\r
85 #define portCLEAR_ON_TGRA_COMPARE_MATCH ( ( unsigned char ) 0x20 )\r
86 #define portCLOCK_DIV_64                                ( ( unsigned char ) 0x03 )\r
87 #define portCLOCK_DIV                                   ( ( unsigned long ) 64 )\r
88 #define portTGRA_INTERRUPT_ENABLE               ( ( unsigned char ) 0x01 )\r
89 #define portTIMER_CHANNEL                               ( ( unsigned char ) 0x02 )\r
90 #define portMSTP13                                              ( ( unsigned short ) 0x2000 )\r
91 \r
92 /*\r
93  * Setup TPU channel one for the RTOS tick at the requested frequency.\r
94  */\r
95 static void prvSetupTimerInterrupt( void );\r
96 \r
97 /*\r
98  * The ISR used by portYIELD(). This is installed as a trap handler.\r
99  */\r
100 void vPortYield( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /* \r
105  * See header file for description. \r
106  */\r
107 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
108 {\r
109 unsigned long ulValue;\r
110 \r
111         /* This requires an even address. */\r
112         ulValue = ( unsigned long ) pxTopOfStack;\r
113         if( ulValue & 1UL )\r
114         {\r
115                 pxTopOfStack = pxTopOfStack - 1;\r
116         }\r
117 \r
118         /* Place a few bytes of known values on the bottom of the stack. \r
119         This is just useful for debugging. */\r
120         pxTopOfStack--;\r
121         *pxTopOfStack = 0xaa;\r
122         pxTopOfStack--;\r
123         *pxTopOfStack = 0xbb;\r
124         pxTopOfStack--;\r
125         *pxTopOfStack = 0xcc;\r
126         pxTopOfStack--;\r
127         *pxTopOfStack = 0xdd;\r
128 \r
129         /* The initial stack mimics an interrupt stack.  First there is the program\r
130         counter (24 bits). */\r
131         ulValue = ( unsigned long ) pxCode;\r
132 \r
133         pxTopOfStack--;\r
134         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
135         pxTopOfStack--;\r
136         ulValue >>= 8UL;\r
137         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
138         pxTopOfStack--;\r
139         ulValue >>= 8UL;\r
140         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
141 \r
142         /* Followed by the CCR. */      \r
143         pxTopOfStack--;\r
144         *pxTopOfStack = portINITIAL_CCR;\r
145 \r
146         /* Next all the general purpose registers - with the parameters being passed\r
147         in ER0.  The parameter order must match that used by the compiler when the\r
148         "saveall" function attribute is used. */\r
149 \r
150         /* ER6 */\r
151         pxTopOfStack--;\r
152         *pxTopOfStack = 0x66;\r
153         pxTopOfStack--;\r
154         *pxTopOfStack = 0x66;\r
155         pxTopOfStack--;\r
156         *pxTopOfStack = 0x66;\r
157         pxTopOfStack--;\r
158         *pxTopOfStack = 0x66;\r
159         \r
160         /* ER0 */\r
161         ulValue = ( unsigned long ) pvParameters;\r
162 \r
163         pxTopOfStack--;\r
164         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
165         pxTopOfStack--;\r
166         ulValue >>= 8UL;\r
167         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
168         pxTopOfStack--;\r
169         ulValue >>= 8UL;\r
170         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
171         pxTopOfStack--;\r
172         ulValue >>= 8UL;\r
173         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
174         \r
175         /* ER1 */\r
176         pxTopOfStack--;\r
177         *pxTopOfStack = 0x11;\r
178         pxTopOfStack--;\r
179         *pxTopOfStack = 0x11;\r
180         pxTopOfStack--;\r
181         *pxTopOfStack = 0x11;\r
182         pxTopOfStack--;\r
183         *pxTopOfStack = 0x11;\r
184 \r
185         /* ER2 */\r
186         pxTopOfStack--;\r
187         *pxTopOfStack = 0x22;\r
188         pxTopOfStack--;\r
189         *pxTopOfStack = 0x22;\r
190         pxTopOfStack--;\r
191         *pxTopOfStack = 0x22;\r
192         pxTopOfStack--;\r
193         *pxTopOfStack = 0x22;\r
194 \r
195         /* ER3 */\r
196         pxTopOfStack--;\r
197         *pxTopOfStack = 0x33;\r
198         pxTopOfStack--;\r
199         *pxTopOfStack = 0x33;\r
200         pxTopOfStack--;\r
201         *pxTopOfStack = 0x33;\r
202         pxTopOfStack--;\r
203         *pxTopOfStack = 0x33;\r
204 \r
205         /* ER4 */\r
206         pxTopOfStack--;\r
207         *pxTopOfStack = 0x44;\r
208         pxTopOfStack--;\r
209         *pxTopOfStack = 0x44;\r
210         pxTopOfStack--;\r
211         *pxTopOfStack = 0x44;\r
212         pxTopOfStack--;\r
213         *pxTopOfStack = 0x44;\r
214 \r
215         /* ER5 */\r
216         pxTopOfStack--;\r
217         *pxTopOfStack = 0x55;\r
218         pxTopOfStack--;\r
219         *pxTopOfStack = 0x55;\r
220         pxTopOfStack--;\r
221         *pxTopOfStack = 0x55;\r
222         pxTopOfStack--;\r
223         *pxTopOfStack = 0x55;\r
224 \r
225         return pxTopOfStack;\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 portBASE_TYPE xPortStartScheduler( void )\r
230 {\r
231 extern void * pxCurrentTCB;\r
232 \r
233         /* Setup the hardware to generate the tick. */\r
234         prvSetupTimerInterrupt();\r
235 \r
236         /* Restore the context of the first task that is going to run.  This\r
237         mirrors the function epilogue code generated by the compiler when the\r
238         "saveall" function attribute is used. */\r
239         asm volatile ( \r
240                                         "MOV.L          @_pxCurrentTCB, ER6                     \n\t"\r
241                                         "MOV.L          @ER6, ER7                                       \n\t"\r
242                                         "LDM.L          @SP+, (ER4-ER5)                         \n\t"\r
243                                         "LDM.L          @SP+, (ER0-ER3)                         \n\t"\r
244                                         "MOV.L          @ER7+, ER6                                      \n\t"\r
245                                         "RTE                                                                    \n\t"\r
246                                 );\r
247 \r
248         ( void ) pxCurrentTCB;\r
249 \r
250         /* Should not get here. */\r
251         return pdTRUE;\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 void vPortEndScheduler( void )\r
256 {\r
257         /* It is unlikely that the h8 port will get stopped. */\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 /*\r
262  * Manual context switch.  This is a trap handler.  The "saveall" function\r
263  * attribute is used so the context is saved by the compiler prologue.  All\r
264  * we have to do is save the stack pointer.\r
265  */\r
266 void vPortYield( void )\r
267 {\r
268         portSAVE_STACK_POINTER();\r
269                 vTaskSwitchContext();\r
270         portRESTORE_STACK_POINTER();\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 /* \r
275  * The interrupt handler installed for the RTOS tick depends on whether the \r
276  * preemptive or cooperative scheduler is being used. \r
277  */\r
278 #if( configUSE_PREEMPTION == 1 )\r
279 \r
280         /* \r
281          * The preemptive scheduler is used so the ISR calls vTaskSwitchContext().\r
282          * The function prologue saves the context so all we have to do is save\r
283          * the stack pointer.\r
284          */\r
285         void vTickISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
286         void vTickISR( void )\r
287         {\r
288                 portSAVE_STACK_POINTER();\r
289                 \r
290                 vTaskIncrementTick();\r
291                 vTaskSwitchContext();\r
292 \r
293                 /* Clear the interrupt. */\r
294                 TSR1 &= ~0x01;\r
295 \r
296                 portRESTORE_STACK_POINTER();\r
297         }\r
298 \r
299 #else\r
300 \r
301         /*\r
302          * The cooperative scheduler is being used so all we have to do is \r
303          * periodically increment the tick.  This can just be a normal ISR and\r
304          * the "saveall" attribute is not required.\r
305          */\r
306         void vTickISR( void ) __attribute__ ( ( interrupt_handler ) );\r
307         void vTickISR( void )\r
308         {\r
309                 vTaskIncrementTick();\r
310 \r
311                 /* Clear the interrupt. */\r
312                 TSR1 &= ~0x01;\r
313         }\r
314 \r
315 #endif\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 /*\r
319  * Setup timer 1 compare match to generate a tick interrupt.\r
320  */\r
321 static void prvSetupTimerInterrupt( void )\r
322 {\r
323 const unsigned long ulCompareMatch = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / portCLOCK_DIV;\r
324 \r
325         /* Turn the module on. */\r
326         MSTPCR &= ~portMSTP13;\r
327 \r
328         /* Configure timer 1. */\r
329         TCR1 = portCLEAR_ON_TGRA_COMPARE_MATCH | portCLOCK_DIV_64;\r
330 \r
331         /* Configure the compare match value for a tick of configTICK_RATE_HZ. */\r
332         TGR1A = ulCompareMatch;\r
333 \r
334         /* Start the timer and enable the interrupt - we can do this here as \r
335         interrupts are globally disabled when this function is called. */\r
336         TIER1 |= portTGRA_INTERRUPT_ENABLE;\r
337         TSTR |= portTIMER_CHANNEL;\r
338 }\r
339 /*-----------------------------------------------------------*/\r
340 \r
341 \r
342 \r