]> begriffs open source - freertos/blob - Source/portable/GCC/AVR32_UC3/port.c
Prepare for V5.0.4 release.
[freertos] / Source / portable / GCC / 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:           GNU GCC for AVR32\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.0.4 - Copyright (C) 2003-2008 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\r
21         it under the terms of the GNU General Public License as published by\r
22         the Free Software Foundation; either version 2 of the License, or\r
23         (at your option) any later version.\r
24 \r
25         FreeRTOS.org is distributed in the hope that it will be useful,\r
26         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
27         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
28         GNU General Public License for more details.\r
29 \r
30         You should have received a copy of the GNU General Public License\r
31         along with FreeRTOS.org; if not, write to the Free Software\r
32         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
33 \r
34         A special exception to the GPL can be applied should you wish to distribute\r
35         a combined work that includes FreeRTOS.org, without being obliged to provide\r
36         the source code for any proprietary components.  See the licensing section\r
37         of http://www.FreeRTOS.org for full details of how and when the exception\r
38         can be applied.\r
39 \r
40     ***************************************************************************\r
41     ***************************************************************************\r
42     *                                                                         *\r
43     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
44     * and even write all or part of your application on your behalf.          *\r
45     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
46     * expedite your project.                                                  *\r
47     *                                                                         *\r
48     ***************************************************************************\r
49     ***************************************************************************\r
50 \r
51         Please ensure to read the configuration and relevant port sections of the\r
52         online documentation.\r
53 \r
54         http://www.FreeRTOS.org - Documentation, latest information, license and \r
55         contact details.\r
56 \r
57         http://www.SafeRTOS.com - A version that is certified for use in safety \r
58         critical systems.\r
59 \r
60         http://www.OpenRTOS.com - Commercial support, development, porting, \r
61         licensing and training services.\r
62 */\r
63 \r
64 \r
65 /* Standard includes. */\r
66 #include <sys/cpu.h>\r
67 #include <sys/usart.h>\r
68 #include <malloc.h>\r
69 \r
70 /* Scheduler includes. */\r
71 #include "FreeRTOS.h"\r
72 #include "task.h"\r
73 \r
74 /* AVR32 UC3 includes. */\r
75 #include <avr32/io.h>\r
76 #include "gpio.h"\r
77 #if( configTICK_USE_TC==1 )\r
78         #include "tc.h"\r
79 #endif\r
80 \r
81 \r
82 /* Constants required to setup the task context. */\r
83 #define portINITIAL_SR            ( ( portSTACK_TYPE ) 0x00400000 ) /* AVR32 : [M2:M0]=001 I1M=0 I0M=0, GM=0 */\r
84 #define portINSTRUCTION_SIZE      ( ( portSTACK_TYPE ) 0 )\r
85 \r
86 /* Each task maintains its own critical nesting variable. */\r
87 #define portNO_CRITICAL_NESTING   ( ( unsigned portLONG ) 0 )\r
88 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
89 \r
90 #if( configTICK_USE_TC==0 )\r
91         static void prvScheduleNextTick( void );\r
92 #else\r
93         static void prvClearTcInt( void );\r
94 #endif\r
95 \r
96 /* Setup the timer to generate the tick interrupts. */\r
97 static void prvSetupTimerInterrupt( void );\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /*\r
102  * Low-level initialization routine called during startup, before the main\r
103  * function.\r
104  * This version comes in replacement to the default one provided by Newlib.\r
105  * Newlib's _init_startup only calls init_exceptions, but Newlib's exception\r
106  * vectors are not compatible with the SCALL management in the current FreeRTOS\r
107  * port. More low-level initializations are besides added here.\r
108  */\r
109 void _init_startup(void)\r
110 {\r
111         /* Import the Exception Vector Base Address. */\r
112         extern void _evba;\r
113 \r
114         #if configHEAP_INIT\r
115                 extern void __heap_start__;\r
116                 extern void __heap_end__;\r
117                 portBASE_TYPE *pxMem;\r
118         #endif\r
119 \r
120         /* Load the Exception Vector Base Address in the corresponding system register. */\r
121         Set_system_register( AVR32_EVBA, ( int ) &_evba );\r
122 \r
123         /* Enable exceptions. */\r
124         ENABLE_ALL_EXCEPTIONS();\r
125 \r
126         /* Initialize interrupt handling. */\r
127         INTC_init_interrupts();\r
128 \r
129         #if configHEAP_INIT\r
130 \r
131                 /* Initialize the heap used by malloc. */\r
132                 for( pxMem = &__heap_start__; pxMem < ( portBASE_TYPE * )&__heap_end__; )\r
133                 {\r
134                         *pxMem++ = 0xA5A5A5A5;\r
135                 }\r
136 \r
137         #endif\r
138 \r
139         /* Give the used CPU clock frequency to Newlib, so it can work properly. */\r
140         set_cpu_hz( configCPU_CLOCK_HZ );\r
141 \r
142         /* Code section present if and only if the debug trace is activated. */\r
143         #if configDBG\r
144         {\r
145                 static const gpio_map_t DBG_USART_GPIO_MAP =\r
146                 {\r
147                         { configDBG_USART_RX_PIN, configDBG_USART_RX_FUNCTION },\r
148                         { configDBG_USART_TX_PIN, configDBG_USART_TX_FUNCTION }\r
149                 };\r
150 \r
151                 /* Initialize the USART used for the debug trace with the configured parameters. */\r
152                 set_usart_base( ( void * ) configDBG_USART );\r
153                 gpio_enable_module( DBG_USART_GPIO_MAP,\r
154                                     sizeof( DBG_USART_GPIO_MAP ) / sizeof( DBG_USART_GPIO_MAP[0] ) );\r
155                 usart_init( configDBG_USART_BAUDRATE );\r
156         }\r
157         #endif\r
158 }\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 /*\r
162  * malloc, realloc and free are meant to be called through respectively\r
163  * pvPortMalloc, pvPortRealloc and vPortFree.\r
164  * The latter functions call the former ones from within sections where tasks\r
165  * are suspended, so the latter functions are task-safe. __malloc_lock and\r
166  * __malloc_unlock use the same mechanism to also keep the former functions\r
167  * task-safe as they may be called directly from Newlib's functions.\r
168  * However, all these functions are interrupt-unsafe and SHALL THEREFORE NOT BE\r
169  * CALLED FROM WITHIN AN INTERRUPT, because __malloc_lock and __malloc_unlock do\r
170  * not call portENTER_CRITICAL and portEXIT_CRITICAL in order not to disable\r
171  * interrupts during memory allocation management as this may be a very time-\r
172  * consuming process.\r
173  */\r
174 \r
175 /*\r
176  * Lock routine called by Newlib on malloc / realloc / free entry to guarantee a\r
177  * safe section as memory allocation management uses global data.\r
178  * See the aforementioned details.\r
179  */\r
180 void __malloc_lock(struct _reent *ptr)\r
181 {\r
182         vTaskSuspendAll();\r
183 }\r
184 \r
185 /*\r
186  * Unlock routine called by Newlib on malloc / realloc / free exit to guarantee\r
187  * a safe section as memory allocation management uses global data.\r
188  * See the aforementioned details.\r
189  */\r
190 void __malloc_unlock(struct _reent *ptr)\r
191 {\r
192         xTaskResumeAll();\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 /* Added as there is no such function in FreeRTOS. */\r
197 void *pvPortRealloc( void *pv, size_t xWantedSize )\r
198 {\r
199 void *pvReturn;\r
200 \r
201         vTaskSuspendAll();\r
202         {\r
203                 pvReturn = realloc( pv, xWantedSize );\r
204         }\r
205         xTaskResumeAll();\r
206 \r
207         return pvReturn;\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 /* The cooperative scheduler requires a normal IRQ service routine to\r
212 simply increment the system tick. */\r
213 /* The preemptive scheduler is defined as "naked" as the full context is saved\r
214 on entry as part of the context switch. */\r
215 __attribute__((__naked__)) static void vTick( void )\r
216 {\r
217         /* Save the context of the interrupted task. */\r
218         portSAVE_CONTEXT_OS_INT();\r
219 \r
220         #if( configTICK_USE_TC==1 )\r
221                 /* Clear the interrupt flag. */\r
222                 prvClearTcInt();\r
223         #else\r
224                 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)\r
225                 clock cycles from now. */\r
226                 prvScheduleNextTick();\r
227         #endif\r
228 \r
229         /* Because FreeRTOS is not supposed to run with nested interrupts, put all OS\r
230         calls in a critical section . */\r
231         portENTER_CRITICAL();\r
232                 vTaskIncrementTick();\r
233         portEXIT_CRITICAL();\r
234 \r
235         /* Restore the context of the "elected task". */\r
236         portRESTORE_CONTEXT_OS_INT();\r
237 }\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 __attribute__((__naked__)) void SCALLYield( void )\r
241 {\r
242         /* Save the context of the interrupted task. */\r
243         portSAVE_CONTEXT_SCALL();\r
244         vTaskSwitchContext();\r
245         portRESTORE_CONTEXT_SCALL();\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 /* The code generated by the GCC compiler uses the stack in different ways at\r
250 different optimisation levels.  The interrupt flags can therefore not always\r
251 be saved to the stack.  Instead the critical section nesting level is stored\r
252 in a variable, which is then saved as part of the stack context. */\r
253 __attribute__((__noinline__)) void vPortEnterCritical( void )\r
254 {\r
255         /* Disable interrupts */\r
256         portDISABLE_INTERRUPTS();\r
257 \r
258         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
259          directly.  Increment ulCriticalNesting to keep a count of how many times\r
260          portENTER_CRITICAL() has been called. */\r
261         ulCriticalNesting++;\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 __attribute__((__noinline__)) void vPortExitCritical( void )\r
266 {\r
267         if(ulCriticalNesting > portNO_CRITICAL_NESTING)\r
268         {\r
269                 ulCriticalNesting--;\r
270                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
271                 {\r
272                         /* Enable all interrupt/exception. */\r
273                         portENABLE_INTERRUPTS();\r
274                 }\r
275         }\r
276 }\r
277 /*-----------------------------------------------------------*/\r
278 \r
279 /*\r
280  * Initialise the stack of a task to look exactly as if a call to\r
281  * portSAVE_CONTEXT had been called.\r
282  *\r
283  * See header file for description.\r
284  */\r
285 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
286 {\r
287         /* Setup the initial stack of the task.  The stack is set exactly as\r
288         expected by the portRESTORE_CONTEXT() macro. */\r
289 \r
290         /* When the task starts, it will expect to find the function parameter in R12. */\r
291         pxTopOfStack--;\r
292         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x08080808;                                        /* R8 */\r
293         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x09090909;                                        /* R9 */\r
294         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x0A0A0A0A;                                        /* R10 */\r
295         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x0B0B0B0B;                                        /* R11 */\r
296         *pxTopOfStack-- = ( portSTACK_TYPE ) pvParameters;                                      /* R12 */\r
297         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xDEADBEEF;                                        /* R14/LR */\r
298         *pxTopOfStack-- = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE; /* R15/PC */\r
299         *pxTopOfStack-- = ( portSTACK_TYPE ) portINITIAL_SR;                            /* SR */\r
300         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xFF0000FF;                                        /* R0 */\r
301         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x01010101;                                        /* R1 */\r
302         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x02020202;                                        /* R2 */\r
303         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x03030303;                                        /* R3 */\r
304         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x04040404;                                        /* R4 */\r
305         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x05050505;                                        /* R5 */\r
306         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x06060606;                                        /* R6 */\r
307         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x07070707;                                        /* R7 */\r
308         *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_NESTING;                     /* ulCriticalNesting */\r
309 \r
310         return pxTopOfStack;\r
311 }\r
312 /*-----------------------------------------------------------*/\r
313 \r
314 portBASE_TYPE xPortStartScheduler( void )\r
315 {\r
316         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
317         here already. */\r
318         prvSetupTimerInterrupt();\r
319 \r
320         /* Start the first task. */\r
321         portRESTORE_CONTEXT();\r
322 \r
323         /* Should not get here! */\r
324         return 0;\r
325 }\r
326 /*-----------------------------------------------------------*/\r
327 \r
328 void vPortEndScheduler( void )\r
329 {\r
330         /* It is unlikely that the AVR32 port will require this function as there\r
331         is nothing to return to.  */\r
332 }\r
333 /*-----------------------------------------------------------*/\r
334 \r
335 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)\r
336 clock cycles from now. */\r
337 #if( configTICK_USE_TC==0 )\r
338         static void prvScheduleFirstTick(void)\r
339         {\r
340                 unsigned long lCycles;\r
341 \r
342                 lCycles = Get_system_register(AVR32_COUNT);\r
343                 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
344                 // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception\r
345                 // generation feature does not get disabled.\r
346                 if(0 == lCycles)\r
347                 {\r
348                         lCycles++;\r
349                 }\r
350                 Set_system_register(AVR32_COMPARE, lCycles);\r
351         }\r
352         \r
353         __attribute__((__noinline__)) static void prvScheduleNextTick(void)\r
354         {\r
355                 unsigned long lCycles, lCount;\r
356 \r
357                 lCycles = Get_system_register(AVR32_COMPARE);\r
358                 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
359                 // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception\r
360                 // generation feature does not get disabled.\r
361                 if(0 == lCycles)\r
362                 {\r
363                         lCycles++;\r
364                 }\r
365                 lCount = Get_system_register(AVR32_COUNT);\r
366                 if( lCycles < lCount )\r
367                 {               // We missed a tick, recover for the next.\r
368                         lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
369                 }\r
370                 Set_system_register(AVR32_COMPARE, lCycles);\r
371         }\r
372 #else\r
373         __attribute__((__noinline__)) static void prvClearTcInt(void)\r
374         {\r
375                 AVR32_TC.channel[configTICK_TC_CHANNEL].sr;\r
376         }\r
377 #endif\r
378 /*-----------------------------------------------------------*/\r
379 \r
380 /* Setup the timer to generate the tick interrupts. */\r
381 static void prvSetupTimerInterrupt(void)\r
382 {\r
383 #if( configTICK_USE_TC==1 )\r
384 \r
385         volatile avr32_tc_t *tc = &AVR32_TC;\r
386 \r
387         // Options for waveform genration.\r
388         tc_waveform_opt_t waveform_opt =\r
389         {\r
390         .channel  = configTICK_TC_CHANNEL,             /* Channel selection. */\r
391 \r
392         .bswtrg   = TC_EVT_EFFECT_NOOP,                /* Software trigger effect on TIOB. */\r
393         .beevt    = TC_EVT_EFFECT_NOOP,                /* External event effect on TIOB. */\r
394         .bcpc     = TC_EVT_EFFECT_NOOP,                /* RC compare effect on TIOB. */\r
395         .bcpb     = TC_EVT_EFFECT_NOOP,                /* RB compare effect on TIOB. */\r
396 \r
397         .aswtrg   = TC_EVT_EFFECT_NOOP,                /* Software trigger effect on TIOA. */\r
398         .aeevt    = TC_EVT_EFFECT_NOOP,                /* External event effect on TIOA. */\r
399         .acpc     = TC_EVT_EFFECT_NOOP,                /* RC compare effect on TIOA: toggle. */\r
400         .acpa     = TC_EVT_EFFECT_NOOP,                /* RA compare effect on TIOA: toggle (other possibilities are none, set and clear). */\r
401 \r
402         .wavsel   = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,/* Waveform selection: Up mode without automatic trigger on RC compare. */\r
403         .enetrg   = FALSE,                             /* External event trigger enable. */\r
404         .eevt     = 0,                                 /* External event selection. */\r
405         .eevtedg  = TC_SEL_NO_EDGE,                    /* External event edge selection. */\r
406         .cpcdis   = FALSE,                             /* Counter disable when RC compare. */\r
407         .cpcstop  = FALSE,                             /* Counter clock stopped with RC compare. */\r
408 \r
409         .burst    = FALSE,                             /* Burst signal selection. */\r
410         .clki     = FALSE,                             /* Clock inversion. */\r
411         .tcclks   = TC_CLOCK_SOURCE_TC2                /* Internal source clock 2. */\r
412         };\r
413 \r
414         tc_interrupt_t tc_interrupt =\r
415         {\r
416                 .etrgs=0,\r
417                 .ldrbs=0,\r
418                 .ldras=0,\r
419                 .cpcs =1,\r
420                 .cpbs =0,\r
421                 .cpas =0,\r
422                 .lovrs=0,\r
423                 .covfs=0,\r
424         };\r
425 \r
426 #endif\r
427 \r
428         /* Disable all interrupt/exception. */\r
429         portDISABLE_INTERRUPTS();\r
430 \r
431         /* Register the compare interrupt handler to the interrupt controller and\r
432         enable the compare interrupt. */\r
433 \r
434         #if( configTICK_USE_TC==1 )\r
435         {\r
436                 INTC_register_interrupt(&vTick, configTICK_TC_IRQ, INT0);\r
437 \r
438                 /* Initialize the timer/counter. */\r
439                 tc_init_waveform(tc, &waveform_opt);\r
440 \r
441                 /* Set the compare triggers.\r
442                 Remember TC counter is 16-bits, so counting second is not possible!\r
443                 That's why we configure it to count ms. */\r
444                 tc_write_rc( tc, configTICK_TC_CHANNEL, ( configPBA_CLOCK_HZ / 4) / configTICK_RATE_HZ );\r
445 \r
446                 tc_configure_interrupts( tc, configTICK_TC_CHANNEL, &tc_interrupt );\r
447 \r
448                 /* Start the timer/counter. */\r
449                 tc_start(tc, configTICK_TC_CHANNEL);\r
450         }\r
451         #else\r
452         {\r
453                 INTC_register_interrupt(&vTick, AVR32_CORE_COMPARE_IRQ, INT0);\r
454                 prvScheduleFirstTick();\r
455         }\r
456         #endif\r
457 }\r