]> begriffs open source - freertos/blob - portable/GCC/AVR32_UC3/port.c
[AUTO][RELEASE]: Bump file header version to "10.4.4"
[freertos] / portable / GCC / AVR32_UC3 / port.c
1 /*\r
2  * FreeRTOS Kernel V10.4.4\r
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * SPDX-License-Identifier: MIT AND BSD-3-Clause\r
6  *\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
13  *\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
16  *\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
23  *\r
24  * https://www.FreeRTOS.org\r
25  * https://github.com/FreeRTOS\r
26  *\r
27  */\r
28 \r
29 /*This file has been prepared for Doxygen automatic documentation generation.*/\r
30 /*! \file *********************************************************************\r
31  *\r
32  * \brief FreeRTOS port source for AVR32 UC3.\r
33  *\r
34  * - Compiler:           GNU GCC for AVR32\r
35  * - Supported devices:  All AVR32 devices can be used.\r
36  * - AppNote:\r
37  *\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
41  *\r
42  *****************************************************************************/\r
43 \r
44 /*\r
45  * Copyright (c) 2007, Atmel Corporation All rights reserved.\r
46  *\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
49  *\r
50  * 1. Redistributions of source code must retain the above copyright notice,\r
51  * this list of conditions and the following disclaimer.\r
52  *\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
56  *\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
59  *\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
70  */\r
71 \r
72 /* Standard includes. */\r
73 #include <sys/cpu.h>\r
74 #include <sys/usart.h>\r
75 #include <malloc.h>\r
76 \r
77 /* Scheduler includes. */\r
78 #include "FreeRTOS.h"\r
79 #include "task.h"\r
80 \r
81 /* AVR32 UC3 includes. */\r
82 #include <avr32/io.h>\r
83 #include "gpio.h"\r
84 #if( configTICK_USE_TC==1 )\r
85         #include "tc.h"\r
86 #endif\r
87 \r
88 \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
92 \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
96 \r
97 #if( configTICK_USE_TC==0 )\r
98         static void prvScheduleNextTick( void );\r
99 #else\r
100         static void prvClearTcInt( void );\r
101 #endif\r
102 \r
103 /* Setup the timer to generate the tick interrupts. */\r
104 static void prvSetupTimerInterrupt( void );\r
105 \r
106 /*-----------------------------------------------------------*/\r
107 \r
108 /*\r
109  * Low-level initialization routine called during startup, before the main\r
110  * function.\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
115  */\r
116 void _init_startup(void)\r
117 {\r
118         /* Import the Exception Vector Base Address. */\r
119         extern void _evba;\r
120 \r
121         #if configHEAP_INIT\r
122                 extern void __heap_start__;\r
123                 extern void __heap_end__;\r
124                 BaseType_t *pxMem;\r
125         #endif\r
126 \r
127         /* Load the Exception Vector Base Address in the corresponding system register. */\r
128         Set_system_register( AVR32_EVBA, ( int ) &_evba );\r
129 \r
130         /* Enable exceptions. */\r
131         ENABLE_ALL_EXCEPTIONS();\r
132 \r
133         /* Initialize interrupt handling. */\r
134         INTC_init_interrupts();\r
135 \r
136         #if configHEAP_INIT\r
137 \r
138                 /* Initialize the heap used by malloc. */\r
139                 for( pxMem = &__heap_start__; pxMem < ( BaseType_t * )&__heap_end__; )\r
140                 {\r
141                         *pxMem++ = 0xA5A5A5A5;\r
142                 }\r
143 \r
144         #endif\r
145 \r
146         /* Give the used CPU clock frequency to Newlib, so it can work properly. */\r
147         set_cpu_hz( configCPU_CLOCK_HZ );\r
148 \r
149         /* Code section present if and only if the debug trace is activated. */\r
150         #if configDBG\r
151         {\r
152                 static const gpio_map_t DBG_USART_GPIO_MAP =\r
153                 {\r
154                         { configDBG_USART_RX_PIN, configDBG_USART_RX_FUNCTION },\r
155                         { configDBG_USART_TX_PIN, configDBG_USART_TX_FUNCTION }\r
156                 };\r
157 \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
163         }\r
164         #endif\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 /*\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
180  */\r
181 \r
182 /*\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
186  */\r
187 void __malloc_lock(struct _reent *ptr)\r
188 {\r
189         vTaskSuspendAll();\r
190 }\r
191 \r
192 /*\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
196  */\r
197 void __malloc_unlock(struct _reent *ptr)\r
198 {\r
199         xTaskResumeAll();\r
200 }\r
201 /*-----------------------------------------------------------*/\r
202 \r
203 /* Added as there is no such function in FreeRTOS. */\r
204 void *pvPortRealloc( void *pv, size_t xWantedSize )\r
205 {\r
206 void *pvReturn;\r
207 \r
208         vTaskSuspendAll();\r
209         {\r
210                 pvReturn = realloc( pv, xWantedSize );\r
211         }\r
212         xTaskResumeAll();\r
213 \r
214         return pvReturn;\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \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
223 {\r
224         /* Save the context of the interrupted task. */\r
225         portSAVE_CONTEXT_OS_INT();\r
226 \r
227         #if( configTICK_USE_TC==1 )\r
228                 /* Clear the interrupt flag. */\r
229                 prvClearTcInt();\r
230         #else\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
234         #endif\r
235 \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
241 \r
242         /* Restore the context of the "elected task". */\r
243         portRESTORE_CONTEXT_OS_INT();\r
244 }\r
245 /*-----------------------------------------------------------*/\r
246 \r
247 __attribute__((__naked__)) void SCALLYield( void )\r
248 {\r
249         /* Save the context of the interrupted task. */\r
250         portSAVE_CONTEXT_SCALL();\r
251         vTaskSwitchContext();\r
252         portRESTORE_CONTEXT_SCALL();\r
253 }\r
254 /*-----------------------------------------------------------*/\r
255 \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
261 {\r
262         /* Disable interrupts */\r
263         portDISABLE_INTERRUPTS();\r
264 \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
269 }\r
270 /*-----------------------------------------------------------*/\r
271 \r
272 __attribute__((__noinline__)) void vPortExitCritical( void )\r
273 {\r
274         if(ulCriticalNesting > portNO_CRITICAL_NESTING)\r
275         {\r
276                 ulCriticalNesting--;\r
277                 if( ulCriticalNesting == portNO_CRITICAL_NESTING )\r
278                 {\r
279                         /* Enable all interrupt/exception. */\r
280                         portENABLE_INTERRUPTS();\r
281                 }\r
282         }\r
283 }\r
284 /*-----------------------------------------------------------*/\r
285 \r
286 /*\r
287  * Initialise the stack of a task to look exactly as if a call to\r
288  * portSAVE_CONTEXT had been called.\r
289  *\r
290  * See header file for description.\r
291  */\r
292 StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters )\r
293 {\r
294         /* Setup the initial stack of the task.  The stack is set exactly as\r
295         expected by the portRESTORE_CONTEXT() macro. */\r
296 \r
297         /* When the task starts, it will expect to find the function parameter in R12. */\r
298         pxTopOfStack--;\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
316 \r
317         return pxTopOfStack;\r
318 }\r
319 /*-----------------------------------------------------------*/\r
320 \r
321 BaseType_t xPortStartScheduler( void )\r
322 {\r
323         /* Start the timer that generates the tick ISR.  Interrupts are disabled\r
324         here already. */\r
325         prvSetupTimerInterrupt();\r
326 \r
327         /* Start the first task. */\r
328         portRESTORE_CONTEXT();\r
329 \r
330         /* Should not get here! */\r
331         return 0;\r
332 }\r
333 /*-----------------------------------------------------------*/\r
334 \r
335 void vPortEndScheduler( void )\r
336 {\r
337         /* It is unlikely that the AVR32 port will require this function as there\r
338         is nothing to return to.  */\r
339 }\r
340 /*-----------------------------------------------------------*/\r
341 \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
346         {\r
347                 uint32_t lCycles;\r
348 \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
353                 if(0 == lCycles)\r
354                 {\r
355                         lCycles++;\r
356                 }\r
357                 Set_system_register(AVR32_COMPARE, lCycles);\r
358         }\r
359 \r
360         __attribute__((__noinline__)) static void prvScheduleNextTick(void)\r
361         {\r
362                 uint32_t lCycles, lCount;\r
363 \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
368                 if(0 == lCycles)\r
369                 {\r
370                         lCycles++;\r
371                 }\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
376                 }\r
377                 Set_system_register(AVR32_COMPARE, lCycles);\r
378         }\r
379 #else\r
380         __attribute__((__noinline__)) static void prvClearTcInt(void)\r
381         {\r
382                 AVR32_TC.channel[configTICK_TC_CHANNEL].sr;\r
383         }\r
384 #endif\r
385 /*-----------------------------------------------------------*/\r
386 \r
387 /* Setup the timer to generate the tick interrupts. */\r
388 static void prvSetupTimerInterrupt(void)\r
389 {\r
390 #if( configTICK_USE_TC==1 )\r
391 \r
392         volatile avr32_tc_t *tc = &AVR32_TC;\r
393 \r
394         // Options for waveform genration.\r
395         tc_waveform_opt_t waveform_opt =\r
396         {\r
397         .channel  = configTICK_TC_CHANNEL,             /* Channel selection. */\r
398 \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
403 \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
408 \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
415 \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
419         };\r
420 \r
421         tc_interrupt_t tc_interrupt =\r
422         {\r
423                 .etrgs=0,\r
424                 .ldrbs=0,\r
425                 .ldras=0,\r
426                 .cpcs =1,\r
427                 .cpbs =0,\r
428                 .cpas =0,\r
429                 .lovrs=0,\r
430                 .covfs=0,\r
431         };\r
432 \r
433 #endif\r
434 \r
435         /* Disable all interrupt/exception. */\r
436         portDISABLE_INTERRUPTS();\r
437 \r
438         /* Register the compare interrupt handler to the interrupt controller and\r
439         enable the compare interrupt. */\r
440 \r
441         #if( configTICK_USE_TC==1 )\r
442         {\r
443                 INTC_register_interrupt(&vTick, configTICK_TC_IRQ, INT0);\r
444 \r
445                 /* Initialize the timer/counter. */\r
446                 tc_init_waveform(tc, &waveform_opt);\r
447 \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
452 \r
453                 tc_configure_interrupts( tc, configTICK_TC_CHANNEL, &tc_interrupt );\r
454 \r
455                 /* Start the timer/counter. */\r
456                 tc_start(tc, configTICK_TC_CHANNEL);\r
457         }\r
458         #else\r
459         {\r
460                 INTC_register_interrupt(&vTick, AVR32_CORE_COMPARE_IRQ, INT0);\r
461                 prvScheduleFirstTick();\r
462         }\r
463         #endif\r
464 }\r