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