]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/GCC/H8S2329/port.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Source / portable / GCC / H8S2329 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /* Scheduler includes. */\r
30 #include "FreeRTOS.h"\r
31 #include "task.h"\r
32 \r
33 \r
34 /*-----------------------------------------------------------\r
35  * Implementation of functions defined in portable.h for the H8S port.\r
36  *----------------------------------------------------------*/\r
37 \r
38 \r
39 /*-----------------------------------------------------------*/\r
40 \r
41 /* When the task starts interrupts should be enabled. */\r
42 #define portINITIAL_CCR                 ( ( StackType_t ) 0x00 )\r
43 \r
44 /* Hardware specific constants used to generate the RTOS tick from the TPU. */\r
45 #define portCLEAR_ON_TGRA_COMPARE_MATCH ( ( uint8_t ) 0x20 )\r
46 #define portCLOCK_DIV_64                                ( ( uint8_t ) 0x03 )\r
47 #define portCLOCK_DIV                                   ( ( uint32_t ) 64 )\r
48 #define portTGRA_INTERRUPT_ENABLE               ( ( uint8_t ) 0x01 )\r
49 #define portTIMER_CHANNEL                               ( ( uint8_t ) 0x02 )\r
50 #define portMSTP13                                              ( ( uint16_t ) 0x2000 )\r
51 \r
52 /*\r
53  * Setup TPU channel one for the RTOS tick at the requested frequency.\r
54  */\r
55 static void prvSetupTimerInterrupt( void );\r
56 \r
57 /*\r
58  * The ISR used by portYIELD(). This is installed as a trap handler.\r
59  */\r
60 void vPortYield( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
61 \r
62 /*-----------------------------------------------------------*/\r
63 \r
64 /* \r
65  * See header file for description. \r
66  */\r
67 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
68 {\r
69 uint32_t ulValue;\r
70 \r
71         /* This requires an even address. */\r
72         ulValue = ( uint32_t ) pxTopOfStack;\r
73         if( ulValue & 1UL )\r
74         {\r
75                 pxTopOfStack = pxTopOfStack - 1;\r
76         }\r
77 \r
78         /* Place a few bytes of known values on the bottom of the stack. \r
79         This is just useful for debugging. */\r
80         pxTopOfStack--;\r
81         *pxTopOfStack = 0xaa;\r
82         pxTopOfStack--;\r
83         *pxTopOfStack = 0xbb;\r
84         pxTopOfStack--;\r
85         *pxTopOfStack = 0xcc;\r
86         pxTopOfStack--;\r
87         *pxTopOfStack = 0xdd;\r
88 \r
89         /* The initial stack mimics an interrupt stack.  First there is the program\r
90         counter (24 bits). */\r
91         ulValue = ( uint32_t ) pxCode;\r
92 \r
93         pxTopOfStack--;\r
94         *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );\r
95         pxTopOfStack--;\r
96         ulValue >>= 8UL;\r
97         *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );\r
98         pxTopOfStack--;\r
99         ulValue >>= 8UL;\r
100         *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );\r
101 \r
102         /* Followed by the CCR. */      \r
103         pxTopOfStack--;\r
104         *pxTopOfStack = portINITIAL_CCR;\r
105 \r
106         /* Next all the general purpose registers - with the parameters being passed\r
107         in ER0.  The parameter order must match that used by the compiler when the\r
108         "saveall" function attribute is used. */\r
109 \r
110         /* ER6 */\r
111         pxTopOfStack--;\r
112         *pxTopOfStack = 0x66;\r
113         pxTopOfStack--;\r
114         *pxTopOfStack = 0x66;\r
115         pxTopOfStack--;\r
116         *pxTopOfStack = 0x66;\r
117         pxTopOfStack--;\r
118         *pxTopOfStack = 0x66;\r
119         \r
120         /* ER0 */\r
121         ulValue = ( uint32_t ) pvParameters;\r
122 \r
123         pxTopOfStack--;\r
124         *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );\r
125         pxTopOfStack--;\r
126         ulValue >>= 8UL;\r
127         *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );\r
128         pxTopOfStack--;\r
129         ulValue >>= 8UL;\r
130         *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );\r
131         pxTopOfStack--;\r
132         ulValue >>= 8UL;\r
133         *pxTopOfStack = ( StackType_t ) ( ulValue & 0xff );\r
134         \r
135         /* ER1 */\r
136         pxTopOfStack--;\r
137         *pxTopOfStack = 0x11;\r
138         pxTopOfStack--;\r
139         *pxTopOfStack = 0x11;\r
140         pxTopOfStack--;\r
141         *pxTopOfStack = 0x11;\r
142         pxTopOfStack--;\r
143         *pxTopOfStack = 0x11;\r
144 \r
145         /* ER2 */\r
146         pxTopOfStack--;\r
147         *pxTopOfStack = 0x22;\r
148         pxTopOfStack--;\r
149         *pxTopOfStack = 0x22;\r
150         pxTopOfStack--;\r
151         *pxTopOfStack = 0x22;\r
152         pxTopOfStack--;\r
153         *pxTopOfStack = 0x22;\r
154 \r
155         /* ER3 */\r
156         pxTopOfStack--;\r
157         *pxTopOfStack = 0x33;\r
158         pxTopOfStack--;\r
159         *pxTopOfStack = 0x33;\r
160         pxTopOfStack--;\r
161         *pxTopOfStack = 0x33;\r
162         pxTopOfStack--;\r
163         *pxTopOfStack = 0x33;\r
164 \r
165         /* ER4 */\r
166         pxTopOfStack--;\r
167         *pxTopOfStack = 0x44;\r
168         pxTopOfStack--;\r
169         *pxTopOfStack = 0x44;\r
170         pxTopOfStack--;\r
171         *pxTopOfStack = 0x44;\r
172         pxTopOfStack--;\r
173         *pxTopOfStack = 0x44;\r
174 \r
175         /* ER5 */\r
176         pxTopOfStack--;\r
177         *pxTopOfStack = 0x55;\r
178         pxTopOfStack--;\r
179         *pxTopOfStack = 0x55;\r
180         pxTopOfStack--;\r
181         *pxTopOfStack = 0x55;\r
182         pxTopOfStack--;\r
183         *pxTopOfStack = 0x55;\r
184 \r
185         return pxTopOfStack;\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 BaseType_t xPortStartScheduler( void )\r
190 {\r
191 extern void * pxCurrentTCB;\r
192 \r
193         /* Setup the hardware to generate the tick. */\r
194         prvSetupTimerInterrupt();\r
195 \r
196         /* Restore the context of the first task that is going to run.  This\r
197         mirrors the function epilogue code generated by the compiler when the\r
198         "saveall" function attribute is used. */\r
199         asm volatile ( \r
200                                         "MOV.L          @_pxCurrentTCB, ER6                     \n\t"\r
201                                         "MOV.L          @ER6, ER7                                       \n\t"\r
202                                         "LDM.L          @SP+, (ER4-ER5)                         \n\t"\r
203                                         "LDM.L          @SP+, (ER0-ER3)                         \n\t"\r
204                                         "MOV.L          @ER7+, ER6                                      \n\t"\r
205                                         "RTE                                                                    \n\t"\r
206                                 );\r
207 \r
208         ( void ) pxCurrentTCB;\r
209 \r
210         /* Should not get here. */\r
211         return pdTRUE;\r
212 }\r
213 /*-----------------------------------------------------------*/\r
214 \r
215 void vPortEndScheduler( void )\r
216 {\r
217         /* It is unlikely that the h8 port will get stopped. */\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 /*\r
222  * Manual context switch.  This is a trap handler.  The "saveall" function\r
223  * attribute is used so the context is saved by the compiler prologue.  All\r
224  * we have to do is save the stack pointer.\r
225  */\r
226 void vPortYield( void )\r
227 {\r
228         portSAVE_STACK_POINTER();\r
229                 vTaskSwitchContext();\r
230         portRESTORE_STACK_POINTER();\r
231 }\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 /* \r
235  * The interrupt handler installed for the RTOS tick depends on whether the \r
236  * preemptive or cooperative scheduler is being used. \r
237  */\r
238 #if( configUSE_PREEMPTION == 1 )\r
239 \r
240         /* \r
241          * The preemptive scheduler is used so the ISR calls vTaskSwitchContext().\r
242          * The function prologue saves the context so all we have to do is save\r
243          * the stack pointer.\r
244          */\r
245         void vTickISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
246         void vTickISR( void )\r
247         {\r
248                 portSAVE_STACK_POINTER();\r
249                 \r
250                 if( xTaskIncrementTick() != pdFALSE )\r
251                 {\r
252                         vTaskSwitchContext();\r
253                 }\r
254 \r
255                 /* Clear the interrupt. */\r
256                 TSR1 &= ~0x01;\r
257 \r
258                 portRESTORE_STACK_POINTER();\r
259         }\r
260 \r
261 #else\r
262 \r
263         /*\r
264          * The cooperative scheduler is being used so all we have to do is \r
265          * periodically increment the tick.  This can just be a normal ISR and\r
266          * the "saveall" attribute is not required.\r
267          */\r
268         void vTickISR( void ) __attribute__ ( ( interrupt_handler ) );\r
269         void vTickISR( void )\r
270         {\r
271                 xTaskIncrementTick();\r
272 \r
273                 /* Clear the interrupt. */\r
274                 TSR1 &= ~0x01;\r
275         }\r
276 \r
277 #endif\r
278 /*-----------------------------------------------------------*/\r
279 \r
280 /*\r
281  * Setup timer 1 compare match to generate a tick interrupt.\r
282  */\r
283 static void prvSetupTimerInterrupt( void )\r
284 {\r
285 const uint32_t ulCompareMatch = ( configCPU_CLOCK_HZ / configTICK_RATE_HZ ) / portCLOCK_DIV;\r
286 \r
287         /* Turn the module on. */\r
288         MSTPCR &= ~portMSTP13;\r
289 \r
290         /* Configure timer 1. */\r
291         TCR1 = portCLEAR_ON_TGRA_COMPARE_MATCH | portCLOCK_DIV_64;\r
292 \r
293         /* Configure the compare match value for a tick of configTICK_RATE_HZ. */\r
294         TGR1A = ulCompareMatch;\r
295 \r
296         /* Start the timer and enable the interrupt - we can do this here as \r
297         interrupts are globally disabled when this function is called. */\r
298         TIER1 |= portTGRA_INTERRUPT_ENABLE;\r
299         TSTR |= portTIMER_CHANNEL;\r
300 }\r
301 /*-----------------------------------------------------------*/\r
302 \r
303 \r
304 \r