]> git.sur5r.net Git - freertos/blob - Source/portable/IAR/AVR32_UC3/port.c
Update version number.
[freertos] / Source / portable / IAR / AVR32_UC3 / port.c
1 /*This file has been prepared for Doxygen automatic documentation generation.*/\r
2 /*! \file *********************************************************************\r
3  *\r
4  * \brief FreeRTOS port source for AVR32 UC3.\r
5  *\r
6  * - Compiler:           IAR EWAVR32\r
7  * - Supported devices:  All AVR32 devices can be used.\r
8  * - AppNote:\r
9  *\r
10  * \author               Atmel Corporation: http://www.atmel.com \n\r
11  *                       Support and FAQ: http://support.atmel.no/\r
12  *\r
13  *****************************************************************************/\r
14 \r
15 /*\r
16         FreeRTOS.org V5.4.0 - Copyright (C) 2003-2009 Richard Barry.\r
17 \r
18         This file is part of the FreeRTOS.org distribution.\r
19 \r
20         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
21         under the terms of the GNU General Public License (version 2) as published\r
22         by the Free Software Foundation and modified by the FreeRTOS exception.\r
23         **NOTE** The exception to the GPL is included to allow you to distribute a\r
24         combined work that includes FreeRTOS.org without being obliged to provide\r
25         the source code for any proprietary components.  Alternative commercial\r
26         license and support terms are also available upon request.  See the \r
27         licensing section of http://www.FreeRTOS.org for full details.\r
28 \r
29         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
30         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
31         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
32         more details.\r
33 \r
34         You should have received a copy of the GNU General Public License along\r
35         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
36         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
37 \r
38 \r
39         ***************************************************************************\r
40         *                                                                         *\r
41         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
42         *                                                                         *\r
43         * This is a concise, step by step, 'hands on' guide that describes both   *\r
44         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
45         * explains numerous examples that are written using the FreeRTOS API.     *\r
46         * Full source code for all the examples is provided in an accompanying    *\r
47         * .zip file.                                                              *\r
48         *                                                                         *\r
49         ***************************************************************************\r
50 \r
51         1 tab == 4 spaces!\r
52 \r
53         Please ensure to read the configuration and relevant port sections of the\r
54         online documentation.\r
55 \r
56         http://www.FreeRTOS.org - Documentation, latest information, license and\r
57         contact details.\r
58 \r
59         http://www.SafeRTOS.com - A version that is certified for use in safety\r
60         critical systems.\r
61 \r
62         http://www.OpenRTOS.com - Commercial support, development, porting,\r
63         licensing and training services.\r
64 */\r
65 \r
66 \r
67 /* Scheduler includes. */\r
68 #include "FreeRTOS.h"\r
69 #include "task.h"\r
70 \r
71 /* AVR32 UC3 includes. */\r
72 #include <avr32/io.h>\r
73 #include <intrinsics.h>\r
74 #include "gpio.h"\r
75 \r
76 #if configDBG\r
77         #include "usart.h"\r
78 #endif\r
79 \r
80 #if( configTICK_USE_TC==1 )\r
81         #include "tc.h"\r
82 #endif\r
83 \r
84 \r
85 /* Constants required to setup the task context. */\r
86 #define portINITIAL_SR            ( ( portSTACK_TYPE ) 0x00400000 ) /* AVR32 : [M2:M0]=001 I1M=0 I0M=0, GM=0 */\r
87 #define portINSTRUCTION_SIZE      ( ( portSTACK_TYPE ) 0 )\r
88 \r
89 /* Each task maintains its own critical nesting variable. */\r
90 #define portNO_CRITICAL_NESTING   ( ( unsigned portLONG ) 0 )\r
91 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
92 \r
93 #if( configTICK_USE_TC==0 )\r
94         static void prvScheduleNextTick( void );\r
95 #else\r
96         static void prvClearTcInt( void );\r
97 #endif\r
98 \r
99 /* Setup the timer to generate the tick interrupts. */\r
100 static void prvSetupTimerInterrupt( void );\r
101 \r
102 /*-----------------------------------------------------------*/\r
103 \r
104 /*\r
105  * Low-level initialization routine called during startup, before the main\r
106  * function.\r
107  */\r
108 int __low_level_init(void)\r
109 {\r
110         #if configHEAP_INIT\r
111                 #pragma segment = "HEAP"\r
112                 portBASE_TYPE *pxMem;\r
113         #endif\r
114 \r
115         /* Enable exceptions. */\r
116         ENABLE_ALL_EXCEPTIONS();\r
117 \r
118         /* Initialize interrupt handling. */\r
119         INTC_init_interrupts();\r
120 \r
121         #if configHEAP_INIT\r
122         {\r
123                 /* Initialize the heap used by malloc. */\r
124                 for( pxMem = __segment_begin( "HEAP" ); pxMem < ( portBASE_TYPE * ) __segment_end( "HEAP" ); )\r
125                 {\r
126                         *pxMem++ = 0xA5A5A5A5;\r
127                 }\r
128         }\r
129         #endif\r
130 \r
131         /* Code section present if and only if the debug trace is activated. */\r
132         #if configDBG\r
133         {\r
134                 static const gpio_map_t DBG_USART_GPIO_MAP =\r
135                 {\r
136                         { configDBG_USART_RX_PIN, configDBG_USART_RX_FUNCTION },\r
137                         { configDBG_USART_TX_PIN, configDBG_USART_TX_FUNCTION }\r
138                 };\r
139 \r
140                 static const usart_options_t DBG_USART_OPTIONS =\r
141                 {\r
142                         .baudrate = configDBG_USART_BAUDRATE,\r
143                         .charlength = 8,\r
144                         .paritytype = USART_NO_PARITY,\r
145                         .stopbits = USART_1_STOPBIT,\r
146                         .channelmode = USART_NORMAL_CHMODE\r
147                 };\r
148 \r
149                 /* Initialize the USART used for the debug trace with the configured parameters. */\r
150                 extern volatile avr32_usart_t *volatile stdio_usart_base;\r
151                 stdio_usart_base = configDBG_USART;\r
152                 gpio_enable_module( DBG_USART_GPIO_MAP,\r
153                                     sizeof( DBG_USART_GPIO_MAP ) / sizeof( DBG_USART_GPIO_MAP[0] ) );\r
154                 usart_init_rs232(configDBG_USART, &DBG_USART_OPTIONS, configCPU_CLOCK_HZ);\r
155         }\r
156         #endif\r
157 \r
158         /* Request initialization of data segments. */\r
159         return 1;\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 /* Added as there is no such function in FreeRTOS. */\r
164 void *pvPortRealloc( void *pv, size_t xWantedSize )\r
165 {\r
166 void *pvReturn;\r
167 \r
168         vTaskSuspendAll();\r
169         {\r
170                 pvReturn = realloc( pv, xWantedSize );\r
171         }\r
172         xTaskResumeAll();\r
173 \r
174         return pvReturn;\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 /* The cooperative scheduler requires a normal IRQ service routine to\r
179 simply increment the system tick. */\r
180 /* The preemptive scheduler is defined as "naked" as the full context is saved\r
181 on entry as part of the context switch. */\r
182 #pragma shadow_registers = full   // Naked.\r
183 static void vTick( void )\r
184 {\r
185         /* Save the context of the interrupted task. */\r
186         portSAVE_CONTEXT_OS_INT();\r
187 \r
188         #if( configTICK_USE_TC==1 )\r
189                 /* Clear the interrupt flag. */\r
190                 prvClearTcInt();\r
191         #else\r
192                 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)\r
193                 clock cycles from now. */\r
194                 prvScheduleNextTick();\r
195         #endif\r
196 \r
197         /* Because FreeRTOS is not supposed to run with nested interrupts, put all OS\r
198         calls in a critical section . */\r
199         portENTER_CRITICAL();\r
200                 vTaskIncrementTick();\r
201         portEXIT_CRITICAL();\r
202 \r
203         /* Restore the context of the "elected task". */\r
204         portRESTORE_CONTEXT_OS_INT();\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 #pragma shadow_registers = full   // Naked.\r
209 void SCALLYield( void )\r
210 {\r
211         /* Save the context of the interrupted task. */\r
212         portSAVE_CONTEXT_SCALL();\r
213         vTaskSwitchContext();\r
214         portRESTORE_CONTEXT_SCALL();\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 /* The code generated by the GCC compiler uses the stack in different ways at\r
219 different optimisation levels.  The interrupt flags can therefore not always\r
220 be saved to the stack.  Instead the critical section nesting level is stored\r
221 in a variable, which is then saved as part of the stack context. */\r
222 #pragma optimize = no_inline\r
223 void vPortEnterCritical( void )\r
224 {\r
225         /* Disable interrupts */\r
226         portDISABLE_INTERRUPTS();\r
227 \r
228         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
229          directly.  Increment ulCriticalNesting to keep a count of how many times\r
230          portENTER_CRITICAL() has been called. */\r
231         ulCriticalNesting++;\r
232 }\r
233 /*-----------------------------------------------------------*/\r
234 \r
235 #pragma optimize = no_inline\r
236 void vPortExitCritical( void )\r
237 {\r
238         if(ulCriticalNesting > portNO_CRITICAL_NESTING)\r
239         {\r
240                 ulCriticalNesting--;\r
241                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
242                 {\r
243                         /* Enable all interrupt/exception. */\r
244                         portENABLE_INTERRUPTS();\r
245                 }\r
246         }\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 /*\r
251  * Initialise the stack of a task to look exactly as if a call to\r
252  * portSAVE_CONTEXT had been called.\r
253  *\r
254  * See header file for description.\r
255  */\r
256 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
257 {\r
258         /* Setup the initial stack of the task.  The stack is set exactly as\r
259         expected by the portRESTORE_CONTEXT() macro. */\r
260 \r
261         /* When the task starts, it will expect to find the function parameter in R12. */\r
262         pxTopOfStack--;\r
263         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x08080808;                                        /* R8 */\r
264         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x09090909;                                        /* R9 */\r
265         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x0A0A0A0A;                                        /* R10 */\r
266         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x0B0B0B0B;                                        /* R11 */\r
267         *pxTopOfStack-- = ( portSTACK_TYPE ) pvParameters;                                      /* R12 */\r
268         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xDEADBEEF;                                        /* R14/LR */\r
269         *pxTopOfStack-- = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE; /* R15/PC */\r
270         *pxTopOfStack-- = ( portSTACK_TYPE ) portINITIAL_SR;                            /* SR */\r
271         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xFF0000FF;                                        /* R0 */\r
272         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x01010101;                                        /* R1 */\r
273         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x02020202;                                        /* R2 */\r
274         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x03030303;                                        /* R3 */\r
275         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x04040404;                                        /* R4 */\r
276         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x05050505;                                        /* R5 */\r
277         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x06060606;                                        /* R6 */\r
278         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x07070707;                                        /* R7 */\r
279         *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_NESTING;                     /* ulCriticalNesting */\r
280 \r
281         return pxTopOfStack;\r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 portBASE_TYPE xPortStartScheduler( void )\r
286 {\r
287         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
288         here already. */\r
289         prvSetupTimerInterrupt();\r
290 \r
291         /* Start the first task. */\r
292         portRESTORE_CONTEXT();\r
293 \r
294         /* Should not get here! */\r
295         return 0;\r
296 }\r
297 /*-----------------------------------------------------------*/\r
298 \r
299 void vPortEndScheduler( void )\r
300 {\r
301         /* It is unlikely that the AVR32 port will require this function as there\r
302         is nothing to return to.  */\r
303 }\r
304 /*-----------------------------------------------------------*/\r
305 \r
306 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)\r
307 clock cycles from now. */\r
308 #if( configTICK_USE_TC==0 )\r
309         static void prvScheduleFirstTick(void)\r
310         {\r
311                 unsigned long lCycles;\r
312 \r
313                 lCycles = Get_system_register(AVR32_COUNT);\r
314                 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
315                 // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception\r
316                 // generation feature does not get disabled.\r
317                 if(0 == lCycles)\r
318                 {\r
319                         lCycles++;\r
320                 }\r
321                 Set_system_register(AVR32_COMPARE, lCycles);\r
322         }\r
323         \r
324         #pragma optimize = no_inline\r
325         static void prvScheduleNextTick(void)\r
326         {\r
327                 unsigned long lCycles, lCount;\r
328 \r
329                 lCycles = Get_system_register(AVR32_COMPARE);\r
330                 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
331                 // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception\r
332                 // generation feature does not get disabled.\r
333                 if(0 == lCycles)\r
334                 {\r
335                         lCycles++;\r
336                 }\r
337                 lCount = Get_system_register(AVR32_COUNT);\r
338                 if( lCycles < lCount )\r
339                 {               // We missed a tick, recover for the next.\r
340                         lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
341                 }\r
342                 Set_system_register(AVR32_COMPARE, lCycles);\r
343         }\r
344 #else\r
345         #pragma optimize = no_inline\r
346         static void prvClearTcInt(void)\r
347         {\r
348                 AVR32_TC.channel[configTICK_TC_CHANNEL].sr;\r
349         }\r
350 #endif\r
351 /*-----------------------------------------------------------*/\r
352 \r
353 /* Setup the timer to generate the tick interrupts. */\r
354 static void prvSetupTimerInterrupt(void)\r
355 {\r
356         #if( configTICK_USE_TC==1 )\r
357 \r
358                 volatile avr32_tc_t *tc = &AVR32_TC;\r
359 \r
360                 // Options for waveform genration.\r
361                 tc_waveform_opt_t waveform_opt =\r
362                 {\r
363                 .channel  = configTICK_TC_CHANNEL,             /* Channel selection. */\r
364 \r
365                 .bswtrg   = TC_EVT_EFFECT_NOOP,                /* Software trigger effect on TIOB. */\r
366                 .beevt    = TC_EVT_EFFECT_NOOP,                /* External event effect on TIOB. */\r
367                 .bcpc     = TC_EVT_EFFECT_NOOP,                /* RC compare effect on TIOB. */\r
368                 .bcpb     = TC_EVT_EFFECT_NOOP,                /* RB compare effect on TIOB. */\r
369 \r
370                 .aswtrg   = TC_EVT_EFFECT_NOOP,                /* Software trigger effect on TIOA. */\r
371                 .aeevt    = TC_EVT_EFFECT_NOOP,                /* External event effect on TIOA. */\r
372                 .acpc     = TC_EVT_EFFECT_NOOP,                /* RC compare effect on TIOA: toggle. */\r
373                 .acpa     = TC_EVT_EFFECT_NOOP,                /* RA compare effect on TIOA: toggle (other possibilities are none, set and clear). */\r
374 \r
375                 .wavsel   = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,/* Waveform selection: Up mode without automatic trigger on RC compare. */\r
376                 .enetrg   = FALSE,                             /* External event trigger enable. */\r
377                 .eevt     = 0,                                 /* External event selection. */\r
378                 .eevtedg  = TC_SEL_NO_EDGE,                    /* External event edge selection. */\r
379                 .cpcdis   = FALSE,                             /* Counter disable when RC compare. */\r
380                 .cpcstop  = FALSE,                             /* Counter clock stopped with RC compare. */\r
381 \r
382                 .burst    = FALSE,                             /* Burst signal selection. */\r
383                 .clki     = FALSE,                             /* Clock inversion. */\r
384                 .tcclks   = TC_CLOCK_SOURCE_TC2                /* Internal source clock 2. */\r
385                 };\r
386 \r
387                 tc_interrupt_t tc_interrupt =\r
388                 {\r
389                         .etrgs=0,\r
390                         .ldrbs=0,\r
391                         .ldras=0,\r
392                         .cpcs =1,\r
393                         .cpbs =0,\r
394                         .cpas =0,\r
395                         .lovrs=0,\r
396                         .covfs=0,\r
397                 };\r
398 \r
399         #endif\r
400 \r
401         /* Disable all interrupt/exception. */\r
402         portDISABLE_INTERRUPTS();\r
403 \r
404         /* Register the compare interrupt handler to the interrupt controller and\r
405         enable the compare interrupt. */\r
406 \r
407         #if( configTICK_USE_TC==1 )\r
408         {\r
409                 INTC_register_interrupt((__int_handler)&vTick, configTICK_TC_IRQ, INT0);\r
410 \r
411                 /* Initialize the timer/counter. */\r
412                 tc_init_waveform(tc, &waveform_opt);\r
413 \r
414                 /* Set the compare triggers.\r
415                 Remember TC counter is 16-bits, so counting second is not possible!\r
416                 That's why we configure it to count ms. */\r
417                 tc_write_rc( tc, configTICK_TC_CHANNEL, ( configPBA_CLOCK_HZ / 4) / configTICK_RATE_HZ );\r
418 \r
419                 tc_configure_interrupts( tc, configTICK_TC_CHANNEL, &tc_interrupt );\r
420 \r
421                 /* Start the timer/counter. */\r
422                 tc_start(tc, configTICK_TC_CHANNEL);\r
423         }\r
424         #else\r
425         {\r
426                 INTC_register_interrupt((__int_handler)&vTick, AVR32_CORE_COMPARE_IRQ, INT0);\r
427                 prvScheduleFirstTick();\r
428         }\r
429         #endif\r
430 }\r