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