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