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