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