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