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