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