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