]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_ATSAM4E_Atmel_Studio/src/main_full.c
22376afca1d8e4a6bffa905aa4155d80a0ff823d
[freertos] / FreeRTOS / Demo / CORTEX_M4_ATSAM4E_Atmel_Studio / src / main_full.c
1 /*\r
2     FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\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
12      *                                                                       *\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
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \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
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \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
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \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
54 \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
58 \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
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* FreeRTOS includes. */\r
67 #include "FreeRTOS.h"\r
68 #include "task.h"\r
69 #include "timers.h"\r
70 \r
71 /* FreeRTOS+UDP includes. */\r
72 #include "FreeRTOS_UDP_IP.h"\r
73 \r
74 /* Demo application includes. */\r
75 #include "UDPCommandInterpreter.h"\r
76 #include "partest.h"\r
77 #include "blocktim.h"\r
78 #include "flash_timer.h"\r
79 #include "semtest.h"\r
80 #include "GenQTest.h"\r
81 #include "QPeek.h"\r
82 #include "IntQueue.h"\r
83 #include "countsem.h"\r
84 #include "dynamic.h"\r
85 #include "QueueOverwrite.h"\r
86 #include "QueueSet.h"\r
87 #include "recmutex.h"\r
88 \r
89 /* The period after which the check timer will expire, in ms, provided no errors\r
90 have been reported by any of the standard demo tasks.  ms are converted to the\r
91 equivalent in ticks using the portTICK_RATE_MS constant. */\r
92 #define mainCHECK_TIMER_PERIOD_MS                       ( 3000UL / portTICK_RATE_MS )\r
93 \r
94 /* The period at which the check timer will expire, in ms, if an error has been\r
95 reported in one of the standard demo tasks.  ms are converted to the equivalent\r
96 in ticks using the portTICK_RATE_MS constant. */\r
97 #define mainERROR_CHECK_TIMER_PERIOD_MS         ( 200UL / portTICK_RATE_MS )\r
98 \r
99 /* The priorities of the various demo application tasks. */\r
100 #define mainSEM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 1 )\r
101 #define mainBLOCK_Q_PRIORITY                            ( tskIDLE_PRIORITY + 2 )\r
102 #define mainCOM_TEST_PRIORITY                           ( tskIDLE_PRIORITY + 2 )\r
103 #define mainINTEGER_TASK_PRIORITY           ( tskIDLE_PRIORITY )\r
104 #define mainGEN_QUEUE_TASK_PRIORITY                     ( tskIDLE_PRIORITY )\r
105 #define mainQUEUE_OVERWRITE_TASK_PRIORITY       ( tskIDLE_PRIORITY )\r
106 \r
107 /* The LED controlled by the 'check' software timer. */\r
108 #define mainCHECK_LED                                           ( 2 )\r
109 \r
110 /* The number of LEDs that should be controlled by the flash software timer\r
111 standard demo.  In this case it is only 1 as the starter kit has three LEDs, one\r
112 of which is controlled by the check timer and one of which is controlled by the\r
113 ISR triggered task. */\r
114 #define mainNUM_FLASH_TIMER_LEDS                        ( 1 )\r
115 \r
116 /* Misc. */\r
117 #define mainDONT_BLOCK                                          ( 0 )\r
118 \r
119 /* Note:  If the application is started without the network cable plugged in \r
120 then ipconfigUDP_TASK_PRIORITY should be set to 0 in FreeRTOSIPConfig.h to\r
121 ensure the IP task is created at the idle priority.  This is because the Atmel \r
122 ASF GMAC driver polls the GMAC looking for a connection, and doing so will \r
123 prevent any lower priority tasks from executing.  In this demo the IP task is \r
124 started at the idle priority, then set to configMAX_PRIORITIES - 2 in the \r
125 network event hook only after a connection has been established (when the event\r
126 passed into the network event hook is eNetworkUp). */\r
127 #define mainCONNECTED_IP_TASK_PRIORITY          ( configMAX_PRIORITIES - 2 )\r
128 #define mainDISCONNECTED_IP_TASK_PRIORITY       ( tskIDLE_PRIORITY )\r
129 \r
130 /* UDP command server task parameters. */\r
131 #define mainUDP_CLI_TASK_PRIORITY                                       ( tskIDLE_PRIORITY )\r
132 #define mainUDP_CLI_PORT_NUMBER                                         ( 5001UL )\r
133 #define mainUDP_CLI_TASK_STACK_SIZE                                     ( configMINIMAL_STACK_SIZE * 2U )\r
134 \r
135 /*-----------------------------------------------------------*/\r
136 \r
137 /*\r
138  * The check timer callback function, as described at the top of this file.\r
139  */\r
140 static void prvCheckTimerCallback( xTimerHandle xTimer );\r
141 \r
142 /* \r
143  * Creates a set of sample files on a RAM disk. \r
144  */\r
145 extern void vCreateAndVerifySampleFiles( void );\r
146 \r
147 /*\r
148  * Register the generic commands that can be used with FreeRTOS+CLI.\r
149  */\r
150 extern void vRegisterSampleCLICommands( void );\r
151 \r
152 /*\r
153  * Register the file system commands that can be used with FreeRTOS+CLI.\r
154  */\r
155 extern void vRegisterFileSystemCLICommands( void );\r
156 \r
157 /*\r
158  * Register the UDP related commands that can be used with FreeRTOS+CLI.\r
159  */\r
160 extern void vRegisterUDPCLICommands( void );\r
161 \r
162 /*-----------------------------------------------------------*/\r
163 \r
164 /* The default IP and MAC address used by the demo.  The address configuration\r
165 defined here will be used if ipconfigUSE_DHCP is 0, or if ipconfigUSE_DHCP is\r
166 1 but a DHCP server could not be contacted.  See the online documentation for\r
167 more information. */\r
168 static const uint8_t ucIPAddress[ 4 ] = { configIP_ADDR0, configIP_ADDR1, configIP_ADDR2, configIP_ADDR3 };\r
169 static const uint8_t ucNetMask[ 4 ] = { configNET_MASK0, configNET_MASK1, configNET_MASK2, configNET_MASK3 };\r
170 static const uint8_t ucGatewayAddress[ 4 ] = { configGATEWAY_ADDR0, configGATEWAY_ADDR1, configGATEWAY_ADDR2, configGATEWAY_ADDR3 };\r
171 static const uint8_t ucDNSServerAddress[ 4 ] = { configDNS_SERVER_ADDR0, configDNS_SERVER_ADDR1, configDNS_SERVER_ADDR2, configDNS_SERVER_ADDR3 };\r
172 \r
173 /* The MAC address used by the demo.  In production units the MAC address would\r
174 probably be read from flash memory or an EEPROM.  Here it is just hard coded.\r
175 Note each node on a network must have a unique MAC address. */\r
176 const uint8_t ucMACAddress[ 6 ] = { configMAC_ADDR0, configMAC_ADDR1, configMAC_ADDR2, configMAC_ADDR3, configMAC_ADDR4, configMAC_ADDR5 };\r
177 \r
178 /*-----------------------------------------------------------*/\r
179 int main_full( void )\r
180 {\r
181 xTimerHandle xTimer = NULL;\r
182 \r
183         /* If the file system is only going to be accessed from one task then\r
184         F_FS_THREAD_AWARE can be set to 0 and the set of example files are created\r
185         before the RTOS scheduler is started.  If the file system is going to be\r
186         access from more than one task then F_FS_THREAD_AWARE must be set to 1 and\r
187         the     set of sample files are created from the idle task hook function\r
188         vApplicationIdleHook() - which is defined in this file. */\r
189         #if( F_FS_THREAD_AWARE == 0 )\r
190         {\r
191                 /* Initialise the drive and file system, then create a few example\r
192                 files.  The output from this function just goes to the stdout window,\r
193                 allowing the output to be viewed when the UDP command console is not\r
194                 connected. */\r
195                 vCreateAndVerifySampleFiles();\r
196         }\r
197         #endif\r
198 \r
199         /* Register example generic, file system related and UDP related CLI \r
200         commands respectively. */\r
201         vRegisterSampleCLICommands();\r
202         vRegisterFileSystemCLICommands();\r
203         vRegisterUDPCLICommands();\r
204 \r
205         /* Initialise the network interface.  Tasks that use the network are\r
206         created in the network event hook when the network is connected and ready\r
207         for use.  The address values passed in here are used if ipconfigUSE_DHCP is\r
208         set to 0, or if ipconfigUSE_DHCP is set to 1 but a DHCP server cannot be\r
209         contacted.  The Nabto service task is created automatically if\r
210         ipconfigFREERTOS_PLUS_NABTO is set to 1 in FreeRTOSIPConfig.h. */\r
211         FreeRTOS_IPInit( ucIPAddress, ucNetMask, ucGatewayAddress, ucDNSServerAddress, ucMACAddress );\r
212 \r
213         /* Create all the other standard demo tasks. */\r
214         vStartLEDFlashTimers( mainNUM_FLASH_TIMER_LEDS );\r
215         vCreateBlockTimeTasks();\r
216         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
217         vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );\r
218         vStartQueuePeekTasks();\r
219         vStartCountingSemaphoreTasks();\r
220         vStartDynamicPriorityTasks();\r
221         vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_TASK_PRIORITY );\r
222         vStartQueueSetTasks();\r
223         vStartRecursiveMutexTasks();\r
224 \r
225         /* Create the software timer that performs the 'check' functionality, as\r
226         described at the top of this file. */\r
227         xTimer = xTimerCreate(  ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */\r
228                                                         ( mainCHECK_TIMER_PERIOD_MS ),          /* The timer period, in this case 3000ms (3s). */\r
229                                                         pdTRUE,                                                         /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */\r
230                                                         ( void * ) 0,                                           /* The ID is not used, so can be set to anything. */\r
231                                                         prvCheckTimerCallback );                        /* The callback function that inspects the status of all the other tasks. */\r
232 \r
233         if( xTimer != NULL )\r
234         {\r
235                 xTimerStart( xTimer, mainDONT_BLOCK );\r
236         }\r
237 \r
238         /* Start the scheduler itself. */\r
239         vTaskStartScheduler();\r
240 \r
241         /* If all is well, the scheduler will now be running, and the following line\r
242         will never be reached.  If the following line does execute, then there was\r
243         insufficient FreeRTOS heap memory available for the idle and/or timer tasks\r
244         to be created.  See the memory management section on the FreeRTOS web site\r
245         for more details. */\r
246         for( ;; );\r
247 }\r
248 /*-----------------------------------------------------------*/\r
249 \r
250 static void prvCheckTimerCallback( xTimerHandle xTimer )\r
251 {\r
252 static long lChangedTimerPeriodAlready = pdFALSE;\r
253 unsigned long ulErrorOccurred = pdFALSE;\r
254 \r
255         /* Avoid compiler warnings. */\r
256         ( void ) xTimer;\r
257 \r
258         /* Have any of the standard demo tasks detected an error in their\r
259         operation? */\r
260         if( xAreGenericQueueTasksStillRunning() != pdTRUE )\r
261         {\r
262                 ulErrorOccurred |= ( 0x01UL << 3UL );\r
263         }\r
264         else if( xAreQueuePeekTasksStillRunning() != pdTRUE )\r
265         {\r
266                 ulErrorOccurred |= ( 0x01UL << 4UL );\r
267         }\r
268         else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )\r
269         {\r
270                 ulErrorOccurred |= ( 0x01UL << 5UL );\r
271         }\r
272         else if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
273         {\r
274                 ulErrorOccurred |= ( 0x01UL << 6UL );\r
275         }\r
276         else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )\r
277         {\r
278                 ulErrorOccurred |= ( 0x01UL << 8UL );\r
279         }\r
280         else if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
281         {\r
282                 ulErrorOccurred |= ( 0x01UL << 9UL );\r
283         }\r
284         else if( xIsQueueOverwriteTaskStillRunning() != pdTRUE )\r
285         {\r
286                 ulErrorOccurred |= ( 0x01UL << 10UL );\r
287         }\r
288         else if( xAreQueueSetTasksStillRunning() != pdTRUE )\r
289         {\r
290                 ulErrorOccurred |= ( 0x01UL << 11UL );\r
291         }\r
292         else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )\r
293         {\r
294                 ulErrorOccurred |= ( 0x01UL << 12UL );\r
295         }\r
296 \r
297         if( ulErrorOccurred != pdFALSE )\r
298         {\r
299                 /* An error occurred.  Increase the frequency at which the check timer\r
300                 toggles its LED to give visual feedback of the potential error\r
301                 condition. */\r
302                 if( lChangedTimerPeriodAlready == pdFALSE )\r
303                 {\r
304                         lChangedTimerPeriodAlready = pdTRUE;\r
305 \r
306                         /* This call to xTimerChangePeriod() uses a zero block time.\r
307                         Functions called from inside of a timer callback function must\r
308                         *never* attempt to block as to do so could impact other software\r
309                         timers. */\r
310                         xTimerChangePeriod( xTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );\r
311                 }\r
312         }\r
313 \r
314         vParTestToggleLED( mainCHECK_LED );\r
315 }\r
316 /*-----------------------------------------------------------*/\r
317 \r
318 /* Called by FreeRTOS+UDP when the network connects. */\r
319 void vApplicationIPNetworkEventHook( eIPCallbackEvent_t eNetworkEvent )\r
320 {\r
321 static long lTasksAlreadyCreated = pdFALSE;\r
322 \r
323         /* Note:  If the application is started without the network cable plugged in\r
324         then ipconfigUDP_TASK_PRIORITY should be set to 0 in FreeRTOSIPConfig.h to\r
325         ensure the IP task is created at the idle priority.  This is because the Atmel\r
326         ASF GMAC driver polls the GMAC looking for a connection, and doing so will\r
327         prevent any lower priority tasks from executing.  In this demo the IP task is\r
328         started at the idle priority, then set to configMAX_PRIORITIES - 2 in the\r
329         network event hook only after a connection has been established (when the event\r
330         passed into the network event hook is eNetworkUp). */\r
331         if( eNetworkEvent == eNetworkUp )\r
332         {\r
333                 vTaskPrioritySet( NULL, mainCONNECTED_IP_TASK_PRIORITY );\r
334 \r
335                 if( lTasksAlreadyCreated == pdFALSE )\r
336                 {               \r
337                         /* Create the task that handles the CLI on a UDP port.  The port number\r
338                         is set using the configUDP_CLI_PORT_NUMBER setting in FreeRTOSConfig.h. */\r
339                         vStartUDPCommandInterpreterTask( mainUDP_CLI_TASK_STACK_SIZE, mainUDP_CLI_PORT_NUMBER, mainUDP_CLI_TASK_PRIORITY );\r
340                 }\r
341         }\r
342 \r
343         if( eNetworkEvent == eNetworkDown )\r
344         {\r
345                 vTaskPrioritySet( NULL, tskIDLE_PRIORITY );\r
346         }\r
347 }\r
348 /*-----------------------------------------------------------*/\r
349 \r
350 void vFullDemoIdleHook( void )\r
351 {       \r
352         /* If the file system is only going to be accessed from one task then\r
353         F_FS_THREAD_AWARE can be set to 0 and the set of example files is created\r
354         before the RTOS scheduler is started.  If the file system is going to be\r
355         access from more than one task then F_FS_THREAD_AWARE must be set to 1 and\r
356         the     set of sample files are created from the idle task hook function. */\r
357         #if( F_FS_THREAD_AWARE == 1 )\r
358         {\r
359                 static portBASE_TYPE xCreatedSampleFiles = pdFALSE;\r
360 \r
361                 /* Initialise the drive and file system, then create a few example\r
362                 files.  The output from this function just goes to the stdout window,\r
363                 allowing the output to be viewed when the UDP command console is not\r
364                 connected. */\r
365                 if( xCreatedSampleFiles == pdFALSE )\r
366                 {\r
367                         vCreateAndVerifySampleFiles();\r
368                         xCreatedSampleFiles = pdTRUE;\r
369                 }\r
370         }\r
371         #endif\r
372 }\r
373 /*-----------------------------------------------------------*/\r
374 \r
375 void vFullDemoTickHook( void )\r
376 {\r
377         /* Call the periodic queue overwrite from ISR demo. */\r
378         vQueueOverwritePeriodicISRDemo();\r
379 \r
380         /* Call the queue set ISR test function. */\r
381         vQueueSetAccessQueueSetFromISR();\r
382 }\r
383 /*-----------------------------------------------------------*/\r
384 \r
385 /* Called automatically when a reply to an outgoing ping is received. */\r
386 void vApplicationPingReplyHook( ePingReplyStatus_t eStatus, uint16_t usIdentifier )\r
387 {\r
388         /* This demo has nowhere to output any information so does nothing. */\r
389         ( void ) usIdentifier;\r
390         \r
391         switch( eStatus )\r
392         {\r
393                 case eSuccess   :\r
394                         break;\r
395 \r
396                 case eInvalidChecksum :\r
397                         break;\r
398 \r
399                 case eInvalidData :\r
400                         break;\r
401 \r
402                 default :\r
403                         /* It is not possible to get here as all enums have their own\r
404                         case. */\r
405                         break;\r
406         }\r
407 }\r
408 \r
409 \r
410 \r
411 \r