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