]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Cyclone_V_SoC_DK/main.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Demo / CORTEX_A9_Cyclone_V_SoC_DK / main.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /******************************************************************************\r
29  * This project provides two demo applications.  A simple blinky style project,\r
30  * and a more comprehensive test and demo application.  The\r
31  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
32  * select between the two.  The simply blinky demo is implemented and described\r
33  * in main_blinky.c.  The more comprehensive test and demo application is\r
34  * implemented and described in main_full.c.\r
35  *\r
36  * This file implements the code that is not demo specific, including the\r
37  * hardware setup and FreeRTOS hook functions.\r
38  *\r
39  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
40  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
41  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
42  *\r
43  */\r
44 \r
45 /* Standard includes. */\r
46 #include <stdio.h>\r
47 #include <limits.h>\r
48 \r
49 /* Scheduler include files. */\r
50 #include "FreeRTOS.h"\r
51 #include "task.h"\r
52 #include "semphr.h"\r
53 \r
54 /* Standard demo includes. */\r
55 #include "partest.h"\r
56 #include "TimerDemo.h"\r
57 #include "QueueOverwrite.h"\r
58 #include "EventGroupsDemo.h"\r
59 #include "IntSemTest.h"\r
60 \r
61 /* Altera library includes. */\r
62 #include "alt_timers.h"\r
63 #include "alt_clock_manager.h"\r
64 #include "alt_interrupt.h"\r
65 #include "alt_globaltmr.h"\r
66 #include "alt_address_space.h"\r
67 #include "mmu_support.h"\r
68 #include "cache_support.h"\r
69 \r
70 /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is used to select between two demo\r
71  * applications, as described at the top of this file.\r
72  *\r
73  * When mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1 the simple blinky example\r
74  * will be run.\r
75  *\r
76  * When mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0 the comprehensive test\r
77  * and demo application will be run.\r
78  */\r
79 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      1\r
80 \r
81 /*-----------------------------------------------------------*/\r
82 \r
83 /*\r
84  * Configure the hardware as necessary to run this demo.\r
85  */\r
86 static void prvSetupHardware( void );\r
87 \r
88 /*\r
89  * See the comments at the top of this file and above the\r
90  * mainSELECTED_APPLICATION definition.\r
91  */\r
92 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
93         extern void main_blinky( void );\r
94 #else\r
95         extern void main_full( void );\r
96 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
97 \r
98 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
99 within this file. */\r
100 void vApplicationMallocFailedHook( void );\r
101 void vApplicationIdleHook( void );\r
102 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
103 void vApplicationTickHook( void );\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /* configAPPLICATION_ALLOCATED_HEAP is set to 1 in FreeRTOSConfig.h so the\r
108 application can define the array used as the FreeRTOS heap.  This is done so the\r
109 heap can be forced into fast internal RAM - useful because the stacks used by\r
110 the tasks come from this space. */\r
111 uint8_t ucHeap[ configTOTAL_HEAP_SIZE ] __attribute__ ( ( section( ".oc_ram" ) ) );\r
112 \r
113 /* FreeRTOS uses its own interrupt handler code.  This code cannot use the array\r
114 of handlers defined by the Altera drivers because the array is declared static,\r
115 and so not accessible outside of the dirver's source file.  Instead declare an\r
116 array for use by the FreeRTOS handler.  See:\r
117 http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html. */\r
118 static INT_DISPATCH_t xISRHandlers[ ALT_INT_PROVISION_INT_COUNT ];\r
119 \r
120 /*-----------------------------------------------------------*/\r
121 \r
122 int main( void )\r
123 {\r
124         /* Configure the hardware ready to run the demo. */\r
125         prvSetupHardware();\r
126 \r
127         /* The mainSELECTED_APPLICATION setting is described at the top\r
128         of this file. */\r
129         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
130         {\r
131                 main_blinky();\r
132         }\r
133         #else\r
134         {\r
135                 main_full();\r
136         }\r
137         #endif\r
138 \r
139         /* Don't expect to reach here. */\r
140         return 0;\r
141 }\r
142 /*-----------------------------------------------------------*/\r
143 \r
144 static void prvSetupHardware( void )\r
145 {\r
146 extern uint8_t __cs3_interrupt_vector;\r
147 uint32_t ulSCTLR, ulVectorTable = ( uint32_t ) &__cs3_interrupt_vector;\r
148 const uint32_t ulVBit = 13U;\r
149 \r
150         alt_int_global_init();\r
151         alt_int_cpu_binary_point_set( 0 );\r
152 \r
153         /* Clear SCTLR.V for low vectors and map the vector table to the beginning\r
154         of the code. */\r
155         __asm( "MRC p15, 0, %0, c1, c0, 0" : "=r" ( ulSCTLR ) );\r
156         ulSCTLR &= ~( 1 << ulVBit );\r
157         __asm( "MCR p15, 0, %0, c1, c0, 0" : : "r" ( ulSCTLR ) );\r
158         __asm( "MCR p15, 0, %0, c12, c0, 0" : : "r" ( ulVectorTable ) );\r
159 \r
160         cache_init();\r
161         mmu_init();\r
162 \r
163         /* GPIO for LEDs.  ParTest is a historic name which used to stand for\r
164         parallel port test. */\r
165         vParTestInitialise();\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 void vApplicationMallocFailedHook( void )\r
170 {\r
171         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
172         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
173         internally by FreeRTOS API functions that create tasks, queues, software\r
174         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
175         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
176         taskDISABLE_INTERRUPTS();\r
177         for( ;; );\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
182 {\r
183         ( void ) pcTaskName;\r
184         ( void ) pxTask;\r
185 \r
186         /* Run time stack overflow checking is performed if\r
187         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
188         function is called if a stack overflow is detected. */\r
189         taskDISABLE_INTERRUPTS();\r
190         for( ;; );\r
191 }\r
192 /*-----------------------------------------------------------*/\r
193 \r
194 void vApplicationIdleHook( void )\r
195 {\r
196 volatile size_t xFreeHeapSpace;\r
197 \r
198         /* This is just a trivial example of an idle hook.  It is called on each\r
199         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
200         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
201         memory management section on the http://www.FreeRTOS.org web site for memory\r
202         management options.  If there is a lot of heap memory free then the\r
203         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
204         RAM. */\r
205         xFreeHeapSpace = xPortGetFreeHeapSize();\r
206 \r
207         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
208         ( void ) xFreeHeapSpace;\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 void vAssertCalled( const char * pcFile, unsigned long ulLine )\r
213 {\r
214 volatile unsigned long ul = 0;\r
215 \r
216         ( void ) pcFile;\r
217         ( void ) ulLine;\r
218 \r
219         taskENTER_CRITICAL();\r
220         {\r
221                 /* Set ul to a non-zero value using the debugger to step out of this\r
222                 function. */\r
223                 while( ul == 0 )\r
224                 {\r
225                         portNOP();\r
226                 }\r
227         }\r
228         taskEXIT_CRITICAL();\r
229 }\r
230 /*-----------------------------------------------------------*/\r
231 \r
232 void vApplicationTickHook( void )\r
233 {\r
234         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )\r
235         {\r
236                 /* The full demo includes a software timer demo/test that requires\r
237                 prodding periodically from the tick interrupt. */\r
238                 vTimerPeriodicISRTests();\r
239 \r
240                 /* Call the periodic queue overwrite from ISR demo. */\r
241                 vQueueOverwritePeriodicISRDemo();\r
242 \r
243                 /* Call the periodic event group from ISR demo. */\r
244                 vPeriodicEventGroupsProcessing();\r
245 \r
246                 /* Call the periodic test that uses mutexes form an interrupt. */\r
247                 vInterruptSemaphorePeriodicTest();\r
248         }\r
249         #endif\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 void vConfigureTickInterrupt( void )\r
254 {\r
255 alt_freq_t ulTempFrequency;\r
256 const alt_freq_t ulMicroSecondsPerSecond = 1000000UL;\r
257 void FreeRTOS_Tick_Handler( void );\r
258 \r
259         /* Interrupts are disabled when this function is called. */\r
260 \r
261         /* Initialise the general purpose timer modules. */\r
262         alt_gpt_all_tmr_init();\r
263 \r
264         /* ALT_CLK_MPU_PERIPH = mpu_periph_clk */\r
265         alt_clk_freq_get( ALT_CLK_MPU_PERIPH, &ulTempFrequency );\r
266 \r
267         /* Use the local private timer. */\r
268         alt_gpt_counter_set( ALT_GPT_CPU_PRIVATE_TMR, ulTempFrequency / configTICK_RATE_HZ );\r
269 \r
270         /* Sanity check. */\r
271         configASSERT( alt_gpt_time_microsecs_get( ALT_GPT_CPU_PRIVATE_TMR ) == ( ulMicroSecondsPerSecond / configTICK_RATE_HZ ) );\r
272 \r
273         /* Set to periodic mode. */\r
274         alt_gpt_mode_set( ALT_GPT_CPU_PRIVATE_TMR, ALT_GPT_RESTART_MODE_PERIODIC );\r
275 \r
276         /* The timer can be started here as interrupts are disabled. */\r
277         alt_gpt_tmr_start( ALT_GPT_CPU_PRIVATE_TMR );\r
278 \r
279         /* Register the standard FreeRTOS Cortex-A tick handler as the timer's\r
280         interrupt handler.  The handler clears the interrupt using the\r
281         configCLEAR_TICK_INTERRUPT() macro, which is defined in FreeRTOSConfig.h. */\r
282         vRegisterIRQHandler( ALT_INT_INTERRUPT_PPI_TIMER_PRIVATE, ( alt_int_callback_t ) FreeRTOS_Tick_Handler, NULL );\r
283 \r
284         /* This tick interrupt must run at the lowest priority. */\r
285         alt_int_dist_priority_set( ALT_INT_INTERRUPT_PPI_TIMER_PRIVATE, portLOWEST_USABLE_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
286 \r
287         /* Ensure the interrupt is forwarded to the CPU. */\r
288     alt_int_dist_enable( ALT_INT_INTERRUPT_PPI_TIMER_PRIVATE );\r
289 \r
290     /* Finally, enable the interrupt. */\r
291         alt_gpt_int_clear_pending( ALT_GPT_CPU_PRIVATE_TMR );\r
292         alt_gpt_int_enable( ALT_GPT_CPU_PRIVATE_TMR );\r
293 \r
294 }\r
295 /*-----------------------------------------------------------*/\r
296 \r
297 void vRegisterIRQHandler( uint32_t ulID, alt_int_callback_t pxHandlerFunction, void *pvContext )\r
298 {\r
299         if( ulID < ALT_INT_PROVISION_INT_COUNT )\r
300         {\r
301                 xISRHandlers[ ulID ].pxISR = pxHandlerFunction;\r
302                 xISRHandlers[ ulID ].pvContext = pvContext;\r
303         }\r
304 }\r
305 /*-----------------------------------------------------------*/\r
306 \r
307 void vApplicationIRQHandler( uint32_t ulICCIAR )\r
308 {\r
309 uint32_t ulInterruptID;\r
310 void *pvContext;\r
311 alt_int_callback_t pxISR;\r
312 \r
313         /* Re-enable interrupts. */\r
314     __asm ( "cpsie i" );\r
315 \r
316         /* The ID of the interrupt is obtained by bitwise anding the ICCIAR value\r
317         with 0x3FF. */\r
318         ulInterruptID = ulICCIAR & 0x3FFUL;\r
319 \r
320         if( ulInterruptID < ALT_INT_PROVISION_INT_COUNT )\r
321         {\r
322                 /* Call the function installed in the array of installed handler\r
323                 functions. */\r
324                 pxISR = xISRHandlers[ ulInterruptID ].pxISR;\r
325                 pvContext = xISRHandlers[ ulInterruptID ].pvContext;\r
326                 pxISR( ulICCIAR, pvContext );\r
327         }\r
328 }\r
329 \r