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