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