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