]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_Zynq_ZC702/RTOSDemo/src/main.c
Final check in before tagging V9.0.0.
[freertos] / FreeRTOS / Demo / CORTEX_A9_Zynq_ZC702 / RTOSDemo / src / main.c
1 /*\r
2     FreeRTOS V9.0.0 - Copyright (C) 2016 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  *\r
72  * See http://www.freertos.org/RTOS-Xilinx-Zynq.html for instructions.\r
73  *\r
74  * This project provides three demo applications.  A simple blinky style\r
75  * project, a more comprehensive test and demo application, and an lwIP example.\r
76  * The mainSELECTED_APPLICATION setting (defined in this file) is used to\r
77  * select between the three.  The simply blinky demo is implemented and\r
78  * described in main_blinky.c.  The more comprehensive test and demo application\r
79  * is implemented and described in main_full.c.  The lwIP example is implemented\r
80  * and described in main_lwIP.c.\r
81  *\r
82  * This file implements the code that is not demo specific, including the\r
83  * hardware setup and FreeRTOS hook functions.\r
84  *\r
85  * !!! IMPORTANT NOTE !!!\r
86  * The GCC libraries that ship with the Xilinx SDK make use of the floating\r
87  * point registers.  To avoid this causing corruption it is necessary to avoid\r
88  * their use unless a task has been given a floating point context.  See\r
89  * http://www.freertos.org/Using-FreeRTOS-on-Cortex-A-Embedded-Processors.html\r
90  * for information on how to give a task a floating point context, and how to\r
91  * handle floating point operations in interrupts.  As this demo does not give\r
92  * all tasks a floating point context main.c contains very basic C\r
93  * implementations of the standard C library functions memset(), memcpy() and\r
94  * memcmp(), which are are used by FreeRTOS itself.  Defining these functions in\r
95  * the project prevents the linker pulling them in from the library.  Any other\r
96  * standard C library functions that are used by the application must likewise\r
97  * be defined in C.\r
98  *\r
99  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
100  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
101  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
102  *\r
103  */\r
104 \r
105 /* Standard includes. */\r
106 #include <stdio.h>\r
107 #include <limits.h>\r
108 \r
109 /* Scheduler include files. */\r
110 #include "FreeRTOS.h"\r
111 #include "task.h"\r
112 #include "semphr.h"\r
113 \r
114 /* Standard demo includes. */\r
115 #include "partest.h"\r
116 #include "TimerDemo.h"\r
117 #include "QueueOverwrite.h"\r
118 #include "EventGroupsDemo.h"\r
119 #include "TaskNotify.h"\r
120 #include "IntSemTest.h"\r
121 \r
122 /* Xilinx includes. */\r
123 #include "platform.h"\r
124 #include "xparameters.h"\r
125 #include "xscutimer.h"\r
126 #include "xscugic.h"\r
127 #include "xil_exception.h"\r
128 \r
129 /* mainSELECTED_APPLICATION is used to select between three demo applications,\r
130  * as described at the top of this file.\r
131  *\r
132  * When mainSELECTED_APPLICATION is set to 0 the simple blinky example will\r
133  * be run.\r
134  *\r
135  * When mainSELECTED_APPLICATION is set to 1 the comprehensive test and demo\r
136  * application will be run.\r
137  *\r
138  * When mainSELECTED_APPLICATION is set to 2 the lwIP example will be run.\r
139  */\r
140 #define mainSELECTED_APPLICATION        1\r
141 \r
142 /*-----------------------------------------------------------*/\r
143 \r
144 /*\r
145  * Configure the hardware as necessary to run this demo.\r
146  */\r
147 static void prvSetupHardware( void );\r
148 \r
149 /*\r
150  * See the comments at the top of this file and above the\r
151  * mainSELECTED_APPLICATION definition.\r
152  */\r
153 #if ( mainSELECTED_APPLICATION == 0 )\r
154         extern void main_blinky( void );\r
155 #elif ( mainSELECTED_APPLICATION == 1 )\r
156         extern void main_full( void );\r
157 #elif ( mainSELECTED_APPLICATION == 2 )\r
158         extern void main_lwIP( void );\r
159 #else\r
160         #error Invalid mainSELECTED_APPLICATION setting.  See the comments at the top of this file and above the mainSELECTED_APPLICATION definition.\r
161 #endif\r
162 \r
163 /*\r
164  * The Xilinx projects use a BSP that do not allow the start up code to be\r
165  * altered easily.  Therefore the vector table used by FreeRTOS is defined in\r
166  * FreeRTOS_asm_vectors.S, which is part of this project.  Switch to use the\r
167  * FreeRTOS vector table.\r
168  */\r
169 extern void vPortInstallFreeRTOSVectorTable( void );\r
170 \r
171 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
172 within this file. */\r
173 void vApplicationMallocFailedHook( void );\r
174 void vApplicationIdleHook( void );\r
175 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
176 void vApplicationTickHook( void );\r
177 \r
178 /* The private watchdog is used as the timer that generates run time\r
179 stats.  This frequency means it will overflow quite quickly. */\r
180 XScuWdt xWatchDogInstance;\r
181 \r
182 /*-----------------------------------------------------------*/\r
183 \r
184 /* The interrupt controller is initialised in this file, and made available to\r
185 other modules. */\r
186 XScuGic xInterruptController;\r
187 \r
188 /*-----------------------------------------------------------*/\r
189 \r
190 int main( void )\r
191 {\r
192         /* See http://www.freertos.org/RTOS-Xilinx-Zynq.html for instructions. */\r
193 \r
194         /* Configure the hardware ready to run the demo. */\r
195         prvSetupHardware();\r
196 \r
197         /* The mainSELECTED_APPLICATION setting is described at the top of this\r
198         file. */\r
199         #if( mainSELECTED_APPLICATION == 0 )\r
200         {\r
201                 main_blinky();\r
202         }\r
203         #elif( mainSELECTED_APPLICATION == 1 )\r
204         {\r
205                 main_full();\r
206         }\r
207         #else\r
208         {\r
209                 main_lwIP();\r
210         }\r
211         #endif\r
212 \r
213         /* Don't expect to reach here. */\r
214         return 0;\r
215 }\r
216 /*-----------------------------------------------------------*/\r
217 \r
218 static void prvSetupHardware( void )\r
219 {\r
220 BaseType_t xStatus;\r
221 XScuGic_Config *pxGICConfig;\r
222 \r
223         /* Ensure no interrupts execute while the scheduler is in an inconsistent\r
224         state.  Interrupts are automatically enabled when the scheduler is\r
225         started. */\r
226         portDISABLE_INTERRUPTS();\r
227 \r
228         /* Obtain the configuration of the GIC. */\r
229         pxGICConfig = XScuGic_LookupConfig( XPAR_SCUGIC_SINGLE_DEVICE_ID );\r
230 \r
231         /* Sanity check the FreeRTOSConfig.h settings are correct for the\r
232         hardware. */\r
233         configASSERT( pxGICConfig );\r
234         configASSERT( pxGICConfig->CpuBaseAddress == ( configINTERRUPT_CONTROLLER_BASE_ADDRESS + configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET ) );\r
235         configASSERT( pxGICConfig->DistBaseAddress == configINTERRUPT_CONTROLLER_BASE_ADDRESS );\r
236 \r
237         /* Install a default handler for each GIC interrupt. */\r
238         xStatus = XScuGic_CfgInitialize( &xInterruptController, pxGICConfig, pxGICConfig->CpuBaseAddress );\r
239         configASSERT( xStatus == XST_SUCCESS );\r
240         ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */\r
241 \r
242         /* Initialise the LED port. */\r
243         vParTestInitialise();\r
244 \r
245         /* The Xilinx projects use a BSP that do not allow the start up code to be\r
246         altered easily.  Therefore the vector table used by FreeRTOS is defined in\r
247         FreeRTOS_asm_vectors.S, which is part of this project.  Switch to use the\r
248         FreeRTOS vector table. */\r
249         vPortInstallFreeRTOSVectorTable();\r
250 }\r
251 /*-----------------------------------------------------------*/\r
252 \r
253 void vApplicationMallocFailedHook( void )\r
254 {\r
255         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
256         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
257         internally by FreeRTOS API functions that create tasks, queues, software\r
258         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
259         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
260         taskDISABLE_INTERRUPTS();\r
261         for( ;; );\r
262 }\r
263 /*-----------------------------------------------------------*/\r
264 \r
265 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
266 {\r
267         ( void ) pcTaskName;\r
268         ( void ) pxTask;\r
269 \r
270         /* Run time stack overflow checking is performed if\r
271         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
272         function is called if a stack overflow is detected. */\r
273         taskDISABLE_INTERRUPTS();\r
274         for( ;; );\r
275 }\r
276 /*-----------------------------------------------------------*/\r
277 \r
278 void vApplicationIdleHook( void )\r
279 {\r
280 volatile size_t xFreeHeapSpace, xMinimumEverFreeHeapSpace;\r
281 \r
282         /* This is just a trivial example of an idle hook.  It is called on each\r
283         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
284         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
285         memory management section on the http://www.FreeRTOS.org web site for memory\r
286         management options.  If there is a lot of heap memory free then the\r
287         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
288         RAM. */\r
289         xFreeHeapSpace = xPortGetFreeHeapSize();\r
290         xMinimumEverFreeHeapSpace = xPortGetMinimumEverFreeHeapSize();\r
291 \r
292         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
293         ( void ) xFreeHeapSpace;\r
294         ( void ) xMinimumEverFreeHeapSpace;\r
295 }\r
296 /*-----------------------------------------------------------*/\r
297 \r
298 void vAssertCalled( const char * pcFile, unsigned long ulLine )\r
299 {\r
300 volatile unsigned long ul = 0;\r
301 \r
302         ( void ) pcFile;\r
303         ( void ) ulLine;\r
304 \r
305         taskENTER_CRITICAL();\r
306         {\r
307                 /* Set ul to a non-zero value using the debugger to step out of this\r
308                 function. */\r
309                 while( ul == 0 )\r
310                 {\r
311                         portNOP();\r
312                 }\r
313         }\r
314         taskEXIT_CRITICAL();\r
315 }\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 void vApplicationTickHook( void )\r
319 {\r
320         #if( mainSELECTED_APPLICATION == 1 )\r
321         {\r
322                 /* The full demo includes a software timer demo/test that requires\r
323                 prodding periodically from the tick interrupt. */\r
324                 vTimerPeriodicISRTests();\r
325 \r
326                 /* Call the periodic queue overwrite from ISR demo. */\r
327                 vQueueOverwritePeriodicISRDemo();\r
328 \r
329                 /* Call the periodic event group from ISR demo. */\r
330                 vPeriodicEventGroupsProcessing();\r
331 \r
332                 /* Use task notifications from an interrupt. */\r
333                 xNotifyTaskFromISR();\r
334 \r
335                 /* Use mutexes from interrupts. */\r
336                 vInterruptSemaphorePeriodicTest();\r
337         }\r
338         #endif\r
339 }\r
340 /*-----------------------------------------------------------*/\r
341 \r
342 void *memcpy( void *pvDest, const void *pvSource, size_t xBytes )\r
343 {\r
344 /* The compiler used during development seems to err unless these volatiles are\r
345 included at -O3 optimisation.  */\r
346 volatile unsigned char *pcDest = ( volatile unsigned char * ) pvDest, *pcSource = ( volatile unsigned char * ) pvSource;\r
347 size_t x;\r
348 \r
349         /* Extremely crude standard library implementations in lieu of having a C\r
350         library. */\r
351         if( pvDest != pvSource )\r
352         {\r
353                 for( x = 0; x < xBytes; x++ )\r
354                 {\r
355                         pcDest[ x ] = pcSource[ x ];\r
356                 }\r
357         }\r
358 \r
359         return pvDest;\r
360 }\r
361 /*-----------------------------------------------------------*/\r
362 \r
363 void *memset( void *pvDest, int iValue, size_t xBytes )\r
364 {\r
365 /* The compiler used during development seems to err unless these volatiles are\r
366 included at -O3 optimisation.  */\r
367 volatile unsigned char * volatile pcDest = ( volatile unsigned char * volatile ) pvDest;\r
368 volatile size_t x;\r
369 \r
370         /* Extremely crude standard library implementations in lieu of having a C\r
371         library. */\r
372         for( x = 0; x < xBytes; x++ )\r
373         {\r
374                 pcDest[ x ] = ( unsigned char ) iValue;\r
375         }\r
376 \r
377         return pvDest;\r
378 }\r
379 /*-----------------------------------------------------------*/\r
380 \r
381 int memcmp( const void *pvMem1, const void *pvMem2, size_t xBytes )\r
382 {\r
383 const volatile unsigned char *pucMem1 = pvMem1, *pucMem2 = pvMem2;\r
384 volatile size_t x;\r
385 \r
386         /* Extremely crude standard library implementations in lieu of having a C\r
387         library. */\r
388         for( x = 0; x < xBytes; x++ )\r
389         {\r
390                 if( pucMem1[ x ] != pucMem2[ x ] )\r
391                 {\r
392                         break;\r
393                 }\r
394         }\r
395 \r
396         return xBytes - x;\r
397 }\r
398 /*-----------------------------------------------------------*/\r
399 \r
400 void vInitialiseTimerForRunTimeStats( void )\r
401 {\r
402 XScuWdt_Config *pxWatchDogInstance;\r
403 uint32_t ulValue;\r
404 const uint32_t ulMaxDivisor = 0xff, ulDivisorShift = 0x08;\r
405 \r
406          pxWatchDogInstance = XScuWdt_LookupConfig( XPAR_SCUWDT_0_DEVICE_ID );\r
407          XScuWdt_CfgInitialize( &xWatchDogInstance, pxWatchDogInstance, pxWatchDogInstance->BaseAddr );\r
408 \r
409          ulValue = XScuWdt_GetControlReg( &xWatchDogInstance );\r
410          ulValue |= ulMaxDivisor << ulDivisorShift;\r
411          XScuWdt_SetControlReg( &xWatchDogInstance, ulValue );\r
412 \r
413          XScuWdt_LoadWdt( &xWatchDogInstance, UINT_MAX );\r
414          XScuWdt_SetTimerMode( &xWatchDogInstance );\r
415          XScuWdt_Start( &xWatchDogInstance );\r
416 }\r
417 /*-----------------------------------------------------------*/\r
418 \r
419 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an\r
420 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is\r
421 used by the Idle task. */\r
422 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )\r
423 {\r
424 /* If the buffers to be provided to the Idle task are declared inside this\r
425 function then they must be declared static - otherwise they will be allocated on\r
426 the stack and so not exists after this function exits. */\r
427 static StaticTask_t xIdleTaskTCB;\r
428 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];\r
429 \r
430         /* Pass out a pointer to the StaticTask_t structure in which the Idle task's\r
431         state will be stored. */\r
432         *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;\r
433 \r
434         /* Pass out the array that will be used as the Idle task's stack. */\r
435         *ppxIdleTaskStackBuffer = uxIdleTaskStack;\r
436 \r
437         /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.\r
438         Note that, as the array is necessarily of type StackType_t,\r
439         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
440         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;\r
441 }\r
442 /*-----------------------------------------------------------*/\r
443 \r
444 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the\r
445 application must provide an implementation of vApplicationGetTimerTaskMemory()\r
446 to provide the memory that is used by the Timer service task. */\r
447 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )\r
448 {\r
449 /* If the buffers to be provided to the Timer task are declared inside this\r
450 function then they must be declared static - otherwise they will be allocated on\r
451 the stack and so not exists after this function exits. */\r
452 static StaticTask_t xTimerTaskTCB;\r
453 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];\r
454 \r
455         /* Pass out a pointer to the StaticTask_t structure in which the Timer\r
456         task's state will be stored. */\r
457         *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;\r
458 \r
459         /* Pass out the array that will be used as the Timer task's stack. */\r
460         *ppxTimerTaskStackBuffer = uxTimerTaskStack;\r
461 \r
462         /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.\r
463         Note that, as the array is necessarily of type StackType_t,\r
464         configMINIMAL_STACK_SIZE is specified in words, not bytes. */\r
465         *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;\r
466 }\r
467 \r
468 \r