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