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