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