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