]> begriffs open source - freertos/blob - Source/portable/IAR/ATMega323/port.c
Update to V5.1.2.
[freertos] / Source / portable / IAR / ATMega323 / port.c
1 /*\r
2         FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26     ***************************************************************************\r
27     ***************************************************************************\r
28     *                                                                         *\r
29     * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
30         *                                                                         *\r
31         * This is a concise, step by step, 'hands on' guide that describes both   *\r
32         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
33         * explains numerous examples that are written using the FreeRTOS API.     *\r
34         * Full source code for all the examples is provided in an accompanying    *\r
35         * .zip file.                                                              *\r
36     *                                                                         *\r
37     ***************************************************************************\r
38     ***************************************************************************\r
39 \r
40         Please ensure to read the configuration and relevant port sections of the\r
41         online documentation.\r
42 \r
43         http://www.FreeRTOS.org - Documentation, latest information, license and \r
44         contact details.\r
45 \r
46         http://www.SafeRTOS.com - A version that is certified for use in safety \r
47         critical systems.\r
48 \r
49         http://www.OpenRTOS.com - Commercial support, development, porting, \r
50         licensing and training services.\r
51 */\r
52 \r
53 #include <stdlib.h>\r
54 \r
55 #include "FreeRTOS.h"\r
56 #include "task.h"\r
57 \r
58 /*-----------------------------------------------------------\r
59  * Implementation of functions defined in portable.h for the AVR/IAR port.\r
60  *----------------------------------------------------------*/\r
61 \r
62 /* Start tasks with interrupts enables. */\r
63 #define portFLAGS_INT_ENABLED                                   ( ( portSTACK_TYPE ) 0x80 )\r
64 \r
65 /* Hardware constants for timer 1. */\r
66 #define portCLEAR_COUNTER_ON_MATCH                              ( ( unsigned portCHAR ) 0x08 )\r
67 #define portPRESCALE_64                                                 ( ( unsigned portCHAR ) 0x03 )\r
68 #define portCLOCK_PRESCALER                                             ( ( unsigned portLONG ) 64 )\r
69 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE    ( ( unsigned portCHAR ) 0x10 )\r
70 \r
71 /* The number of bytes used on the hardware stack by the task start address. */\r
72 #define portBYTES_USED_BY_RETURN_ADDRESS                ( 2 )\r
73 /*-----------------------------------------------------------*/\r
74 \r
75 /* Stores the critical section nesting.  This must not be initialised to 0.\r
76 It will be initialised when a task starts. */\r
77 #define portNO_CRITICAL_NESTING                                 ( ( unsigned portBASE_TYPE ) 0 )\r
78 unsigned portBASE_TYPE uxCriticalNesting = 0x50;\r
79 \r
80 \r
81 /*\r
82  * Perform hardware setup to enable ticks from timer 1, compare match A.\r
83  */\r
84 static void prvSetupTimerInterrupt( void );\r
85 \r
86 /*\r
87  * The IAR compiler does not have full support for inline assembler, so\r
88  * these are defined in the portmacro assembler file.\r
89  */\r
90 extern void vPortYieldFromTick( void );\r
91 extern void vPortStart( void );\r
92 \r
93 /*-----------------------------------------------------------*/\r
94 \r
95 /*\r
96  * See header file for description.\r
97  */\r
98 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
99 {\r
100 unsigned portSHORT usAddress;\r
101 portSTACK_TYPE *pxTopOfHardwareStack;\r
102 \r
103         /* Place a few bytes of known values on the bottom of the stack.\r
104         This is just useful for debugging. */\r
105 \r
106         *pxTopOfStack = 0x11;\r
107         pxTopOfStack--;\r
108         *pxTopOfStack = 0x22;\r
109         pxTopOfStack--;\r
110         *pxTopOfStack = 0x33;\r
111         pxTopOfStack--;\r
112 \r
113         /* Remember where the top of the hardware stack is - this is required\r
114         below. */\r
115         pxTopOfHardwareStack = pxTopOfStack;\r
116 \r
117 \r
118         /* Simulate how the stack would look after a call to vPortYield(). */\r
119 \r
120         /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */\r
121 \r
122 \r
123 \r
124         /* The IAR compiler requires two stacks per task.  First there is the\r
125         hardware call stack which uses the AVR stack pointer.  Second there is the\r
126         software stack (local variables, parameter passing, etc.) which uses the\r
127         AVR Y register.\r
128         \r
129         This function places both stacks within the memory block passed in as the\r
130         first parameter.  The hardware stack is placed at the bottom of the memory\r
131         block.  A gap is then left for the hardware stack to grow.  Next the software\r
132         stack is placed.  The amount of space between the software and hardware\r
133         stacks is defined by configCALL_STACK_SIZE.\r
134 \r
135 \r
136 \r
137         The first part of the stack is the hardware stack.  Place the start\r
138         address of the task on the hardware stack. */\r
139         usAddress = ( unsigned portSHORT ) pxCode;\r
140         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );\r
141         pxTopOfStack--;\r
142 \r
143         usAddress >>= 8;\r
144         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );\r
145         pxTopOfStack--;\r
146 \r
147 \r
148         /* Leave enough space for the hardware stack before starting the software\r
149         stack.  The '- 2' is because we have already used two spaces for the\r
150         address of the start of the task. */\r
151         pxTopOfStack -= ( configCALL_STACK_SIZE - 2 );\r
152 \r
153 \r
154 \r
155         /* Next simulate the stack as if after a call to portSAVE_CONTEXT().\r
156         portSAVE_CONTEXT places the flags on the stack immediately after r0\r
157         to ensure the interrupts get disabled as soon as possible, and so ensuring\r
158         the stack use is minimal should a context switch interrupt occur. */\r
159         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;        /* R0 */\r
160         pxTopOfStack--;\r
161         *pxTopOfStack = portFLAGS_INT_ENABLED;\r
162         pxTopOfStack--;\r
163 \r
164         /* Next place the address of the hardware stack.  This is required so\r
165         the AVR stack pointer can be restored to point to the hardware stack. */\r
166         pxTopOfHardwareStack -= portBYTES_USED_BY_RETURN_ADDRESS;\r
167         usAddress = ( unsigned portSHORT ) pxTopOfHardwareStack;\r
168 \r
169         /* SPL */\r
170         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );\r
171         pxTopOfStack--;\r
172 \r
173         /* SPH */\r
174         usAddress >>= 8;\r
175         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );\r
176         pxTopOfStack--;\r
177 \r
178 \r
179 \r
180 \r
181         /* Now the remaining registers. */\r
182         *pxTopOfStack = ( portSTACK_TYPE ) 0x01;        /* R1 */\r
183         pxTopOfStack--;\r
184         *pxTopOfStack = ( portSTACK_TYPE ) 0x02;        /* R2 */\r
185         pxTopOfStack--;\r
186         *pxTopOfStack = ( portSTACK_TYPE ) 0x03;        /* R3 */\r
187         pxTopOfStack--;\r
188         *pxTopOfStack = ( portSTACK_TYPE ) 0x04;        /* R4 */\r
189         pxTopOfStack--;\r
190         *pxTopOfStack = ( portSTACK_TYPE ) 0x05;        /* R5 */\r
191         pxTopOfStack--;\r
192         *pxTopOfStack = ( portSTACK_TYPE ) 0x06;        /* R6 */\r
193         pxTopOfStack--;\r
194         *pxTopOfStack = ( portSTACK_TYPE ) 0x07;        /* R7 */\r
195         pxTopOfStack--;\r
196         *pxTopOfStack = ( portSTACK_TYPE ) 0x08;        /* R8 */\r
197         pxTopOfStack--;\r
198         *pxTopOfStack = ( portSTACK_TYPE ) 0x09;        /* R9 */\r
199         pxTopOfStack--;\r
200         *pxTopOfStack = ( portSTACK_TYPE ) 0x10;        /* R10 */\r
201         pxTopOfStack--;\r
202         *pxTopOfStack = ( portSTACK_TYPE ) 0x11;        /* R11 */\r
203         pxTopOfStack--;\r
204         *pxTopOfStack = ( portSTACK_TYPE ) 0x12;        /* R12 */\r
205         pxTopOfStack--;\r
206         *pxTopOfStack = ( portSTACK_TYPE ) 0x13;        /* R13 */\r
207         pxTopOfStack--;\r
208         *pxTopOfStack = ( portSTACK_TYPE ) 0x14;        /* R14 */\r
209         pxTopOfStack--;\r
210         *pxTopOfStack = ( portSTACK_TYPE ) 0x15;        /* R15 */\r
211         pxTopOfStack--;\r
212 \r
213         /* Place the parameter on the stack in the expected location. */\r
214         usAddress = ( unsigned portSHORT ) pvParameters;\r
215         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );\r
216         pxTopOfStack--;\r
217 \r
218         usAddress >>= 8;\r
219         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );\r
220         pxTopOfStack--;\r
221 \r
222         *pxTopOfStack = ( portSTACK_TYPE ) 0x18;        /* R18 */\r
223         pxTopOfStack--;\r
224         *pxTopOfStack = ( portSTACK_TYPE ) 0x19;        /* R19 */\r
225         pxTopOfStack--;\r
226         *pxTopOfStack = ( portSTACK_TYPE ) 0x20;        /* R20 */\r
227         pxTopOfStack--;\r
228         *pxTopOfStack = ( portSTACK_TYPE ) 0x21;        /* R21 */\r
229         pxTopOfStack--;\r
230         *pxTopOfStack = ( portSTACK_TYPE ) 0x22;        /* R22 */\r
231         pxTopOfStack--;\r
232         *pxTopOfStack = ( portSTACK_TYPE ) 0x23;        /* R23 */\r
233         pxTopOfStack--;\r
234         *pxTopOfStack = ( portSTACK_TYPE ) 0x24;        /* R24 */\r
235         pxTopOfStack--;\r
236         *pxTopOfStack = ( portSTACK_TYPE ) 0x25;        /* R25 */\r
237         pxTopOfStack--;\r
238         *pxTopOfStack = ( portSTACK_TYPE ) 0x26;        /* R26 X */\r
239         pxTopOfStack--;\r
240         *pxTopOfStack = ( portSTACK_TYPE ) 0x27;        /* R27 */\r
241         pxTopOfStack--;\r
242 \r
243         /* The Y register is not stored as it is used as the software stack and\r
244         gets saved into the task control block. */\r
245 \r
246         *pxTopOfStack = ( portSTACK_TYPE ) 0x30;        /* R30 Z */\r
247         pxTopOfStack--;\r
248         *pxTopOfStack = ( portSTACK_TYPE ) 0x031;       /* R31 */\r
249 \r
250         pxTopOfStack--;\r
251         *pxTopOfStack = portNO_CRITICAL_NESTING;        /* Critical nesting is zero when the task starts. */\r
252 \r
253         /*lint +e950 +e611 +e923 */\r
254 \r
255         return pxTopOfStack;\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 portBASE_TYPE xPortStartScheduler( void )\r
260 {\r
261         /* Setup the hardware to generate the tick. */\r
262         prvSetupTimerInterrupt();\r
263 \r
264         /* Restore the context of the first task that is going to run.\r
265         Normally we would just call portRESTORE_CONTEXT() here, but as the IAR\r
266         compiler does not fully support inline assembler we have to make a call.*/\r
267         vPortStart();\r
268 \r
269 \r
270         /* Should not get here! */\r
271         return pdTRUE;\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 void vPortEndScheduler( void )\r
276 {\r
277         /* It is unlikely that the AVR port will get stopped.  If required simply\r
278         disable the tick interrupt here. */\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 /*\r
283  * Setup timer 1 compare match A to generate a tick interrupt.\r
284  */\r
285 static void prvSetupTimerInterrupt( void )\r
286 {\r
287 unsigned portLONG ulCompareMatch;\r
288 unsigned portCHAR ucHighByte, ucLowByte;\r
289 \r
290         /* Using 16bit timer 1 to generate the tick.  Correct fuses must be\r
291         selected for the configCPU_CLOCK_HZ clock. */\r
292 \r
293         ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
294 \r
295         /* We only have 16 bits so have to scale to get our required tick rate. */\r
296         ulCompareMatch /= portCLOCK_PRESCALER;\r
297 \r
298         /* Adjust for correct value. */\r
299         ulCompareMatch -= ( unsigned portLONG ) 1;\r
300 \r
301         /* Setup compare match value for compare match A.  Interrupts are disabled\r
302         before this is called so we need not worry here. */\r
303         ucLowByte = ( unsigned portCHAR ) ( ulCompareMatch & ( unsigned portLONG ) 0xff );\r
304         ulCompareMatch >>= 8;\r
305         ucHighByte = ( unsigned portCHAR ) ( ulCompareMatch & ( unsigned portLONG ) 0xff );\r
306         OCR1AH = ucHighByte;\r
307         OCR1AL = ucLowByte;\r
308 \r
309         /* Setup clock source and compare match behaviour. */\r
310         ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;\r
311         TCCR1B = ucLowByte;\r
312 \r
313         /* Enable the interrupt - this is okay as interrupt are currently globally\r
314         disabled. */\r
315         TIMSK |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;\r
316 }\r
317 /*-----------------------------------------------------------*/\r
318 \r
319 #if configUSE_PREEMPTION == 1\r
320 \r
321         /*\r
322          * Tick ISR for preemptive scheduler.  We can use a __task attribute as\r
323          * the context is saved at the start of vPortYieldFromTick().  The tick\r
324          * count is incremented after the context is saved.\r
325          */\r
326         __task void SIG_OUTPUT_COMPARE1A( void )\r
327         {\r
328                 vPortYieldFromTick();\r
329                 asm( "reti" );\r
330         }\r
331         \r
332 #else\r
333 \r
334         /*\r
335          * Tick ISR for the cooperative scheduler.  All this does is increment the\r
336          * tick count.  We don't need to switch context, this can only be done by\r
337          * manual calls to taskYIELD();\r
338          *\r
339          * THE INTERRUPT VECTOR IS POPULATED IN portmacro.s90.  DO NOT INSTALL\r
340          * IT HERE USING THE USUAL PRAGMA.\r
341          */             \r
342         __interrupt void SIG_OUTPUT_COMPARE1A( void )\r
343         {\r
344                 vTaskIncrementTick();\r
345         }\r
346 #endif\r
347 /*-----------------------------------------------------------*/\r
348 \r
349 void vPortEnterCritical( void )\r
350 {\r
351         portDISABLE_INTERRUPTS();\r
352         uxCriticalNesting++;\r
353 }\r
354 /*-----------------------------------------------------------*/\r
355 \r
356 void vPortExitCritical( void )\r
357 {\r
358         uxCriticalNesting--;\r
359         if( uxCriticalNesting == portNO_CRITICAL_NESTING )\r
360         {\r
361                 portENABLE_INTERRUPTS();\r
362         }\r
363 }\r
364 \r
365         \r