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