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