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