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