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