2 * FreeRTOS Kernel V10.2.1
\r
3 * Copyright (C) 2019 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.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
28 /******************************************************************************
\r
30 * See http://www.freertos.org/RTOS-Xilinx-Zynq.html for instructions.
\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
40 * This file implements the code that is not demo specific, including the
\r
41 * hardware setup and FreeRTOS hook functions.
\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
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
63 /* Standard includes. */
\r
67 /* Scheduler include files. */
\r
68 #include "FreeRTOS.h"
\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
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 #include "xuartps_hw.h"
\r
91 /* mainSELECTED_APPLICATION is used to select between three demo applications,
\r
92 * as described at the top of this file.
\r
94 * When mainSELECTED_APPLICATION is set to 0 the simple blinky example will
\r
97 * When mainSELECTED_APPLICATION is set to 1 the comprehensive test and demo
\r
98 * application will be run.
\r
100 * When mainSELECTED_APPLICATION is set to 2 the lwIP example will be run.
\r
102 #define mainSELECTED_APPLICATION 1
\r
104 /*-----------------------------------------------------------*/
\r
107 * Configure the hardware as necessary to run this demo.
\r
109 static void prvSetupHardware( void );
\r
112 * See the comments at the top of this file and above the
\r
113 * mainSELECTED_APPLICATION definition.
\r
115 #if ( mainSELECTED_APPLICATION == 0 )
\r
116 extern void main_blinky( void );
\r
117 #elif ( mainSELECTED_APPLICATION == 1 )
\r
118 extern void main_full( void );
\r
119 #elif ( mainSELECTED_APPLICATION == 2 )
\r
120 extern void main_lwIP( void );
\r
122 #error Invalid mainSELECTED_APPLICATION setting. See the comments at the top of this file and above the mainSELECTED_APPLICATION definition.
\r
126 * The Xilinx projects use a BSP that do not allow the start up code to be
\r
127 * altered easily. Therefore the vector table used by FreeRTOS is defined in
\r
128 * FreeRTOS_asm_vectors.S, which is part of this project. Switch to use the
\r
129 * FreeRTOS vector table.
\r
131 extern void vPortInstallFreeRTOSVectorTable( void );
\r
133 /* Prototypes for the standard FreeRTOS callback/hook functions implemented
\r
134 within this file. */
\r
135 void vApplicationMallocFailedHook( void );
\r
136 void vApplicationIdleHook( void );
\r
137 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
\r
138 void vApplicationTickHook( void );
\r
140 /* The private watchdog is used as the timer that generates run time
\r
141 stats. This frequency means it will overflow quite quickly. */
\r
142 XScuWdt xWatchDogInstance;
\r
144 /*-----------------------------------------------------------*/
\r
146 /* The interrupt controller is initialised in this file, and made available to
\r
148 XScuGic xInterruptController;
\r
150 /*-----------------------------------------------------------*/
\r
154 /* See http://www.freertos.org/RTOS-Xilinx-Zynq.html for instructions. */
\r
156 /* Configure the hardware ready to run the demo. */
\r
157 prvSetupHardware();
\r
159 /* The mainSELECTED_APPLICATION setting is described at the top of this
\r
161 #if( mainSELECTED_APPLICATION == 0 )
\r
165 #elif( mainSELECTED_APPLICATION == 1 )
\r
175 /* Don't expect to reach here. */
\r
178 /*-----------------------------------------------------------*/
\r
180 static void prvSetupHardware( void )
\r
182 BaseType_t xStatus;
\r
183 XScuGic_Config *pxGICConfig;
\r
185 /* Ensure no interrupts execute while the scheduler is in an inconsistent
\r
186 state. Interrupts are automatically enabled when the scheduler is
\r
188 portDISABLE_INTERRUPTS();
\r
190 /* Obtain the configuration of the GIC. */
\r
191 pxGICConfig = XScuGic_LookupConfig( XPAR_SCUGIC_SINGLE_DEVICE_ID );
\r
193 /* Sanity check the FreeRTOSConfig.h settings are correct for the
\r
195 configASSERT( pxGICConfig );
\r
196 configASSERT( pxGICConfig->CpuBaseAddress == ( configINTERRUPT_CONTROLLER_BASE_ADDRESS + configINTERRUPT_CONTROLLER_CPU_INTERFACE_OFFSET ) );
\r
197 configASSERT( pxGICConfig->DistBaseAddress == configINTERRUPT_CONTROLLER_BASE_ADDRESS );
\r
199 /* Install a default handler for each GIC interrupt. */
\r
200 xStatus = XScuGic_CfgInitialize( &xInterruptController, pxGICConfig, pxGICConfig->CpuBaseAddress );
\r
201 configASSERT( xStatus == XST_SUCCESS );
\r
202 ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */
\r
204 /* Initialise the LED port. */
\r
205 vParTestInitialise();
\r
207 /* The Xilinx projects use a BSP that do not allow the start up code to be
\r
208 altered easily. Therefore the vector table used by FreeRTOS is defined in
\r
209 FreeRTOS_asm_vectors.S, which is part of this project. Switch to use the
\r
210 FreeRTOS vector table. */
\r
211 vPortInstallFreeRTOSVectorTable();
\r
213 /* Initialise UART for use with QEMU. */
\r
214 XUartPs_ResetHw( 0xE0000000 );
\r
215 XUartPs_WriteReg(0xE0000000, XUARTPS_CR_OFFSET,
\r
216 ((u32)XUARTPS_CR_RX_DIS | (u32)XUARTPS_CR_TX_EN |
\r
217 (u32)XUARTPS_CR_STOPBRK));
\r
219 /*-----------------------------------------------------------*/
\r
221 void vApplicationMallocFailedHook( void )
\r
223 /* Called if a call to pvPortMalloc() fails because there is insufficient
\r
224 free memory available in the FreeRTOS heap. pvPortMalloc() is called
\r
225 internally by FreeRTOS API functions that create tasks, queues, software
\r
226 timers, and semaphores. The size of the FreeRTOS heap is set by the
\r
227 configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */
\r
228 taskDISABLE_INTERRUPTS();
\r
231 /*-----------------------------------------------------------*/
\r
233 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
\r
235 ( void ) pcTaskName;
\r
238 /* Run time stack overflow checking is performed if
\r
239 configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
\r
240 function is called if a stack overflow is detected. */
\r
241 taskDISABLE_INTERRUPTS();
\r
244 /*-----------------------------------------------------------*/
\r
246 void vApplicationIdleHook( void )
\r
248 volatile size_t xFreeHeapSpace, xMinimumEverFreeHeapSpace;
\r
250 /* This is just a trivial example of an idle hook. It is called on each
\r
251 cycle of the idle task. It must *NOT* attempt to block. In this case the
\r
252 idle task just queries the amount of FreeRTOS heap that remains. See the
\r
253 memory management section on the http://www.FreeRTOS.org web site for memory
\r
254 management options. If there is a lot of heap memory free then the
\r
255 configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up
\r
257 xFreeHeapSpace = xPortGetFreeHeapSize();
\r
258 xMinimumEverFreeHeapSpace = xPortGetMinimumEverFreeHeapSize();
\r
260 /* Remove compiler warning about xFreeHeapSpace being set but never used. */
\r
261 ( void ) xFreeHeapSpace;
\r
262 ( void ) xMinimumEverFreeHeapSpace;
\r
264 /*-----------------------------------------------------------*/
\r
266 void vAssertCalled( const char * pcFile, unsigned long ulLine )
\r
268 volatile unsigned long ul = 0;
\r
273 taskENTER_CRITICAL();
\r
275 /* Set ul to a non-zero value using the debugger to step out of this
\r
282 taskEXIT_CRITICAL();
\r
284 /*-----------------------------------------------------------*/
\r
286 void vApplicationTickHook( void )
\r
288 #if( mainSELECTED_APPLICATION == 1 )
\r
290 /* The full demo includes a software timer demo/test that requires
\r
291 prodding periodically from the tick interrupt. */
\r
292 vTimerPeriodicISRTests();
\r
294 /* Call the periodic queue overwrite from ISR demo. */
\r
295 vQueueOverwritePeriodicISRDemo();
\r
297 /* Call the periodic event group from ISR demo. */
\r
298 vPeriodicEventGroupsProcessing();
\r
300 /* Use task notifications from an interrupt. */
\r
301 xNotifyTaskFromISR();
\r
303 /* Use mutexes from interrupts. */
\r
304 vInterruptSemaphorePeriodicTest();
\r
306 /* Writes to stream buffer byte by byte to test the stream buffer trigger
\r
307 level functionality. */
\r
308 vPeriodicStreamBufferProcessing();
\r
310 /* Writes a string to a string buffer four bytes at a time to demonstrate
\r
311 a stream being sent from an interrupt to a task. */
\r
312 vBasicStreamBufferSendFromISR();
\r
314 #if( configUSE_QUEUE_SETS == 1 )
\r
316 vQueueSetAccessQueueSetFromISR();
\r
320 /* Test flop alignment in interrupts - calling printf from an interrupt
\r
322 #if( configASSERT_DEFINED == 1 )
\r
325 UBaseType_t uxSavedInterruptStatus;
\r
327 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
\r
329 sprintf( cBuf, "%1.3f", 1.234 );
\r
331 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
\r
333 configASSERT( strcmp( cBuf, "1.234" ) == 0 );
\r
335 #endif /* configASSERT_DEFINED */
\r
339 /*-----------------------------------------------------------*/
\r
341 void *memcpy( void *pvDest, const void *pvSource, size_t xBytes )
\r
343 /* The compiler used during development seems to err unless these volatiles are
\r
344 included at -O3 optimisation. */
\r
345 volatile unsigned char *pcDest = ( volatile unsigned char * ) pvDest, *pcSource = ( volatile unsigned char * ) pvSource;
\r
348 /* Extremely crude standard library implementations in lieu of having a C
\r
350 if( pvDest != pvSource )
\r
352 for( x = 0; x < xBytes; x++ )
\r
354 pcDest[ x ] = pcSource[ x ];
\r
360 /*-----------------------------------------------------------*/
\r
362 void *memset( void *pvDest, int iValue, size_t xBytes )
\r
364 /* The compiler used during development seems to err unless these volatiles are
\r
365 included at -O3 optimisation. */
\r
366 volatile unsigned char * volatile pcDest = ( volatile unsigned char * volatile ) pvDest;
\r
369 /* Extremely crude standard library implementations in lieu of having a C
\r
371 for( x = 0; x < xBytes; x++ )
\r
373 pcDest[ x ] = ( unsigned char ) iValue;
\r
378 /*-----------------------------------------------------------*/
\r
380 int memcmp( const void *pvMem1, const void *pvMem2, size_t xBytes )
\r
382 const volatile unsigned char *pucMem1 = pvMem1, *pucMem2 = pvMem2;
\r
385 /* Extremely crude standard library implementations in lieu of having a C
\r
387 for( x = 0; x < xBytes; x++ )
\r
389 if( pucMem1[ x ] != pucMem2[ x ] )
\r
397 /*-----------------------------------------------------------*/
\r
399 void vInitialiseTimerForRunTimeStats( void )
\r
401 XScuWdt_Config *pxWatchDogInstance;
\r
403 const uint32_t ulMaxDivisor = 0xff, ulDivisorShift = 0x08;
\r
405 pxWatchDogInstance = XScuWdt_LookupConfig( XPAR_SCUWDT_0_DEVICE_ID );
\r
406 XScuWdt_CfgInitialize( &xWatchDogInstance, pxWatchDogInstance, pxWatchDogInstance->BaseAddr );
\r
408 ulValue = XScuWdt_GetControlReg( &xWatchDogInstance );
\r
409 ulValue |= ulMaxDivisor << ulDivisorShift;
\r
410 XScuWdt_SetControlReg( &xWatchDogInstance, ulValue );
\r
412 XScuWdt_LoadWdt( &xWatchDogInstance, UINT_MAX );
\r
413 XScuWdt_SetTimerMode( &xWatchDogInstance );
\r
414 XScuWdt_Start( &xWatchDogInstance );
\r
416 /*-----------------------------------------------------------*/
\r
418 /* configUSE_STATIC_ALLOCATION is set to 1, so the application must provide an
\r
419 implementation of vApplicationGetIdleTaskMemory() to provide the memory that is
\r
420 used by the Idle task. */
\r
421 void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
\r
423 /* If the buffers to be provided to the Idle task are declared inside this
\r
424 function then they must be declared static - otherwise they will be allocated on
\r
425 the stack and so not exists after this function exits. */
\r
426 static StaticTask_t xIdleTaskTCB;
\r
427 static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
\r
429 /* Pass out a pointer to the StaticTask_t structure in which the Idle task's
\r
430 state will be stored. */
\r
431 *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
\r
433 /* Pass out the array that will be used as the Idle task's stack. */
\r
434 *ppxIdleTaskStackBuffer = uxIdleTaskStack;
\r
436 /* Pass out the size of the array pointed to by *ppxIdleTaskStackBuffer.
\r
437 Note that, as the array is necessarily of type StackType_t,
\r
438 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
\r
439 *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
\r
441 /*-----------------------------------------------------------*/
\r
443 /* configUSE_STATIC_ALLOCATION and configUSE_TIMERS are both set to 1, so the
\r
444 application must provide an implementation of vApplicationGetTimerTaskMemory()
\r
445 to provide the memory that is used by the Timer service task. */
\r
446 void vApplicationGetTimerTaskMemory( StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize )
\r
448 /* If the buffers to be provided to the Timer task are declared inside this
\r
449 function then they must be declared static - otherwise they will be allocated on
\r
450 the stack and so not exists after this function exits. */
\r
451 static StaticTask_t xTimerTaskTCB;
\r
452 static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
\r
454 /* Pass out a pointer to the StaticTask_t structure in which the Timer
\r
455 task's state will be stored. */
\r
456 *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
\r
458 /* Pass out the array that will be used as the Timer task's stack. */
\r
459 *ppxTimerTaskStackBuffer = uxTimerTaskStack;
\r
461 /* Pass out the size of the array pointed to by *ppxTimerTaskStackBuffer.
\r
462 Note that, as the array is necessarily of type StackType_t,
\r
463 configMINIMAL_STACK_SIZE is specified in words, not bytes. */
\r
464 *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
\r