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