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