]> begriffs open source - cmsis-freertos/blob - Source/portable/Softune/MB91460/port.c
Update FreeRTOS-Kernel to v10.6.2
[cmsis-freertos] / Source / portable / Softune / MB91460 / port.c
1 /*
2  * FreeRTOS Kernel V10.6.2
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy of
8  * this software and associated documentation files (the "Software"), to deal in
9  * the Software without restriction, including without limitation the rights to
10  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11  * the Software, and to permit persons to whom the Software is furnished to do so,
12  * subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28
29 #include "FreeRTOS.h"
30 #include "task.h"
31 #include "mb91467d.h"
32
33 /*-----------------------------------------------------------*/
34
35 /* We require the address of the pxCurrentTCB variable, but don't want to know
36 any details of its type. */
37 typedef void TCB_t;
38 extern volatile TCB_t * volatile pxCurrentTCB;
39
40 /*-----------------------------------------------------------*/
41
42 #pragma asm
43 #macro  SaveContext
44      ORCCR #0x20                                ;Switch to user stack
45      ST RP,@-R15                                ;Store RP
46      STM0 (R7,R6,R5,R4,R3,R2,R1,R0)             ;Store R7-R0
47      STM1 (R14,R13,R12,R11,R10,R9,R8)           ;Store R14-R8
48      ST MDH, @-R15                              ;Store MDH
49      ST MDL, @-R15                              ;Store MDL
50
51      ANDCCR #0xDF                               ;Switch back to system stack
52      LD @R15+,R0                                ;Store PC to R0
53      ORCCR #0x20                                ;Switch to user stack
54      ST R0,@-R15                                ;Store PC to User stack
55
56      ANDCCR #0xDF                               ;Switch back to system stack
57      LD @R15+,R0                                ;Store PS to R0
58      ORCCR #0x20                                ;Switch to user stack
59      ST R0,@-R15                                ;Store PS to User stack
60
61      LDI #_pxCurrentTCB, R0                     ;Get pxCurrentTCB address
62      LD @R0, R0                                 ;Get the pxCurrentTCB->pxTopOfStack address
63      ST R15,@R0                                 ;Store USP to pxCurrentTCB->pxTopOfStack
64
65      ANDCCR #0xDF                               ;Switch back to system stack for the rest of tick ISR
66 #endm
67
68 #macro RestoreContext
69      LDI #_pxCurrentTCB, R0                     ;Get pxCurrentTCB address
70      LD @R0, R0                                 ;Get the pxCurrentTCB->pxTopOfStack address
71      ORCCR #0x20                                ;Switch to user stack
72      LD @R0, R15                                ;Restore USP from pxCurrentTCB->pxTopOfStack
73
74      LD @R15+,R0                                ;Store PS to R0
75      ANDCCR #0xDF                               ;Switch to system stack
76      ST R0,@-R15                                ;Store PS to system stack
77
78      ORCCR #0x20                                ;Switch to user stack
79      LD @R15+,R0                                ;Store PC to R0
80      ANDCCR #0xDF                               ;Switch to system stack
81      ST R0,@-R15                                ;Store PC to system stack
82
83      ORCCR #0x20                                ;Switch back to retrieve the remaining context
84
85      LD @R15+, MDL                              ;Restore MDL
86      LD @R15+, MDH                              ;Restore MDH
87      LDM1 (R14,R13,R12,R11,R10,R9,R8)           ;Restore R14-R8
88      LDM0 (R7,R6,R5,R4,R3,R2,R1,R0)             ;Restore R7-R0
89      LD @R15+, RP                               ;Restore RP
90
91      ANDCCR #0xDF                               ;Switch back to system stack for the rest of tick ISR
92 #endm
93 #pragma endasm
94
95 /*-----------------------------------------------------------*/
96
97 /*
98  * Perform hardware setup to enable ticks from timer 1,
99  */
100 static void prvSetupTimerInterrupt( void );
101 /*-----------------------------------------------------------*/
102
103 /*
104  * Initialise the stack of a task to look exactly as if a call to
105  * portSAVE_CONTEXT had been called.
106  *
107  * See the header file portable.h.
108  */
109 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
110 {
111     /* Place a few bytes of known values on the bottom of the stack.
112     This is just useful for debugging. */
113
114     *pxTopOfStack = 0x11111111;
115     pxTopOfStack--;
116     *pxTopOfStack = 0x22222222;
117     pxTopOfStack--;
118     *pxTopOfStack = 0x33333333;
119     pxTopOfStack--;
120
121     /* This is a redundant push to the stack, it may be required if
122     in some implementations of the compiler the parameter to the task
123     is passed on to the stack rather than in R4 register. */
124     *pxTopOfStack = (StackType_t)(pvParameters);
125     pxTopOfStack--;
126
127     *pxTopOfStack = ( StackType_t ) 0x00000000; /* RP */
128     pxTopOfStack--;
129     *pxTopOfStack = ( StackType_t ) 0x00007777; /* R7 */
130     pxTopOfStack--;
131     *pxTopOfStack = ( StackType_t ) 0x00006666; /* R6 */
132     pxTopOfStack--;
133     *pxTopOfStack = ( StackType_t ) 0x00005555; /* R5 */
134     pxTopOfStack--;
135
136     /* In the current implementation of the compiler the first
137     parameter to the task (or function) is passed via R4 parameter
138     to the task, hence the pvParameters pointer is copied into the R4
139     register. See compiler manual section 4.6.2 for more information. */
140     *pxTopOfStack = ( StackType_t ) (pvParameters); /* R4 */
141     pxTopOfStack--;
142     *pxTopOfStack = ( StackType_t ) 0x00003333; /* R3 */
143     pxTopOfStack--;
144     *pxTopOfStack = ( StackType_t ) 0x00002222; /* R2 */
145     pxTopOfStack--;
146     *pxTopOfStack = ( StackType_t ) 0x00001111; /* R1 */
147     pxTopOfStack--;
148     *pxTopOfStack = ( StackType_t ) 0x00000001; /* R0 */
149     pxTopOfStack--;
150     *pxTopOfStack = ( StackType_t ) 0x0000EEEE; /* R14 */
151     pxTopOfStack--;
152     *pxTopOfStack = ( StackType_t ) 0x0000DDDD; /* R13 */
153     pxTopOfStack--;
154     *pxTopOfStack = ( StackType_t ) 0x0000CCCC; /* R12 */
155     pxTopOfStack--;
156     *pxTopOfStack = ( StackType_t ) 0x0000BBBB; /* R11 */
157     pxTopOfStack--;
158     *pxTopOfStack = ( StackType_t ) 0x0000AAAA; /* R10 */
159     pxTopOfStack--;
160     *pxTopOfStack = ( StackType_t ) 0x00009999; /* R9 */
161     pxTopOfStack--;
162     *pxTopOfStack = ( StackType_t ) 0x00008888; /* R8 */
163     pxTopOfStack--;
164     *pxTopOfStack = ( StackType_t ) 0x11110000; /* MDH */
165     pxTopOfStack--;
166     *pxTopOfStack = ( StackType_t ) 0x22220000; /* MDL */
167     pxTopOfStack--;
168
169     /* The start of the task code. */
170     *pxTopOfStack = ( StackType_t ) pxCode; /* PC */
171     pxTopOfStack--;
172
173     /* PS - User Mode, USP, ILM=31, Interrupts enabled */
174     *pxTopOfStack = ( StackType_t ) 0x001F0030; /* PS */
175
176     return pxTopOfStack;
177 }
178 /*-----------------------------------------------------------*/
179
180 BaseType_t xPortStartScheduler( void )
181 {
182     /* Setup the hardware to generate the tick. */
183     prvSetupTimerInterrupt();
184
185     /* Restore the context of the first task that is going to run. */
186     #pragma asm
187         RestoreContext
188     #pragma endasm
189
190     /* Simulate a function call end as generated by the compiler.  We will now
191     jump to the start of the task the context of which we have just restored. */
192     __asm(" reti ");
193
194     /* Should not get here. */
195     return pdFAIL;
196 }
197 /*-----------------------------------------------------------*/
198
199 void vPortEndScheduler( void )
200 {
201     /* Not implemented - unlikely to ever be required as there is nothing to
202     return to. */
203 }
204 /*-----------------------------------------------------------*/
205
206 static void prvSetupTimerInterrupt( void )
207 {
208 /* The peripheral clock divided by 32 is used by the timer. */
209 const uint16_t usReloadValue = ( uint16_t ) ( ( ( configPER_CLOCK_HZ / configTICK_RATE_HZ ) / 32UL ) - 1UL );
210
211     /* Setup RLT0 to generate a tick interrupt. */
212
213     TMCSR0_CNTE = 0;        /* Count Disable */
214     TMCSR0_CSL = 0x2;       /* CLKP/32 */
215     TMCSR0_MOD = 0;         /* Software trigger */
216     TMCSR0_RELD = 1;        /* Reload */
217
218     TMCSR0_UF = 0;          /* Clear underflow flag */
219     TMRLR0 = usReloadValue;
220     TMCSR0_INTE = 1;        /* Interrupt Enable */
221     TMCSR0_CNTE = 1;        /* Count Enable */
222     TMCSR0_TRG = 1;         /* Trigger */
223
224     PORTEN = 0x3;           /* Port Enable */
225 }
226 /*-----------------------------------------------------------*/
227
228 #if configUSE_PREEMPTION == 1
229
230     /*
231      * Tick ISR for preemptive scheduler. The tick count is incremented
232      * after the context is saved. Then the context is switched if required,
233      * and last the context of the task which is to be resumed is restored.
234      */
235
236     #pragma asm
237
238     .global _ReloadTimer0_IRQHandler
239     _ReloadTimer0_IRQHandler:
240
241     ANDCCR #0xEF                            ;Disable Interrupts
242     SaveContext                             ;Save context
243     ORCCR #0x10                             ;Re-enable Interrupts
244
245     LDI #0xFFFB,R1
246     LDI #_tmcsr0, R0
247     AND R1,@R0                              ;Clear RLT0 interrupt flag
248
249     CALL32   _xTaskIncrementTick,R12        ;Increment Tick
250     CALL32   _vTaskSwitchContext,R12        ;Switch context if required
251
252     ANDCCR #0xEF                            ;Disable Interrupts
253     RestoreContext                          ;Restore context
254     ORCCR #0x10                             ;Re-enable Interrupts
255
256     RETI
257
258     #pragma endasm
259
260 #else
261
262     /*
263      * Tick ISR for the cooperative scheduler.  All this does is increment the
264      * tick count.  We don't need to switch context, this can only be done by
265      * manual calls to taskYIELD();
266      */
267     __interrupt void ReloadTimer0_IRQHandler( void )
268     {
269         /* Clear RLT0 interrupt flag */
270         TMCSR0_UF = 0;
271         xTaskIncrementTick();
272     }
273
274 #endif
275
276 /*
277  * Manual context switch. We can use a __nosavereg attribute  as the context
278  * would be saved by PortSAVE_CONTEXT().  The context is switched and then
279  * the context of the new task is restored saved.
280  */
281 #pragma asm
282
283     .global _vPortYieldDelayed
284     _vPortYieldDelayed:
285
286     ANDCCR #0xEF                            ;Disable Interrupts
287     SaveContext                             ;Save context
288     ORCCR #0x10                             ;Re-enable Interrupts
289
290     LDI #_dicr, R0
291     BANDL #0x0E, @R0                        ;Clear Delayed interrupt flag
292
293     CALL32   _vTaskSwitchContext,R12        ;Switch context if required
294
295     ANDCCR #0xEF                            ;Disable Interrupts
296     RestoreContext                          ;Restore context
297     ORCCR #0x10                             ;Re-enable Interrupts
298
299     RETI
300
301 #pragma endasm
302 /*-----------------------------------------------------------*/
303
304 /*
305  * Manual context switch. We can use a __nosavereg attribute  as the context
306  * would be saved by PortSAVE_CONTEXT().  The context is switched and then
307  * the context of the new task is restored saved.
308  */
309 #pragma asm
310
311     .global _vPortYield
312     _vPortYield:
313
314     SaveContext                             ;Save context
315     CALL32   _vTaskSwitchContext,R12        ;Switch context if required
316     RestoreContext                          ;Restore context
317
318     RETI
319
320 #pragma endasm
321 /*-----------------------------------------------------------*/