1 /*This file has been prepared for Doxygen automatic documentation generation.*/
\r
2 /*! \file *********************************************************************
\r
4 * \brief FreeRTOS port source for AVR32 UC3.
\r
6 * - Compiler: GNU GCC for AVR32
\r
7 * - Supported devices: All AVR32 devices can be used.
\r
10 * \author Atmel Corporation: http://www.atmel.com \n
\r
11 * Support and FAQ: http://support.atmel.no/
\r
13 *****************************************************************************/
\r
16 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
19 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
21 This file is part of the FreeRTOS distribution.
\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
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * FreeRTOS provides completely free yet professionally developed, *
\r
51 * robust, strictly quality controlled, supported, and cross *
\r
52 * platform software that is more than just the market leader, it *
\r
53 * is the industry's de facto standard. *
\r
55 * Help yourself get started quickly while simultaneously helping *
\r
56 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
57 * tutorial book, reference manual, or both: *
\r
58 * http://www.FreeRTOS.org/Documentation *
\r
60 ***************************************************************************
\r
62 ***************************************************************************
\r
64 * Investing in training allows your team to be as productive as *
\r
65 * possible as early as possible, lowering your overall development *
\r
66 * cost, and enabling you to bring a more robust product to market *
\r
67 * earlier than would otherwise be possible. Richard Barry is both *
\r
68 * the architect and key author of FreeRTOS, and so also the world's *
\r
69 * leading authority on what is the world's most popular real time *
\r
70 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
71 * from Richard ensures your team will gain directly from his in-depth *
\r
72 * product knowledge and years of usage experience. Contact Real Time *
\r
73 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
74 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
76 ***************************************************************************
\r
78 ***************************************************************************
\r
80 * You are receiving this top quality software for free. Please play *
\r
81 * fair and reciprocate by reporting any suspected issues and *
\r
82 * participating in the community forum: *
\r
83 * http://www.FreeRTOS.org/support *
\r
87 ***************************************************************************
\r
89 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
90 license and Real Time Engineers Ltd. contact details.
\r
92 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
93 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
94 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
96 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
97 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
99 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
100 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
101 licenses offer ticketed support, indemnification and commercial middleware.
\r
103 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
104 engineered and independently SIL3 certified version for use in safety and
\r
105 mission critical applications that require provable dependability.
\r
111 /* Standard includes. */
\r
112 #include <sys/cpu.h>
\r
113 #include <sys/usart.h>
\r
114 #include <malloc.h>
\r
116 /* Scheduler includes. */
\r
117 #include "FreeRTOS.h"
\r
120 /* AVR32 UC3 includes. */
\r
121 #include <avr32/io.h>
\r
123 #if( configTICK_USE_TC==1 )
\r
128 /* Constants required to setup the task context. */
\r
129 #define portINITIAL_SR ( ( StackType_t ) 0x00400000 ) /* AVR32 : [M2:M0]=001 I1M=0 I0M=0, GM=0 */
\r
130 #define portINSTRUCTION_SIZE ( ( StackType_t ) 0 )
\r
132 /* Each task maintains its own critical nesting variable. */
\r
133 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
\r
134 volatile uint32_t ulCriticalNesting = 9999UL;
\r
136 #if( configTICK_USE_TC==0 )
\r
137 static void prvScheduleNextTick( void );
\r
139 static void prvClearTcInt( void );
\r
142 /* Setup the timer to generate the tick interrupts. */
\r
143 static void prvSetupTimerInterrupt( void );
\r
145 /*-----------------------------------------------------------*/
\r
148 * Low-level initialization routine called during startup, before the main
\r
150 * This version comes in replacement to the default one provided by Newlib.
\r
151 * Newlib's _init_startup only calls init_exceptions, but Newlib's exception
\r
152 * vectors are not compatible with the SCALL management in the current FreeRTOS
\r
153 * port. More low-level initializations are besides added here.
\r
155 void _init_startup(void)
\r
157 /* Import the Exception Vector Base Address. */
\r
160 #if configHEAP_INIT
\r
161 extern void __heap_start__;
\r
162 extern void __heap_end__;
\r
166 /* Load the Exception Vector Base Address in the corresponding system register. */
\r
167 Set_system_register( AVR32_EVBA, ( int ) &_evba );
\r
169 /* Enable exceptions. */
\r
170 ENABLE_ALL_EXCEPTIONS();
\r
172 /* Initialize interrupt handling. */
\r
173 INTC_init_interrupts();
\r
175 #if configHEAP_INIT
\r
177 /* Initialize the heap used by malloc. */
\r
178 for( pxMem = &__heap_start__; pxMem < ( BaseType_t * )&__heap_end__; )
\r
180 *pxMem++ = 0xA5A5A5A5;
\r
185 /* Give the used CPU clock frequency to Newlib, so it can work properly. */
\r
186 set_cpu_hz( configCPU_CLOCK_HZ );
\r
188 /* Code section present if and only if the debug trace is activated. */
\r
191 static const gpio_map_t DBG_USART_GPIO_MAP =
\r
193 { configDBG_USART_RX_PIN, configDBG_USART_RX_FUNCTION },
\r
194 { configDBG_USART_TX_PIN, configDBG_USART_TX_FUNCTION }
\r
197 /* Initialize the USART used for the debug trace with the configured parameters. */
\r
198 set_usart_base( ( void * ) configDBG_USART );
\r
199 gpio_enable_module( DBG_USART_GPIO_MAP,
\r
200 sizeof( DBG_USART_GPIO_MAP ) / sizeof( DBG_USART_GPIO_MAP[0] ) );
\r
201 usart_init( configDBG_USART_BAUDRATE );
\r
205 /*-----------------------------------------------------------*/
\r
208 * malloc, realloc and free are meant to be called through respectively
\r
209 * pvPortMalloc, pvPortRealloc and vPortFree.
\r
210 * The latter functions call the former ones from within sections where tasks
\r
211 * are suspended, so the latter functions are task-safe. __malloc_lock and
\r
212 * __malloc_unlock use the same mechanism to also keep the former functions
\r
213 * task-safe as they may be called directly from Newlib's functions.
\r
214 * However, all these functions are interrupt-unsafe and SHALL THEREFORE NOT BE
\r
215 * CALLED FROM WITHIN AN INTERRUPT, because __malloc_lock and __malloc_unlock do
\r
216 * not call portENTER_CRITICAL and portEXIT_CRITICAL in order not to disable
\r
217 * interrupts during memory allocation management as this may be a very time-
\r
218 * consuming process.
\r
222 * Lock routine called by Newlib on malloc / realloc / free entry to guarantee a
\r
223 * safe section as memory allocation management uses global data.
\r
224 * See the aforementioned details.
\r
226 void __malloc_lock(struct _reent *ptr)
\r
232 * Unlock routine called by Newlib on malloc / realloc / free exit to guarantee
\r
233 * a safe section as memory allocation management uses global data.
\r
234 * See the aforementioned details.
\r
236 void __malloc_unlock(struct _reent *ptr)
\r
240 /*-----------------------------------------------------------*/
\r
242 /* Added as there is no such function in FreeRTOS. */
\r
243 void *pvPortRealloc( void *pv, size_t xWantedSize )
\r
249 pvReturn = realloc( pv, xWantedSize );
\r
255 /*-----------------------------------------------------------*/
\r
257 /* The cooperative scheduler requires a normal IRQ service routine to
\r
258 simply increment the system tick. */
\r
259 /* The preemptive scheduler is defined as "naked" as the full context is saved
\r
260 on entry as part of the context switch. */
\r
261 __attribute__((__naked__)) static void vTick( void )
\r
263 /* Save the context of the interrupted task. */
\r
264 portSAVE_CONTEXT_OS_INT();
\r
266 #if( configTICK_USE_TC==1 )
\r
267 /* Clear the interrupt flag. */
\r
270 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)
\r
271 clock cycles from now. */
\r
272 prvScheduleNextTick();
\r
275 /* Because FreeRTOS is not supposed to run with nested interrupts, put all OS
\r
276 calls in a critical section . */
\r
277 portENTER_CRITICAL();
\r
278 xTaskIncrementTick();
\r
279 portEXIT_CRITICAL();
\r
281 /* Restore the context of the "elected task". */
\r
282 portRESTORE_CONTEXT_OS_INT();
\r
284 /*-----------------------------------------------------------*/
\r
286 __attribute__((__naked__)) void SCALLYield( void )
\r
288 /* Save the context of the interrupted task. */
\r
289 portSAVE_CONTEXT_SCALL();
\r
290 vTaskSwitchContext();
\r
291 portRESTORE_CONTEXT_SCALL();
\r
293 /*-----------------------------------------------------------*/
\r
295 /* The code generated by the GCC compiler uses the stack in different ways at
\r
296 different optimisation levels. The interrupt flags can therefore not always
\r
297 be saved to the stack. Instead the critical section nesting level is stored
\r
298 in a variable, which is then saved as part of the stack context. */
\r
299 __attribute__((__noinline__)) void vPortEnterCritical( void )
\r
301 /* Disable interrupts */
\r
302 portDISABLE_INTERRUPTS();
\r
304 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
305 directly. Increment ulCriticalNesting to keep a count of how many times
\r
306 portENTER_CRITICAL() has been called. */
\r
307 ulCriticalNesting++;
\r
309 /*-----------------------------------------------------------*/
\r
311 __attribute__((__noinline__)) void vPortExitCritical( void )
\r
313 if(ulCriticalNesting > portNO_CRITICAL_NESTING)
\r
315 ulCriticalNesting--;
\r
316 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
318 /* Enable all interrupt/exception. */
\r
319 portENABLE_INTERRUPTS();
\r
323 /*-----------------------------------------------------------*/
\r
326 * Initialise the stack of a task to look exactly as if a call to
\r
327 * portSAVE_CONTEXT had been called.
\r
329 * See header file for description.
\r
331 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
333 /* Setup the initial stack of the task. The stack is set exactly as
\r
334 expected by the portRESTORE_CONTEXT() macro. */
\r
336 /* When the task starts, it will expect to find the function parameter in R12. */
\r
338 *pxTopOfStack-- = ( StackType_t ) 0x08080808; /* R8 */
\r
339 *pxTopOfStack-- = ( StackType_t ) 0x09090909; /* R9 */
\r
340 *pxTopOfStack-- = ( StackType_t ) 0x0A0A0A0A; /* R10 */
\r
341 *pxTopOfStack-- = ( StackType_t ) 0x0B0B0B0B; /* R11 */
\r
342 *pxTopOfStack-- = ( StackType_t ) pvParameters; /* R12 */
\r
343 *pxTopOfStack-- = ( StackType_t ) 0xDEADBEEF; /* R14/LR */
\r
344 *pxTopOfStack-- = ( StackType_t ) pxCode + portINSTRUCTION_SIZE; /* R15/PC */
\r
345 *pxTopOfStack-- = ( StackType_t ) portINITIAL_SR; /* SR */
\r
346 *pxTopOfStack-- = ( StackType_t ) 0xFF0000FF; /* R0 */
\r
347 *pxTopOfStack-- = ( StackType_t ) 0x01010101; /* R1 */
\r
348 *pxTopOfStack-- = ( StackType_t ) 0x02020202; /* R2 */
\r
349 *pxTopOfStack-- = ( StackType_t ) 0x03030303; /* R3 */
\r
350 *pxTopOfStack-- = ( StackType_t ) 0x04040404; /* R4 */
\r
351 *pxTopOfStack-- = ( StackType_t ) 0x05050505; /* R5 */
\r
352 *pxTopOfStack-- = ( StackType_t ) 0x06060606; /* R6 */
\r
353 *pxTopOfStack-- = ( StackType_t ) 0x07070707; /* R7 */
\r
354 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_NESTING; /* ulCriticalNesting */
\r
356 return pxTopOfStack;
\r
358 /*-----------------------------------------------------------*/
\r
360 BaseType_t xPortStartScheduler( void )
\r
362 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
364 prvSetupTimerInterrupt();
\r
366 /* Start the first task. */
\r
367 portRESTORE_CONTEXT();
\r
369 /* Should not get here! */
\r
372 /*-----------------------------------------------------------*/
\r
374 void vPortEndScheduler( void )
\r
376 /* It is unlikely that the AVR32 port will require this function as there
\r
377 is nothing to return to. */
\r
379 /*-----------------------------------------------------------*/
\r
381 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)
\r
382 clock cycles from now. */
\r
383 #if( configTICK_USE_TC==0 )
\r
384 static void prvScheduleFirstTick(void)
\r
388 lCycles = Get_system_register(AVR32_COUNT);
\r
389 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);
\r
390 // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception
\r
391 // generation feature does not get disabled.
\r
396 Set_system_register(AVR32_COMPARE, lCycles);
\r
399 __attribute__((__noinline__)) static void prvScheduleNextTick(void)
\r
401 uint32_t lCycles, lCount;
\r
403 lCycles = Get_system_register(AVR32_COMPARE);
\r
404 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);
\r
405 // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception
\r
406 // generation feature does not get disabled.
\r
411 lCount = Get_system_register(AVR32_COUNT);
\r
412 if( lCycles < lCount )
\r
413 { // We missed a tick, recover for the next.
\r
414 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);
\r
416 Set_system_register(AVR32_COMPARE, lCycles);
\r
419 __attribute__((__noinline__)) static void prvClearTcInt(void)
\r
421 AVR32_TC.channel[configTICK_TC_CHANNEL].sr;
\r
424 /*-----------------------------------------------------------*/
\r
426 /* Setup the timer to generate the tick interrupts. */
\r
427 static void prvSetupTimerInterrupt(void)
\r
429 #if( configTICK_USE_TC==1 )
\r
431 volatile avr32_tc_t *tc = &AVR32_TC;
\r
433 // Options for waveform genration.
\r
434 tc_waveform_opt_t waveform_opt =
\r
436 .channel = configTICK_TC_CHANNEL, /* Channel selection. */
\r
438 .bswtrg = TC_EVT_EFFECT_NOOP, /* Software trigger effect on TIOB. */
\r
439 .beevt = TC_EVT_EFFECT_NOOP, /* External event effect on TIOB. */
\r
440 .bcpc = TC_EVT_EFFECT_NOOP, /* RC compare effect on TIOB. */
\r
441 .bcpb = TC_EVT_EFFECT_NOOP, /* RB compare effect on TIOB. */
\r
443 .aswtrg = TC_EVT_EFFECT_NOOP, /* Software trigger effect on TIOA. */
\r
444 .aeevt = TC_EVT_EFFECT_NOOP, /* External event effect on TIOA. */
\r
445 .acpc = TC_EVT_EFFECT_NOOP, /* RC compare effect on TIOA: toggle. */
\r
446 .acpa = TC_EVT_EFFECT_NOOP, /* RA compare effect on TIOA: toggle (other possibilities are none, set and clear). */
\r
448 .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,/* Waveform selection: Up mode without automatic trigger on RC compare. */
\r
449 .enetrg = FALSE, /* External event trigger enable. */
\r
450 .eevt = 0, /* External event selection. */
\r
451 .eevtedg = TC_SEL_NO_EDGE, /* External event edge selection. */
\r
452 .cpcdis = FALSE, /* Counter disable when RC compare. */
\r
453 .cpcstop = FALSE, /* Counter clock stopped with RC compare. */
\r
455 .burst = FALSE, /* Burst signal selection. */
\r
456 .clki = FALSE, /* Clock inversion. */
\r
457 .tcclks = TC_CLOCK_SOURCE_TC2 /* Internal source clock 2. */
\r
460 tc_interrupt_t tc_interrupt =
\r
474 /* Disable all interrupt/exception. */
\r
475 portDISABLE_INTERRUPTS();
\r
477 /* Register the compare interrupt handler to the interrupt controller and
\r
478 enable the compare interrupt. */
\r
480 #if( configTICK_USE_TC==1 )
\r
482 INTC_register_interrupt(&vTick, configTICK_TC_IRQ, INT0);
\r
484 /* Initialize the timer/counter. */
\r
485 tc_init_waveform(tc, &waveform_opt);
\r
487 /* Set the compare triggers.
\r
488 Remember TC counter is 16-bits, so counting second is not possible!
\r
489 That's why we configure it to count ms. */
\r
490 tc_write_rc( tc, configTICK_TC_CHANNEL, ( configPBA_CLOCK_HZ / 4) / configTICK_RATE_HZ );
\r
492 tc_configure_interrupts( tc, configTICK_TC_CHANNEL, &tc_interrupt );
\r
494 /* Start the timer/counter. */
\r
495 tc_start(tc, configTICK_TC_CHANNEL);
\r
499 INTC_register_interrupt(&vTick, AVR32_CORE_COMPARE_IRQ, INT0);
\r
500 prvScheduleFirstTick();
\r