]> git.sur5r.net Git - freertos/blob - Source/portable/IAR/ATMega323/port.c
4189923adf16616f3b06d435b1824f68ef9f0bd6
[freertos] / Source / portable / IAR / ATMega323 / port.c
1 /*\r
2         FreeRTOS.org V4.0.5 - Copyright (C) 2003-2006 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         See http://www.FreeRTOS.org for documentation, latest information, license\r
28         and contact details.  Please ensure to read the configuration and relevant\r
29         port sections of the online documentation.\r
30         ***************************************************************************\r
31 */\r
32 \r
33 #include <stdlib.h>\r
34 \r
35 #include "FreeRTOS.h"\r
36 #include "task.h"\r
37 \r
38 /*-----------------------------------------------------------\r
39  * Implementation of functions defined in portable.h for the AVR/IAR port.\r
40  *----------------------------------------------------------*/\r
41 \r
42 /* Start tasks with interrupts enables. */\r
43 #define portFLAGS_INT_ENABLED                                   ( ( portSTACK_TYPE ) 0x80 )\r
44 \r
45 /* Hardware constants for timer 1. */\r
46 #define portCLEAR_COUNTER_ON_MATCH                              ( ( unsigned portCHAR ) 0x08 )\r
47 #define portPRESCALE_64                                                 ( ( unsigned portCHAR ) 0x03 )\r
48 #define portCLOCK_PRESCALER                                             ( ( unsigned portLONG ) 64 )\r
49 #define portCOMPARE_MATCH_A_INTERRUPT_ENABLE    ( ( unsigned portCHAR ) 0x10 )\r
50 \r
51 /* The number of bytes used on the hardware stack by the task start address. */\r
52 #define portBYTES_USED_BY_RETURN_ADDRESS                ( 2 )\r
53 /*-----------------------------------------------------------*/\r
54 \r
55 /*\r
56  * Perform hardware setup to enable ticks from timer 1, compare match A.\r
57  */\r
58 static void prvSetupTimerInterrupt( void );\r
59 \r
60 /*\r
61  * The IAR compiler does not have full support for inline assembler, so\r
62  * these are defined in the portmacro assembler file.\r
63  */\r
64 extern void vPortYieldFromTick( void );\r
65 extern void vPortStart( void );\r
66 \r
67 /*-----------------------------------------------------------*/\r
68 \r
69 /*\r
70  * See header file for description.\r
71  */\r
72 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
73 {\r
74 unsigned portSHORT usAddress;\r
75 portSTACK_TYPE *pxTopOfHardwareStack;\r
76 \r
77         /* Place a few bytes of known values on the bottom of the stack.\r
78         This is just useful for debugging. */\r
79 \r
80         *pxTopOfStack = 0x11;\r
81         pxTopOfStack--;\r
82         *pxTopOfStack = 0x22;\r
83         pxTopOfStack--;\r
84         *pxTopOfStack = 0x33;\r
85         pxTopOfStack--;\r
86 \r
87         /* Remember where the top of the hardware stack is - this is required\r
88         below. */\r
89         pxTopOfHardwareStack = pxTopOfStack;\r
90 \r
91 \r
92         /* Simulate how the stack would look after a call to vPortYield(). */\r
93 \r
94         /*lint -e950 -e611 -e923 Lint doesn't like this much - but nothing I can do about it. */\r
95 \r
96 \r
97 \r
98         /* The IAR compiler requires two stacks per task.  First there is the\r
99         hardware call stack which uses the AVR stack pointer.  Second there is the\r
100         software stack (local variables, parameter passing, etc.) which uses the\r
101         AVR Y register.\r
102         \r
103         This function places both stacks within the memory block passed in as the\r
104         first parameter.  The hardware stack is placed at the bottom of the memory\r
105         block.  A gap is then left for the hardware stack to grow.  Next the software\r
106         stack is placed.  The amount of space between the software and hardware\r
107         stacks is defined by configCALL_STACK_SIZE.\r
108 \r
109 \r
110 \r
111         The first part of the stack is the hardware stack.  Place the start\r
112         address of the task on the hardware stack. */\r
113         usAddress = ( unsigned portSHORT ) pxCode;\r
114         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );\r
115         pxTopOfStack--;\r
116 \r
117         usAddress >>= 8;\r
118         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );\r
119         pxTopOfStack--;\r
120 \r
121 \r
122         /* Leave enough space for the hardware stack before starting the software\r
123         stack.  The '- 2' is because we have already used two spaces for the\r
124         address of the start of the task. */\r
125         pxTopOfStack -= ( configCALL_STACK_SIZE - 2 );\r
126 \r
127 \r
128 \r
129         /* Next simulate the stack as if after a call to portSAVE_CONTEXT().\r
130         portSAVE_CONTEXT places the flags on the stack immediately after r0\r
131         to ensure the interrupts get disabled as soon as possible, and so ensuring\r
132         the stack use is minimal should a context switch interrupt occur. */\r
133         *pxTopOfStack = ( portSTACK_TYPE ) 0x00;        /* R0 */\r
134         pxTopOfStack--;\r
135         *pxTopOfStack = portFLAGS_INT_ENABLED;\r
136         pxTopOfStack--;\r
137 \r
138         /* Next place the address of the hardware stack.  This is required so\r
139         the AVR stack pointer can be restored to point to the hardware stack. */\r
140         pxTopOfHardwareStack -= portBYTES_USED_BY_RETURN_ADDRESS;\r
141         usAddress = ( unsigned portSHORT ) pxTopOfHardwareStack;\r
142 \r
143         /* SPL */\r
144         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );\r
145         pxTopOfStack--;\r
146 \r
147         /* SPH */\r
148         usAddress >>= 8;\r
149         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );\r
150         pxTopOfStack--;\r
151 \r
152 \r
153 \r
154 \r
155         /* Now the remaining registers. */\r
156         *pxTopOfStack = ( portSTACK_TYPE ) 0x01;        /* R1 */\r
157         pxTopOfStack--;\r
158         *pxTopOfStack = ( portSTACK_TYPE ) 0x02;        /* R2 */\r
159         pxTopOfStack--;\r
160         *pxTopOfStack = ( portSTACK_TYPE ) 0x03;        /* R3 */\r
161         pxTopOfStack--;\r
162         *pxTopOfStack = ( portSTACK_TYPE ) 0x04;        /* R4 */\r
163         pxTopOfStack--;\r
164         *pxTopOfStack = ( portSTACK_TYPE ) 0x05;        /* R5 */\r
165         pxTopOfStack--;\r
166         *pxTopOfStack = ( portSTACK_TYPE ) 0x06;        /* R6 */\r
167         pxTopOfStack--;\r
168         *pxTopOfStack = ( portSTACK_TYPE ) 0x07;        /* R7 */\r
169         pxTopOfStack--;\r
170         *pxTopOfStack = ( portSTACK_TYPE ) 0x08;        /* R8 */\r
171         pxTopOfStack--;\r
172         *pxTopOfStack = ( portSTACK_TYPE ) 0x09;        /* R9 */\r
173         pxTopOfStack--;\r
174         *pxTopOfStack = ( portSTACK_TYPE ) 0x10;        /* R10 */\r
175         pxTopOfStack--;\r
176         *pxTopOfStack = ( portSTACK_TYPE ) 0x11;        /* R11 */\r
177         pxTopOfStack--;\r
178         *pxTopOfStack = ( portSTACK_TYPE ) 0x12;        /* R12 */\r
179         pxTopOfStack--;\r
180         *pxTopOfStack = ( portSTACK_TYPE ) 0x13;        /* R13 */\r
181         pxTopOfStack--;\r
182         *pxTopOfStack = ( portSTACK_TYPE ) 0x14;        /* R14 */\r
183         pxTopOfStack--;\r
184         *pxTopOfStack = ( portSTACK_TYPE ) 0x15;        /* R15 */\r
185         pxTopOfStack--;\r
186 \r
187         /* Place the parameter on the stack in the expected location. */\r
188         usAddress = ( unsigned portSHORT ) pvParameters;\r
189         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );\r
190         pxTopOfStack--;\r
191 \r
192         usAddress >>= 8;\r
193         *pxTopOfStack = ( portSTACK_TYPE ) ( usAddress & ( unsigned portSHORT ) 0x00ff );\r
194         pxTopOfStack--;\r
195 \r
196         *pxTopOfStack = ( portSTACK_TYPE ) 0x18;        /* R18 */\r
197         pxTopOfStack--;\r
198         *pxTopOfStack = ( portSTACK_TYPE ) 0x19;        /* R19 */\r
199         pxTopOfStack--;\r
200         *pxTopOfStack = ( portSTACK_TYPE ) 0x20;        /* R20 */\r
201         pxTopOfStack--;\r
202         *pxTopOfStack = ( portSTACK_TYPE ) 0x21;        /* R21 */\r
203         pxTopOfStack--;\r
204         *pxTopOfStack = ( portSTACK_TYPE ) 0x22;        /* R22 */\r
205         pxTopOfStack--;\r
206         *pxTopOfStack = ( portSTACK_TYPE ) 0x23;        /* R23 */\r
207         pxTopOfStack--;\r
208         *pxTopOfStack = ( portSTACK_TYPE ) 0x24;        /* R24 */\r
209         pxTopOfStack--;\r
210         *pxTopOfStack = ( portSTACK_TYPE ) 0x25;        /* R25 */\r
211         pxTopOfStack--;\r
212         *pxTopOfStack = ( portSTACK_TYPE ) 0x26;        /* R26 X */\r
213         pxTopOfStack--;\r
214         *pxTopOfStack = ( portSTACK_TYPE ) 0x27;        /* R27 */\r
215         pxTopOfStack--;\r
216 \r
217         /* The Y register is not stored as it is used as the software stack and\r
218         gets saved into the task control block. */\r
219 \r
220         *pxTopOfStack = ( portSTACK_TYPE ) 0x30;        /* R30 Z */\r
221         pxTopOfStack--;\r
222         *pxTopOfStack = ( portSTACK_TYPE ) 0x031;       /* R31 */\r
223 \r
224         /*lint +e950 +e611 +e923 */\r
225 \r
226         return pxTopOfStack;\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 portBASE_TYPE xPortStartScheduler( void )\r
231 {\r
232         /* Setup the hardware to generate the tick. */\r
233         prvSetupTimerInterrupt();\r
234 \r
235         /* Restore the context of the first task that is going to run.\r
236         Normally we would just call portRESTORE_CONTEXT() here, but as the IAR\r
237         compiler does not fully support inline assembler we have to make a call.*/\r
238         vPortStart();\r
239 \r
240 \r
241         /* Should not get here! */\r
242         return pdTRUE;\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 void vPortEndScheduler( void )\r
247 {\r
248         /* It is unlikely that the AVR port will get stopped.  If required simply\r
249         disable the tick interrupt here. */\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 /*\r
254  * Setup timer 1 compare match A to generate a tick interrupt.\r
255  */\r
256 static void prvSetupTimerInterrupt( void )\r
257 {\r
258 unsigned portLONG ulCompareMatch;\r
259 unsigned portCHAR ucHighByte, ucLowByte;\r
260 \r
261         /* Using 16bit timer 1 to generate the tick.  Correct fuses must be\r
262         selected for the configCPU_CLOCK_HZ clock. */\r
263 \r
264         ulCompareMatch = configCPU_CLOCK_HZ / configTICK_RATE_HZ;\r
265 \r
266         /* We only have 16 bits so have to scale to get our required tick rate. */\r
267         ulCompareMatch /= portCLOCK_PRESCALER;\r
268 \r
269         /* Adjust for correct value. */\r
270         ulCompareMatch -= ( unsigned portLONG ) 1;\r
271 \r
272         /* Setup compare match value for compare match A.  Interrupts are disabled\r
273         before this is called so we need not worry here. */\r
274         ucLowByte = ( unsigned portCHAR ) ( ulCompareMatch & ( unsigned portLONG ) 0xff );\r
275         ulCompareMatch >>= 8;\r
276         ucHighByte = ( unsigned portCHAR ) ( ulCompareMatch & ( unsigned portLONG ) 0xff );\r
277         OCR1AH = ucHighByte;\r
278         OCR1AL = ucLowByte;\r
279 \r
280         /* Setup clock source and compare match behaviour. */\r
281         ucLowByte = portCLEAR_COUNTER_ON_MATCH | portPRESCALE_64;\r
282         TCCR1B = ucLowByte;\r
283 \r
284         /* Enable the interrupt - this is okay as interrupt are currently globally\r
285         disabled. */\r
286         TIMSK |= portCOMPARE_MATCH_A_INTERRUPT_ENABLE;\r
287 }\r
288 /*-----------------------------------------------------------*/\r
289 \r
290 #if configUSE_PREEMPTION == 1\r
291 \r
292         /*\r
293          * Tick ISR for preemptive scheduler.  We can use a __task attribute as\r
294          * the context is saved at the start of vPortYieldFromTick().  The tick\r
295          * count is incremented after the context is saved.\r
296          */\r
297         __task void SIG_OUTPUT_COMPARE1A( void )\r
298         {\r
299                 vPortYieldFromTick();\r
300                 asm( "reti" );\r
301         }\r
302         \r
303 #else\r
304 \r
305         /*\r
306          * Tick ISR for the cooperative scheduler.  All this does is increment the\r
307          * tick count.  We don't need to switch context, this can only be done by\r
308          * manual calls to taskYIELD();\r
309          *\r
310          * THE INTERRUPT VECTOR IS POPULATED IN portmacro.s90.  DO NOT INSTALL\r
311          * IT HERE USING THE USUAL PRAGMA.\r
312          */             \r
313         __interrupt void SIG_OUTPUT_COMPARE1A( void )\r
314         {\r
315                 vTaskIncrementTick();\r
316         }\r
317 #endif\r
318 \r
319 \r
320         \r