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