2 FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
28 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
29 >>! obliged to provide the source code for proprietary components !<<
\r
30 >>! outside of the FreeRTOS kernel. !<<
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 /******************************************************************************
\r
67 * NOTE 1: This project provides two demo applications. A simple blinky style
\r
68 * project, and a more comprehensive test and demo application that makes use of
\r
69 * the FreeRTOS+CLI, FreeRTOS+UDP and FreeRTOS+FAT SL components. The
\r
70 * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting in main.c is used to select
\r
71 * between the two. See the notes on using mainCREATE_SIMPLE_BLINKY_DEMO_ONLY
\r
72 * in main.c. This file implements the comprehensive test and demo version,
\r
73 * which is fully documented on the following URL:
\r
74 * http://www.FreeRTOS.org/Atmel_SAM4E_RTOS_Demo.html
\r
76 * NOTE 2: This file only contains the source code that is specific to the
\r
77 * full demo. Generic functions, such FreeRTOS hook functions, and functions
\r
78 * required to configure the hardware, are defined in main.c.
\r
79 ******************************************************************************
\r
81 * Full user instructions are provided on the following URL:
\r
82 * http://www.FreeRTOS.org/Atmel_SAM4E_RTOS_Demo.html
\r
85 * + Uses FreeRTOS+FAT SL to create a set of example files on a RAM disk.
\r
86 * + Displays some bitmaps on the LCD.
\r
87 * + Registers sample generic, file system related and UDP related commands
\r
88 * with FreeRTOS+CLI.
\r
89 * + Creates all the standard demo application tasks and software timers.
\r
90 * + Starts the scheduler.
\r
92 * A UDP command server and optionally two UDP echo client tasks are created
\r
93 * from the network event hook after an IP address has been obtained. The IP
\r
94 * address is displayed on the LCD.
\r
96 * A "check software timer" is created to provide visual feedback of the system
\r
97 * status. The timer's period is initially set to three seconds. The callback
\r
98 * function associated with the timer checks all the standard demo tasks are not
\r
99 * only still executed, but are executing without reporting any errors. If the
\r
100 * timer discovers a task has either stalled, or reported an error, then it
\r
101 * changes its own period from the initial three seconds, to just 200ms. The
\r
102 * check software timer also toggles the LED marked D4 - so if the LED toggles
\r
103 * every three seconds then no potential errors have been found, and if the LED
\r
104 * toggles every 200ms then a potential error has been found in at least one
\r
107 * Information on accessing the CLI and file system, and using the UDP echo
\r
108 * tasks is provided on http://www.FreeRTOS.org/Atmel_SAM4E_RTOS_Demo.html
\r
112 /* FreeRTOS includes. */
\r
113 #include "FreeRTOS.h"
\r
115 #include "timers.h"
\r
117 /* FreeRTOS+UDP includes. */
\r
118 #include "FreeRTOS_UDP_IP.h"
\r
119 #include "FreeRTOS_Sockets.h"
\r
121 /* UDP demo includes. */
\r
122 #include "UDPCommandInterpreter.h"
\r
123 #include "TwoEchoClients.h"
\r
125 /* Standard demo includes. */
\r
126 #include "partest.h"
\r
127 #include "blocktim.h"
\r
128 #include "flash_timer.h"
\r
129 #include "semtest.h"
\r
130 #include "GenQTest.h"
\r
132 #include "IntQueue.h"
\r
133 #include "countsem.h"
\r
134 #include "dynamic.h"
\r
135 #include "QueueOverwrite.h"
\r
136 #include "QueueSet.h"
\r
137 #include "recmutex.h"
\r
138 #include "EventGroupsDemo.h"
\r
140 /* The period after which the check timer will expire, in ms, provided no errors
\r
141 have been reported by any of the standard demo tasks. ms are converted to the
\r
142 equivalent in ticks using the portTICK_PERIOD_MS constant. */
\r
143 #define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_PERIOD_MS )
\r
145 /* The period at which the check timer will expire, in ms, if an error has been
\r
146 reported in one of the standard demo tasks. ms are converted to the equivalent
\r
147 in ticks using the portTICK_PERIOD_MS constant. */
\r
148 #define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_PERIOD_MS )
\r
150 /* The priorities of the various demo application tasks. */
\r
151 #define mainSEM_TEST_PRIORITY ( tskIDLE_PRIORITY + 1 )
\r
152 #define mainBLOCK_Q_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
153 #define mainCOM_TEST_PRIORITY ( tskIDLE_PRIORITY + 2 )
\r
154 #define mainINTEGER_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
155 #define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
156 #define mainQUEUE_OVERWRITE_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
158 /* The LED controlled by the 'check' software timer. */
\r
159 #define mainCHECK_LED ( 2 )
\r
161 /* The number of LEDs that should be controlled by the flash software timer
\r
162 standard demo. In this case it is only 1 as the starter kit has three LEDs, one
\r
163 of which is controlled by the check timer and one of which is controlled by the
\r
164 ISR triggered task. */
\r
165 #define mainNUM_FLASH_TIMER_LEDS ( 1 )
\r
168 #define mainDONT_BLOCK ( 0 )
\r
170 /* Note: If the application is started without the network cable plugged in
\r
171 then ipconfigUDP_TASK_PRIORITY should be set to 0 in FreeRTOSIPConfig.h to
\r
172 ensure the IP task is created at the idle priority. This is because the Atmel
\r
173 ASF GMAC driver polls the GMAC looking for a connection, and doing so will
\r
174 prevent any lower priority tasks from executing. In this demo the IP task is
\r
175 started at the idle priority, then set to configMAX_PRIORITIES - 2 in the
\r
176 network event hook only after a connection has been established (when the event
\r
177 passed into the network event hook is eNetworkUp).
\r
178 http://www.FreeRTOS.org/udp */
\r
179 #define mainCONNECTED_IP_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
\r
180 #define mainDISCONNECTED_IP_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
182 /* UDP command server and echo task parameters. */
\r
183 #define mainUDP_CLI_TASK_PRIORITY ( tskIDLE_PRIORITY )
\r
184 #define mainUDP_CLI_PORT_NUMBER ( 5001UL )
\r
185 #define mainUDP_CLI_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE * 2U )
\r
186 #define mainECHO_CLIENT_STACK_SIZE ( configMINIMAL_STACK_SIZE + 30 )
\r
188 /* Set to 1 to include the UDP echo client tasks in the build. The echo clients
\r
189 require the IP address of the echo server to be defined using the
\r
190 configECHO_SERVER_ADDR0 to configECHO_SERVER_ADDR3 constants in
\r
191 FreeRTOSConfig.h. */
\r
192 #define mainINCLUDE_ECHO_CLIENT_TASKS 1
\r
194 /*-----------------------------------------------------------*/
\r
197 * The check timer callback function, as described at the top of this file.
\r
199 static void prvCheckTimerCallback( TimerHandle_t xTimer );
\r
202 * Creates a set of sample files on a RAM disk. http://www.FreeRTOS.org/fat_sl
\r
204 extern void vCreateAndVerifySampleFiles( void );
\r
207 * Register sample generic commands that can be used with FreeRTOS+CLI. Type
\r
208 * 'help' in the command line to see a list of registered commands.
\r
209 * http://www.FreeRTOS.org/cli
\r
211 extern void vRegisterSampleCLICommands( void );
\r
214 * Register sample file system commands that can be used with FreeRTOS+CLI.
\r
216 extern void vRegisterFileSystemCLICommands( void );
\r
219 * Register sample UDP related commands that can be used with FreeRTOS+CLI.
\r
221 extern void vRegisterUDPCLICommands( void );
\r
224 * Initialise the LCD and output a bitmap.
\r
226 extern void vInitialiseLCD( void );
\r
228 /*-----------------------------------------------------------*/
\r
230 /* The default IP and MAC address used by the demo. The address configuration
\r
231 defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is
\r
232 1 but a DHCP server could not be contacted. See the online documentation for
\r
233 more information. */
\r
234 static const uint8_t ucIPAddress[ 4 ] = { configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 };
\r
235 static const uint8_t ucNetMask[ 4 ] = { configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 };
\r
236 static const uint8_t ucGatewayAddress[ 4 ] = { configGATEWAY_ADDR0, configGATEWAY_ADDR1, configGATEWAY_ADDR2, configGATEWAY_ADDR3 };
\r
237 static const uint8_t ucDNSServerAddress[ 4 ] = { configDNS_SERVER_ADDR0, configDNS_SERVER_ADDR1, configDNS_SERVER_ADDR2, configDNS_SERVER_ADDR3 };
\r
239 /* The MAC address used by the demo. In production units the MAC address would
\r
240 probably be read from flash memory or an EEPROM. Here it is just hard coded.
\r
241 Note each node on a network must have a unique MAC address. */
\r
242 const uint8_t ucMACAddress[ 6 ] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };
\r
244 /*-----------------------------------------------------------*/
\r
246 int main_full( void )
\r
248 TimerHandle_t xTimer = NULL;
\r
250 /* Usage instructions on http://www.FreeRTOS.org/Atmel_SAM4E_RTOS_Demo.html */
\r
252 /* Initialise the LCD and output a bitmap. The IP address will also be
\r
253 displayed on the LCD when it has been obtained. */
\r
256 /* If the file system is only going to be accessed from one task then
\r
257 F_FS_THREAD_AWARE can be set to 0 and the set of example files are created
\r
258 before the RTOS scheduler is started. If the file system is going to be
\r
259 access from more than one task then F_FS_THREAD_AWARE must be set to 1 and
\r
260 the set of sample files are created from the idle task hook function
\r
261 vApplicationIdleHook(). */
\r
262 #if( F_FS_THREAD_AWARE == 0 )
\r
264 /* Initialise the drive and file system, then create a few example
\r
265 files. The files can be viewed and accessed via the CLI. View the
\r
266 documentation page for this demo (link at the top of this file) for more
\r
268 vCreateAndVerifySampleFiles();
\r
272 /* Register example generic, file system related and UDP related CLI
\r
273 commands respectively. Type 'help' into the command console to view a list
\r
274 of registered commands. */
\r
275 vRegisterSampleCLICommands();
\r
276 vRegisterFileSystemCLICommands();
\r
277 vRegisterUDPCLICommands();
\r
279 /* Initialise the network interface. Tasks that use the network are
\r
280 created in the network event hook when the network is connected and ready
\r
281 for use. The address values passed in here are used if ipconfigUSE_DHCP is
\r
282 set to 0, or if ipconfigUSE_DHCP is set to 1 but a DHCP server cannot be
\r
283 contacted. The IP address actually used is displayed on the LCD (after DHCP
\r
284 has completed if DHCP is used). */
\r
285 FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );
\r
287 /* Create all the other standard demo tasks. */
\r
288 vStartLEDFlashTimers( mainNUM_FLASH_TIMER_LEDS );
\r
289 vCreateBlockTimeTasks();
\r
290 vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
\r
291 vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
\r
292 vStartQueuePeekTasks();
\r
293 vStartCountingSemaphoreTasks();
\r
294 vStartDynamicPriorityTasks();
\r
295 vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_TASK_PRIORITY );
\r
296 vStartQueueSetTasks();
\r
297 vStartRecursiveMutexTasks();
\r
298 vStartEventGroupTasks();
\r
300 /* Create the software timer that performs the 'check' functionality, as
\r
301 described at the top of this file. */
\r
302 xTimer = xTimerCreate( "CheckTimer", /* A text name, purely to help debugging. */
\r
303 ( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 3000ms (3s). */
\r
304 pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
\r
305 ( void * ) 0, /* The ID is not used, so can be set to anything. */
\r
306 prvCheckTimerCallback ); /* The callback function that inspects the status of all the other tasks. */
\r
308 if( xTimer != NULL )
\r
310 xTimerStart( xTimer, mainDONT_BLOCK );
\r
313 /* Start the scheduler itself. */
\r
314 vTaskStartScheduler();
\r
316 /* If all is well, the scheduler will now be running, and the following line
\r
317 will never be reached. If the following line does execute, then there was
\r
318 insufficient FreeRTOS heap memory available for the idle and/or timer tasks
\r
319 to be created. See the memory management section on the FreeRTOS web site
\r
320 for more details. */
\r
323 /*-----------------------------------------------------------*/
\r
325 static void prvCheckTimerCallback( TimerHandle_t xTimer )
\r
327 static long lChangedTimerPeriodAlready = pdFALSE;
\r
328 unsigned long ulErrorOccurred = pdFALSE;
\r
330 /* Avoid compiler warnings. */
\r
333 /* Have any of the standard demo tasks detected an error in their
\r
335 if( xAreGenericQueueTasksStillRunning() != pdTRUE )
\r
337 ulErrorOccurred |= ( 0x01UL << 3UL );
\r
339 else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
\r
341 ulErrorOccurred |= ( 0x01UL << 4UL );
\r
343 else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
\r
345 ulErrorOccurred |= ( 0x01UL << 5UL );
\r
347 else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
\r
349 ulErrorOccurred |= ( 0x01UL << 6UL );
\r
351 else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
\r
353 ulErrorOccurred |= ( 0x01UL << 8UL );
\r
355 else if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
\r
357 ulErrorOccurred |= ( 0x01UL << 9UL );
\r
359 else if( xIsQueueOverwriteTaskStillRunning() != pdTRUE )
\r
361 ulErrorOccurred |= ( 0x01UL << 10UL );
\r
363 else if( xAreQueueSetTasksStillRunning() != pdTRUE )
\r
365 ulErrorOccurred |= ( 0x01UL << 11UL );
\r
367 else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
\r
369 ulErrorOccurred |= ( 0x01UL << 12UL );
\r
371 else if( xAreEventGroupTasksStillRunning() != pdTRUE )
\r
373 ulErrorOccurred |= ( 0x01UL << 13UL );
\r
376 if( ulErrorOccurred != pdFALSE )
\r
378 /* An error occurred. Increase the frequency at which the check timer
\r
379 toggles its LED to give visual feedback of the potential error
\r
381 if( lChangedTimerPeriodAlready == pdFALSE )
\r
383 lChangedTimerPeriodAlready = pdTRUE;
\r
385 /* This call to xTimerChangePeriod() uses a zero block time.
\r
386 Functions called from inside of a timer callback function must
\r
387 *never* attempt to block as to do so could impact other software
\r
389 xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
\r
393 /* Toggle the LED to give visual feedback of the system status. The rate at
\r
394 which the LED toggles will increase to mainERROR_CHECK_TIMER_PERIOD_MS if a
\r
395 suspected error has been found in any of the standard demo tasks. */
\r
396 vParTestToggleLED( mainCHECK_LED );
\r
398 /*-----------------------------------------------------------*/
\r
400 /* Called by FreeRTOS+UDP when the network connects. */
\r
401 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )
\r
403 static long lTasksAlreadyCreated = pdFALSE;
\r
404 const unsigned long ulXCoord = 3, ulYCoord = 3, ulIPAddressOffset = 45;
\r
405 unsigned long ulIPAddress;
\r
406 char cIPAddress[ 20 ];
\r
408 /* Note: If the application is started without the network cable plugged in
\r
409 then ipconfigUDP_TASK_PRIORITY should be set to 0 in FreeRTOSIPConfig.h to
\r
410 ensure the IP task is created at the idle priority. This is because the Atmel
\r
411 ASF GMAC driver polls the GMAC looking for a connection, and doing so will
\r
412 prevent any lower priority tasks from executing. In this demo the IP task is
\r
413 started at the idle priority, then set to configMAX_PRIORITIES - 2 in the
\r
414 network event hook only after a connection has been established (when the event
\r
415 passed into the network event hook is eNetworkUp). */
\r
416 if( eNetworkEvent == eNetworkUp )
\r
418 /* Ensure tasks are only created once. */
\r
419 if( lTasksAlreadyCreated == pdFALSE )
\r
421 /* Create the task that handles the CLI on a UDP port. The port
\r
422 number is set using the configUDP_CLI_PORT_NUMBER setting in
\r
423 FreeRTOSConfig.h. */
\r
424 vStartUDPCommandInterpreterTask( mainUDP_CLI_TASK_STACK_SIZE, mainUDP_CLI_PORT_NUMBER, mainUDP_CLI_TASK_PRIORITY );
\r
426 #if( mainINCLUDE_ECHO_CLIENT_TASKS == 1 )
\r
428 /* Create the UDP echo tasks. The UDP echo tasks require the IP
\r
429 address of the echo server to be defined using the
\r
430 configECHO_SERVER_ADDR0 to configECHO_SERVER_ADDR3 constants in
\r
431 FreeRTOSConfig.h. */
\r
432 vStartEchoClientTasks( mainECHO_CLIENT_STACK_SIZE, tskIDLE_PRIORITY );
\r
437 /* Obtain the IP address, convert it to a string, then display it on the
\r
439 FreeRTOS_GetAddressConfiguration( &ulIPAddress, NULL, NULL, NULL );
\r
440 FreeRTOS_inet_ntoa( ulIPAddress, cIPAddress );
\r
441 ili93xx_draw_string( ulXCoord, ulYCoord, ( uint8_t * ) "IP: " );
\r
442 ili93xx_draw_string( ulXCoord + ulIPAddressOffset, ulYCoord, ( uint8_t * ) cIPAddress );
\r
444 /* Set the priority of the IP task up to the desired priority now it has
\r
446 vTaskPrioritySet( NULL, mainCONNECTED_IP_TASK_PRIORITY );
\r
449 /* NOTE: At the time of writing the Ethernet driver does not report the
\r
450 cable being unplugged - so the following if() condition will never be met.
\r
451 It is included for possible future updates to the driver. */
\r
452 if( eNetworkEvent == eNetworkDown )
\r
454 /* Ensure the Atmel GMAC drivers don't hog all the CPU time as they look
\r
455 for a new connection by lowering the priority of the IP task to that of
\r
457 vTaskPrioritySet( NULL, tskIDLE_PRIORITY );
\r
459 /* Disconnected - so no IP address. */
\r
460 ili93xx_draw_string( ulXCoord, ulYCoord, ( uint8_t * ) "IP: " );
\r
463 /*-----------------------------------------------------------*/
\r
465 void vFullDemoIdleHook( void )
\r
467 /* If the file system is only going to be accessed from one task then
\r
468 F_FS_THREAD_AWARE can be set to 0 and the set of example files is created
\r
469 before the RTOS scheduler is started. If the file system is going to be
\r
470 access from more than one task then F_FS_THREAD_AWARE must be set to 1 and
\r
471 the set of sample files are created from the idle task hook function. */
\r
472 #if( F_FS_THREAD_AWARE == 1 )
\r
474 static portBASE_TYPE xCreatedSampleFiles = pdFALSE;
\r
476 /* Initialise the drive and file system, then create a few example
\r
477 files. The output from this function just goes to the stdout window,
\r
478 allowing the output to be viewed when the UDP command console is not
\r
480 if( xCreatedSampleFiles == pdFALSE )
\r
482 vCreateAndVerifySampleFiles();
\r
483 xCreatedSampleFiles = pdTRUE;
\r
488 /*-----------------------------------------------------------*/
\r
490 void vFullDemoTickHook( void )
\r
492 /* Call the periodic queue overwrite from ISR test function. */
\r
493 vQueueOverwritePeriodicISRDemo();
\r
495 /* Call the periodic queue set ISR test function. */
\r
496 vQueueSetAccessQueueSetFromISR();
\r
498 /* Call the event group ISR tests. */
\r
499 vPeriodicEventGroupsProcessing();
\r
501 /*-----------------------------------------------------------*/
\r
503 /* Called automatically when a reply to an outgoing ping is received. */
\r
504 void vApplicationPingReplyHook( ePingReplyStatus_t eStatus, uint16_t usIdentifier )
\r
506 /* This demo has nowhere to output any information so does nothing, but the
\r
507 IP address resolved for the pined URL is displayed in the CLI. */
\r
508 ( void ) usIdentifier;
\r
511 /*-----------------------------------------------------------*/
\r