]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Cyclone_V_SoC_DK/main.c
617587ca65cbd29ed24ed2a8a81bdea694a5759c
[freertos] / FreeRTOS / Demo / CORTEX_A9_Cyclone_V_SoC_DK / main.c
1 /*\r
2     FreeRTOS V8.2.3 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /******************************************************************************\r
71  * This project provides two demo applications.  A simple blinky style project,\r
72  * and a more comprehensive test and demo application.  The\r
73  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
74  * select between the two.  The simply blinky demo is implemented and described\r
75  * in main_blinky.c.  The more comprehensive test and demo application is\r
76  * implemented and described in main_full.c.\r
77  *\r
78  * This file implements the code that is not demo specific, including the\r
79  * hardware setup and FreeRTOS hook functions.\r
80  *\r
81  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
82  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
83  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
84  *\r
85  */\r
86 \r
87 /* Standard includes. */\r
88 #include <stdio.h>\r
89 #include <limits.h>\r
90 \r
91 /* Scheduler include files. */\r
92 #include "FreeRTOS.h"\r
93 #include "task.h"\r
94 #include "semphr.h"\r
95 \r
96 /* Standard demo includes. */\r
97 #include "partest.h"\r
98 #include "TimerDemo.h"\r
99 #include "QueueOverwrite.h"\r
100 #include "EventGroupsDemo.h"\r
101 #include "IntSemTest.h"\r
102 \r
103 /* Altera library includes. */\r
104 #include "alt_timers.h"\r
105 #include "alt_clock_manager.h"\r
106 #include "alt_interrupt.h"\r
107 #include "alt_globaltmr.h"\r
108 #include "alt_address_space.h"\r
109 #include "mmu_support.h"\r
110 #include "cache_support.h"\r
111 \r
112 /* mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is used to select between two demo\r
113  * applications, as described at the top of this file.\r
114  *\r
115  * When mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1 the simple blinky example\r
116  * will be run.\r
117  *\r
118  * When mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0 the comprehensive test\r
119  * and demo application will be run.\r
120  */\r
121 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      1\r
122 \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 /*\r
126  * Configure the hardware as necessary to run this demo.\r
127  */\r
128 static void prvSetupHardware( void );\r
129 \r
130 /*\r
131  * See the comments at the top of this file and above the\r
132  * mainSELECTED_APPLICATION definition.\r
133  */\r
134 #if ( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
135         extern void main_blinky( void );\r
136 #else\r
137         extern void main_full( void );\r
138 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
139 \r
140 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
141 within this file. */\r
142 void vApplicationMallocFailedHook( void );\r
143 void vApplicationIdleHook( void );\r
144 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
145 void vApplicationTickHook( void );\r
146 \r
147 /*-----------------------------------------------------------*/\r
148 \r
149 /* configAPPLICATION_ALLOCATED_HEAP is set to 1 in FreeRTOSConfig.h so the\r
150 application can define the array used as the FreeRTOS heap.  This is done so the\r
151 heap can be forced into fast internal RAM - useful because the stacks used by\r
152 the tasks come from this space. */\r
153 uint8_t ucHeap[ configTOTAL_HEAP_SIZE ] __attribute__ ( ( section( ".oc_ram" ) ) );\r
154 \r
155 /* FreeRTOS uses its own interrupt handler code.  This code cannot use the array\r
156 of handlers defined by the Altera drivers because the array is declared static,\r
157 and so not accessible outside of the dirver's source file.  Instead declare an\r
158 array for use by the FreeRTOS handler.  See:\r
159 http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html. */\r
160 static INT_DISPATCH_t xISRHandlers[ ALT_INT_PROVISION_INT_COUNT ];\r
161 \r
162 /*-----------------------------------------------------------*/\r
163 \r
164 int main( void )\r
165 {\r
166         /* Configure the hardware ready to run the demo. */\r
167         prvSetupHardware();\r
168 \r
169         /* The mainSELECTED_APPLICATION setting is described at the top\r
170         of this file. */\r
171         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
172         {\r
173                 main_blinky();\r
174         }\r
175         #else\r
176         {\r
177                 main_full();\r
178         }\r
179         #endif\r
180 \r
181         /* Don't expect to reach here. */\r
182         return 0;\r
183 }\r
184 /*-----------------------------------------------------------*/\r
185 \r
186 static void prvSetupHardware( void )\r
187 {\r
188 extern uint8_t __cs3_interrupt_vector;\r
189 uint32_t ulSCTLR, ulVectorTable = ( uint32_t ) &__cs3_interrupt_vector;\r
190 const uint32_t ulVBit = 13U;\r
191 \r
192         alt_int_global_init();\r
193         alt_int_cpu_binary_point_set( 0 );\r
194 \r
195         /* Clear SCTLR.V for low vectors and map the vector table to the beginning\r
196         of the code. */\r
197         __asm( "MRC p15, 0, %0, c1, c0, 0" : "=r" ( ulSCTLR ) );\r
198         ulSCTLR &= ~( 1 << ulVBit );\r
199         __asm( "MCR p15, 0, %0, c1, c0, 0" : : "r" ( ulSCTLR ) );\r
200         __asm( "MCR p15, 0, %0, c12, c0, 0" : : "r" ( ulVectorTable ) );\r
201 \r
202         cache_init();\r
203         mmu_init();\r
204 \r
205         /* GPIO for LEDs.  ParTest is a historic name which used to stand for\r
206         parallel port test. */\r
207         vParTestInitialise();\r
208 }\r
209 /*-----------------------------------------------------------*/\r
210 \r
211 void vApplicationMallocFailedHook( void )\r
212 {\r
213         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
214         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
215         internally by FreeRTOS API functions that create tasks, queues, software\r
216         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
217         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
218         taskDISABLE_INTERRUPTS();\r
219         for( ;; );\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
224 {\r
225         ( void ) pcTaskName;\r
226         ( void ) pxTask;\r
227 \r
228         /* Run time stack overflow checking is performed if\r
229         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
230         function is called if a stack overflow is detected. */\r
231         taskDISABLE_INTERRUPTS();\r
232         for( ;; );\r
233 }\r
234 /*-----------------------------------------------------------*/\r
235 \r
236 void vApplicationIdleHook( void )\r
237 {\r
238 volatile size_t xFreeHeapSpace;\r
239 \r
240         /* This is just a trivial example of an idle hook.  It is called on each\r
241         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
242         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
243         memory management section on the http://www.FreeRTOS.org web site for memory\r
244         management options.  If there is a lot of heap memory free then the\r
245         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
246         RAM. */\r
247         xFreeHeapSpace = xPortGetFreeHeapSize();\r
248 \r
249         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
250         ( void ) xFreeHeapSpace;\r
251 }\r
252 /*-----------------------------------------------------------*/\r
253 \r
254 void vAssertCalled( const char * pcFile, unsigned long ulLine )\r
255 {\r
256 volatile unsigned long ul = 0;\r
257 \r
258         ( void ) pcFile;\r
259         ( void ) ulLine;\r
260 \r
261         taskENTER_CRITICAL();\r
262         {\r
263                 /* Set ul to a non-zero value using the debugger to step out of this\r
264                 function. */\r
265                 while( ul == 0 )\r
266                 {\r
267                         portNOP();\r
268                 }\r
269         }\r
270         taskEXIT_CRITICAL();\r
271 }\r
272 /*-----------------------------------------------------------*/\r
273 \r
274 void vApplicationTickHook( void )\r
275 {\r
276         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )\r
277         {\r
278                 /* The full demo includes a software timer demo/test that requires\r
279                 prodding periodically from the tick interrupt. */\r
280                 vTimerPeriodicISRTests();\r
281 \r
282                 /* Call the periodic queue overwrite from ISR demo. */\r
283                 vQueueOverwritePeriodicISRDemo();\r
284 \r
285                 /* Call the periodic event group from ISR demo. */\r
286                 vPeriodicEventGroupsProcessing();\r
287 \r
288                 /* Call the periodic test that uses mutexes form an interrupt. */\r
289                 vInterruptSemaphorePeriodicTest();\r
290         }\r
291         #endif\r
292 }\r
293 /*-----------------------------------------------------------*/\r
294 \r
295 void vConfigureTickInterrupt( void )\r
296 {\r
297 alt_freq_t ulTempFrequency;\r
298 const alt_freq_t ulMicroSecondsPerSecond = 1000000UL;\r
299 void FreeRTOS_Tick_Handler( void );\r
300 \r
301         /* Interrupts are disabled when this function is called. */\r
302 \r
303         /* Initialise the general purpose timer modules. */\r
304         alt_gpt_all_tmr_init();\r
305 \r
306         /* ALT_CLK_MPU_PERIPH = mpu_periph_clk */\r
307         alt_clk_freq_get( ALT_CLK_MPU_PERIPH, &ulTempFrequency );\r
308 \r
309         /* Use the local private timer. */\r
310         alt_gpt_counter_set( ALT_GPT_CPU_PRIVATE_TMR, ulTempFrequency / configTICK_RATE_HZ );\r
311 \r
312         /* Sanity check. */\r
313         configASSERT( alt_gpt_time_microsecs_get( ALT_GPT_CPU_PRIVATE_TMR ) == ( ulMicroSecondsPerSecond / configTICK_RATE_HZ ) );\r
314 \r
315         /* Set to periodic mode. */\r
316         alt_gpt_mode_set( ALT_GPT_CPU_PRIVATE_TMR, ALT_GPT_RESTART_MODE_PERIODIC );\r
317 \r
318         /* The timer can be started here as interrupts are disabled. */\r
319         alt_gpt_tmr_start( ALT_GPT_CPU_PRIVATE_TMR );\r
320 \r
321         /* Register the standard FreeRTOS Cortex-A tick handler as the timer's\r
322         interrupt handler.  The handler clears the interrupt using the\r
323         configCLEAR_TICK_INTERRUPT() macro, which is defined in FreeRTOSConfig.h. */\r
324         vRegisterIRQHandler( ALT_INT_INTERRUPT_PPI_TIMER_PRIVATE, ( alt_int_callback_t ) FreeRTOS_Tick_Handler, NULL );\r
325 \r
326         /* This tick interrupt must run at the lowest priority. */\r
327         alt_int_dist_priority_set( ALT_INT_INTERRUPT_PPI_TIMER_PRIVATE, portLOWEST_USABLE_INTERRUPT_PRIORITY << portPRIORITY_SHIFT );\r
328 \r
329         /* Ensure the interrupt is forwarded to the CPU. */\r
330     alt_int_dist_enable( ALT_INT_INTERRUPT_PPI_TIMER_PRIVATE );\r
331 \r
332     /* Finally, enable the interrupt. */\r
333         alt_gpt_int_clear_pending( ALT_GPT_CPU_PRIVATE_TMR );\r
334         alt_gpt_int_enable( ALT_GPT_CPU_PRIVATE_TMR );\r
335 \r
336 }\r
337 /*-----------------------------------------------------------*/\r
338 \r
339 void vRegisterIRQHandler( uint32_t ulID, alt_int_callback_t pxHandlerFunction, void *pvContext )\r
340 {\r
341         if( ulID < ALT_INT_PROVISION_INT_COUNT )\r
342         {\r
343                 xISRHandlers[ ulID ].pxISR = pxHandlerFunction;\r
344                 xISRHandlers[ ulID ].pvContext = pvContext;\r
345         }\r
346 }\r
347 /*-----------------------------------------------------------*/\r
348 \r
349 void vApplicationIRQHandler( uint32_t ulICCIAR )\r
350 {\r
351 uint32_t ulInterruptID;\r
352 void *pvContext;\r
353 alt_int_callback_t pxISR;\r
354 \r
355         /* Re-enable interrupts. */\r
356     __asm ( "cpsie i" );\r
357 \r
358         /* The ID of the interrupt is obtained by bitwise anding the ICCIAR value\r
359         with 0x3FF. */\r
360         ulInterruptID = ulICCIAR & 0x3FFUL;\r
361 \r
362         if( ulInterruptID < ALT_INT_PROVISION_INT_COUNT )\r
363         {\r
364                 /* Call the function installed in the array of installed handler\r
365                 functions. */\r
366                 pxISR = xISRHandlers[ ulInterruptID ].pxISR;\r
367                 pvContext = xISRHandlers[ ulInterruptID ].pvContext;\r
368                 pxISR( ulICCIAR, pvContext );\r
369         }\r
370 }\r
371 \r