]> begriffs open source - freertos/blob - Source/portable/GCC/H8S2329/port.c
Ready for V5.1.1 release.
[freertos] / Source / portable / GCC / H8S2329 / port.c
1 /*\r
2         FreeRTOS.org V5.1.1 - Copyright (C) 2003-2008 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     ***************************************************************************\r
28     *                                                                         *\r
29     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
30     * and even write all or part of your application on your behalf.          *\r
31     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
32     * expedite your project.                                                  *\r
33     *                                                                         *\r
34     ***************************************************************************\r
35     ***************************************************************************\r
36 \r
37         Please ensure to read the configuration and relevant port sections of the\r
38         online documentation.\r
39 \r
40         http://www.FreeRTOS.org - Documentation, latest information, license and \r
41         contact details.\r
42 \r
43         http://www.SafeRTOS.com - A version that is certified for use in safety \r
44         critical systems.\r
45 \r
46         http://www.OpenRTOS.com - Commercial support, development, porting, \r
47         licensing and training services.\r
48 */\r
49 \r
50 /* Scheduler includes. */\r
51 #include "FreeRTOS.h"\r
52 #include "task.h"\r
53 \r
54 \r
55 /*-----------------------------------------------------------\r
56  * Implementation of functions defined in portable.h for the H8S port.\r
57  *----------------------------------------------------------*/\r
58 \r
59 \r
60 /*-----------------------------------------------------------*/\r
61 \r
62 /* When the task starts interrupts should be enabled. */\r
63 #define portINITIAL_CCR                 ( ( portSTACK_TYPE ) 0x00 )\r
64 \r
65 /* Hardware specific constants used to generate the RTOS tick from the TPU. */\r
66 #define portCLEAR_ON_TGRA_COMPARE_MATCH ( ( unsigned portCHAR ) 0x20 )\r
67 #define portCLOCK_DIV_64                                ( ( unsigned portCHAR ) 0x03 )\r
68 #define portCLOCK_DIV                                   ( ( unsigned portLONG ) 64 )\r
69 #define portTGRA_INTERRUPT_ENABLE               ( ( unsigned portCHAR ) 0x01 )\r
70 #define portTIMER_CHANNEL                               ( ( unsigned portCHAR ) 0x02 )\r
71 #define portMSTP13                                              ( ( unsigned portSHORT ) 0x2000 )\r
72 \r
73 /*\r
74  * Setup TPU channel one for the RTOS tick at the requested frequency.\r
75  */\r
76 static void prvSetupTimerInterrupt( void );\r
77 \r
78 /*\r
79  * The ISR used by portYIELD(). This is installed as a trap handler.\r
80  */\r
81 void vPortYield( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 /* \r
86  * See header file for description. \r
87  */\r
88 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
89 {\r
90 unsigned portLONG ulValue;\r
91 \r
92         /* This requires an even address. */\r
93         ulValue = ( unsigned portLONG ) pxTopOfStack;\r
94         if( ulValue & 1UL )\r
95         {\r
96                 pxTopOfStack = pxTopOfStack - 1;\r
97         }\r
98 \r
99         /* Place a few bytes of known values on the bottom of the stack. \r
100         This is just useful for debugging. */\r
101         pxTopOfStack--;\r
102         *pxTopOfStack = 0xaa;\r
103         pxTopOfStack--;\r
104         *pxTopOfStack = 0xbb;\r
105         pxTopOfStack--;\r
106         *pxTopOfStack = 0xcc;\r
107         pxTopOfStack--;\r
108         *pxTopOfStack = 0xdd;\r
109 \r
110         /* The initial stack mimics an interrupt stack.  First there is the program\r
111         counter (24 bits). */\r
112         ulValue = ( unsigned portLONG ) pxCode;\r
113 \r
114         pxTopOfStack--;\r
115         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
116         pxTopOfStack--;\r
117         ulValue >>= 8UL;\r
118         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
119         pxTopOfStack--;\r
120         ulValue >>= 8UL;\r
121         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
122 \r
123         /* Followed by the CCR. */      \r
124         pxTopOfStack--;\r
125         *pxTopOfStack = portINITIAL_CCR;\r
126 \r
127         /* Next all the general purpose registers - with the parameters being passed\r
128         in ER0.  The parameter order must match that used by the compiler when the\r
129         "saveall" function attribute is used. */\r
130 \r
131         /* ER6 */\r
132         pxTopOfStack--;\r
133         *pxTopOfStack = 0x66;\r
134         pxTopOfStack--;\r
135         *pxTopOfStack = 0x66;\r
136         pxTopOfStack--;\r
137         *pxTopOfStack = 0x66;\r
138         pxTopOfStack--;\r
139         *pxTopOfStack = 0x66;\r
140         \r
141         /* ER0 */\r
142         ulValue = ( unsigned portLONG ) pvParameters;\r
143 \r
144         pxTopOfStack--;\r
145         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
146         pxTopOfStack--;\r
147         ulValue >>= 8UL;\r
148         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
149         pxTopOfStack--;\r
150         ulValue >>= 8UL;\r
151         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
152         pxTopOfStack--;\r
153         ulValue >>= 8UL;\r
154         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
155         \r
156         /* ER1 */\r
157         pxTopOfStack--;\r
158         *pxTopOfStack = 0x11;\r
159         pxTopOfStack--;\r
160         *pxTopOfStack = 0x11;\r
161         pxTopOfStack--;\r
162         *pxTopOfStack = 0x11;\r
163         pxTopOfStack--;\r
164         *pxTopOfStack = 0x11;\r
165 \r
166         /* ER2 */\r
167         pxTopOfStack--;\r
168         *pxTopOfStack = 0x22;\r
169         pxTopOfStack--;\r
170         *pxTopOfStack = 0x22;\r
171         pxTopOfStack--;\r
172         *pxTopOfStack = 0x22;\r
173         pxTopOfStack--;\r
174         *pxTopOfStack = 0x22;\r
175 \r
176         /* ER3 */\r
177         pxTopOfStack--;\r
178         *pxTopOfStack = 0x33;\r
179         pxTopOfStack--;\r
180         *pxTopOfStack = 0x33;\r
181         pxTopOfStack--;\r
182         *pxTopOfStack = 0x33;\r
183         pxTopOfStack--;\r
184         *pxTopOfStack = 0x33;\r
185 \r
186         /* ER4 */\r
187         pxTopOfStack--;\r
188         *pxTopOfStack = 0x44;\r
189         pxTopOfStack--;\r
190         *pxTopOfStack = 0x44;\r
191         pxTopOfStack--;\r
192         *pxTopOfStack = 0x44;\r
193         pxTopOfStack--;\r
194         *pxTopOfStack = 0x44;\r
195 \r
196         /* ER5 */\r
197         pxTopOfStack--;\r
198         *pxTopOfStack = 0x55;\r
199         pxTopOfStack--;\r
200         *pxTopOfStack = 0x55;\r
201         pxTopOfStack--;\r
202         *pxTopOfStack = 0x55;\r
203         pxTopOfStack--;\r
204         *pxTopOfStack = 0x55;\r
205 \r
206         return pxTopOfStack;\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 portBASE_TYPE xPortStartScheduler( void )\r
211 {\r
212 extern void * pxCurrentTCB;\r
213 \r
214         /* Setup the hardware to generate the tick. */\r
215         prvSetupTimerInterrupt();\r
216 \r
217         /* Restore the context of the first task that is going to run.  This\r
218         mirrors the function epilogue code generated by the compiler when the\r
219         "saveall" function attribute is used. */\r
220         asm volatile ( \r
221                                         "MOV.L          @_pxCurrentTCB, ER6                     \n\t"\r
222                                         "MOV.L          @ER6, ER7                                       \n\t"\r
223                                         "LDM.L          @SP+, (ER4-ER5)                         \n\t"\r
224                                         "LDM.L          @SP+, (ER0-ER3)                         \n\t"\r
225                                         "MOV.L          @ER7+, ER6                                      \n\t"\r
226                                         "RTE                                                                    \n\t"\r
227                                 );\r
228 \r
229         ( void ) pxCurrentTCB;\r
230 \r
231         /* Should not get here. */\r
232         return pdTRUE;\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vPortEndScheduler( void )\r
237 {\r
238         /* It is unlikely that the h8 port will get stopped. */\r
239 }\r
240 /*-----------------------------------------------------------*/\r
241 \r
242 /*\r
243  * Manual context switch.  This is a trap handler.  The "saveall" function\r
244  * attribute is used so the context is saved by the compiler prologue.  All\r
245  * we have to do is save the stack pointer.\r
246  */\r
247 void vPortYield( void )\r
248 {\r
249         portSAVE_STACK_POINTER();\r
250                 vTaskSwitchContext();\r
251         portRESTORE_STACK_POINTER();\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 /* \r
256  * The interrupt handler installed for the RTOS tick depends on whether the \r
257  * preemptive or cooperative scheduler is being used. \r
258  */\r
259 #if( configUSE_PREEMPTION == 1 )\r
260 \r
261         /* \r
262          * The preemptive scheduler is used so the ISR calls vTaskSwitchContext().\r
263          * The function prologue saves the context so all we have to do is save\r
264          * the stack pointer.\r
265          */\r
266         void vTickISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
267         void vTickISR( void )\r
268         {\r
269                 portSAVE_STACK_POINTER();\r
270                 \r
271                 vTaskIncrementTick();\r
272                 vTaskSwitchContext();\r
273 \r
274                 /* Clear the interrupt. */\r
275                 TSR1 &= ~0x01;\r
276 \r
277                 portRESTORE_STACK_POINTER();\r
278         }\r
279 \r
280 #else\r
281 \r
282         /*\r
283          * The cooperative scheduler is being used so all we have to do is \r
284          * periodically increment the tick.  This can just be a normal ISR and\r
285          * the "saveall" attribute is not required.\r
286          */\r
287         void vTickISR( void ) __attribute__ ( ( interrupt_handler ) );\r
288         void vTickISR( void )\r
289         {\r
290                 vTaskIncrementTick();\r
291 \r
292                 /* Clear the interrupt. */\r
293                 TSR1 &= ~0x01;\r
294         }\r
295 \r
296 #endif\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 /*\r
300  * Setup timer 1 compare match to generate a tick interrupt.\r
301  */\r
302 static void prvSetupTimerInterrupt( void )\r
303 {\r
304 const unsigned portLONG ulCompareMatch = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / portCLOCK_DIV;\r
305 \r
306         /* Turn the module on. */\r
307         MSTPCR &= ~portMSTP13;\r
308 \r
309         /* Configure timer 1. */\r
310         TCR1 = portCLEAR_ON_TGRA_COMPARE_MATCH | portCLOCK_DIV_64;\r
311 \r
312         /* Configure the compare match value for a tick of configTICK_RATE_HZ. */\r
313         TGR1A = ulCompareMatch;\r
314 \r
315         /* Start the timer and enable the interrupt - we can do this here as \r
316         interrupts are globally disabled when this function is called. */\r
317         TIER1 |= portTGRA_INTERRUPT_ENABLE;\r
318         TSTR |= portTIMER_CHANNEL;\r
319 }\r
320 /*-----------------------------------------------------------*/\r
321 \r
322 \r
323 \r