2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
29 /******************************************************************************
\r
31 * See http://www.freertos.org/RTOS-Xilinx-Zynq.html for instructions.
\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
41 * This file implements the code that is not demo specific, including the
\r
42 * hardware setup and FreeRTOS hook functions.
\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
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
64 /* Standard includes. */
\r
68 /* Scheduler include files. */
\r
69 #include "FreeRTOS.h"
\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
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
88 /* mainSELECTED_APPLICATION is used to select between three demo applications,
\r
89 * as described at the top of this file.
\r
91 * When mainSELECTED_APPLICATION is set to 0 the simple blinky example will
\r
94 * When mainSELECTED_APPLICATION is set to 1 the comprehensive test and demo
\r
95 * application will be run.
\r
97 * When mainSELECTED_APPLICATION is set to 2 the lwIP example will be run.
\r
99 #define mainSELECTED_APPLICATION 1
\r
101 /*-----------------------------------------------------------*/
\r
104 * Configure the hardware as necessary to run this demo.
\r
106 static void prvSetupHardware( void );
\r
109 * See the comments at the top of this file and above the
\r
110 * mainSELECTED_APPLICATION definition.
\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
119 #error Invalid mainSELECTED_APPLICATION setting. See the comments at the top of this file and above the mainSELECTED_APPLICATION definition.
\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
128 extern void vPortInstallFreeRTOSVectorTable( void );
\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
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
141 /*-----------------------------------------------------------*/
\r
143 /* The interrupt controller is initialised in this file, and made available to
\r
145 XScuGic xInterruptController;
\r
147 /*-----------------------------------------------------------*/
\r
151 /* See http://www.freertos.org/RTOS-Xilinx-Zynq.html for instructions. */
\r
153 /* Configure the hardware ready to run the demo. */
\r
154 prvSetupHardware();
\r
156 /* The mainSELECTED_APPLICATION setting is described at the top of this
\r
158 #if( mainSELECTED_APPLICATION == 0 )
\r
162 #elif( mainSELECTED_APPLICATION == 1 )
\r
172 /* Don't expect to reach here. */
\r
175 /*-----------------------------------------------------------*/
\r
177 static void prvSetupHardware( void )
\r
179 BaseType_t xStatus;
\r
180 XScuGic_Config *pxGICConfig;
\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
185 portDISABLE_INTERRUPTS();
\r
187 /* Obtain the configuration of the GIC. */
\r
188 pxGICConfig = XScuGic_LookupConfig( XPAR_SCUGIC_SINGLE_DEVICE_ID );
\r
190 /* Sanity check the FreeRTOSConfig.h settings are correct for the
\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
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
201 /* Initialise the LED port. */
\r
202 vParTestInitialise();
\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
210 /*-----------------------------------------------------------*/
\r
212 void vApplicationMallocFailedHook( void )
\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
222 /*-----------------------------------------------------------*/
\r
224 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
226 ( void ) pcTaskName;
\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
235 /*-----------------------------------------------------------*/
\r
237 void vApplicationIdleHook( void )
\r
239 volatile size_t xFreeHeapSpace, xMinimumEverFreeHeapSpace;
\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
248 xFreeHeapSpace = xPortGetFreeHeapSize();
\r
249 xMinimumEverFreeHeapSpace = xPortGetMinimumEverFreeHeapSize();
\r
251 /* Remove compiler warning about xFreeHeapSpace being set but never used. */
\r
252 ( void ) xFreeHeapSpace;
\r
253 ( void ) xMinimumEverFreeHeapSpace;
\r
255 /*-----------------------------------------------------------*/
\r
257 void vAssertCalled( const char * pcFile, unsigned long ulLine )
\r
259 volatile unsigned long ul = 0;
\r
264 taskENTER_CRITICAL();
\r
266 /* Set ul to a non-zero value using the debugger to step out of this
\r
273 taskEXIT_CRITICAL();
\r
275 /*-----------------------------------------------------------*/
\r
277 void vApplicationTickHook( void )
\r
279 #if( mainSELECTED_APPLICATION == 1 )
\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
285 /* Call the periodic queue overwrite from ISR demo. */
\r
286 vQueueOverwritePeriodicISRDemo();
\r
288 /* Call the periodic event group from ISR demo. */
\r
289 vPeriodicEventGroupsProcessing();
\r
291 /* Use task notifications from an interrupt. */
\r
292 xNotifyTaskFromISR();
\r
294 /* Use mutexes from interrupts. */
\r
295 vInterruptSemaphorePeriodicTest();
\r
297 /* Test flop alignment in interrupts - calling printf from an interrupt
\r
299 #if( configASSERT_DEFINED == 1 )
\r
302 UBaseType_t uxSavedInterruptStatus;
\r
304 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
306 sprintf( cBuf, "%1.3f", 1.234 );
\r
308 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
310 configASSERT( strcmp( cBuf, "1.234" ) == 0 );
\r
312 #endif /* configASSERT_DEFINED */
\r
316 /*-----------------------------------------------------------*/
\r
318 void *memcpy( void *pvDest, const void *pvSource, size_t xBytes )
\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
325 /* Extremely crude standard library implementations in lieu of having a C
\r
327 if( pvDest != pvSource )
\r
329 for( x = 0; x < xBytes; x++ )
\r
331 pcDest[ x ] = pcSource[ x ];
\r
337 /*-----------------------------------------------------------*/
\r
339 void *memset( void *pvDest, int iValue, size_t xBytes )
\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
346 /* Extremely crude standard library implementations in lieu of having a C
\r
348 for( x = 0; x < xBytes; x++ )
\r
350 pcDest[ x ] = ( unsigned char ) iValue;
\r
355 /*-----------------------------------------------------------*/
\r
357 int memcmp( const void *pvMem1, const void *pvMem2, size_t xBytes )
\r
359 const volatile unsigned char *pucMem1 = pvMem1, *pucMem2 = pvMem2;
\r
362 /* Extremely crude standard library implementations in lieu of having a C
\r
364 for( x = 0; x < xBytes; x++ )
\r
366 if( pucMem1[ x ] != pucMem2[ x ] )
\r
374 /*-----------------------------------------------------------*/
\r
376 void vInitialiseTimerForRunTimeStats( void )
\r
378 XScuWdt_Config *pxWatchDogInstance;
\r
380 const uint32_t ulMaxDivisor = 0xff, ulDivisorShift = 0x08;
\r
382 pxWatchDogInstance = XScuWdt_LookupConfig( XPAR_SCUWDT_0_DEVICE_ID );
\r
383 XScuWdt_CfgInitialize( &xWatchDogInstance, pxWatchDogInstance, pxWatchDogInstance->BaseAddr );
\r
385 ulValue = XScuWdt_GetControlReg( &xWatchDogInstance );
\r
386 ulValue |= ulMaxDivisor << ulDivisorShift;
\r
387 XScuWdt_SetControlReg( &xWatchDogInstance, ulValue );
\r
389 XScuWdt_LoadWdt( &xWatchDogInstance, UINT_MAX );
\r
390 XScuWdt_SetTimerMode( &xWatchDogInstance );
\r
391 XScuWdt_Start( &xWatchDogInstance );
\r
393 /*-----------------------------------------------------------*/
\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
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
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
410 /* Pass out the array that will be used as the Idle task's stack. */
\r
411 *ppxIdleTaskStackBuffer = uxIdleTaskStack;
\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
418 /*-----------------------------------------------------------*/
\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
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
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
435 /* Pass out the array that will be used as the Timer task's stack. */
\r
436 *ppxTimerTaskStackBuffer = uxTimerTaskStack;
\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