]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/AVR32_UC3/port.c
Update version number ready for version 9 release candidate 1.
[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 V9.0.0rc1 - 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 /* Scheduler includes. */\r
86 #include "FreeRTOS.h"\r
87 #include "task.h"\r
88 \r
89 /* AVR32 UC3 includes. */\r
90 #include <avr32/io.h>\r
91 #include <intrinsics.h>\r
92 #include "gpio.h"\r
93 \r
94 #if configDBG\r
95         #include "usart.h"\r
96 #endif\r
97 \r
98 #if( configTICK_USE_TC==1 )\r
99         #include "tc.h"\r
100 #endif\r
101 \r
102 \r
103 /* Constants required to setup the task context. */\r
104 #define portINITIAL_SR            ( ( StackType_t ) 0x00400000 ) /* AVR32 : [M2:M0]=001 I1M=0 I0M=0, GM=0 */\r
105 #define portINSTRUCTION_SIZE      ( ( StackType_t ) 0 )\r
106 \r
107 /* Each task maintains its own critical nesting variable. */\r
108 #define portNO_CRITICAL_NESTING   ( ( uint32_t ) 0 )\r
109 volatile uint32_t ulCriticalNesting = 9999UL;\r
110 \r
111 #if( configTICK_USE_TC==0 )\r
112         static void prvScheduleNextTick( void );\r
113 #else\r
114         static void prvClearTcInt( void );\r
115 #endif\r
116 \r
117 /* Setup the timer to generate the tick interrupts. */\r
118 static void prvSetupTimerInterrupt( void );\r
119 \r
120 /*-----------------------------------------------------------*/\r
121 \r
122 /*\r
123  * Low-level initialization routine called during startup, before the main\r
124  * function.\r
125  */\r
126 int __low_level_init(void)\r
127 {\r
128         #if configHEAP_INIT\r
129                 #pragma segment = "HEAP"\r
130                 BaseType_t *pxMem;\r
131         #endif\r
132 \r
133         /* Enable exceptions. */\r
134         ENABLE_ALL_EXCEPTIONS();\r
135 \r
136         /* Initialize interrupt handling. */\r
137         INTC_init_interrupts();\r
138 \r
139         #if configHEAP_INIT\r
140         {\r
141                 /* Initialize the heap used by malloc. */\r
142                 for( pxMem = __segment_begin( "HEAP" ); pxMem < ( BaseType_t * ) __segment_end( "HEAP" ); )\r
143                 {\r
144                         *pxMem++ = 0xA5A5A5A5;\r
145                 }\r
146         }\r
147         #endif\r
148 \r
149         /* Code section present if and only if the debug trace is activated. */\r
150         #if configDBG\r
151         {\r
152                 static const gpio_map_t DBG_USART_GPIO_MAP =\r
153                 {\r
154                         { configDBG_USART_RX_PIN, configDBG_USART_RX_FUNCTION },\r
155                         { configDBG_USART_TX_PIN, configDBG_USART_TX_FUNCTION }\r
156                 };\r
157 \r
158                 static const usart_options_t DBG_USART_OPTIONS =\r
159                 {\r
160                         .baudrate = configDBG_USART_BAUDRATE,\r
161                         .charlength = 8,\r
162                         .paritytype = USART_NO_PARITY,\r
163                         .stopbits = USART_1_STOPBIT,\r
164                         .channelmode = USART_NORMAL_CHMODE\r
165                 };\r
166 \r
167                 /* Initialize the USART used for the debug trace with the configured parameters. */\r
168                 extern volatile avr32_usart_t *volatile stdio_usart_base;\r
169                 stdio_usart_base = configDBG_USART;\r
170                 gpio_enable_module( DBG_USART_GPIO_MAP,\r
171                                     sizeof( DBG_USART_GPIO_MAP ) / sizeof( DBG_USART_GPIO_MAP[0] ) );\r
172                 usart_init_rs232(configDBG_USART, &DBG_USART_OPTIONS, configCPU_CLOCK_HZ);\r
173         }\r
174         #endif\r
175 \r
176         /* Request initialization of data segments. */\r
177         return 1;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 /* Added as there is no such function in FreeRTOS. */\r
182 void *pvPortRealloc( void *pv, size_t xWantedSize )\r
183 {\r
184 void *pvReturn;\r
185 \r
186         vTaskSuspendAll();\r
187         {\r
188                 pvReturn = realloc( pv, xWantedSize );\r
189         }\r
190         xTaskResumeAll();\r
191 \r
192         return pvReturn;\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 /* The cooperative scheduler requires a normal IRQ service routine to\r
197 simply increment the system tick. */\r
198 /* The preemptive scheduler is defined as "naked" as the full context is saved\r
199 on entry as part of the context switch. */\r
200 #pragma shadow_registers = full   // Naked.\r
201 static void vTick( void )\r
202 {\r
203         /* Save the context of the interrupted task. */\r
204         portSAVE_CONTEXT_OS_INT();\r
205 \r
206         #if( configTICK_USE_TC==1 )\r
207                 /* Clear the interrupt flag. */\r
208                 prvClearTcInt();\r
209         #else\r
210                 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)\r
211                 clock cycles from now. */\r
212                 prvScheduleNextTick();\r
213         #endif\r
214 \r
215         /* Because FreeRTOS is not supposed to run with nested interrupts, put all OS\r
216         calls in a critical section . */\r
217         portENTER_CRITICAL();\r
218                 xTaskIncrementTick();\r
219         portEXIT_CRITICAL();\r
220 \r
221         /* Restore the context of the "elected task". */\r
222         portRESTORE_CONTEXT_OS_INT();\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 #pragma shadow_registers = full   // Naked.\r
227 void SCALLYield( void )\r
228 {\r
229         /* Save the context of the interrupted task. */\r
230         portSAVE_CONTEXT_SCALL();\r
231         vTaskSwitchContext();\r
232         portRESTORE_CONTEXT_SCALL();\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 /* The code generated by the GCC compiler uses the stack in different ways at\r
237 different optimisation levels.  The interrupt flags can therefore not always\r
238 be saved to the stack.  Instead the critical section nesting level is stored\r
239 in a variable, which is then saved as part of the stack context. */\r
240 #pragma optimize = no_inline\r
241 void vPortEnterCritical( void )\r
242 {\r
243         /* Disable interrupts */\r
244         portDISABLE_INTERRUPTS();\r
245 \r
246         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
247          directly.  Increment ulCriticalNesting to keep a count of how many times\r
248          portENTER_CRITICAL() has been called. */\r
249         ulCriticalNesting++;\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 #pragma optimize = no_inline\r
254 void vPortExitCritical( void )\r
255 {\r
256         if(ulCriticalNesting > portNO_CRITICAL_NESTING)\r
257         {\r
258                 ulCriticalNesting--;\r
259                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
260                 {\r
261                         /* Enable all interrupt/exception. */\r
262                         portENABLE_INTERRUPTS();\r
263                 }\r
264         }\r
265 }\r
266 /*-----------------------------------------------------------*/\r
267 \r
268 /*\r
269  * Initialise the stack of a task to look exactly as if a call to\r
270  * portSAVE_CONTEXT had been called.\r
271  *\r
272  * See header file for description.\r
273  */\r
274 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
275 {\r
276         /* Setup the initial stack of the task.  The stack is set exactly as\r
277         expected by the portRESTORE_CONTEXT() macro. */\r
278 \r
279         /* When the task starts, it will expect to find the function parameter in R12. */\r
280         pxTopOfStack--;\r
281         *pxTopOfStack-- = ( StackType_t ) 0x08080808;                                   /* R8 */\r
282         *pxTopOfStack-- = ( StackType_t ) 0x09090909;                                   /* R9 */\r
283         *pxTopOfStack-- = ( StackType_t ) 0x0A0A0A0A;                                   /* R10 */\r
284         *pxTopOfStack-- = ( StackType_t ) 0x0B0B0B0B;                                   /* R11 */\r
285         *pxTopOfStack-- = ( StackType_t ) pvParameters;                                 /* R12 */\r
286         *pxTopOfStack-- = ( StackType_t ) 0xDEADBEEF;                                   /* R14/LR */\r
287         *pxTopOfStack-- = ( StackType_t ) pxCode + portINSTRUCTION_SIZE; /* R15/PC */\r
288         *pxTopOfStack-- = ( StackType_t ) portINITIAL_SR;                               /* SR */\r
289         *pxTopOfStack-- = ( StackType_t ) 0xFF0000FF;                                   /* R0 */\r
290         *pxTopOfStack-- = ( StackType_t ) 0x01010101;                                   /* R1 */\r
291         *pxTopOfStack-- = ( StackType_t ) 0x02020202;                                   /* R2 */\r
292         *pxTopOfStack-- = ( StackType_t ) 0x03030303;                                   /* R3 */\r
293         *pxTopOfStack-- = ( StackType_t ) 0x04040404;                                   /* R4 */\r
294         *pxTopOfStack-- = ( StackType_t ) 0x05050505;                                   /* R5 */\r
295         *pxTopOfStack-- = ( StackType_t ) 0x06060606;                                   /* R6 */\r
296         *pxTopOfStack-- = ( StackType_t ) 0x07070707;                                   /* R7 */\r
297         *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_NESTING;                        /* ulCriticalNesting */\r
298 \r
299         return pxTopOfStack;\r
300 }\r
301 /*-----------------------------------------------------------*/\r
302 \r
303 BaseType_t xPortStartScheduler( void )\r
304 {\r
305         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
306         here already. */\r
307         prvSetupTimerInterrupt();\r
308 \r
309         /* Start the first task. */\r
310         portRESTORE_CONTEXT();\r
311 \r
312         /* Should not get here! */\r
313         return 0;\r
314 }\r
315 /*-----------------------------------------------------------*/\r
316 \r
317 void vPortEndScheduler( void )\r
318 {\r
319         /* It is unlikely that the AVR32 port will require this function as there\r
320         is nothing to return to.  */\r
321 }\r
322 /*-----------------------------------------------------------*/\r
323 \r
324 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)\r
325 clock cycles from now. */\r
326 #if( configTICK_USE_TC==0 )\r
327         static void prvScheduleFirstTick(void)\r
328         {\r
329                 uint32_t lCycles;\r
330 \r
331                 lCycles = Get_system_register(AVR32_COUNT);\r
332                 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
333                 // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception\r
334                 // generation feature does not get disabled.\r
335                 if(0 == lCycles)\r
336                 {\r
337                         lCycles++;\r
338                 }\r
339                 Set_system_register(AVR32_COMPARE, lCycles);\r
340         }\r
341         \r
342         #pragma optimize = no_inline\r
343         static void prvScheduleNextTick(void)\r
344         {\r
345                 uint32_t lCycles, lCount;\r
346 \r
347                 lCycles = Get_system_register(AVR32_COMPARE);\r
348                 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
349                 // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception\r
350                 // generation feature does not get disabled.\r
351                 if(0 == lCycles)\r
352                 {\r
353                         lCycles++;\r
354                 }\r
355                 lCount = Get_system_register(AVR32_COUNT);\r
356                 if( lCycles < lCount )\r
357                 {               // We missed a tick, recover for the next.\r
358                         lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
359                 }\r
360                 Set_system_register(AVR32_COMPARE, lCycles);\r
361         }\r
362 #else\r
363         #pragma optimize = no_inline\r
364         static void prvClearTcInt(void)\r
365         {\r
366                 AVR32_TC.channel[configTICK_TC_CHANNEL].sr;\r
367         }\r
368 #endif\r
369 /*-----------------------------------------------------------*/\r
370 \r
371 /* Setup the timer to generate the tick interrupts. */\r
372 static void prvSetupTimerInterrupt(void)\r
373 {\r
374         #if( configTICK_USE_TC==1 )\r
375 \r
376                 volatile avr32_tc_t *tc = &AVR32_TC;\r
377 \r
378                 // Options for waveform genration.\r
379                 tc_waveform_opt_t waveform_opt =\r
380                 {\r
381                 .channel  = configTICK_TC_CHANNEL,             /* Channel selection. */\r
382 \r
383                 .bswtrg   = TC_EVT_EFFECT_NOOP,                /* Software trigger effect on TIOB. */\r
384                 .beevt    = TC_EVT_EFFECT_NOOP,                /* External event effect on TIOB. */\r
385                 .bcpc     = TC_EVT_EFFECT_NOOP,                /* RC compare effect on TIOB. */\r
386                 .bcpb     = TC_EVT_EFFECT_NOOP,                /* RB compare effect on TIOB. */\r
387 \r
388                 .aswtrg   = TC_EVT_EFFECT_NOOP,                /* Software trigger effect on TIOA. */\r
389                 .aeevt    = TC_EVT_EFFECT_NOOP,                /* External event effect on TIOA. */\r
390                 .acpc     = TC_EVT_EFFECT_NOOP,                /* RC compare effect on TIOA: toggle. */\r
391                 .acpa     = TC_EVT_EFFECT_NOOP,                /* RA compare effect on TIOA: toggle (other possibilities are none, set and clear). */\r
392 \r
393                 .wavsel   = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,/* Waveform selection: Up mode without automatic trigger on RC compare. */\r
394                 .enetrg   = FALSE,                             /* External event trigger enable. */\r
395                 .eevt     = 0,                                 /* External event selection. */\r
396                 .eevtedg  = TC_SEL_NO_EDGE,                    /* External event edge selection. */\r
397                 .cpcdis   = FALSE,                             /* Counter disable when RC compare. */\r
398                 .cpcstop  = FALSE,                             /* Counter clock stopped with RC compare. */\r
399 \r
400                 .burst    = FALSE,                             /* Burst signal selection. */\r
401                 .clki     = FALSE,                             /* Clock inversion. */\r
402                 .tcclks   = TC_CLOCK_SOURCE_TC2                /* Internal source clock 2. */\r
403                 };\r
404 \r
405                 tc_interrupt_t tc_interrupt =\r
406                 {\r
407                         .etrgs=0,\r
408                         .ldrbs=0,\r
409                         .ldras=0,\r
410                         .cpcs =1,\r
411                         .cpbs =0,\r
412                         .cpas =0,\r
413                         .lovrs=0,\r
414                         .covfs=0,\r
415                 };\r
416 \r
417         #endif\r
418 \r
419         /* Disable all interrupt/exception. */\r
420         portDISABLE_INTERRUPTS();\r
421 \r
422         /* Register the compare interrupt handler to the interrupt controller and\r
423         enable the compare interrupt. */\r
424 \r
425         #if( configTICK_USE_TC==1 )\r
426         {\r
427                 INTC_register_interrupt((__int_handler)&vTick, configTICK_TC_IRQ, INT0);\r
428 \r
429                 /* Initialize the timer/counter. */\r
430                 tc_init_waveform(tc, &waveform_opt);\r
431 \r
432                 /* Set the compare triggers.\r
433                 Remember TC counter is 16-bits, so counting second is not possible!\r
434                 That's why we configure it to count ms. */\r
435                 tc_write_rc( tc, configTICK_TC_CHANNEL, ( configPBA_CLOCK_HZ / 4) / configTICK_RATE_HZ );\r
436 \r
437                 tc_configure_interrupts( tc, configTICK_TC_CHANNEL, &tc_interrupt );\r
438 \r
439                 /* Start the timer/counter. */\r
440                 tc_start(tc, configTICK_TC_CHANNEL);\r
441         }\r
442         #else\r
443         {\r
444                 INTC_register_interrupt((__int_handler)&vTick, AVR32_CORE_COMPARE_IRQ, INT0);\r
445                 prvScheduleFirstTick();\r
446         }\r
447         #endif\r
448 }\r