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