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