]> begriffs open source - freertos/blob - portable/GCC/AVR_AVRDx/port.c
Add SPDX-License-Identifier: MIT to MIT licensed files.
[freertos] / portable / GCC / AVR_AVRDx / port.c
1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
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 <stdlib.h>
30
31 #include <avr/interrupt.h>
32 #include "porthardware.h"
33 #include "FreeRTOS.h"
34 #include "task.h"
35
36 /*-----------------------------------------------------------
37 * Implementation of functions defined in portable.h for the AVR port.
38 *----------------------------------------------------------*/
39
40 /* Start tasks with interrupts enables. */
41 #define portFLAGS_INT_ENABLED    ( ( StackType_t ) 0x80 )
42
43 /*-----------------------------------------------------------*/
44
45 /* We require the address of the pxCurrentTCB variable, but don't want to know
46  * any details of its type. */
47 typedef void RTOS_TCB_t;
48 extern volatile RTOS_TCB_t * volatile pxCurrentTCB;
49
50 /*-----------------------------------------------------------*/
51
52 /*
53  * Macro to save all the general purpose registers, the save the stack pointer
54  * into the TCB.
55  *
56  * The first thing we do is save the flags then disable interrupts.  This is to
57  * guard our stack against having a context switch interrupt after we have already
58  * pushed the registers onto the stack - causing the 32 registers to be on the
59  * stack twice.
60  *
61  * r1 is set to zero as the compiler expects it to be thus, however some
62  * of the math routines make use of R1.
63  *
64  * The interrupts will have been disabled during the call to portSAVE_CONTEXT()
65  * so we need not worry about reading/writing to the stack pointer.
66  */
67
68 #define portSAVE_CONTEXT()                              \
69     asm volatile ( "push  r0                      \n\t" \
70                    "in    r0, __SREG__            \n\t" \
71                    "cli                           \n\t" \
72                    "push  r0                      \n\t" \
73                    "in    r0, __RAMPZ__           \n\t" \
74                    "push  r0                      \n\t" \
75                    "push  r1                      \n\t" \
76                    "clr   r1                      \n\t" \
77                    "push  r2                      \n\t" \
78                    "push  r3                      \n\t" \
79                    "push  r4                      \n\t" \
80                    "push  r5                      \n\t" \
81                    "push  r6                      \n\t" \
82                    "push  r7                      \n\t" \
83                    "push  r8                      \n\t" \
84                    "push  r9                      \n\t" \
85                    "push  r10                     \n\t" \
86                    "push  r11                     \n\t" \
87                    "push  r12                     \n\t" \
88                    "push  r13                     \n\t" \
89                    "push  r14                     \n\t" \
90                    "push  r15                     \n\t" \
91                    "push  r16                     \n\t" \
92                    "push  r17                     \n\t" \
93                    "push  r18                     \n\t" \
94                    "push  r19                     \n\t" \
95                    "push  r20                     \n\t" \
96                    "push  r21                     \n\t" \
97                    "push  r22                     \n\t" \
98                    "push  r23                     \n\t" \
99                    "push  r24                     \n\t" \
100                    "push  r25                     \n\t" \
101                    "push  r26                     \n\t" \
102                    "push  r27                     \n\t" \
103                    "push  r28                     \n\t" \
104                    "push  r29                     \n\t" \
105                    "push  r30                     \n\t" \
106                    "push  r31                     \n\t" \
107                    "lds   r26, pxCurrentTCB       \n\t" \
108                    "lds   r27, pxCurrentTCB + 1   \n\t" \
109                    "in    r0, __SP_L__            \n\t" \
110                    "st    x+, r0                  \n\t" \
111                    "in    r0, __SP_H__            \n\t" \
112                    "st    x+, r0                  \n\t" );
113
114 /*
115  * Opposite to portSAVE_CONTEXT().  Interrupts will have been disabled during
116  * the context save so we can write to the stack pointer.
117  */
118
119 #define portRESTORE_CONTEXT()                           \
120     asm volatile ( "lds   r26, pxCurrentTCB       \n\t" \
121                    "lds   r27, pxCurrentTCB + 1   \n\t" \
122                    "ld    r28, x+                 \n\t" \
123                    "out   __SP_L__, r28           \n\t" \
124                    "ld    r29, x+                 \n\t" \
125                    "out   __SP_H__, r29           \n\t" \
126                    "pop   r31                     \n\t" \
127                    "pop   r30                     \n\t" \
128                    "pop   r29                     \n\t" \
129                    "pop   r28                     \n\t" \
130                    "pop   r27                     \n\t" \
131                    "pop   r26                     \n\t" \
132                    "pop   r25                     \n\t" \
133                    "pop   r24                     \n\t" \
134                    "pop   r23                     \n\t" \
135                    "pop   r22                     \n\t" \
136                    "pop   r21                     \n\t" \
137                    "pop   r20                     \n\t" \
138                    "pop   r19                     \n\t" \
139                    "pop   r18                     \n\t" \
140                    "pop   r17                     \n\t" \
141                    "pop   r16                     \n\t" \
142                    "pop   r15                     \n\t" \
143                    "pop   r14                     \n\t" \
144                    "pop   r13                     \n\t" \
145                    "pop   r12                     \n\t" \
146                    "pop   r11                     \n\t" \
147                    "pop   r10                     \n\t" \
148                    "pop   r9                      \n\t" \
149                    "pop   r8                      \n\t" \
150                    "pop   r7                      \n\t" \
151                    "pop   r6                      \n\t" \
152                    "pop   r5                      \n\t" \
153                    "pop   r4                      \n\t" \
154                    "pop   r3                      \n\t" \
155                    "pop   r2                      \n\t" \
156                    "pop   r1                      \n\t" \
157                    "pop   r0                      \n\t" \
158                    "out   __RAMPZ__, r0           \n\t" \
159                    "pop   r0                      \n\t" \
160                    "out   __SREG__, r0            \n\t" \
161                    "pop   r0                      \n\t" );
162
163 /*-----------------------------------------------------------*/
164
165 /*
166  * Perform hardware setup to enable ticks from timer.
167  */
168 static void prvSetupTimerInterrupt( void );
169 /*-----------------------------------------------------------*/
170
171 /*
172  * See header file for description.
173  */
174 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
175                                      TaskFunction_t pxCode,
176                                      void * pvParameters )
177 {
178     uint16_t usAddress;
179
180     /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */
181
182     /* Place a few bytes of known values on the bottom of the stack.
183      * This is just useful for debugging. Uncomment if needed. */
184     /* *pxTopOfStack = 0x11; */
185     /* pxTopOfStack--; */
186     /* *pxTopOfStack = 0x22; */
187     /* pxTopOfStack--; */
188     /* *pxTopOfStack = 0x33; */
189     /* pxTopOfStack--; */
190
191     /* The start of the task code will be popped off the stack last, so place
192      * it on first. */
193     usAddress = ( uint16_t ) pxCode;
194     *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
195     pxTopOfStack--;
196
197     usAddress >>= 8;
198     *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
199     pxTopOfStack--;
200
201     /* Next simulate the stack as if after a call to portSAVE_CONTEXT().
202     *  portSAVE_CONTEXT places the flags on the stack immediately after r0
203     *  to ensure the interrupts get disabled as soon as possible, and so ensuring
204     *  the stack use is minimal should a context switch interrupt occur. */
205     *pxTopOfStack = ( StackType_t ) 0x00; /* R0 */
206     pxTopOfStack--;
207     *pxTopOfStack = portFLAGS_INT_ENABLED;
208     pxTopOfStack--;
209     *pxTopOfStack = ( StackType_t ) 0x00; /* RAMPZ */
210     pxTopOfStack--;
211
212     /* Now the remaining registers.   The compiler expects R1 to be 0. */
213     *pxTopOfStack = ( StackType_t ) 0x00; /* R1 */
214
215     /* Leave R2 - R23 untouched */
216     pxTopOfStack -= 23;
217
218     /* Place the parameter on the stack in the expected location. */
219     usAddress = ( uint16_t ) pvParameters;
220     *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
221     pxTopOfStack--;
222
223     usAddress >>= 8;
224     *pxTopOfStack = ( StackType_t ) ( usAddress & ( uint16_t ) 0x00ff );
225
226     /* Leave register R26 - R31 untouched */
227     pxTopOfStack -= 7;
228
229     /*lint +e950 +e611 +e923 */
230
231     return pxTopOfStack;
232 }
233 /*-----------------------------------------------------------*/
234
235 BaseType_t xPortStartScheduler( void )
236 {
237     /* Setup the hardware to generate the tick. */
238     prvSetupTimerInterrupt();
239
240     /* Restore the context of the first task that is going to run. */
241     portRESTORE_CONTEXT();
242
243     /* Simulate a function call end as generated by the compiler.  We will now
244      * jump to the start of the task the context of which we have just restored. */
245     asm volatile ( "ret" );
246
247     /* Should not get here. */
248     return pdTRUE;
249 }
250 /*-----------------------------------------------------------*/
251
252 void vPortEndScheduler( void )
253 {
254     /* vPortEndScheduler is not implemented in this port. */
255 }
256 /*-----------------------------------------------------------*/
257
258 /*
259  * Manual context switch.  The first thing we do is save the registers so we
260  * can use a naked attribute.
261  */
262 void vPortYield( void ) __attribute__( ( naked ) );
263 void vPortYield( void )
264 {
265     portSAVE_CONTEXT();
266     vTaskSwitchContext();
267     portRESTORE_CONTEXT();
268     asm volatile ( "ret" );
269 }
270 /*-----------------------------------------------------------*/
271
272 /*
273  * Manual context switch callable from ISRs. The first thing
274  * we do is save the registers so we can use a naked attribute.
275  */
276 void vPortYieldFromISR( void ) __attribute__( ( naked ) );
277 void vPortYieldFromISR( void )
278 {
279     portSAVE_CONTEXT();
280     vTaskSwitchContext();
281     portRESTORE_CONTEXT();
282     asm volatile ( "reti" );
283 }
284 /*-----------------------------------------------------------*/
285
286 /*
287  * Context switch function used by the tick.  This must be identical to
288  * vPortYield() from the call to vTaskSwitchContext() onwards.  The only
289  * difference from vPortYield() is the tick count is incremented as the
290  * call comes from the tick ISR.
291  */
292 void vPortYieldFromTick( void ) __attribute__( ( naked ) );
293 void vPortYieldFromTick( void )
294 {
295     portSAVE_CONTEXT();
296
297     if( xTaskIncrementTick() != pdFALSE )
298     {
299         vTaskSwitchContext();
300     }
301
302     portRESTORE_CONTEXT();
303
304     asm volatile ( "reti" );
305 }
306 /*-----------------------------------------------------------*/
307
308 /*
309  * Setup timer to generate a tick interrupt.
310  */
311 static void prvSetupTimerInterrupt( void )
312 {
313     TICK_init();
314 }
315 /*-----------------------------------------------------------*/
316
317 #if configUSE_PREEMPTION == 1
318
319 /*
320  * Tick ISR for preemptive scheduler.  We can use a naked attribute as
321  * the context is saved at the start of vPortYieldFromTick().  The tick
322  * count is incremented after the context is saved.
323  */
324     ISR( TICK_INT_vect, ISR_NAKED )
325     {
326         /* Clear tick interrupt flag. */
327         CLR_INT( INT_FLAGS, INT_MASK );
328
329         vPortYieldFromTick();
330
331         asm volatile ( "reti" );
332     }
333 #else  /* if configUSE_PREEMPTION == 1 */
334
335 /*
336  * Tick ISR for the cooperative scheduler.  All this does is increment the
337  * tick count.  We don't need to switch context, this can only be done by
338  * manual calls to taskYIELD();
339  */
340     ISR( TICK_INT_vect )
341     {
342         /* Clear tick interrupt flag. */
343         INT_FLAGS = INT_MASK;
344         xTaskIncrementTick();
345     }
346 #endif /* if configUSE_PREEMPTION == 1 */