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