]> git.sur5r.net Git - freertos/blob - Demo/lwIP_MCF5235_GCC/demo.c
Small mods, and update file headers.
[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     * SAVE TIME AND MONEY!  We can port FreeRTOS.org to your own hardware,    *\r
31     * and even write all or part of your application on your behalf.          *\r
32     * See http://www.OpenRTOS.com for details of the services we provide to   *\r
33     * expedite your project.                                                  *\r
34     *                                                                         *\r
35     ***************************************************************************\r
36     ***************************************************************************\r
37 \r
38         Please ensure to read the configuration and relevant port sections of the\r
39         online documentation.\r
40 \r
41         http://www.FreeRTOS.org - Documentation, latest information, license and \r
42         contact details.\r
43 \r
44         http://www.SafeRTOS.com - A version that is certified for use in safety \r
45         critical systems.\r
46 \r
47         http://www.OpenRTOS.com - Commercial support, development, porting, \r
48         licensing and training services.\r
49 */\r
50 \r
51 /* ------------------------ System includes ------------------------------- */\r
52 #include <stdlib.h>\r
53 #include <stdio.h>\r
54 #include <string.h>\r
55 #include <assert.h>\r
56 \r
57 /* ------------------------ FreeRTOS includes ----------------------------- */\r
58 #include "FreeRTOS.h"\r
59 #include "task.h"\r
60 \r
61 /* ------------------------ LWIP includes --------------------------------- */\r
62 #include "lwip/api.h"\r
63 #include "lwip/tcpip.h"\r
64 #include "lwip/memp.h"\r
65 \r
66 /* ------------------------ Project includes ------------------------------ */\r
67 #include "mcf5xxx.h"\r
68 #include "mcf523x.h"\r
69 #include "serial.h"\r
70 \r
71 #include "web.h"\r
72 #include "integer.h"\r
73 #include "PollQ.h"\r
74 #include "semtest.h"\r
75 #include "BlockQ.h"\r
76 #include "dynamic.h"\r
77 #include "flop.h"\r
78 \r
79 /* ------------------------ Defines --------------------------------------- */\r
80 #define mainCOM_TEST_BAUD_RATE      ( ( unsigned portLONG ) 38400 )\r
81 \r
82 /* Priorities for the demo application tasks. */\r
83 #define mainLED_TASK_PRIORITY       ( tskIDLE_PRIORITY + 3 )\r
84 #define mainCOM_TEST_PRIORITY       ( tskIDLE_PRIORITY + 2 )\r
85 #define mainQUEUE_POLL_PRIORITY     ( tskIDLE_PRIORITY + 2 )\r
86 #define mainCHECK_TASK_PRIORITY     ( tskIDLE_PRIORITY + 4 )\r
87 #define mainSEM_TEST_PRIORITY       ( tskIDLE_PRIORITY + 1 )\r
88 #define mainBLOCK_Q_PRIORITY        ( tskIDLE_PRIORITY + 2 )\r
89 #define mainWEB_TASK_PRIORITY       ( tskIDLE_PRIORITY + 3 )\r
90 #define STACK_DEFAULT               ( 1024 )\r
91 \r
92 /* Interval in which tasks are checked. */\r
93 #define mainCHECK_PERIOD            ( ( portTickType ) 2000 / portTICK_RATE_MS  )\r
94 \r
95 /* Constants used by the vMemCheckTask() task. */\r
96 #define mainCOUNT_INITIAL_VALUE     ( ( unsigned portLONG ) 0 )\r
97 #define mainNO_TASK                 ( 0 )\r
98 \r
99 /* The size of the memory blocks allocated by the vMemCheckTask() task. */\r
100 #define mainMEM_CHECK_SIZE_1        ( ( size_t ) 51 )\r
101 #define mainMEM_CHECK_SIZE_2        ( ( size_t ) 52 )\r
102 #define mainMEM_CHECK_SIZE_3        ( ( size_t ) 151 )\r
103 \r
104 /* ------------------------ Static variables ------------------------------ */\r
105 xComPortHandle  xSTDComPort = NULL;\r
106 \r
107 /* ------------------------ Static functions ------------------------------ */\r
108 static          portTASK_FUNCTION( vErrorChecks, pvParameters );\r
109 static portLONG prvCheckOtherTasksAreStillRunning( unsigned portLONG\r
110                                                    ulMemCheckTaskCount );\r
111 static          portTASK_FUNCTION( vMemCheckTask, pvParameters );\r
112 \r
113 /* ------------------------ Implementation -------------------------------- */\r
114 int\r
115 main( int argc, char *argv[] )\r
116 {\r
117     asm volatile    ( "move.w  #0x2000, %sr\n\t" );\r
118 \r
119     xSTDComPort = xSerialPortInitMinimal( 38400, 8 );\r
120     vlwIPInit(  );\r
121 \r
122     /* Start the demo/test application tasks. */\r
123     vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
124     vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
125     //vStartMathTasks( tskIDLE_PRIORITY );\r
126     vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
127     vStartDynamicPriorityTasks(  );\r
128     vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
129 \r
130     /* Start the webserver. */\r
131     ( void )sys_thread_new( vBasicWEBServer, NULL, mainWEB_TASK_PRIORITY );\r
132 \r
133     /* Start the check task - which is defined in this file. */\r
134     xTaskCreate( vErrorChecks, ( signed portCHAR * )"Check", 512, NULL,\r
135                  mainCHECK_TASK_PRIORITY, NULL );\r
136         /* Now all the tasks have been started - start the scheduler. */\r
137     vTaskStartScheduler(  );\r
138 \r
139     /* Should never get here! */\r
140     return 0;\r
141 }\r
142 \r
143 static\r
144 portTASK_FUNCTION( vErrorChecks, pvParameters )\r
145 {\r
146     unsigned portLONG ulMemCheckTaskRunningCount;\r
147     xTaskHandle     xCreatedTask;\r
148 \r
149     /* The parameters are not used in this function. */\r
150     ( void )pvParameters;\r
151 \r
152     for( ;; )\r
153     {\r
154         ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;\r
155         xCreatedTask = mainNO_TASK;\r
156         if( xTaskCreate( vMemCheckTask, ( signed portCHAR * )"MEM",\r
157                          configMINIMAL_STACK_SIZE, ( void * )&ulMemCheckTaskRunningCount,\r
158                          tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )\r
159         {\r
160             xSerialPutChar( xSTDComPort, 'E', portMAX_DELAY );\r
161         }\r
162         /* Delay until it is time to execute again. */\r
163         vTaskDelay( mainCHECK_PERIOD );\r
164 \r
165         /* Delete the dynamically created task. */\r
166         if( xCreatedTask != mainNO_TASK )\r
167         {\r
168             vTaskDelete( xCreatedTask );\r
169         }\r
170 \r
171         if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )\r
172         {\r
173             xSerialPutChar( xSTDComPort, 'E', portMAX_DELAY );\r
174         }\r
175         else\r
176         {\r
177             xSerialPutChar( xSTDComPort, '.', portMAX_DELAY );\r
178         }\r
179     }\r
180 }\r
181 \r
182 static portLONG\r
183 prvCheckOtherTasksAreStillRunning( unsigned portLONG ulMemCheckTaskCount )\r
184 {\r
185     portLONG        lReturn = ( portLONG ) pdPASS;\r
186 \r
187     /* Check all the demo tasks (other than the flash tasks) to ensure\r
188      * that they are all still running, and that none of them have detected\r
189      * an error.\r
190      */\r
191     if( xAreIntegerMathsTaskStillRunning(  ) != pdTRUE )\r
192     {\r
193         lReturn = ( portLONG ) pdFAIL;\r
194     }\r
195 \r
196     if( xArePollingQueuesStillRunning(  ) != pdTRUE )\r
197     {\r
198         lReturn = ( portLONG ) pdFAIL;\r
199     }\r
200 \r
201     if( xAreSemaphoreTasksStillRunning(  ) != pdTRUE )\r
202     {\r
203         lReturn = ( portLONG ) pdFAIL;\r
204     }\r
205 \r
206     if( xAreDynamicPriorityTasksStillRunning(  ) != pdTRUE )\r
207     {\r
208         lReturn = ( portLONG ) pdFAIL;\r
209     }\r
210 \r
211     if( xAreBlockingQueuesStillRunning(  ) != pdTRUE )\r
212     {\r
213         lReturn = ( portLONG ) pdFAIL;\r
214     }\r
215 \r
216     if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )\r
217     {\r
218         /* The vMemCheckTask did not increment the counter - it must\r
219          * have failed.\r
220          */\r
221         lReturn = ( portLONG ) pdFAIL;\r
222     }\r
223     return lReturn;\r
224 }\r
225 \r
226 static void\r
227 vMemCheckTask( void *pvParameters )\r
228 {\r
229     unsigned portLONG *pulMemCheckTaskRunningCounter;\r
230     void           *pvMem1, *pvMem2, *pvMem3;\r
231     static portLONG lErrorOccurred = pdFALSE;\r
232 \r
233     /* This task is dynamically created then deleted during each cycle of the\r
234        vErrorChecks task to check the operation of the memory allocator.  Each time\r
235        the task is created memory is allocated for the stack and TCB.  Each time\r
236        the task is deleted this memory is returned to the heap.  This task itself\r
237        exercises the allocator by allocating and freeing blocks.\r
238 \r
239        The task executes at the idle priority so does not require a delay.\r
240 \r
241        pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the\r
242        vErrorChecks() task that this task is still executing without error. */\r
243 \r
244     pulMemCheckTaskRunningCounter = ( unsigned portLONG * )pvParameters;\r
245 \r
246     for( ;; )\r
247     {\r
248         if( lErrorOccurred == pdFALSE )\r
249         {\r
250             /* We have never seen an error so increment the counter. */\r
251             ( *pulMemCheckTaskRunningCounter )++;\r
252         }\r
253 \r
254         /* Allocate some memory - just to give the allocator some extra\r
255            exercise.  This has to be in a critical section to ensure the\r
256            task does not get deleted while it has memory allocated. */\r
257         vTaskSuspendAll(  );\r
258         {\r
259             pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );\r
260             if( pvMem1 == NULL )\r
261             {\r
262                 lErrorOccurred = pdTRUE;\r
263             }\r
264             else\r
265             {\r
266                 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );\r
267                 vPortFree( pvMem1 );\r
268             }\r
269         }\r
270         xTaskResumeAll(  );\r
271 \r
272         /* Again - with a different size block. */\r
273         vTaskSuspendAll(  );\r
274         {\r
275             pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );\r
276             if( pvMem2 == NULL )\r
277             {\r
278                 lErrorOccurred = pdTRUE;\r
279             }\r
280             else\r
281             {\r
282                 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );\r
283                 vPortFree( pvMem2 );\r
284             }\r
285         }\r
286         xTaskResumeAll(  );\r
287 \r
288         /* Again - with a different size block. */\r
289         vTaskSuspendAll(  );\r
290         {\r
291             pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );\r
292             if( pvMem3 == NULL )\r
293             {\r
294                 lErrorOccurred = pdTRUE;\r
295             }\r
296             else\r
297             {\r
298                 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );\r
299                 vPortFree( pvMem3 );\r
300             }\r
301         }\r
302         xTaskResumeAll(  );\r
303     }\r
304 }\r