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