]> git.sur5r.net Git - freertos/blob - Source/portable/GCC/H8S2329/port.c
Update to V4.3.0 as described in http://www.FreeRTOS.org/History.txt
[freertos] / Source / portable / GCC / H8S2329 / port.c
1 /*\r
2         FreeRTOS.org V4.3.0 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 /* Scheduler includes. */\r
37 #include "FreeRTOS.h"\r
38 #include "task.h"\r
39 \r
40 \r
41 /*-----------------------------------------------------------\r
42  * Implementation of functions defined in portable.h for the H8S port.\r
43  *----------------------------------------------------------*/\r
44 \r
45 \r
46 /*-----------------------------------------------------------*/\r
47 \r
48 /* When the task starts interrupts should be enabled. */\r
49 #define portINITIAL_CCR                 ( ( portSTACK_TYPE ) 0x00 )\r
50 \r
51 /* Hardware specific constants used to generate the RTOS tick from the TPU. */\r
52 #define portCLEAR_ON_TGRA_COMPARE_MATCH ( ( unsigned portCHAR ) 0x20 )\r
53 #define portCLOCK_DIV_64                                ( ( unsigned portCHAR ) 0x03 )\r
54 #define portCLOCK_DIV                                   ( ( unsigned portLONG ) 64 )\r
55 #define portTGRA_INTERRUPT_ENABLE               ( ( unsigned portCHAR ) 0x01 )\r
56 #define portTIMER_CHANNEL                               ( ( unsigned portCHAR ) 0x02 )\r
57 #define portMSTP13                                              ( ( unsigned portSHORT ) 0x2000 )\r
58 \r
59 /*\r
60  * Setup TPU channel one for the RTOS tick at the requested frequency.\r
61  */\r
62 static void prvSetupTimerInterrupt( void );\r
63 \r
64 /*\r
65  * The ISR used by portYIELD(). This is installed as a trap handler.\r
66  */\r
67 void vPortYield( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* \r
72  * See header file for description. \r
73  */\r
74 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
75 {\r
76 unsigned portLONG ulValue;\r
77 \r
78         /* This requires an even address. */\r
79         ulValue = ( unsigned portLONG ) pxTopOfStack;\r
80         if( ulValue & 1UL )\r
81         {\r
82                 pxTopOfStack = pxTopOfStack - 1;\r
83         }\r
84 \r
85         /* Place a few bytes of known values on the bottom of the stack. \r
86         This is just useful for debugging. */\r
87         pxTopOfStack--;\r
88         *pxTopOfStack = 0xaa;\r
89         pxTopOfStack--;\r
90         *pxTopOfStack = 0xbb;\r
91         pxTopOfStack--;\r
92         *pxTopOfStack = 0xcc;\r
93         pxTopOfStack--;\r
94         *pxTopOfStack = 0xdd;\r
95 \r
96         /* The initial stack mimics an interrupt stack.  First there is the program\r
97         counter (24 bits). */\r
98         ulValue = ( unsigned portLONG ) pxCode;\r
99 \r
100         pxTopOfStack--;\r
101         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
102         pxTopOfStack--;\r
103         ulValue >>= 8UL;\r
104         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
105         pxTopOfStack--;\r
106         ulValue >>= 8UL;\r
107         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
108 \r
109         /* Followed by the CCR. */      \r
110         pxTopOfStack--;\r
111         *pxTopOfStack = portINITIAL_CCR;\r
112 \r
113         /* Next all the general purpose registers - with the parameters being passed\r
114         in ER0.  The parameter order must match that used by the compiler when the\r
115         "saveall" function attribute is used. */\r
116 \r
117         /* ER6 */\r
118         pxTopOfStack--;\r
119         *pxTopOfStack = 0x66;\r
120         pxTopOfStack--;\r
121         *pxTopOfStack = 0x66;\r
122         pxTopOfStack--;\r
123         *pxTopOfStack = 0x66;\r
124         pxTopOfStack--;\r
125         *pxTopOfStack = 0x66;\r
126         \r
127         /* ER0 */\r
128         ulValue = ( unsigned portLONG ) pvParameters;\r
129 \r
130         pxTopOfStack--;\r
131         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
132         pxTopOfStack--;\r
133         ulValue >>= 8UL;\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         /* ER1 */\r
143         pxTopOfStack--;\r
144         *pxTopOfStack = 0x11;\r
145         pxTopOfStack--;\r
146         *pxTopOfStack = 0x11;\r
147         pxTopOfStack--;\r
148         *pxTopOfStack = 0x11;\r
149         pxTopOfStack--;\r
150         *pxTopOfStack = 0x11;\r
151 \r
152         /* ER2 */\r
153         pxTopOfStack--;\r
154         *pxTopOfStack = 0x22;\r
155         pxTopOfStack--;\r
156         *pxTopOfStack = 0x22;\r
157         pxTopOfStack--;\r
158         *pxTopOfStack = 0x22;\r
159         pxTopOfStack--;\r
160         *pxTopOfStack = 0x22;\r
161 \r
162         /* ER3 */\r
163         pxTopOfStack--;\r
164         *pxTopOfStack = 0x33;\r
165         pxTopOfStack--;\r
166         *pxTopOfStack = 0x33;\r
167         pxTopOfStack--;\r
168         *pxTopOfStack = 0x33;\r
169         pxTopOfStack--;\r
170         *pxTopOfStack = 0x33;\r
171 \r
172         /* ER4 */\r
173         pxTopOfStack--;\r
174         *pxTopOfStack = 0x44;\r
175         pxTopOfStack--;\r
176         *pxTopOfStack = 0x44;\r
177         pxTopOfStack--;\r
178         *pxTopOfStack = 0x44;\r
179         pxTopOfStack--;\r
180         *pxTopOfStack = 0x44;\r
181 \r
182         /* ER5 */\r
183         pxTopOfStack--;\r
184         *pxTopOfStack = 0x55;\r
185         pxTopOfStack--;\r
186         *pxTopOfStack = 0x55;\r
187         pxTopOfStack--;\r
188         *pxTopOfStack = 0x55;\r
189         pxTopOfStack--;\r
190         *pxTopOfStack = 0x55;\r
191 \r
192         return pxTopOfStack;\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 portBASE_TYPE xPortStartScheduler( void )\r
197 {\r
198 extern void * pxCurrentTCB;\r
199 \r
200         /* Setup the hardware to generate the tick. */\r
201         prvSetupTimerInterrupt();\r
202 \r
203         /* Restore the context of the first task that is going to run.  This\r
204         mirrors the function epilogue code generated by the compiler when the\r
205         "saveall" function attribute is used. */\r
206         asm volatile ( \r
207                                         "MOV.L          @_pxCurrentTCB, ER6                     \n\t"\r
208                                         "MOV.L          @ER6, ER7                                       \n\t"\r
209                                         "LDM.L          @SP+, (ER4-ER5)                         \n\t"\r
210                                         "LDM.L          @SP+, (ER0-ER3)                         \n\t"\r
211                                         "MOV.L          @ER7+, ER6                                      \n\t"\r
212                                         "RTE                                                                    \n\t"\r
213                                 );\r
214 \r
215         ( void ) pxCurrentTCB;\r
216 \r
217         /* Should not get here. */\r
218         return pdTRUE;\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 void vPortEndScheduler( void )\r
223 {\r
224         /* It is unlikely that the h8 port will get stopped. */\r
225 }\r
226 /*-----------------------------------------------------------*/\r
227 \r
228 /*\r
229  * Manual context switch.  This is a trap handler.  The "saveall" function\r
230  * attribute is used so the context is saved by the compiler prologue.  All\r
231  * we have to do is save the stack pointer.\r
232  */\r
233 void vPortYield( void )\r
234 {\r
235         portSAVE_STACK_POINTER();\r
236                 vTaskSwitchContext();\r
237         portRESTORE_STACK_POINTER();\r
238 }\r
239 /*-----------------------------------------------------------*/\r
240 \r
241 /* \r
242  * The interrupt handler installed for the RTOS tick depends on whether the \r
243  * preemptive or cooperative scheduler is being used. \r
244  */\r
245 #if( configUSE_PREEMPTION == 1 )\r
246 \r
247         /* \r
248          * The preemptive scheduler is used so the ISR calls vTaskSwitchContext().\r
249          * The function prologue saves the context so all we have to do is save\r
250          * the stack pointer.\r
251          */\r
252         void vTickISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
253         void vTickISR( void )\r
254         {\r
255                 portSAVE_STACK_POINTER();\r
256                 \r
257                 vTaskIncrementTick();\r
258                 vTaskSwitchContext();\r
259 \r
260                 /* Clear the interrupt. */\r
261                 TSR1 &= ~0x01;\r
262 \r
263                 portRESTORE_STACK_POINTER();\r
264         }\r
265 \r
266 #else\r
267 \r
268         /*\r
269          * The cooperative scheduler is being used so all we have to do is \r
270          * periodically increment the tick.  This can just be a normal ISR and\r
271          * the "saveall" attribute is not required.\r
272          */\r
273         void vTickISR( void ) __attribute__ ( ( interrupt_handler ) );\r
274         void vTickISR( void )\r
275         {\r
276                 vTaskIncrementTick();\r
277 \r
278                 /* Clear the interrupt. */\r
279                 TSR1 &= ~0x01;\r
280         }\r
281 \r
282 #endif\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 /*\r
286  * Setup timer 1 compare match to generate a tick interrupt.\r
287  */\r
288 static void prvSetupTimerInterrupt( void )\r
289 {\r
290 const unsigned portLONG ulCompareMatch = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / portCLOCK_DIV;\r
291 \r
292         /* Turn the module on. */\r
293         MSTPCR &= ~portMSTP13;\r
294 \r
295         /* Configure timer 1. */\r
296         TCR1 = portCLEAR_ON_TGRA_COMPARE_MATCH | portCLOCK_DIV_64;\r
297 \r
298         /* Configure the compare match value for a tick of configTICK_RATE_HZ. */\r
299         TGR1A = ulCompareMatch;\r
300 \r
301         /* Start the timer and enable the interrupt - we can do this here as \r
302         interrupts are globally disabled when this function is called. */\r
303         TIER1 |= portTGRA_INTERRUPT_ENABLE;\r
304         TSTR |= portTIMER_CHANNEL;\r
305 }\r
306 /*-----------------------------------------------------------*/\r
307 \r
308 \r
309 \r