]> git.sur5r.net Git - freertos/blob - Source/portable/IAR/AVR32_UC3/port.c
c14992cef36f99da469208218909a6b339e58358
[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 email: avr32@atmel.com\r
12  *\r
13  *****************************************************************************/\r
14 \r
15 /*\r
16         FreeRTOS.org V4.2.1 - Copyright (C) 2003-2007 Richard Barry.\r
17 \r
18         This file is part of the FreeRTOS.org distribution.\r
19 \r
20         FreeRTOS.org is free software; you can redistribute it and/or modify\r
21         it under the terms of the GNU General Public License as published by\r
22         the Free Software Foundation; either version 2 of the License, or\r
23         (at your option) any later version.\r
24 \r
25         FreeRTOS.org is distributed in the hope that it will be useful,\r
26         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
27         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
28         GNU General Public License for more details.\r
29 \r
30         You should have received a copy of the GNU General Public License\r
31         along with FreeRTOS.org; if not, write to the Free Software\r
32         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
33 \r
34         A special exception to the GPL can be applied should you wish to distribute\r
35         a combined work that includes FreeRTOS.org, without being obliged to provide\r
36         the source code for any proprietary components.  See the licensing section\r
37         of http://www.FreeRTOS.org for full details of how and when the exception\r
38         can be applied.\r
39 \r
40         ***************************************************************************\r
41         See http://www.FreeRTOS.org for documentation, latest information, license\r
42         and contact details.  Please ensure to read the configuration and relevant\r
43         port sections of the online documentation.\r
44 \r
45         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
46         with commercial development and support options.\r
47         ***************************************************************************\r
48 */\r
49 \r
50 \r
51 /* Scheduler includes. */\r
52 #include "FreeRTOS.h"\r
53 \r
54 /* Get rid of inline in task.h. */\r
55 #include "task.h"\r
56 \r
57 /* AVR32 UC3 includes. */\r
58 #include <avr32/iouc3a0512.h>\r
59 #include <intrinsics.h>\r
60 #include "gpio.h"\r
61 \r
62 #if configDBG\r
63         #include "usart.h"\r
64 #endif\r
65 \r
66 #if( configTICK_USE_TC==1 )\r
67         #include "tc.h"\r
68 #endif\r
69 \r
70 \r
71 /* Constants required to setup the task context. */\r
72 #define portINITIAL_SR            ( ( portSTACK_TYPE ) 0x00400000 ) /* AVR32 : [M2:M0]=001 I1M=0 I0M=0, GM=0 */\r
73 #define portINSTRUCTION_SIZE      ( ( portSTACK_TYPE ) 0 )\r
74 \r
75 /* Each task maintains its own critical nesting variable. */\r
76 #define portNO_CRITICAL_NESTING   ( ( unsigned portLONG ) 0 )\r
77 volatile unsigned portLONG ulCriticalNesting = 9999UL;\r
78 \r
79 #if( configTICK_USE_TC==0 )\r
80         static void prvScheduleNextTick( void );\r
81 #endif\r
82 /*-----------------------------------------------------------*/\r
83 \r
84 /*\r
85  * Low-level initialization routine called during startup, before the main\r
86  * function.\r
87  */\r
88 int __low_level_init(void)\r
89 {\r
90         #if configHEAP_INIT\r
91                 #pragma segment = "HEAP"\r
92                 portBASE_TYPE *pxMem;\r
93         #endif\r
94 \r
95         /* Enable exceptions. */\r
96         ENABLE_ALL_EXCEPTIONS();\r
97 \r
98         /* Initialize interrupt handling. */\r
99         INTC_init_interrupts();\r
100 \r
101         #if configHEAP_INIT\r
102         {\r
103                 /* Initialize the heap used by malloc. */\r
104                 for( pxMem = __segment_begin( "HEAP" ); pxMem < ( portBASE_TYPE * ) __segment_end( "HEAP" ); )\r
105                 {\r
106                         *pxMem++ = 0xA5A5A5A5;\r
107                 }\r
108         }\r
109         #endif\r
110 \r
111         /* Code section present if and only if the debug trace is activated. */\r
112         #if configDBG\r
113         {\r
114                 static const usart_options_t usart_opt =\r
115                 {\r
116                   .baudrate = configDBG_USART_BAUDRATE,\r
117                   .charlength = 8,\r
118                   .paritytype = USART_NO_PARITY,\r
119                   .stopbits = USART_1_STOPBIT,\r
120                   .channelmode = USART_MODE_NORMAL\r
121                 };\r
122 \r
123                 /* Initialize the USART used for the debug trace with the configured parameters. */\r
124                 extern volatile avr32_usart_t *volatile stdio_usart_base;\r
125                 stdio_usart_base = configDBG_USART;\r
126                 gpio_enable_module_pin(configDBG_USART_RX_PIN, configDBG_USART_RX_FUNCTION);\r
127                 gpio_enable_module_pin(configDBG_USART_TX_PIN, configDBG_USART_TX_FUNCTION);\r
128                 usart_init_rs232(configDBG_USART, &usart_opt, configCPU_CLOCK_HZ);\r
129         }\r
130         #endif\r
131 \r
132         /* Request initialization of data segments. */\r
133         return 1;\r
134 }\r
135 /*-----------------------------------------------------------*/\r
136 \r
137 /* Added as there is no such function in FreeRTOS. */\r
138 void *pvPortRealloc( void *pv, size_t xWantedSize )\r
139 {\r
140 void *pvReturn;\r
141 \r
142         vTaskSuspendAll();\r
143         {\r
144                 pvReturn = realloc( pv, xWantedSize );\r
145         }\r
146         xTaskResumeAll();\r
147 \r
148         return pvReturn;\r
149 }\r
150 /*-----------------------------------------------------------*/\r
151 \r
152 /* The cooperative scheduler requires a normal IRQ service routine to\r
153 simply increment the system tick. */\r
154 /* The preemptive scheduler is defined as "naked" as the full context is saved\r
155 on entry as part of the context switch. */\r
156 #pragma shadow_registers = full   // Naked.\r
157 static void vTick( void )\r
158 {\r
159         /* Save the context of the interrupted task. */\r
160         portSAVE_CONTEXT_OS_INT();\r
161 \r
162         /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)\r
163         clock cycles from now. */\r
164         #if( configTICK_USE_TC==1 )\r
165                 /* Clear the interrupt flag. */\r
166                 AVR32_TC.channel[configTICK_TC_CHANNEL].sr;\r
167         #else\r
168                 prvScheduleNextTick();\r
169         #endif\r
170         \r
171         /* Because FreeRTOS is not supposed to run with nested interrupts, put all OS\r
172         calls in a critical section . */\r
173         portENTER_CRITICAL();\r
174                 vTaskIncrementTick();\r
175         portEXIT_CRITICAL();\r
176 \r
177         /* Restore the context of the "elected task". */\r
178         portRESTORE_CONTEXT_OS_INT();\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 #pragma shadow_registers = full   // Naked.\r
183 void SCALLYield( void )\r
184 {\r
185         /* Save the context of the interrupted task. */\r
186         portSAVE_CONTEXT_SCALL();\r
187         vTaskSwitchContext();\r
188         portRESTORE_CONTEXT_SCALL();\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 /* The code generated by the GCC compiler uses the stack in different ways at\r
193 different optimisation levels.  The interrupt flags can therefore not always\r
194 be saved to the stack.  Instead the critical section nesting level is stored\r
195 in a variable, which is then saved as part of the stack context. */\r
196 void vPortEnterCritical( void )\r
197 {\r
198         /* Disable interrupts */\r
199         portDISABLE_INTERRUPTS();\r
200 \r
201         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
202          directly.  Increment ulCriticalNesting to keep a count of how many times\r
203          portENTER_CRITICAL() has been called. */\r
204         ulCriticalNesting++;\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 void vPortExitCritical( void )\r
209 {\r
210         if(ulCriticalNesting > portNO_CRITICAL_NESTING)\r
211         {\r
212                 ulCriticalNesting--;\r
213                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
214                 {\r
215                         /* Enable all interrupt/exception. */\r
216                         portENABLE_INTERRUPTS();\r
217                 }\r
218         }\r
219 }\r
220 /*-----------------------------------------------------------*/\r
221 \r
222 /* Setup the timer to generate the tick interrupts. */\r
223 static void prvSetupTimerInterrupt( void );\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 /*\r
227  * Initialise the stack of a task to look exactly as if a call to\r
228  * portSAVE_CONTEXT had been called.\r
229  *\r
230  * See header file for description.\r
231  */\r
232 portSTACK_TYPE *pxPortInitialiseStack( portSTACK_TYPE *pxTopOfStack, pdTASK_CODE pxCode, void *pvParameters )\r
233 {\r
234         /* Setup the initial stack of the task.  The stack is set exactly as\r
235         expected by the portRESTORE_CONTEXT() macro. */\r
236 \r
237         /* When the task starts, it will expect to find the function parameter in R12. */\r
238         pxTopOfStack--;\r
239         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x08080808;                                        /* R8 */\r
240         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x09090909;                                        /* R9 */\r
241         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x0A0A0A0A;                                        /* R10 */\r
242         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x0B0B0B0B;                                        /* R11 */\r
243         *pxTopOfStack-- = ( portSTACK_TYPE ) pvParameters;                                      /* R12 */\r
244         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xDEADBEEF;                                        /* R14/LR */\r
245         *pxTopOfStack-- = ( portSTACK_TYPE ) pxCode + portINSTRUCTION_SIZE; /* R15/PC */\r
246         *pxTopOfStack-- = ( portSTACK_TYPE ) portINITIAL_SR;                            /* SR */\r
247         *pxTopOfStack-- = ( portSTACK_TYPE ) 0xFF0000FF;                                        /* R0 */\r
248         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x01010101;                                        /* R1 */\r
249         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x02020202;                                        /* R2 */\r
250         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x03030303;                                        /* R3 */\r
251         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x04040404;                                        /* R4 */\r
252         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x05050505;                                        /* R5 */\r
253         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x06060606;                                        /* R6 */\r
254         *pxTopOfStack-- = ( portSTACK_TYPE ) 0x07070707;                                        /* R7 */\r
255         *pxTopOfStack = ( portSTACK_TYPE ) portNO_CRITICAL_NESTING;                     /* ulCriticalNesting */\r
256 \r
257         return pxTopOfStack;\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 portBASE_TYPE xPortStartScheduler( void )\r
262 {\r
263         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
264         here already. */\r
265         prvSetupTimerInterrupt();\r
266 \r
267         /* Start the first task. */\r
268         portRESTORE_CONTEXT();\r
269 \r
270         /* Should not get here! */\r
271         return 0;\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 void vPortEndScheduler( void )\r
276 {\r
277         /* It is unlikely that the AVR32 port will require this function as there\r
278         is nothing to return to.  */\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)\r
283 clock cycles from now. */\r
284 #if( configTICK_USE_TC==0 )\r
285         static void prvScheduleNextTick(void)\r
286         {\r
287                 unsigned long lCountVal, lCompareVal;\r
288 \r
289                 lCountVal = Get_system_register(AVR32_COUNT);\r
290                 lCompareVal = lCountVal + (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
291                 Set_system_register(AVR32_COMPARE, lCompareVal);\r
292         }\r
293 #endif\r
294 /*-----------------------------------------------------------*/\r
295 \r
296 /* Setup the timer to generate the tick interrupts. */\r
297 static void prvSetupTimerInterrupt(void)\r
298 {\r
299         #if( configTICK_USE_TC==1 )\r
300 \r
301                 volatile avr32_tc_t *tc = &AVR32_TC;\r
302 \r
303                 // Options for waveform genration.\r
304                 tc_waveform_opt_t waveform_opt =\r
305                 {\r
306                 .channel  = configTICK_TC_CHANNEL,             /* Channel selection. */\r
307 \r
308                 .bswtrg   = TC_EVT_EFFECT_NOOP,                /* Software trigger effect on TIOB. */\r
309                 .beevt    = TC_EVT_EFFECT_NOOP,                /* External event effect on TIOB. */\r
310                 .bcpc     = TC_EVT_EFFECT_NOOP,                /* RC compare effect on TIOB. */\r
311                 .bcpb     = TC_EVT_EFFECT_NOOP,                /* RB compare effect on TIOB. */\r
312 \r
313                 .aswtrg   = TC_EVT_EFFECT_NOOP,                /* Software trigger effect on TIOA. */\r
314                 .aeevt    = TC_EVT_EFFECT_NOOP,                /* External event effect on TIOA. */\r
315                 .acpc     = TC_EVT_EFFECT_NOOP,                /* RC compare effect on TIOA: toggle. */\r
316                 .acpa     = TC_EVT_EFFECT_NOOP,                /* RA compare effect on TIOA: toggle (other possibilities are none, set and clear). */\r
317 \r
318                 .wavsel   = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,/* Waveform selection: Up mode without automatic trigger on RC compare. */\r
319                 .enetrg   = FALSE,                             /* External event trigger enable. */\r
320                 .eevt     = 0,                                 /* External event selection. */\r
321                 .eevtedg  = TC_SEL_NO_EDGE,                    /* External event edge selection. */\r
322                 .cpcdis   = FALSE,                             /* Counter disable when RC compare. */\r
323                 .cpcstop  = FALSE,                             /* Counter clock stopped with RC compare. */\r
324 \r
325                 .burst    = FALSE,                             /* Burst signal selection. */\r
326                 .clki     = FALSE,                             /* Clock inversion. */\r
327                 .tcclks   = TC_CLOCK_SOURCE_TC2                /* Internal source clock 2. */\r
328                 };\r
329 \r
330                 tc_interrupt_t tc_interrupt =\r
331                 {\r
332                         .etrgs=0,\r
333                         .ldrbs=0,\r
334                         .ldras=0,\r
335                         .cpcs =1,\r
336                         .cpbs =0,\r
337                         .cpas =0,\r
338                         .lovrs=0,\r
339                         .covfs=0,\r
340                 };\r
341 \r
342         #endif\r
343 \r
344         /* Disable all interrupt/exception. */\r
345         portDISABLE_INTERRUPTS();\r
346 \r
347         /* Register the compare interrupt handler to the interrupt controller and\r
348         enable the compare interrupt. */\r
349 \r
350         #if( configTICK_USE_TC==1 )\r
351         {\r
352                 INTC_register_interrupt((__int_handler)&vTick, configTICK_TC_IRQ, INT0);\r
353 \r
354                 /* Initialize the timer/counter. */\r
355                 tc_init_waveform(tc, &waveform_opt);\r
356 \r
357                 /* Set the compare triggers.\r
358                 Remember TC counter is 16-bits, so counting second is not possible!\r
359                 That's why we configure it to count ms. */\r
360                 tc_write_rc( tc, configTICK_TC_CHANNEL, ( configPBA_CLOCK_HZ / 4) / 1000 );\r
361 \r
362                 tc_configure_interrupts( tc, configTICK_TC_CHANNEL, &tc_interrupt );\r
363 \r
364                 /* Start the timer/counter. */\r
365                 tc_start(tc, configTICK_TC_CHANNEL);\r
366         }\r
367         #else\r
368         {\r
369                 INTC_register_interrupt((__int_handler)&vTick, AVR32_CORE_COMPARE_IRQ, INT0);\r
370                 prvScheduleNextTick();\r
371         }\r
372         #endif\r
373 }\r