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