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