]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/H8S2329/port.c
Update version number.
[freertos] / FreeRTOS / Source / portable / GCC / H8S2329 / port.c
1 /*\r
2     FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /* Scheduler includes. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 \r
69 \r
70 /*-----------------------------------------------------------\r
71  * Implementation of functions defined in portable.h for the H8S port.\r
72  *----------------------------------------------------------*/\r
73 \r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 /* When the task starts interrupts should be enabled. */\r
78 #define portINITIAL_CCR                 ( ( portSTACK_TYPE ) 0x00 )\r
79 \r
80 /* Hardware specific constants used to generate the RTOS tick from the TPU. */\r
81 #define portCLEAR_ON_TGRA_COMPARE_MATCH ( ( unsigned char ) 0x20 )\r
82 #define portCLOCK_DIV_64                                ( ( unsigned char ) 0x03 )\r
83 #define portCLOCK_DIV                                   ( ( unsigned long ) 64 )\r
84 #define portTGRA_INTERRUPT_ENABLE               ( ( unsigned char ) 0x01 )\r
85 #define portTIMER_CHANNEL                               ( ( unsigned char ) 0x02 )\r
86 #define portMSTP13                                              ( ( unsigned short ) 0x2000 )\r
87 \r
88 /*\r
89  * Setup TPU channel one for the RTOS tick at the requested frequency.\r
90  */\r
91 static void prvSetupTimerInterrupt( void );\r
92 \r
93 /*\r
94  * The ISR used by portYIELD(). This is installed as a trap handler.\r
95  */\r
96 void vPortYield( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
97 \r
98 /*-----------------------------------------------------------*/\r
99 \r
100 /* \r
101  * See header file for description. \r
102  */\r
103 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
104 {\r
105 unsigned long ulValue;\r
106 \r
107         /* This requires an even address. */\r
108         ulValue = ( unsigned long ) pxTopOfStack;\r
109         if( ulValue & 1UL )\r
110         {\r
111                 pxTopOfStack = pxTopOfStack - 1;\r
112         }\r
113 \r
114         /* Place a few bytes of known values on the bottom of the stack. \r
115         This is just useful for debugging. */\r
116         pxTopOfStack--;\r
117         *pxTopOfStack = 0xaa;\r
118         pxTopOfStack--;\r
119         *pxTopOfStack = 0xbb;\r
120         pxTopOfStack--;\r
121         *pxTopOfStack = 0xcc;\r
122         pxTopOfStack--;\r
123         *pxTopOfStack = 0xdd;\r
124 \r
125         /* The initial stack mimics an interrupt stack.  First there is the program\r
126         counter (24 bits). */\r
127         ulValue = ( unsigned long ) pxCode;\r
128 \r
129         pxTopOfStack--;\r
130         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
131         pxTopOfStack--;\r
132         ulValue >>= 8UL;\r
133         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
134         pxTopOfStack--;\r
135         ulValue >>= 8UL;\r
136         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
137 \r
138         /* Followed by the CCR. */      \r
139         pxTopOfStack--;\r
140         *pxTopOfStack = portINITIAL_CCR;\r
141 \r
142         /* Next all the general purpose registers - with the parameters being passed\r
143         in ER0.  The parameter order must match that used by the compiler when the\r
144         "saveall" function attribute is used. */\r
145 \r
146         /* ER6 */\r
147         pxTopOfStack--;\r
148         *pxTopOfStack = 0x66;\r
149         pxTopOfStack--;\r
150         *pxTopOfStack = 0x66;\r
151         pxTopOfStack--;\r
152         *pxTopOfStack = 0x66;\r
153         pxTopOfStack--;\r
154         *pxTopOfStack = 0x66;\r
155         \r
156         /* ER0 */\r
157         ulValue = ( unsigned long ) pvParameters;\r
158 \r
159         pxTopOfStack--;\r
160         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
161         pxTopOfStack--;\r
162         ulValue >>= 8UL;\r
163         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
164         pxTopOfStack--;\r
165         ulValue >>= 8UL;\r
166         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
167         pxTopOfStack--;\r
168         ulValue >>= 8UL;\r
169         *pxTopOfStack = ( portSTACK_TYPE ) ( ulValue & 0xff );\r
170         \r
171         /* ER1 */\r
172         pxTopOfStack--;\r
173         *pxTopOfStack = 0x11;\r
174         pxTopOfStack--;\r
175         *pxTopOfStack = 0x11;\r
176         pxTopOfStack--;\r
177         *pxTopOfStack = 0x11;\r
178         pxTopOfStack--;\r
179         *pxTopOfStack = 0x11;\r
180 \r
181         /* ER2 */\r
182         pxTopOfStack--;\r
183         *pxTopOfStack = 0x22;\r
184         pxTopOfStack--;\r
185         *pxTopOfStack = 0x22;\r
186         pxTopOfStack--;\r
187         *pxTopOfStack = 0x22;\r
188         pxTopOfStack--;\r
189         *pxTopOfStack = 0x22;\r
190 \r
191         /* ER3 */\r
192         pxTopOfStack--;\r
193         *pxTopOfStack = 0x33;\r
194         pxTopOfStack--;\r
195         *pxTopOfStack = 0x33;\r
196         pxTopOfStack--;\r
197         *pxTopOfStack = 0x33;\r
198         pxTopOfStack--;\r
199         *pxTopOfStack = 0x33;\r
200 \r
201         /* ER4 */\r
202         pxTopOfStack--;\r
203         *pxTopOfStack = 0x44;\r
204         pxTopOfStack--;\r
205         *pxTopOfStack = 0x44;\r
206         pxTopOfStack--;\r
207         *pxTopOfStack = 0x44;\r
208         pxTopOfStack--;\r
209         *pxTopOfStack = 0x44;\r
210 \r
211         /* ER5 */\r
212         pxTopOfStack--;\r
213         *pxTopOfStack = 0x55;\r
214         pxTopOfStack--;\r
215         *pxTopOfStack = 0x55;\r
216         pxTopOfStack--;\r
217         *pxTopOfStack = 0x55;\r
218         pxTopOfStack--;\r
219         *pxTopOfStack = 0x55;\r
220 \r
221         return pxTopOfStack;\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 portBASE_TYPE xPortStartScheduler( void )\r
226 {\r
227 extern void * pxCurrentTCB;\r
228 \r
229         /* Setup the hardware to generate the tick. */\r
230         prvSetupTimerInterrupt();\r
231 \r
232         /* Restore the context of the first task that is going to run.  This\r
233         mirrors the function epilogue code generated by the compiler when the\r
234         "saveall" function attribute is used. */\r
235         asm volatile ( \r
236                                         "MOV.L          @_pxCurrentTCB, ER6                     \n\t"\r
237                                         "MOV.L          @ER6, ER7                                       \n\t"\r
238                                         "LDM.L          @SP+, (ER4-ER5)                         \n\t"\r
239                                         "LDM.L          @SP+, (ER0-ER3)                         \n\t"\r
240                                         "MOV.L          @ER7+, ER6                                      \n\t"\r
241                                         "RTE                                                                    \n\t"\r
242                                 );\r
243 \r
244         ( void ) pxCurrentTCB;\r
245 \r
246         /* Should not get here. */\r
247         return pdTRUE;\r
248 }\r
249 /*-----------------------------------------------------------*/\r
250 \r
251 void vPortEndScheduler( void )\r
252 {\r
253         /* It is unlikely that the h8 port will get stopped. */\r
254 }\r
255 /*-----------------------------------------------------------*/\r
256 \r
257 /*\r
258  * Manual context switch.  This is a trap handler.  The "saveall" function\r
259  * attribute is used so the context is saved by the compiler prologue.  All\r
260  * we have to do is save the stack pointer.\r
261  */\r
262 void vPortYield( void )\r
263 {\r
264         portSAVE_STACK_POINTER();\r
265                 vTaskSwitchContext();\r
266         portRESTORE_STACK_POINTER();\r
267 }\r
268 /*-----------------------------------------------------------*/\r
269 \r
270 /* \r
271  * The interrupt handler installed for the RTOS tick depends on whether the \r
272  * preemptive or cooperative scheduler is being used. \r
273  */\r
274 #if( configUSE_PREEMPTION == 1 )\r
275 \r
276         /* \r
277          * The preemptive scheduler is used so the ISR calls vTaskSwitchContext().\r
278          * The function prologue saves the context so all we have to do is save\r
279          * the stack pointer.\r
280          */\r
281         void vTickISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
282         void vTickISR( void )\r
283         {\r
284                 portSAVE_STACK_POINTER();\r
285                 \r
286                 if( xTaskIncrementTick() != pdFALSE )\r
287                 {\r
288                         vTaskSwitchContext();\r
289                 }\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                 xTaskIncrementTick();\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