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