]> git.sur5r.net Git - freertos/blob - Demo/lwIP_MCF5235_GCC/demo.c
Start to re-arrange files to include FreeRTOS+ in main download.
[freertos] / Demo / lwIP_MCF5235_GCC / demo.c
1 /*\r
2     FreeRTOS V4.6.1 - copyright (C) 2003-2006 Richard Barry.\r
3     MCF5235 Port - Copyright (C) 2006 Christian Walter.\r
4 \r
5     This file is part of the FreeRTOS distribution.\r
6 \r
7     FreeRTOS is free software; you can redistribute it and/or modify\r
8     it under the terms of the GNU General Public License** as published by\r
9     the Free Software Foundation; either version 2 of the License, or\r
10     (at your option) any later version.\r
11 \r
12     FreeRTOS is distributed in the hope that it will be useful,\r
13     but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15     GNU General Public License for more details.\r
16 \r
17     You should have received a copy of the GNU General Public License\r
18     along with FreeRTOS; if not, write to the Free Software\r
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
20 \r
21     A special exception to the GPL can be applied should you wish to distribute\r
22     a combined work that includes FreeRTOS, without being obliged to provide\r
23     the source code for any proprietary components.  See the licensing section\r
24     of http://www.FreeRTOS.org for full details of how and when the exception\r
25     can be applied.\r
26 \r
27     ***************************************************************************\r
28     ***************************************************************************\r
29     *                                                                         *\r
30     * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
31         *                                                                         *\r
32         * This is a concise, step by step, 'hands on' guide that describes both   *\r
33         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
34         * explains numerous examples that are written using the FreeRTOS API.     *\r
35         * Full source code for all the examples is provided in an accompanying    *\r
36         * .zip file.                                                              *\r
37     *                                                                         *\r
38     ***************************************************************************\r
39     ***************************************************************************\r
40 \r
41         Please ensure to read the configuration and relevant port sections of the\r
42         online documentation.\r
43 \r
44         http://www.FreeRTOS.org - Documentation, latest information, license and \r
45         contact details.\r
46 \r
47         http://www.SafeRTOS.com - A version that is certified for use in safety \r
48         critical systems.\r
49 \r
50         http://www.OpenRTOS.com - Commercial support, development, porting, \r
51         licensing and training services.\r
52 */\r
53 \r
54 /* ------------------------ System includes ------------------------------- */\r
55 #include <stdlib.h>\r
56 #include <stdio.h>\r
57 #include <string.h>\r
58 #include <assert.h>\r
59 \r
60 /* ------------------------ FreeRTOS includes ----------------------------- */\r
61 #include "FreeRTOS.h"\r
62 #include "task.h"\r
63 \r
64 /* ------------------------ LWIP includes --------------------------------- */\r
65 #include "lwip/api.h"\r
66 #include "lwip/tcpip.h"\r
67 #include "lwip/memp.h"\r
68 \r
69 /* ------------------------ Project includes ------------------------------ */\r
70 #include "mcf5xxx.h"\r
71 #include "mcf523x.h"\r
72 #include "serial.h"\r
73 \r
74 #include "web.h"\r
75 #include "integer.h"\r
76 #include "PollQ.h"\r
77 #include "semtest.h"\r
78 #include "BlockQ.h"\r
79 #include "dynamic.h"\r
80 #include "flop.h"\r
81 \r
82 /* ------------------------ Defines --------------------------------------- */\r
83 #define mainCOM_TEST_BAUD_RATE      ( ( unsigned long ) 38400 )\r
84 \r
85 /* Priorities for the demo application tasks. */\r
86 #define mainLED_TASK_PRIORITY       ( tskIDLE_PRIORITY + 3 )\r
87 #define mainCOM_TEST_PRIORITY       ( tskIDLE_PRIORITY + 2 )\r
88 #define mainQUEUE_POLL_PRIORITY     ( tskIDLE_PRIORITY + 2 )\r
89 #define mainCHECK_TASK_PRIORITY     ( tskIDLE_PRIORITY + 4 )\r
90 #define mainSEM_TEST_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
91 #define mainBLOCK_Q_PRIORITY        ( tskIDLE_PRIORITY + 2 )\r
92 #define mainWEB_TASK_PRIORITY       ( tskIDLE_PRIORITY + 3 )\r
93 #define STACK_DEFAULT               ( 1024 )\r
94 \r
95 /* Interval in which tasks are checked. */\r
96 #define mainCHECK_PERIOD            ( ( portTickType ) 2000 / portTICK_RATE_MS  )\r
97 \r
98 /* Constants used by the vMemCheckTask() task. */\r
99 #define mainCOUNT_INITIAL_VALUE     ( ( unsigned long ) 0 )\r
100 #define mainNO_TASK                 ( 0 )\r
101 \r
102 /* The size of the memory blocks allocated by the vMemCheckTask() task. */\r
103 #define mainMEM_CHECK_SIZE_1        ( ( size_t ) 51 )\r
104 #define mainMEM_CHECK_SIZE_2        ( ( size_t ) 52 )\r
105 #define mainMEM_CHECK_SIZE_3        ( ( size_t ) 151 )\r
106 \r
107 /* ------------------------ Static variables ------------------------------ */\r
108 xComPortHandle  xSTDComPort = NULL;\r
109 \r
110 /* ------------------------ Static functions ------------------------------ */\r
111 static          portTASK_FUNCTION( vErrorChecks, pvParameters );\r
112 static long prvCheckOtherTasksAreStillRunning( unsigned long\r
113                                                    ulMemCheckTaskCount );\r
114 static          portTASK_FUNCTION( vMemCheckTask, pvParameters );\r
115 \r
116 /* ------------------------ Implementation -------------------------------- */\r
117 int\r
118 main( int argc, char *argv[] )\r
119 {\r
120     asm volatile    ( "move.w  #0x2000, %sr\n\t" );\r
121 \r
122     xSTDComPort = xSerialPortInitMinimal( 38400, 8 );\r
123     vlwIPInit(  );\r
124 \r
125     /* Start the demo/test application tasks. */\r
126     vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
127     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
128     //vStartMathTasks( tskIDLE_PRIORITY );\r
129     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
130     vStartDynamicPriorityTasks(  );\r
131     vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
132 \r
133     /* Start the webserver. */\r
134     ( void )sys_thread_new( vBasicWEBServer, NULL, mainWEB_TASK_PRIORITY );\r
135 \r
136     /* Start the check task - which is defined in this file. */\r
137     xTaskCreate( vErrorChecks, ( signed char * )"Check", 512, NULL,\r
138                  mainCHECK_TASK_PRIORITY, NULL );\r
139         /* Now all the tasks have been started - start the scheduler. */\r
140     vTaskStartScheduler(  );\r
141 \r
142     /* Should never get here! */\r
143     return 0;\r
144 }\r
145 \r
146 static\r
147 portTASK_FUNCTION( vErrorChecks, pvParameters )\r
148 {\r
149     unsigned long ulMemCheckTaskRunningCount;\r
150     xTaskHandle     xCreatedTask;\r
151 \r
152     /* The parameters are not used in this function. */\r
153     ( void )pvParameters;\r
154 \r
155     for( ;; )\r
156     {\r
157         ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;\r
158         xCreatedTask = mainNO_TASK;\r
159         if( xTaskCreate( vMemCheckTask, ( signed char * )"MEM",\r
160                          configMINIMAL_STACK_SIZE, ( void * )&ulMemCheckTaskRunningCount,\r
161                          tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )\r
162         {\r
163             xSerialPutChar( xSTDComPort, 'E', portMAX_DELAY );\r
164         }\r
165         /* Delay until it is time to execute again. */\r
166         vTaskDelay( mainCHECK_PERIOD );\r
167 \r
168         /* Delete the dynamically created task. */\r
169         if( xCreatedTask != mainNO_TASK )\r
170         {\r
171             vTaskDelete( xCreatedTask );\r
172         }\r
173 \r
174         if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )\r
175         {\r
176             xSerialPutChar( xSTDComPort, 'E', portMAX_DELAY );\r
177         }\r
178         else\r
179         {\r
180             xSerialPutChar( xSTDComPort, '.', portMAX_DELAY );\r
181         }\r
182     }\r
183 }\r
184 \r
185 static long\r
186 prvCheckOtherTasksAreStillRunning( unsigned long ulMemCheckTaskCount )\r
187 {\r
188     long        lReturn = ( long ) pdPASS;\r
189 \r
190     /* Check all the demo tasks (other than the flash tasks) to ensure\r
191      * that they are all still running, and that none of them have detected\r
192      * an error.\r
193      */\r
194     if( xAreIntegerMathsTaskStillRunning(  ) != pdTRUE )\r
195     {\r
196         lReturn = ( long ) pdFAIL;\r
197     }\r
198 \r
199     if( xArePollingQueuesStillRunning(  ) != pdTRUE )\r
200     {\r
201         lReturn = ( long ) pdFAIL;\r
202     }\r
203 \r
204     if( xAreSemaphoreTasksStillRunning(  ) != pdTRUE )\r
205     {\r
206         lReturn = ( long ) pdFAIL;\r
207     }\r
208 \r
209     if( xAreDynamicPriorityTasksStillRunning(  ) != pdTRUE )\r
210     {\r
211         lReturn = ( long ) pdFAIL;\r
212     }\r
213 \r
214     if( xAreBlockingQueuesStillRunning(  ) != pdTRUE )\r
215     {\r
216         lReturn = ( long ) pdFAIL;\r
217     }\r
218 \r
219     if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )\r
220     {\r
221         /* The vMemCheckTask did not increment the counter - it must\r
222          * have failed.\r
223          */\r
224         lReturn = ( long ) pdFAIL;\r
225     }\r
226     return lReturn;\r
227 }\r
228 \r
229 static void\r
230 vMemCheckTask( void *pvParameters )\r
231 {\r
232     unsigned long *pulMemCheckTaskRunningCounter;\r
233     void           *pvMem1, *pvMem2, *pvMem3;\r
234     static long lErrorOccurred = pdFALSE;\r
235 \r
236     /* This task is dynamically created then deleted during each cycle of the\r
237        vErrorChecks task to check the operation of the memory allocator.  Each time\r
238        the task is created memory is allocated for the stack and TCB.  Each time\r
239        the task is deleted this memory is returned to the heap.  This task itself\r
240        exercises the allocator by allocating and freeing blocks.\r
241 \r
242        The task executes at the idle priority so does not require a delay.\r
243 \r
244        pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the\r
245        vErrorChecks() task that this task is still executing without error. */\r
246 \r
247     pulMemCheckTaskRunningCounter = ( unsigned long * )pvParameters;\r
248 \r
249     for( ;; )\r
250     {\r
251         if( lErrorOccurred == pdFALSE )\r
252         {\r
253             /* We have never seen an error so increment the counter. */\r
254             ( *pulMemCheckTaskRunningCounter )++;\r
255         }\r
256 \r
257         /* Allocate some memory - just to give the allocator some extra\r
258            exercise.  This has to be in a critical section to ensure the\r
259            task does not get deleted while it has memory allocated. */\r
260         vTaskSuspendAll(  );\r
261         {\r
262             pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );\r
263             if( pvMem1 == NULL )\r
264             {\r
265                 lErrorOccurred = pdTRUE;\r
266             }\r
267             else\r
268             {\r
269                 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );\r
270                 vPortFree( pvMem1 );\r
271             }\r
272         }\r
273         xTaskResumeAll(  );\r
274 \r
275         /* Again - with a different size block. */\r
276         vTaskSuspendAll(  );\r
277         {\r
278             pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );\r
279             if( pvMem2 == NULL )\r
280             {\r
281                 lErrorOccurred = pdTRUE;\r
282             }\r
283             else\r
284             {\r
285                 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );\r
286                 vPortFree( pvMem2 );\r
287             }\r
288         }\r
289         xTaskResumeAll(  );\r
290 \r
291         /* Again - with a different size block. */\r
292         vTaskSuspendAll(  );\r
293         {\r
294             pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );\r
295             if( pvMem3 == NULL )\r
296             {\r
297                 lErrorOccurred = pdTRUE;\r
298             }\r
299             else\r
300             {\r
301                 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );\r
302                 vPortFree( pvMem3 );\r
303             }\r
304         }\r
305         xTaskResumeAll(  );\r
306     }\r
307 }\r