]> git.sur5r.net Git - freertos/blob - FreeRTOS/Source/portable/IAR/AVR32_UC3/port.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[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 Kernel V10.3.0\r
17  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
18  *\r
19  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
20  * this software and associated documentation files (the "Software"), to deal in\r
21  * the Software without restriction, including without limitation the rights to\r
22  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
23  * the Software, and to permit persons to whom the Software is furnished to do so,\r
24  * subject to the following conditions:\r
25  *\r
26  * The above copyright notice and this permission notice shall be included in all\r
27  * copies or substantial portions of the Software.\r
28  *\r
29  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
30  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
31  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
32  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
33  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
34  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
35  *\r
36  * http://www.FreeRTOS.org\r
37  * http://aws.amazon.com/freertos\r
38  *\r
39  * 1 tab == 4 spaces!\r
40  */\r
41 \r
42 \r
43 /* Scheduler includes. */\r
44 #include "FreeRTOS.h"\r
45 #include "task.h"\r
46 \r
47 /* AVR32 UC3 includes. */\r
48 #include <avr32/io.h>\r
49 #include <intrinsics.h>\r
50 #include "gpio.h"\r
51 \r
52 #if configDBG\r
53         #include "usart.h"\r
54 #endif\r
55 \r
56 #if( configTICK_USE_TC==1 )\r
57         #include "tc.h"\r
58 #endif\r
59 \r
60 \r
61 /* Constants required to setup the task context. */\r
62 #define portINITIAL_SR            ( ( StackType_t ) 0x00400000 ) /* AVR32 : [M2:M0]=001 I1M=0 I0M=0, GM=0 */\r
63 #define portINSTRUCTION_SIZE      ( ( StackType_t ) 0 )\r
64 \r
65 /* Each task maintains its own critical nesting variable. */\r
66 #define portNO_CRITICAL_NESTING   ( ( uint32_t ) 0 )\r
67 volatile uint32_t ulCriticalNesting = 9999UL;\r
68 \r
69 #if( configTICK_USE_TC==0 )\r
70         static void prvScheduleNextTick( void );\r
71 #else\r
72         static void prvClearTcInt( void );\r
73 #endif\r
74 \r
75 /* Setup the timer to generate the tick interrupts. */\r
76 static void prvSetupTimerInterrupt( void );\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /*\r
81  * Low-level initialization routine called during startup, before the main\r
82  * function.\r
83  */\r
84 int __low_level_init(void)\r
85 {\r
86         #if configHEAP_INIT\r
87                 #pragma segment = "HEAP"\r
88                 BaseType_t *pxMem;\r
89         #endif\r
90 \r
91         /* Enable exceptions. */\r
92         ENABLE_ALL_EXCEPTIONS();\r
93 \r
94         /* Initialize interrupt handling. */\r
95         INTC_init_interrupts();\r
96 \r
97         #if configHEAP_INIT\r
98         {\r
99                 /* Initialize the heap used by malloc. */\r
100                 for( pxMem = __segment_begin( "HEAP" ); pxMem < ( BaseType_t * ) __segment_end( "HEAP" ); )\r
101                 {\r
102                         *pxMem++ = 0xA5A5A5A5;\r
103                 }\r
104         }\r
105         #endif\r
106 \r
107         /* Code section present if and only if the debug trace is activated. */\r
108         #if configDBG\r
109         {\r
110                 static const gpio_map_t DBG_USART_GPIO_MAP =\r
111                 {\r
112                         { configDBG_USART_RX_PIN, configDBG_USART_RX_FUNCTION },\r
113                         { configDBG_USART_TX_PIN, configDBG_USART_TX_FUNCTION }\r
114                 };\r
115 \r
116                 static const usart_options_t DBG_USART_OPTIONS =\r
117                 {\r
118                         .baudrate = configDBG_USART_BAUDRATE,\r
119                         .charlength = 8,\r
120                         .paritytype = USART_NO_PARITY,\r
121                         .stopbits = USART_1_STOPBIT,\r
122                         .channelmode = USART_NORMAL_CHMODE\r
123                 };\r
124 \r
125                 /* Initialize the USART used for the debug trace with the configured parameters. */\r
126                 extern volatile avr32_usart_t *volatile stdio_usart_base;\r
127                 stdio_usart_base = configDBG_USART;\r
128                 gpio_enable_module( DBG_USART_GPIO_MAP,\r
129                                     sizeof( DBG_USART_GPIO_MAP ) / sizeof( DBG_USART_GPIO_MAP[0] ) );\r
130                 usart_init_rs232(configDBG_USART, &DBG_USART_OPTIONS, configCPU_CLOCK_HZ);\r
131         }\r
132         #endif\r
133 \r
134         /* Request initialization of data segments. */\r
135         return 1;\r
136 }\r
137 /*-----------------------------------------------------------*/\r
138 \r
139 /* Added as there is no such function in FreeRTOS. */\r
140 void *pvPortRealloc( void *pv, size_t xWantedSize )\r
141 {\r
142 void *pvReturn;\r
143 \r
144         vTaskSuspendAll();\r
145         {\r
146                 pvReturn = realloc( pv, xWantedSize );\r
147         }\r
148         xTaskResumeAll();\r
149 \r
150         return pvReturn;\r
151 }\r
152 /*-----------------------------------------------------------*/\r
153 \r
154 /* The cooperative scheduler requires a normal IRQ service routine to\r
155 simply increment the system tick. */\r
156 /* The preemptive scheduler is defined as "naked" as the full context is saved\r
157 on entry as part of the context switch. */\r
158 #pragma shadow_registers = full   // Naked.\r
159 static void vTick( void )\r
160 {\r
161         /* Save the context of the interrupted task. */\r
162         portSAVE_CONTEXT_OS_INT();\r
163 \r
164         #if( configTICK_USE_TC==1 )\r
165                 /* Clear the interrupt flag. */\r
166                 prvClearTcInt();\r
167         #else\r
168                 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)\r
169                 clock cycles from now. */\r
170                 prvScheduleNextTick();\r
171         #endif\r
172 \r
173         /* Because FreeRTOS is not supposed to run with nested interrupts, put all OS\r
174         calls in a critical section . */\r
175         portENTER_CRITICAL();\r
176                 xTaskIncrementTick();\r
177         portEXIT_CRITICAL();\r
178 \r
179         /* Restore the context of the "elected task". */\r
180         portRESTORE_CONTEXT_OS_INT();\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 #pragma shadow_registers = full   // Naked.\r
185 void SCALLYield( void )\r
186 {\r
187         /* Save the context of the interrupted task. */\r
188         portSAVE_CONTEXT_SCALL();\r
189         vTaskSwitchContext();\r
190         portRESTORE_CONTEXT_SCALL();\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 /* The code generated by the GCC compiler uses the stack in different ways at\r
195 different optimisation levels.  The interrupt flags can therefore not always\r
196 be saved to the stack.  Instead the critical section nesting level is stored\r
197 in a variable, which is then saved as part of the stack context. */\r
198 #pragma optimize = no_inline\r
199 void vPortEnterCritical( void )\r
200 {\r
201         /* Disable interrupts */\r
202         portDISABLE_INTERRUPTS();\r
203 \r
204         /* Now interrupts are disabled ulCriticalNesting can be accessed\r
205          directly.  Increment ulCriticalNesting to keep a count of how many times\r
206          portENTER_CRITICAL() has been called. */\r
207         ulCriticalNesting++;\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 #pragma optimize = no_inline\r
212 void vPortExitCritical( void )\r
213 {\r
214         if(ulCriticalNesting > portNO_CRITICAL_NESTING)\r
215         {\r
216                 ulCriticalNesting--;\r
217                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
218                 {\r
219                         /* Enable all interrupt/exception. */\r
220                         portENABLE_INTERRUPTS();\r
221                 }\r
222         }\r
223 }\r
224 /*-----------------------------------------------------------*/\r
225 \r
226 /*\r
227  * Initialise the stack of a task to look exactly as if a call to\r
228  * portSAVE_CONTEXT had been called.\r
229  *\r
230  * See header file for description.\r
231  */\r
232 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
233 {\r
234         /* Setup the initial stack of the task.  The stack is set exactly as\r
235         expected by the portRESTORE_CONTEXT() macro. */\r
236 \r
237         /* When the task starts, it will expect to find the function parameter in R12. */\r
238         pxTopOfStack--;\r
239         *pxTopOfStack-- = ( StackType_t ) 0x08080808;                                   /* R8 */\r
240         *pxTopOfStack-- = ( StackType_t ) 0x09090909;                                   /* R9 */\r
241         *pxTopOfStack-- = ( StackType_t ) 0x0A0A0A0A;                                   /* R10 */\r
242         *pxTopOfStack-- = ( StackType_t ) 0x0B0B0B0B;                                   /* R11 */\r
243         *pxTopOfStack-- = ( StackType_t ) pvParameters;                                 /* R12 */\r
244         *pxTopOfStack-- = ( StackType_t ) 0xDEADBEEF;                                   /* R14/LR */\r
245         *pxTopOfStack-- = ( StackType_t ) pxCode + portINSTRUCTION_SIZE; /* R15/PC */\r
246         *pxTopOfStack-- = ( StackType_t ) portINITIAL_SR;                               /* SR */\r
247         *pxTopOfStack-- = ( StackType_t ) 0xFF0000FF;                                   /* R0 */\r
248         *pxTopOfStack-- = ( StackType_t ) 0x01010101;                                   /* R1 */\r
249         *pxTopOfStack-- = ( StackType_t ) 0x02020202;                                   /* R2 */\r
250         *pxTopOfStack-- = ( StackType_t ) 0x03030303;                                   /* R3 */\r
251         *pxTopOfStack-- = ( StackType_t ) 0x04040404;                                   /* R4 */\r
252         *pxTopOfStack-- = ( StackType_t ) 0x05050505;                                   /* R5 */\r
253         *pxTopOfStack-- = ( StackType_t ) 0x06060606;                                   /* R6 */\r
254         *pxTopOfStack-- = ( StackType_t ) 0x07070707;                                   /* R7 */\r
255         *pxTopOfStack = ( StackType_t ) portNO_CRITICAL_NESTING;                        /* ulCriticalNesting */\r
256 \r
257         return pxTopOfStack;\r
258 }\r
259 /*-----------------------------------------------------------*/\r
260 \r
261 BaseType_t xPortStartScheduler( void )\r
262 {\r
263         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
264         here already. */\r
265         prvSetupTimerInterrupt();\r
266 \r
267         /* Start the first task. */\r
268         portRESTORE_CONTEXT();\r
269 \r
270         /* Should not get here! */\r
271         return 0;\r
272 }\r
273 /*-----------------------------------------------------------*/\r
274 \r
275 void vPortEndScheduler( void )\r
276 {\r
277         /* It is unlikely that the AVR32 port will require this function as there\r
278         is nothing to return to.  */\r
279 }\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 /* Schedule the COUNT&COMPARE match interrupt in (configCPU_CLOCK_HZ/configTICK_RATE_HZ)\r
283 clock cycles from now. */\r
284 #if( configTICK_USE_TC==0 )\r
285         static void prvScheduleFirstTick(void)\r
286         {\r
287                 uint32_t lCycles;\r
288 \r
289                 lCycles = Get_system_register(AVR32_COUNT);\r
290                 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
291                 // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception\r
292                 // generation feature does not get disabled.\r
293                 if(0 == lCycles)\r
294                 {\r
295                         lCycles++;\r
296                 }\r
297                 Set_system_register(AVR32_COMPARE, lCycles);\r
298         }\r
299         \r
300         #pragma optimize = no_inline\r
301         static void prvScheduleNextTick(void)\r
302         {\r
303                 uint32_t lCycles, lCount;\r
304 \r
305                 lCycles = Get_system_register(AVR32_COMPARE);\r
306                 lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
307                 // If lCycles ends up to be 0, make it 1 so that the COMPARE and exception\r
308                 // generation feature does not get disabled.\r
309                 if(0 == lCycles)\r
310                 {\r
311                         lCycles++;\r
312                 }\r
313                 lCount = Get_system_register(AVR32_COUNT);\r
314                 if( lCycles < lCount )\r
315                 {               // We missed a tick, recover for the next.\r
316                         lCycles += (configCPU_CLOCK_HZ/configTICK_RATE_HZ);\r
317                 }\r
318                 Set_system_register(AVR32_COMPARE, lCycles);\r
319         }\r
320 #else\r
321         #pragma optimize = no_inline\r
322         static void prvClearTcInt(void)\r
323         {\r
324                 AVR32_TC.channel[configTICK_TC_CHANNEL].sr;\r
325         }\r
326 #endif\r
327 /*-----------------------------------------------------------*/\r
328 \r
329 /* Setup the timer to generate the tick interrupts. */\r
330 static void prvSetupTimerInterrupt(void)\r
331 {\r
332         #if( configTICK_USE_TC==1 )\r
333 \r
334                 volatile avr32_tc_t *tc = &AVR32_TC;\r
335 \r
336                 // Options for waveform genration.\r
337                 tc_waveform_opt_t waveform_opt =\r
338                 {\r
339                 .channel  = configTICK_TC_CHANNEL,             /* Channel selection. */\r
340 \r
341                 .bswtrg   = TC_EVT_EFFECT_NOOP,                /* Software trigger effect on TIOB. */\r
342                 .beevt    = TC_EVT_EFFECT_NOOP,                /* External event effect on TIOB. */\r
343                 .bcpc     = TC_EVT_EFFECT_NOOP,                /* RC compare effect on TIOB. */\r
344                 .bcpb     = TC_EVT_EFFECT_NOOP,                /* RB compare effect on TIOB. */\r
345 \r
346                 .aswtrg   = TC_EVT_EFFECT_NOOP,                /* Software trigger effect on TIOA. */\r
347                 .aeevt    = TC_EVT_EFFECT_NOOP,                /* External event effect on TIOA. */\r
348                 .acpc     = TC_EVT_EFFECT_NOOP,                /* RC compare effect on TIOA: toggle. */\r
349                 .acpa     = TC_EVT_EFFECT_NOOP,                /* RA compare effect on TIOA: toggle (other possibilities are none, set and clear). */\r
350 \r
351                 .wavsel   = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER,/* Waveform selection: Up mode without automatic trigger on RC compare. */\r
352                 .enetrg   = FALSE,                             /* External event trigger enable. */\r
353                 .eevt     = 0,                                 /* External event selection. */\r
354                 .eevtedg  = TC_SEL_NO_EDGE,                    /* External event edge selection. */\r
355                 .cpcdis   = FALSE,                             /* Counter disable when RC compare. */\r
356                 .cpcstop  = FALSE,                             /* Counter clock stopped with RC compare. */\r
357 \r
358                 .burst    = FALSE,                             /* Burst signal selection. */\r
359                 .clki     = FALSE,                             /* Clock inversion. */\r
360                 .tcclks   = TC_CLOCK_SOURCE_TC2                /* Internal source clock 2. */\r
361                 };\r
362 \r
363                 tc_interrupt_t tc_interrupt =\r
364                 {\r
365                         .etrgs=0,\r
366                         .ldrbs=0,\r
367                         .ldras=0,\r
368                         .cpcs =1,\r
369                         .cpbs =0,\r
370                         .cpas =0,\r
371                         .lovrs=0,\r
372                         .covfs=0,\r
373                 };\r
374 \r
375         #endif\r
376 \r
377         /* Disable all interrupt/exception. */\r
378         portDISABLE_INTERRUPTS();\r
379 \r
380         /* Register the compare interrupt handler to the interrupt controller and\r
381         enable the compare interrupt. */\r
382 \r
383         #if( configTICK_USE_TC==1 )\r
384         {\r
385                 INTC_register_interrupt((__int_handler)&vTick, configTICK_TC_IRQ, INT0);\r
386 \r
387                 /* Initialize the timer/counter. */\r
388                 tc_init_waveform(tc, &waveform_opt);\r
389 \r
390                 /* Set the compare triggers.\r
391                 Remember TC counter is 16-bits, so counting second is not possible!\r
392                 That's why we configure it to count ms. */\r
393                 tc_write_rc( tc, configTICK_TC_CHANNEL, ( configPBA_CLOCK_HZ / 4) / configTICK_RATE_HZ );\r
394 \r
395                 tc_configure_interrupts( tc, configTICK_TC_CHANNEL, &tc_interrupt );\r
396 \r
397                 /* Start the timer/counter. */\r
398                 tc_start(tc, configTICK_TC_CHANNEL);\r
399         }\r
400         #else\r
401         {\r
402                 INTC_register_interrupt((__int_handler)&vTick, AVR32_CORE_COMPARE_IRQ, INT0);\r
403                 prvScheduleFirstTick();\r
404         }\r
405         #endif\r
406 }\r