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