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