]> begriffs open source - freertos/blob - Source/portable/GCC/ATMega323/port.c
Update headers for the FreeRTOS V7.0.2 release.
[freertos] / Source / portable / GCC / ATMega323 / port.c
1 /*\r
2     FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\r
11      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
12      *    ensuring you get running as quickly as possible and with an        *\r
13      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
14      *    the FreeRTOS project to continue with its mission of providing     *\r
15      *    professional grade, cross platform, de facto standard solutions    *\r
16      *    for microcontrollers - completely free of charge!                  *\r
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \r
27     FreeRTOS is free software; you can redistribute it and/or modify it under\r
28     the terms of the GNU General Public License (version 2) as published by the\r
29     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
30     >>>NOTE<<< The modification to the GPL is included to allow you to\r
31     distribute a combined work that includes FreeRTOS without being obliged to\r
32     provide the source code for proprietary components outside of the FreeRTOS\r
33     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
34     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
35     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
36     more details. You should have received a copy of the GNU General Public\r
37     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
38     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
39     by writing to Richard Barry, contact details for whom are available on the\r
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /* \r
55 \r
56 Changes from V2.6.0\r
57 \r
58         + AVR port - Replaced the inb() and outb() functions with direct memory\r
59           access.  This allows the port to be built with the 20050414 build of\r
60           WinAVR.\r
61 */\r
62 \r
63 #include <stdlib.h>\r
64 #include <avr/interrupt.h>\r
65 \r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 \r
69 /*-----------------------------------------------------------\r
70  * Implementation of functions defined in portable.h for the AVR port.\r
71  *----------------------------------------------------------*/\r
72 \r
73 /* Start tasks with interrupts enables. */\r
74 #define portFLAGS_INT_ENABLED                                   ( ( portSTACK_TYPE ) 0x80 )\r
75 \r
76 /* Hardware constants for timer 1. */\r
77 #define portCLEAR_COUNTER_ON_MATCH                              ( ( unsigned char ) 0x08 )\r
78 #define portPRESCALE_64                                                 ( ( unsigned char ) 0x03 )\r
79 #define portCLOCK_PRESCALER                                             ( ( unsigned long ) 64 )\r
80 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE    ( ( unsigned char ) 0x10 )\r
81 \r
82 /*-----------------------------------------------------------*/\r
83 \r
84 /* We require the address of the pxCurrentTCB variable, but don't want to know\r
85 any details of its type. */\r
86 typedef void tskTCB;\r
87 extern volatile tskTCB * volatile pxCurrentTCB;\r
88 \r
89 /*-----------------------------------------------------------*/\r
90 \r
91 /* \r
92  * Macro to save all the general purpose registers, the save the stack pointer\r
93  * into the TCB.  \r
94  * \r
95  * The first thing we do is save the flags then disable interrupts.  This is to \r
96  * guard our stack against having a context switch interrupt after we have already \r
97  * pushed the registers onto the stack - causing the 32 registers to be on the \r
98  * stack twice. \r
99  * \r
100  * r1 is set to zero as the compiler expects it to be thus, however some\r
101  * of the math routines make use of R1. \r
102  * \r
103  * The interrupts will have been disabled during the call to portSAVE_CONTEXT()\r
104  * so we need not worry about reading/writing to the stack pointer. \r
105  */\r
106 \r
107 #define portSAVE_CONTEXT()                                                                      \\r
108         asm volatile (  "push   r0                                              \n\t"   \\r
109                                         "in             r0, __SREG__                    \n\t"   \\r
110                                         "cli                                                    \n\t"   \\r
111                                         "push   r0                                              \n\t"   \\r
112                                         "push   r1                                              \n\t"   \\r
113                                         "clr    r1                                              \n\t"   \\r
114                                         "push   r2                                              \n\t"   \\r
115                                         "push   r3                                              \n\t"   \\r
116                                         "push   r4                                              \n\t"   \\r
117                                         "push   r5                                              \n\t"   \\r
118                                         "push   r6                                              \n\t"   \\r
119                                         "push   r7                                              \n\t"   \\r
120                                         "push   r8                                              \n\t"   \\r
121                                         "push   r9                                              \n\t"   \\r
122                                         "push   r10                                             \n\t"   \\r
123                                         "push   r11                                             \n\t"   \\r
124                                         "push   r12                                             \n\t"   \\r
125                                         "push   r13                                             \n\t"   \\r
126                                         "push   r14                                             \n\t"   \\r
127                                         "push   r15                                             \n\t"   \\r
128                                         "push   r16                                             \n\t"   \\r
129                                         "push   r17                                             \n\t"   \\r
130                                         "push   r18                                             \n\t"   \\r
131                                         "push   r19                                             \n\t"   \\r
132                                         "push   r20                                             \n\t"   \\r
133                                         "push   r21                                             \n\t"   \\r
134                                         "push   r22                                             \n\t"   \\r
135                                         "push   r23                                             \n\t"   \\r
136                                         "push   r24                                             \n\t"   \\r
137                                         "push   r25                                             \n\t"   \\r
138                                         "push   r26                                             \n\t"   \\r
139                                         "push   r27                                             \n\t"   \\r
140                                         "push   r28                                             \n\t"   \\r
141                                         "push   r29                                             \n\t"   \\r
142                                         "push   r30                                             \n\t"   \\r
143                                         "push   r31                                             \n\t"   \\r
144                                         "lds    r26, pxCurrentTCB               \n\t"   \\r
145                                         "lds    r27, pxCurrentTCB + 1   \n\t"   \\r
146                                         "in             r0, 0x3d                                \n\t"   \\r
147                                         "st             x+, r0                                  \n\t"   \\r
148                                         "in             r0, 0x3e                                \n\t"   \\r
149                                         "st             x+, r0                                  \n\t"   \\r
150                                 );\r
151 \r
152 /* \r
153  * Opposite to portSAVE_CONTEXT().  Interrupts will have been disabled during\r
154  * the context save so we can write to the stack pointer. \r
155  */\r
156 \r
157 #define portRESTORE_CONTEXT()                                                           \\r
158         asm volatile (  "lds    r26, pxCurrentTCB               \n\t"   \\r
159                                         "lds    r27, pxCurrentTCB + 1   \n\t"   \\r
160                                         "ld             r28, x+                                 \n\t"   \\r
161                                         "out    __SP_L__, r28                   \n\t"   \\r
162                                         "ld             r29, x+                                 \n\t"   \\r
163                                         "out    __SP_H__, r29                   \n\t"   \\r
164                                         "pop    r31                                             \n\t"   \\r
165                                         "pop    r30                                             \n\t"   \\r
166                                         "pop    r29                                             \n\t"   \\r
167                                         "pop    r28                                             \n\t"   \\r
168                                         "pop    r27                                             \n\t"   \\r
169                                         "pop    r26                                             \n\t"   \\r
170                                         "pop    r25                                             \n\t"   \\r
171                                         "pop    r24                                             \n\t"   \\r
172                                         "pop    r23                                             \n\t"   \\r
173                                         "pop    r22                                             \n\t"   \\r
174                                         "pop    r21                                             \n\t"   \\r
175                                         "pop    r20                                             \n\t"   \\r
176                                         "pop    r19                                             \n\t"   \\r
177                                         "pop    r18                                             \n\t"   \\r
178                                         "pop    r17                                             \n\t"   \\r
179                                         "pop    r16                                             \n\t"   \\r
180                                         "pop    r15                                             \n\t"   \\r
181                                         "pop    r14                                             \n\t"   \\r
182                                         "pop    r13                                             \n\t"   \\r
183                                         "pop    r12                                             \n\t"   \\r
184                                         "pop    r11                                             \n\t"   \\r
185                                         "pop    r10                                             \n\t"   \\r
186                                         "pop    r9                                              \n\t"   \\r
187                                         "pop    r8                                              \n\t"   \\r
188                                         "pop    r7                                              \n\t"   \\r
189                                         "pop    r6                                              \n\t"   \\r
190                                         "pop    r5                                              \n\t"   \\r
191                                         "pop    r4                                              \n\t"   \\r
192                                         "pop    r3                                              \n\t"   \\r
193                                         "pop    r2                                              \n\t"   \\r
194                                         "pop    r1                                              \n\t"   \\r
195                                         "pop    r0                                              \n\t"   \\r
196                                         "out    __SREG__, r0                    \n\t"   \\r
197                                         "pop    r0                                              \n\t"   \\r
198                                 );\r
199 \r
200 /*-----------------------------------------------------------*/\r
201 \r
202 /*\r
203  * Perform hardware setup to enable ticks from timer 1, compare match A.\r
204  */\r
205 static void prvSetupTimerInterrupt( void );\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 /* \r
209  * See header file for description. \r
210  */\r
211 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
212 {\r
213 unsigned short usAddress;\r
214 \r
215         /* Place a few bytes of known values on the bottom of the stack. \r
216         This is just useful for debugging. */\r
217 \r
218         *pxTopOfStack = 0x11;\r
219         pxTopOfStack--;\r
220         *pxTopOfStack = 0x22;\r
221         pxTopOfStack--;\r
222         *pxTopOfStack = 0x33;\r
223         pxTopOfStack--;\r
224 \r
225         /* Simulate how the stack would look after a call to vPortYield() generated by \r
226         the compiler. */\r
227 \r
228         /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */\r
229 \r
230         /* The start of the task code will be popped off the stack last, so place\r
231         it on first. */\r
232         usAddress = ( unsigned short ) pxCode;\r
233         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );\r
234         pxTopOfStack--;\r
235 \r
236         usAddress >>= 8;\r
237         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );\r
238         pxTopOfStack--;\r
239 \r
240         /* Next simulate the stack as if after a call to portSAVE_CONTEXT().  \r
241         portSAVE_CONTEXT places the flags on the stack immediately after r0\r
242         to ensure the interrupts get disabled as soon as possible, and so ensuring\r
243         the stack use is minimal should a context switch interrupt occur. */\r
244         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;        /* R0 */\r
245         pxTopOfStack--;\r
246         *pxTopOfStack = portFLAGS_INT_ENABLED;\r
247         pxTopOfStack--;\r
248 \r
249 \r
250         /* Now the remaining registers.   The compiler expects R1 to be 0. */\r
251         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;        /* R1 */\r
252         pxTopOfStack--;\r
253         *pxTopOfStack = ( portSTACK_TYPE ) 0x02;        /* R2 */\r
254         pxTopOfStack--;\r
255         *pxTopOfStack = ( portSTACK_TYPE ) 0x03;        /* R3 */\r
256         pxTopOfStack--;\r
257         *pxTopOfStack = ( portSTACK_TYPE ) 0x04;        /* R4 */\r
258         pxTopOfStack--;\r
259         *pxTopOfStack = ( portSTACK_TYPE ) 0x05;        /* R5 */\r
260         pxTopOfStack--;\r
261         *pxTopOfStack = ( portSTACK_TYPE ) 0x06;        /* R6 */\r
262         pxTopOfStack--;\r
263         *pxTopOfStack = ( portSTACK_TYPE ) 0x07;        /* R7 */\r
264         pxTopOfStack--;\r
265         *pxTopOfStack = ( portSTACK_TYPE ) 0x08;        /* R8 */\r
266         pxTopOfStack--;\r
267         *pxTopOfStack = ( portSTACK_TYPE ) 0x09;        /* R9 */\r
268         pxTopOfStack--;\r
269         *pxTopOfStack = ( portSTACK_TYPE ) 0x10;        /* R10 */\r
270         pxTopOfStack--;\r
271         *pxTopOfStack = ( portSTACK_TYPE ) 0x11;        /* R11 */\r
272         pxTopOfStack--;\r
273         *pxTopOfStack = ( portSTACK_TYPE ) 0x12;        /* R12 */\r
274         pxTopOfStack--;\r
275         *pxTopOfStack = ( portSTACK_TYPE ) 0x13;        /* R13 */\r
276         pxTopOfStack--;\r
277         *pxTopOfStack = ( portSTACK_TYPE ) 0x14;        /* R14 */\r
278         pxTopOfStack--;\r
279         *pxTopOfStack = ( portSTACK_TYPE ) 0x15;        /* R15 */\r
280         pxTopOfStack--;\r
281         *pxTopOfStack = ( portSTACK_TYPE ) 0x16;        /* R16 */\r
282         pxTopOfStack--;\r
283         *pxTopOfStack = ( portSTACK_TYPE ) 0x17;        /* R17 */\r
284         pxTopOfStack--;\r
285         *pxTopOfStack = ( portSTACK_TYPE ) 0x18;        /* R18 */\r
286         pxTopOfStack--;\r
287         *pxTopOfStack = ( portSTACK_TYPE ) 0x19;        /* R19 */\r
288         pxTopOfStack--;\r
289         *pxTopOfStack = ( portSTACK_TYPE ) 0x20;        /* R20 */\r
290         pxTopOfStack--;\r
291         *pxTopOfStack = ( portSTACK_TYPE ) 0x21;        /* R21 */\r
292         pxTopOfStack--;\r
293         *pxTopOfStack = ( portSTACK_TYPE ) 0x22;        /* R22 */\r
294         pxTopOfStack--;\r
295         *pxTopOfStack = ( portSTACK_TYPE ) 0x23;        /* R23 */\r
296         pxTopOfStack--;\r
297 \r
298         /* Place the parameter on the stack in the expected location. */\r
299         usAddress = ( unsigned short ) pvParameters;\r
300         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );\r
301         pxTopOfStack--;\r
302 \r
303         usAddress >>= 8;\r
304         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned short ) 0x00ff );\r
305         pxTopOfStack--;\r
306 \r
307         *pxTopOfStack = ( portSTACK_TYPE ) 0x26;        /* R26 X */\r
308         pxTopOfStack--;\r
309         *pxTopOfStack = ( portSTACK_TYPE ) 0x27;        /* R27 */\r
310         pxTopOfStack--;\r
311         *pxTopOfStack = ( portSTACK_TYPE ) 0x28;        /* R28 Y */\r
312         pxTopOfStack--;\r
313         *pxTopOfStack = ( portSTACK_TYPE ) 0x29;        /* R29 */\r
314         pxTopOfStack--;\r
315         *pxTopOfStack = ( portSTACK_TYPE ) 0x30;        /* R30 Z */\r
316         pxTopOfStack--;\r
317         *pxTopOfStack = ( portSTACK_TYPE ) 0x031;       /* R31 */\r
318         pxTopOfStack--;\r
319 \r
320         /*lint +e950 +e611 +e923 */\r
321 \r
322         return pxTopOfStack;\r
323 }\r
324 /*-----------------------------------------------------------*/\r
325 \r
326 portBASE_TYPE xPortStartScheduler( void )\r
327 {\r
328         /* Setup the hardware to generate the tick. */\r
329         prvSetupTimerInterrupt();\r
330 \r
331         /* Restore the context of the first task that is going to run. */\r
332         portRESTORE_CONTEXT();\r
333 \r
334         /* Simulate a function call end as generated by the compiler.  We will now\r
335         jump to the start of the task the context of which we have just restored. */\r
336         asm volatile ( "ret" );\r
337 \r
338         /* Should not get here. */\r
339         return pdTRUE;\r
340 }\r
341 /*-----------------------------------------------------------*/\r
342 \r
343 void vPortEndScheduler( void )\r
344 {\r
345         /* It is unlikely that the AVR port will get stopped.  If required simply\r
346         disable the tick interrupt here. */\r
347 }\r
348 /*-----------------------------------------------------------*/\r
349 \r
350 /*\r
351  * Manual context switch.  The first thing we do is save the registers so we\r
352  * can use a naked attribute.\r
353  */\r
354 void vPortYield( void ) __attribute__ ( ( naked ) );\r
355 void vPortYield( void )\r
356 {\r
357         portSAVE_CONTEXT();\r
358         vTaskSwitchContext();\r
359         portRESTORE_CONTEXT();\r
360 \r
361         asm volatile ( "ret" );\r
362 }\r
363 /*-----------------------------------------------------------*/\r
364 \r
365 /*\r
366  * Context switch function used by the tick.  This must be identical to \r
367  * vPortYield() from the call to vTaskSwitchContext() onwards.  The only\r
368  * difference from vPortYield() is the tick count is incremented as the\r
369  * call comes from the tick ISR.\r
370  */\r
371 void vPortYieldFromTick( void ) __attribute__ ( ( naked ) );\r
372 void vPortYieldFromTick( void )\r
373 {\r
374         portSAVE_CONTEXT();\r
375         vTaskIncrementTick();\r
376         vTaskSwitchContext();\r
377         portRESTORE_CONTEXT();\r
378 \r
379         asm volatile ( "ret" );\r
380 }\r
381 /*-----------------------------------------------------------*/\r
382 \r
383 /*\r
384  * Setup timer 1 compare match A to generate a tick interrupt.\r
385  */\r
386 static void prvSetupTimerInterrupt( void )\r
387 {\r
388 unsigned long ulCompareMatch;\r
389 unsigned char ucHighByte, ucLowByte;\r
390 \r
391         /* Using 16bit timer 1 to generate the tick.  Correct fuses must be\r
392         selected for the configCPU_CLOCK_HZ clock. */\r
393 \r
394         ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
395 \r
396         /* We only have 16 bits so have to scale to get our required tick rate. */\r
397         ulCompareMatch /= portCLOCK_PRESCALER;\r
398 \r
399         /* Adjust for correct value. */\r
400         ulCompareMatch -= ( unsigned long ) 1;\r
401 \r
402         /* Setup compare match value for compare match A.  Interrupts are disabled \r
403         before this is called so we need not worry here. */\r
404         ucLowByte = ( unsigned char ) ( ulCompareMatch & ( unsigned long ) 0xff );\r
405         ulCompareMatch >>= 8;\r
406         ucHighByte = ( unsigned char ) ( ulCompareMatch & ( unsigned long ) 0xff );\r
407         OCR1AH = ucHighByte;\r
408         OCR1AL = ucLowByte;\r
409 \r
410         /* Setup clock source and compare match behaviour. */\r
411         ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;\r
412         TCCR1B = ucLowByte;\r
413 \r
414         /* Enable the interrupt - this is okay as interrupt are currently globally\r
415         disabled. */\r
416         ucLowByte = TIMSK;\r
417         ucLowByte |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;\r
418         TIMSK = ucLowByte;\r
419 }\r
420 /*-----------------------------------------------------------*/\r
421 \r
422 #if configUSE_PREEMPTION == 1\r
423 \r
424         /*\r
425          * Tick ISR for preemptive scheduler.  We can use a naked attribute as\r
426          * the context is saved at the start of vPortYieldFromTick().  The tick\r
427          * count is incremented after the context is saved.\r
428          */\r
429         void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal, naked ) );\r
430         void SIG_OUTPUT_COMPARE1A( void )\r
431         {\r
432                 vPortYieldFromTick();\r
433                 asm volatile ( "reti" );\r
434         }\r
435 #else\r
436 \r
437         /*\r
438          * Tick ISR for the cooperative scheduler.  All this does is increment the\r
439          * tick count.  We don't need to switch context, this can only be done by\r
440          * manual calls to taskYIELD();\r
441          */\r
442         void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal ) );\r
443         void SIG_OUTPUT_COMPARE1A( void )\r
444         {\r
445                 vTaskIncrementTick();\r
446         }\r
447 #endif\r
448 \r
449 \r
450         \r