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