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