2 * FreeRTOS Kernel V10.4.4
\r
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * SPDX-License-Identifier: MIT AND BSD-3-Clause
\r
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
8 * this software and associated documentation files (the "Software"), to deal in
\r
9 * the Software without restriction, including without limitation the rights to
\r
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
11 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
12 * subject to the following conditions:
\r
14 * The above copyright notice and this permission notice shall be included in all
\r
15 * copies or substantial portions of the Software.
\r
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
24 * https://www.FreeRTOS.org
\r
25 * https://github.com/FreeRTOS
\r
29 /*This file has been prepared for Doxygen automatic documentation generation.*/
\r
30 /*! \file *********************************************************************
\r
32 * \brief FreeRTOS port source for AVR32 UC3.
\r
34 * - Compiler: GNU GCC for AVR32
\r
35 * - Supported devices: All AVR32 devices can be used.
\r
38 * \author Atmel Corporation (Now Microchip):
\r
39 * https://www.microchip.com \n
\r
40 * Support and FAQ: https://www.microchip.com/support/
\r
42 *****************************************************************************/
\r
45 * Copyright (c) 2007, Atmel Corporation All rights reserved.
\r
47 * Redistribution and use in source and binary forms, with or without
\r
48 * modification, are permitted provided that the following conditions are met:
\r
50 * 1. Redistributions of source code must retain the above copyright notice,
\r
51 * this list of conditions and the following disclaimer.
\r
53 * 2. Redistributions in binary form must reproduce the above copyright notice,
\r
54 * this list of conditions and the following disclaimer in the documentation
\r
55 * and/or other materials provided with the distribution.
\r
57 * 3. The name of ATMEL may not be used to endorse or promote products derived
\r
58 * from this software without specific prior written permission.
\r
60 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
\r
61 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
62 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
\r
63 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
\r
64 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
\r
65 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
\r
66 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
\r
67 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
68 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
69 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
72 /* Standard includes. */
\r
73 #include <sys/cpu.h>
\r
74 #include <sys/usart.h>
\r
77 /* Scheduler includes. */
\r
78 #include "FreeRTOS.h"
\r
81 /* AVR32 UC3 includes. */
\r
82 #include <avr32/io.h>
\r
84 #if( configTICK_USE_TC==1 )
\r
89 /* Constants required to setup the task context. */
\r
90 #define portINITIAL_SR ( ( StackType_t ) 0x00400000 ) /* AVR32 : [M2:M0]=001 I1M=0 I0M=0, GM=0 */
\r
91 #define portINSTRUCTION_SIZE ( ( StackType_t ) 0 )
\r
93 /* Each task maintains its own critical nesting variable. */
\r
94 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
\r
95 volatile uint32_t ulCriticalNesting = 9999UL;
\r
97 #if( configTICK_USE_TC==0 )
\r
98 static void prvScheduleNextTick( void );
\r
100 static void prvClearTcInt( void );
\r
103 /* Setup the timer to generate the tick interrupts. */
\r
104 static void prvSetupTimerInterrupt( void );
\r
106 /*-----------------------------------------------------------*/
\r
109 * Low-level initialization routine called during startup, before the main
\r
111 * This version comes in replacement to the default one provided by Newlib.
\r
112 * Newlib's _init_startup only calls init_exceptions, but Newlib's exception
\r
113 * vectors are not compatible with the SCALL management in the current FreeRTOS
\r
114 * port. More low-level initializations are besides added here.
\r
116 void _init_startup(void)
\r
118 /* Import the Exception Vector Base Address. */
\r
121 #if configHEAP_INIT
\r
122 extern void __heap_start__;
\r
123 extern void __heap_end__;
\r
127 /* Load the Exception Vector Base Address in the corresponding system register. */
\r
128 Set_system_register( AVR32_EVBA, ( int ) &_evba );
\r
130 /* Enable exceptions. */
\r
131 ENABLE_ALL_EXCEPTIONS();
\r
133 /* Initialize interrupt handling. */
\r
134 INTC_init_interrupts();
\r
136 #if configHEAP_INIT
\r
138 /* Initialize the heap used by malloc. */
\r
139 for( pxMem = &__heap_start__; pxMem < ( BaseType_t * )&__heap_end__; )
\r
141 *pxMem++ = 0xA5A5A5A5;
\r
146 /* Give the used CPU clock frequency to Newlib, so it can work properly. */
\r
147 set_cpu_hz( configCPU_CLOCK_HZ );
\r
149 /* Code section present if and only if the debug trace is activated. */
\r
152 static const gpio_map_t DBG_USART_GPIO_MAP =
\r
154 { configDBG_USART_RX_PIN, configDBG_USART_RX_FUNCTION },
\r
155 { configDBG_USART_TX_PIN, configDBG_USART_TX_FUNCTION }
\r
158 /* Initialize the USART used for the debug trace with the configured parameters. */
\r
159 set_usart_base( ( void * ) configDBG_USART );
\r
160 gpio_enable_module( DBG_USART_GPIO_MAP,
\r
161 sizeof( DBG_USART_GPIO_MAP ) / sizeof( DBG_USART_GPIO_MAP[0] ) );
\r
162 usart_init( configDBG_USART_BAUDRATE );
\r
166 /*-----------------------------------------------------------*/
\r
169 * malloc, realloc and free are meant to be called through respectively
\r
170 * pvPortMalloc, pvPortRealloc and vPortFree.
\r
171 * The latter functions call the former ones from within sections where tasks
\r
172 * are suspended, so the latter functions are task-safe. __malloc_lock and
\r
173 * __malloc_unlock use the same mechanism to also keep the former functions
\r
174 * task-safe as they may be called directly from Newlib's functions.
\r
175 * However, all these functions are interrupt-unsafe and SHALL THEREFORE NOT BE
\r
176 * CALLED FROM WITHIN AN INTERRUPT, because __malloc_lock and __malloc_unlock do
\r
177 * not call portENTER_CRITICAL and portEXIT_CRITICAL in order not to disable
\r
178 * interrupts during memory allocation management as this may be a very time-
\r
179 * consuming process.
\r
183 * Lock routine called by Newlib on malloc / realloc / free entry to guarantee a
\r
184 * safe section as memory allocation management uses global data.
\r
185 * See the aforementioned details.
\r
187 void __malloc_lock(struct _reent *ptr)
\r
193 * Unlock routine called by Newlib on malloc / realloc / free exit to guarantee
\r
194 * a safe section as memory allocation management uses global data.
\r
195 * See the aforementioned details.
\r
197 void __malloc_unlock(struct _reent *ptr)
\r
201 /*-----------------------------------------------------------*/
\r
203 /* Added as there is no such function in FreeRTOS. */
\r
204 void *pvPortRealloc( void *pv, size_t xWantedSize )
\r
210 pvReturn = realloc( pv, xWantedSize );
\r
216 /*-----------------------------------------------------------*/
\r
218 /* The cooperative scheduler requires a normal IRQ service routine to
\r
219 simply increment the system tick. */
\r
220 /* The preemptive scheduler is defined as "naked" as the full context is saved
\r
221 on entry as part of the context switch. */
\r
222 __attribute__((__naked__)) static void vTick( void )
\r
224 /* Save the context of the interrupted task. */
\r
225 portSAVE_CONTEXT_OS_INT();
\r
227 #if( configTICK_USE_TC==1 )
\r
228 /* Clear the interrupt flag. */
\r
231 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)
\r
232 clock cycles from now. */
\r
233 prvScheduleNextTick();
\r
236 /* Because FreeRTOS is not supposed to run with nested interrupts, put all OS
\r
237 calls in a critical section . */
\r
238 portENTER_CRITICAL();
\r
239 xTaskIncrementTick();
\r
240 portEXIT_CRITICAL();
\r
242 /* Restore the context of the "elected task". */
\r
243 portRESTORE_CONTEXT_OS_INT();
\r
245 /*-----------------------------------------------------------*/
\r
247 __attribute__((__naked__)) void SCALLYield( void )
\r
249 /* Save the context of the interrupted task. */
\r
250 portSAVE_CONTEXT_SCALL();
\r
251 vTaskSwitchContext();
\r
252 portRESTORE_CONTEXT_SCALL();
\r
254 /*-----------------------------------------------------------*/
\r
256 /* The code generated by the GCC compiler uses the stack in different ways at
\r
257 different optimisation levels. The interrupt flags can therefore not always
\r
258 be saved to the stack. Instead the critical section nesting level is stored
\r
259 in a variable, which is then saved as part of the stack context. */
\r
260 __attribute__((__noinline__)) void vPortEnterCritical( void )
\r
262 /* Disable interrupts */
\r
263 portDISABLE_INTERRUPTS();
\r
265 /* Now interrupts are disabled ulCriticalNesting can be accessed
\r
266 directly. Increment ulCriticalNesting to keep a count of how many times
\r
267 portENTER_CRITICAL() has been called. */
\r
268 ulCriticalNesting++;
\r
270 /*-----------------------------------------------------------*/
\r
272 __attribute__((__noinline__)) void vPortExitCritical( void )
\r
274 if(ulCriticalNesting > portNO_CRITICAL_NESTING)
\r
276 ulCriticalNesting--;
\r
277 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
\r
279 /* Enable all interrupt/exception. */
\r
280 portENABLE_INTERRUPTS();
\r
284 /*-----------------------------------------------------------*/
\r
287 * Initialise the stack of a task to look exactly as if a call to
\r
288 * portSAVE_CONTEXT had been called.
\r
290 * See header file for description.
\r
292 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )
\r
294 /* Setup the initial stack of the task. The stack is set exactly as
\r
295 expected by the portRESTORE_CONTEXT() macro. */
\r
297 /* When the task starts, it will expect to find the function parameter in R12. */
\r
299 *pxTopOfStack-- = ( StackType_t ) 0x08080808; /* R8 */
\r
300 *pxTopOfStack-- = ( StackType_t ) 0x09090909; /* R9 */
\r
301 *pxTopOfStack-- = ( StackType_t ) 0x0A0A0A0A; /* R10 */
\r
302 *pxTopOfStack-- = ( StackType_t ) 0x0B0B0B0B; /* R11 */
\r
303 *pxTopOfStack-- = ( StackType_t ) pvParameters; /* R12 */
\r
304 *pxTopOfStack-- = ( StackType_t ) 0xDEADBEEF; /* R14/LR */
\r
305 *pxTopOfStack-- = ( StackType_t ) pxCode + portINSTRUCTION_SIZE; /* R15/PC */
\r
306 *pxTopOfStack-- = ( StackType_t ) portINITIAL_SR; /* SR */
\r
307 *pxTopOfStack-- = ( StackType_t ) 0xFF0000FF; /* R0 */
\r
308 *pxTopOfStack-- = ( StackType_t ) 0x01010101; /* R1 */
\r
309 *pxTopOfStack-- = ( StackType_t ) 0x02020202; /* R2 */
\r
310 *pxTopOfStack-- = ( StackType_t ) 0x03030303; /* R3 */
\r
311 *pxTopOfStack-- = ( StackType_t ) 0x04040404; /* R4 */
\r
312 *pxTopOfStack-- = ( StackType_t ) 0x05050505; /* R5 */
\r
313 *pxTopOfStack-- = ( StackType_t ) 0x06060606; /* R6 */
\r
314 *pxTopOfStack-- = ( StackType_t ) 0x07070707; /* R7 */
\r
315 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_NESTING; /* ulCriticalNesting */
\r
317 return pxTopOfStack;
\r
319 /*-----------------------------------------------------------*/
\r
321 BaseType_t xPortStartScheduler( void )
\r
323 /* Start the timer that generates the tick ISR. Interrupts are disabled
\r
325 prvSetupTimerInterrupt();
\r
327 /* Start the first task. */
\r
328 portRESTORE_CONTEXT();
\r
330 /* Should not get here! */
\r
333 /*-----------------------------------------------------------*/
\r
335 void vPortEndScheduler( void )
\r
337 /* It is unlikely that the AVR32 port will require this function as there
\r
338 is nothing to return to. */
\r
340 /*-----------------------------------------------------------*/
\r
342 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)
\r
343 clock cycles from now. */
\r
344 #if( configTICK_USE_TC==0 )
\r
345 static void prvScheduleFirstTick(void)
\r
349 lCycles = Get_system_register(AVR32_COUNT);
\r
350 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);
\r
351 // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception
\r
352 // generation feature does not get disabled.
\r
357 Set_system_register(AVR32_COMPARE, lCycles);
\r
360 __attribute__((__noinline__)) static void prvScheduleNextTick(void)
\r
362 uint32_t lCycles, lCount;
\r
364 lCycles = Get_system_register(AVR32_COMPARE);
\r
365 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);
\r
366 // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception
\r
367 // generation feature does not get disabled.
\r
372 lCount = Get_system_register(AVR32_COUNT);
\r
373 if( lCycles < lCount )
\r
374 { // We missed a tick, recover for the next.
\r
375 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);
\r
377 Set_system_register(AVR32_COMPARE, lCycles);
\r
380 __attribute__((__noinline__)) static void prvClearTcInt(void)
\r
382 AVR32_TC.channel[configTICK_TC_CHANNEL].sr;
\r
385 /*-----------------------------------------------------------*/
\r
387 /* Setup the timer to generate the tick interrupts. */
\r
388 static void prvSetupTimerInterrupt(void)
\r
390 #if( configTICK_USE_TC==1 )
\r
392 volatile avr32_tc_t *tc = &AVR32_TC;
\r
394 // Options for waveform genration.
\r
395 tc_waveform_opt_t waveform_opt =
\r
397 .channel = configTICK_TC_CHANNEL, /* Channel selection. */
\r
399 .bswtrg = TC_EVT_EFFECT_NOOP, /* Software trigger effect on TIOB. */
\r
400 .beevt = TC_EVT_EFFECT_NOOP, /* External event effect on TIOB. */
\r
401 .bcpc = TC_EVT_EFFECT_NOOP, /* RC compare effect on TIOB. */
\r
402 .bcpb = TC_EVT_EFFECT_NOOP, /* RB compare effect on TIOB. */
\r
404 .aswtrg = TC_EVT_EFFECT_NOOP, /* Software trigger effect on TIOA. */
\r
405 .aeevt = TC_EVT_EFFECT_NOOP, /* External event effect on TIOA. */
\r
406 .acpc = TC_EVT_EFFECT_NOOP, /* RC compare effect on TIOA: toggle. */
\r
407 .acpa = TC_EVT_EFFECT_NOOP, /* RA compare effect on TIOA: toggle (other possibilities are none, set and clear). */
\r
409 .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,/* Waveform selection: Up mode without automatic trigger on RC compare. */
\r
410 .enetrg = FALSE, /* External event trigger enable. */
\r
411 .eevt = 0, /* External event selection. */
\r
412 .eevtedg = TC_SEL_NO_EDGE, /* External event edge selection. */
\r
413 .cpcdis = FALSE, /* Counter disable when RC compare. */
\r
414 .cpcstop = FALSE, /* Counter clock stopped with RC compare. */
\r
416 .burst = FALSE, /* Burst signal selection. */
\r
417 .clki = FALSE, /* Clock inversion. */
\r
418 .tcclks = TC_CLOCK_SOURCE_TC2 /* Internal source clock 2. */
\r
421 tc_interrupt_t tc_interrupt =
\r
435 /* Disable all interrupt/exception. */
\r
436 portDISABLE_INTERRUPTS();
\r
438 /* Register the compare interrupt handler to the interrupt controller and
\r
439 enable the compare interrupt. */
\r
441 #if( configTICK_USE_TC==1 )
\r
443 INTC_register_interrupt(&vTick, configTICK_TC_IRQ, INT0);
\r
445 /* Initialize the timer/counter. */
\r
446 tc_init_waveform(tc, &waveform_opt);
\r
448 /* Set the compare triggers.
\r
449 Remember TC counter is 16-bits, so counting second is not possible!
\r
450 That's why we configure it to count ms. */
\r
451 tc_write_rc( tc, configTICK_TC_CHANNEL, ( configPBA_CLOCK_HZ / 4) / configTICK_RATE_HZ );
\r
453 tc_configure_interrupts( tc, configTICK_TC_CHANNEL, &tc_interrupt );
\r
455 /* Start the timer/counter. */
\r
456 tc_start(tc, configTICK_TC_CHANNEL);
\r
460 INTC_register_interrupt(&vTick, AVR32_CORE_COMPARE_IRQ, INT0);
\r
461 prvScheduleFirstTick();
\r