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