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