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