2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT AND BSD-3-Clause
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
29 /*This file has been prepared for Doxygen automatic documentation generation.*/
31 /*! \file *********************************************************************
33 * \brief FreeRTOS port source for AVR32 UC3.
35 * - Compiler: IAR EWAVR32
36 * - Supported devices: All AVR32 devices can be used.
39 * \author Atmel Corporation (Now Microchip):
40 * https://www.microchip.com \n
41 * Support and FAQ: https://www.microchip.com/support/
43 *****************************************************************************/
46 * Copyright (c) 2007, Atmel Corporation All rights reserved.
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions are met:
51 * 1. Redistributions of source code must retain the above copyright notice,
52 * this list of conditions and the following disclaimer.
54 * 2. Redistributions in binary form must reproduce the above copyright notice,
55 * this list of conditions and the following disclaimer in the documentation
56 * and/or other materials provided with the distribution.
58 * 3. The name of ATMEL may not be used to endorse or promote products derived
59 * from this software without specific prior written permission.
61 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
62 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
63 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
64 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
65 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
66 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
67 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
68 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
69 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
70 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 /* Scheduler includes. */
77 /* AVR32 UC3 includes. */
79 #include <intrinsics.h>
86 #if ( configTICK_USE_TC == 1 )
91 /* Constants required to setup the task context. */
92 #define portINITIAL_SR ( ( StackType_t ) 0x00400000 ) /* AVR32 : [M2:M0]=001 I1M=0 I0M=0, GM=0 */
93 #define portINSTRUCTION_SIZE ( ( StackType_t ) 0 )
95 /* Each task maintains its own critical nesting variable. */
96 #define portNO_CRITICAL_NESTING ( ( uint32_t ) 0 )
97 volatile uint32_t ulCriticalNesting = 9999UL;
99 #if ( configTICK_USE_TC == 0 )
100 static void prvScheduleNextTick( void );
102 static void prvClearTcInt( void );
105 /* Setup the timer to generate the tick interrupts. */
106 static void prvSetupTimerInterrupt( void );
108 /*-----------------------------------------------------------*/
111 * Low-level initialization routine called during startup, before the main
114 int __low_level_init( void )
117 #pragma segment = "HEAP"
121 /* Enable exceptions. */
122 ENABLE_ALL_EXCEPTIONS();
124 /* Initialize interrupt handling. */
125 INTC_init_interrupts();
129 /* Initialize the heap used by malloc. */
130 for( pxMem = __segment_begin( "HEAP" ); pxMem < ( BaseType_t * ) __segment_end( "HEAP" ); )
132 *pxMem++ = 0xA5A5A5A5;
137 /* Code section present if and only if the debug trace is activated. */
140 static const gpio_map_t DBG_USART_GPIO_MAP =
142 { configDBG_USART_RX_PIN, configDBG_USART_RX_FUNCTION },
143 { configDBG_USART_TX_PIN, configDBG_USART_TX_FUNCTION }
146 static const usart_options_t DBG_USART_OPTIONS =
148 .baudrate = configDBG_USART_BAUDRATE,
150 .paritytype = USART_NO_PARITY,
151 .stopbits = USART_1_STOPBIT,
152 .channelmode = USART_NORMAL_CHMODE
155 /* Initialize the USART used for the debug trace with the configured parameters. */
156 extern volatile avr32_usart_t * volatile stdio_usart_base;
157 stdio_usart_base = configDBG_USART;
158 gpio_enable_module( DBG_USART_GPIO_MAP,
159 sizeof( DBG_USART_GPIO_MAP ) / sizeof( DBG_USART_GPIO_MAP[ 0 ] ) );
160 usart_init_rs232( configDBG_USART, &DBG_USART_OPTIONS, configCPU_CLOCK_HZ );
162 #endif /* if configDBG */
164 /* Request initialization of data segments. */
167 /*-----------------------------------------------------------*/
169 /* Added as there is no such function in FreeRTOS. */
170 void * pvPortRealloc( void * pv,
177 pvReturn = realloc( pv, xWantedSize );
183 /*-----------------------------------------------------------*/
185 /* The cooperative scheduler requires a normal IRQ service routine to
186 * simply increment the system tick. */
188 /* The preemptive scheduler is defined as "naked" as the full context is saved
189 * on entry as part of the context switch. */
190 #pragma shadow_registers = full /* Naked. */
191 static void vTick( void )
193 /* Save the context of the interrupted task. */
194 portSAVE_CONTEXT_OS_INT();
196 #if ( configTICK_USE_TC == 1 )
197 /* Clear the interrupt flag. */
201 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)
202 * clock cycles from now. */
203 prvScheduleNextTick();
206 /* Because FreeRTOS is not supposed to run with nested interrupts, put all OS
207 * calls in a critical section . */
208 portENTER_CRITICAL();
209 xTaskIncrementTick();
212 /* Restore the context of the "elected task". */
213 portRESTORE_CONTEXT_OS_INT();
215 /*-----------------------------------------------------------*/
217 #pragma shadow_registers = full /* Naked. */
218 void SCALLYield( void )
220 /* Save the context of the interrupted task. */
221 portSAVE_CONTEXT_SCALL();
222 vTaskSwitchContext();
223 portRESTORE_CONTEXT_SCALL();
225 /*-----------------------------------------------------------*/
227 /* The code generated by the GCC compiler uses the stack in different ways at
228 * different optimisation levels. The interrupt flags can therefore not always
229 * be saved to the stack. Instead the critical section nesting level is stored
230 * in a variable, which is then saved as part of the stack context. */
231 #pragma optimize = no_inline
232 void vPortEnterCritical( void )
234 /* Disable interrupts */
235 portDISABLE_INTERRUPTS();
237 /* Now that interrupts are disabled, ulCriticalNesting can be accessed
238 * directly. Increment ulCriticalNesting to keep a count of how many times
239 * portENTER_CRITICAL() has been called. */
242 /*-----------------------------------------------------------*/
244 #pragma optimize = no_inline
245 void vPortExitCritical( void )
247 if( ulCriticalNesting > portNO_CRITICAL_NESTING )
251 if( ulCriticalNesting == portNO_CRITICAL_NESTING )
253 /* Enable all interrupt/exception. */
254 portENABLE_INTERRUPTS();
258 /*-----------------------------------------------------------*/
261 * Initialise the stack of a task to look exactly as if a call to
262 * portSAVE_CONTEXT had been called.
264 * See header file for description.
266 StackType_t * pxPortInitialiseStack( StackType_t * pxTopOfStack,
267 TaskFunction_t pxCode,
268 void * pvParameters )
270 /* Setup the initial stack of the task. The stack is set exactly as
271 * expected by the portRESTORE_CONTEXT() macro. */
273 /* When the task starts, it will expect to find the function parameter in R12. */
275 *pxTopOfStack-- = ( StackType_t ) 0x08080808; /* R8 */
276 *pxTopOfStack-- = ( StackType_t ) 0x09090909; /* R9 */
277 *pxTopOfStack-- = ( StackType_t ) 0x0A0A0A0A; /* R10 */
278 *pxTopOfStack-- = ( StackType_t ) 0x0B0B0B0B; /* R11 */
279 *pxTopOfStack-- = ( StackType_t ) pvParameters; /* R12 */
280 *pxTopOfStack-- = ( StackType_t ) 0xDEADBEEF; /* R14/LR */
281 *pxTopOfStack-- = ( StackType_t ) pxCode + portINSTRUCTION_SIZE; /* R15/PC */
282 *pxTopOfStack-- = ( StackType_t ) portINITIAL_SR; /* SR */
283 *pxTopOfStack-- = ( StackType_t ) 0xFF0000FF; /* R0 */
284 *pxTopOfStack-- = ( StackType_t ) 0x01010101; /* R1 */
285 *pxTopOfStack-- = ( StackType_t ) 0x02020202; /* R2 */
286 *pxTopOfStack-- = ( StackType_t ) 0x03030303; /* R3 */
287 *pxTopOfStack-- = ( StackType_t ) 0x04040404; /* R4 */
288 *pxTopOfStack-- = ( StackType_t ) 0x05050505; /* R5 */
289 *pxTopOfStack-- = ( StackType_t ) 0x06060606; /* R6 */
290 *pxTopOfStack-- = ( StackType_t ) 0x07070707; /* R7 */
291 *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_NESTING; /* ulCriticalNesting */
295 /*-----------------------------------------------------------*/
297 BaseType_t xPortStartScheduler( void )
299 /* Start the timer that generates the tick ISR. Interrupts are disabled
301 prvSetupTimerInterrupt();
303 /* Start the first task. */
304 portRESTORE_CONTEXT();
306 /* Should not get here! */
309 /*-----------------------------------------------------------*/
311 void vPortEndScheduler( void )
313 /* It is unlikely that the AVR32 port will require this function as there
314 * is nothing to return to. */
316 /*-----------------------------------------------------------*/
318 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)
319 * clock cycles from now. */
320 #if ( configTICK_USE_TC == 0 )
321 static void prvScheduleFirstTick( void )
325 lCycles = Get_system_register( AVR32_COUNT );
326 lCycles += ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
328 /* If lCycles ends up to be 0, make it 1 so that the COMPARE and exception */
329 /* generation feature does not get disabled. */
335 Set_system_register( AVR32_COMPARE, lCycles );
338 #pragma optimize = no_inline
339 static void prvScheduleNextTick( void )
341 uint32_t lCycles, lCount;
343 lCycles = Get_system_register( AVR32_COMPARE );
344 lCycles += ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
346 /* If lCycles ends up to be 0, make it 1 so that the COMPARE and exception */
347 /* generation feature does not get disabled. */
353 lCount = Get_system_register( AVR32_COUNT );
355 if( lCycles < lCount )
356 { /* We missed a tick, recover for the next. */
357 lCycles += ( configCPU_CLOCK_HZ / configTICK_RATE_HZ );
360 Set_system_register( AVR32_COMPARE, lCycles );
362 #else /* if ( configTICK_USE_TC == 0 ) */
363 #pragma optimize = no_inline
364 static void prvClearTcInt( void )
366 AVR32_TC.channel[ configTICK_TC_CHANNEL ].sr;
368 #endif /* if ( configTICK_USE_TC == 0 ) */
369 /*-----------------------------------------------------------*/
371 /* Setup the timer to generate the tick interrupts. */
372 static void prvSetupTimerInterrupt( void )
374 #if ( configTICK_USE_TC == 1 )
375 volatile avr32_tc_t * tc = &AVR32_TC;
377 /* Options for waveform generation. */
378 tc_waveform_opt_t waveform_opt =
380 .channel = configTICK_TC_CHANNEL, /* Channel selection. */
382 .bswtrg = TC_EVT_EFFECT_NOOP, /* Software trigger effect on TIOB. */
383 .beevt = TC_EVT_EFFECT_NOOP, /* External event effect on TIOB. */
384 .bcpc = TC_EVT_EFFECT_NOOP, /* RC compare effect on TIOB. */
385 .bcpb = TC_EVT_EFFECT_NOOP, /* RB compare effect on TIOB. */
387 .aswtrg = TC_EVT_EFFECT_NOOP, /* Software trigger effect on TIOA. */
388 .aeevt = TC_EVT_EFFECT_NOOP, /* External event effect on TIOA. */
389 .acpc = TC_EVT_EFFECT_NOOP, /* RC compare effect on TIOA: toggle. */
390 .acpa = TC_EVT_EFFECT_NOOP, /* RA compare effect on TIOA: toggle (other possibilities are none, set and clear). */
392 .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER, /* Waveform selection: Up mode without automatic trigger on RC compare. */
393 .enetrg = FALSE, /* External event trigger enable. */
394 .eevt = 0, /* External event selection. */
395 .eevtedg = TC_SEL_NO_EDGE, /* External event edge selection. */
396 .cpcdis = FALSE, /* Counter disable when RC compare. */
397 .cpcstop = FALSE, /* Counter clock stopped with RC compare. */
399 .burst = FALSE, /* Burst signal selection. */
400 .clki = FALSE, /* Clock inversion. */
401 .tcclks = TC_CLOCK_SOURCE_TC2 /* Internal source clock 2. */
404 tc_interrupt_t tc_interrupt =
415 #endif /* if ( configTICK_USE_TC == 1 ) */
417 /* Disable all interrupt/exception. */
418 portDISABLE_INTERRUPTS();
420 /* Register the compare interrupt handler to the interrupt controller and
421 * enable the compare interrupt. */
423 #if ( configTICK_USE_TC == 1 )
425 INTC_register_interrupt( ( __int_handler ) & vTick, configTICK_TC_IRQ, INT0 );
427 /* Initialize the timer/counter. */
428 tc_init_waveform( tc, &waveform_opt );
430 /* Set the compare triggers.
431 * Remember TC counter is 16-bits, so counting second is not possible!
432 * That's why we configure it to count ms. */
433 tc_write_rc( tc, configTICK_TC_CHANNEL, ( configPBA_CLOCK_HZ / 4 ) / configTICK_RATE_HZ );
435 tc_configure_interrupts( tc, configTICK_TC_CHANNEL, &tc_interrupt );
437 /* Start the timer/counter. */
438 tc_start( tc, configTICK_TC_CHANNEL );
440 #else /* if ( configTICK_USE_TC == 1 ) */
442 INTC_register_interrupt( ( __int_handler ) & vTick, AVR32_CORE_COMPARE_IRQ, INT0 );
443 prvScheduleFirstTick();
445 #endif /* if ( configTICK_USE_TC == 1 ) */