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