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