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