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