]> git.sur5r.net Git - freertos/blob - Demo/H8S/RTOSDemo/main.c
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@12 1d2547de-c912-0410-9cb9...
[freertos] / Demo / H8S / RTOSDemo / main.c
1 /*\r
2         FreeRTOS.org V4.0.2 - Copyright (C) 2003-2006 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30         ***************************************************************************\r
31 */\r
32 \r
33 /*\r
34  * Creates all the demo application tasks, then starts the scheduler.  The WEB\r
35  * documentation provides more details of the demo application tasks.\r
36  * \r
37  * Main.c also creates a task called "Check".  This only executes every three \r
38  * seconds but has the highest priority so is guaranteed to get processor time.  \r
39  * Its main function is to check that all the other tasks are still operational.\r
40  * Each task (other than the "flash" tasks) maintains a unique count that is \r
41  * incremented each time the task successfully completes its function.  Should \r
42  * any error occur within such a task the count is permanently halted.  The \r
43  * check task inspects the count of each task to ensure it has changed since\r
44  * the last time the check task executed.  If all the count variables have \r
45  * changed all the tasks are still executing error free, and the check task\r
46  * toggles the onboard LED.  Should any task contain an error at any time \r
47  * the LED toggle rate will change from 3 seconds to 500ms.\r
48  *\r
49  * To check the operation of the memory allocator the check task also \r
50  * dynamically creates a task before delaying, and deletes it again when it \r
51  * wakes.  If memory cannot be allocated for the new task the call to xTaskCreate\r
52  * will fail and an error is signalled.  The dynamically created task itself\r
53  * allocates and frees memory just to give the allocator a bit more exercise.\r
54  *\r
55  */\r
56 \r
57 /* Standard includes. */\r
58 #include <stdlib.h>\r
59 #include <string.h>\r
60 \r
61 /* Scheduler include files. */\r
62 #include "FreeRTOS.h"\r
63 #include "task.h"\r
64 \r
65 /* Demo application file headers. */\r
66 #include "flash.h"\r
67 #include "integer.h"\r
68 #include "PollQ.h"\r
69 #include "comtest2.h"\r
70 #include "semtest.h"\r
71 #include "flop.h"\r
72 #include "dynamic.h"\r
73 #include "BlockQ.h"\r
74 #include "serial.h"\r
75 #include "partest.h"\r
76 \r
77 /* Priority definitions for most of the tasks in the demo application.  Some\r
78 tasks just use the idle priority. */\r
79 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
80 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 2 )\r
81 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
82 #define mainCHECK_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 3 )\r
83 #define mainSEM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
84 #define mainBLOCK_Q_PRIORITY                    ( tskIDLE_PRIORITY + 2 )\r
85 \r
86 /* Baud rate used by the serial port tasks (ComTest tasks). */\r
87 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned portLONG ) 115200 )\r
88 \r
89 /* LED used by the serial port tasks.  This is toggled on each character Tx,\r
90 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */\r
91 #define mainCOM_TEST_LED                                ( 3 )\r
92 \r
93 /* LED that is toggled by the check task.  The check task periodically checks\r
94 that all the other tasks are operating without error.  If no errors are found\r
95 the LED is toggled with mainCHECK_PERIOD frequency.  If an error is found \r
96 the the toggle rate increases to mainERROR_CHECK_PERIOD. */\r
97 #define mainCHECK_TASK_LED                              ( 5 )\r
98 #define mainCHECK_PERIOD                                ( ( portTickType ) 3000 / portTICK_RATE_MS  )\r
99 #define mainERROR_CHECK_PERIOD                  ( ( portTickType ) 500 / portTICK_RATE_MS )\r
100 \r
101 /* Constants used by the vMemCheckTask() task. */\r
102 #define mainCOUNT_INITIAL_VALUE         ( ( unsigned portLONG ) 0 )\r
103 #define mainNO_TASK                                     ( 0 )\r
104 \r
105 /* The size of the memory blocks allocated by the vMemCheckTask() task. */\r
106 #define mainMEM_CHECK_SIZE_1            ( ( size_t ) 51 )\r
107 #define mainMEM_CHECK_SIZE_2            ( ( size_t ) 52 )\r
108 #define mainMEM_CHECK_SIZE_3            ( ( size_t ) 151 )\r
109 \r
110 /*\r
111  * The 'Check' task.\r
112  */\r
113 static void vErrorChecks( void *pvParameters );\r
114 \r
115 /*\r
116  * Checks the unique counts of other tasks to ensure they are still operational.\r
117  */\r
118 static portLONG prvCheckOtherTasksAreStillRunning( unsigned portLONG ulMemCheckTaskCount );\r
119 \r
120 /*\r
121  * Dynamically created and deleted during each cycle of the vErrorChecks()\r
122  * task.  This is done to check the operation of the memory allocator.\r
123  * See the top of vErrorChecks for more details.\r
124  */\r
125 static void vMemCheckTask( void *pvParameters );\r
126 \r
127 /*-----------------------------------------------------------*/\r
128 \r
129 /*\r
130  * Start all the tasks then start the scheduler.\r
131  */\r
132 int main( void )\r
133 {\r
134         /* Setup the LED's for output. */\r
135         vParTestInitialise();\r
136 \r
137         /* Start the various standard demo application tasks. */\r
138         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
139         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
140         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
141         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
142         vStartMathTasks( tskIDLE_PRIORITY );\r
143         vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );\r
144         vStartDynamicPriorityTasks();\r
145         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
146 \r
147         /* Start the 'Check' task. */\r
148         xTaskCreate( vErrorChecks, ( signed portCHAR * )"Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
149 \r
150         /* In this port, to use preemptive scheduler define configUSE_PREEMPTION \r
151         as 1 in portmacro.h.  To use the cooperative scheduler define \r
152         configUSE_PREEMPTION as 0. */\r
153         vTaskStartScheduler();\r
154 \r
155         /* Should never get here! */\r
156         return 0;\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 /*\r
161  * Cycle for ever, delaying then checking all the other tasks are still\r
162  * operating without error.  If an error is detected then the delay period\r
163  * is decreased from mainCHECK_PERIOD to mainERROR_CHECK_PERIOD so\r
164  * the on board LED flash rate will increase. \r
165  *\r
166  * In addition to the standard tests the memory allocator is tested through\r
167  * the dynamic creation and deletion of a task each cycle.  Each time the \r
168  * task is created memory must be allocated for its stack.  When the task is\r
169  * deleted this memory is returned to the heap.  If the task cannot be created \r
170  * then it is likely that the memory allocation failed.   In addition the\r
171  * dynamically created task allocates and frees memory while it runs. \r
172  */\r
173 static void vErrorChecks( void *pvParameters )\r
174 {\r
175 portTickType xDelayPeriod = mainCHECK_PERIOD;\r
176 volatile unsigned portLONG ulMemCheckTaskRunningCount;\r
177 xTaskHandle xCreatedTask;\r
178 portTickType xLastWakeTime;\r
179 \r
180         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
181         functions correctly. */\r
182         xLastWakeTime = xTaskGetTickCount();\r
183 \r
184         for( ;; )\r
185         {\r
186                 /* Set ulMemCheckTaskRunningCount to a known value so we can check\r
187                 later that it has changed. */\r
188                 ulMemCheckTaskRunningCount = mainCOUNT_INITIAL_VALUE;\r
189 \r
190                 /* Dynamically create a task - passing ulMemCheckTaskRunningCount as a \r
191                 parameter. */           \r
192                 xCreatedTask = mainNO_TASK;\r
193                 if( xTaskCreate( vMemCheckTask, ( signed portCHAR * ) "MEM_CHECK", configMINIMAL_STACK_SIZE, ( void * ) &ulMemCheckTaskRunningCount, tskIDLE_PRIORITY, &xCreatedTask ) != pdPASS )\r
194                 {\r
195                         /* Could not create the task - we have probably run out of heap. */\r
196                         xDelayPeriod = mainERROR_CHECK_PERIOD;\r
197                 }\r
198 \r
199 \r
200                 /* Delay until it is time to execute again.  The delay period is \r
201                 shorter following an error. */\r
202                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );\r
203 \r
204         \r
205                 /* Delete the dynamically created task. */\r
206                 if( xCreatedTask != mainNO_TASK )\r
207                 {\r
208                         vTaskDelete( xCreatedTask );\r
209                 }\r
210 \r
211                 /* Check all the standard demo application tasks are executing without \r
212                 error.  ulMemCheckTaskRunningCount is checked to ensure it was\r
213                 modified by the task just deleted. */\r
214                 if( prvCheckOtherTasksAreStillRunning( ulMemCheckTaskRunningCount ) != pdPASS )\r
215                 {\r
216                         /* An error has been detected in one of the tasks - flash faster. */\r
217                         xDelayPeriod = mainERROR_CHECK_PERIOD;\r
218                 }\r
219 \r
220                 vParTestToggleLED( mainCHECK_TASK_LED );\r
221         }\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 /*\r
226  *      Check each set of tasks in turn to see if they have experienced any\r
227  *      error conditions. \r
228  */\r
229 static portLONG prvCheckOtherTasksAreStillRunning( unsigned portLONG ulMemCheckTaskCount )\r
230 {\r
231 portLONG lNoErrorsDiscovered = ( portLONG ) pdTRUE;\r
232 \r
233         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
234         {\r
235                 lNoErrorsDiscovered = pdFALSE;\r
236         }\r
237 \r
238         if( xAreComTestTasksStillRunning() != pdTRUE )\r
239         {\r
240                 lNoErrorsDiscovered = pdFALSE;\r
241         }\r
242 \r
243         if( xArePollingQueuesStillRunning() != pdTRUE )\r
244         {\r
245                 lNoErrorsDiscovered = pdFALSE;\r
246         }\r
247 \r
248         if( xAreMathsTaskStillRunning() != pdTRUE )\r
249         {\r
250                 lNoErrorsDiscovered = pdFALSE;\r
251         }\r
252 \r
253         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
254         {\r
255                 lNoErrorsDiscovered = pdFALSE;\r
256         }\r
257 \r
258         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
259         {\r
260                 lNoErrorsDiscovered = pdFALSE;\r
261         }\r
262 \r
263         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
264         {\r
265                 lNoErrorsDiscovered = pdFALSE;\r
266         }\r
267 \r
268         if( ulMemCheckTaskCount == mainCOUNT_INITIAL_VALUE )\r
269         {\r
270                 /* The vMemCheckTask task did not increment the counter - it must\r
271                 have failed. */\r
272                 lNoErrorsDiscovered = pdFALSE;\r
273         }\r
274 \r
275         return lNoErrorsDiscovered;\r
276 }\r
277 /*-----------------------------------------------------------*/\r
278 \r
279 static void vMemCheckTask( void *pvParameters )\r
280 {\r
281 unsigned portLONG *pulMemCheckTaskRunningCounter;\r
282 void *pvMem1, *pvMem2, *pvMem3;\r
283 static portLONG lErrorOccurred = pdFALSE;\r
284 \r
285         /* This task is dynamically created then deleted during each cycle of the\r
286         vErrorChecks task to check the operation of the memory allocator.  Each time\r
287         the task is created memory is allocated for the stack and TCB.  Each time\r
288         the task is deleted this memory is returned to the heap.  This task itself\r
289         exercises the allocator by allocating and freeing blocks. \r
290         \r
291         The task executes at the idle priority so does not require a delay. \r
292         \r
293         pulMemCheckTaskRunningCounter is incremented each cycle to indicate to the\r
294         vErrorChecks() task that this task is still executing without error. */\r
295 \r
296         pulMemCheckTaskRunningCounter = ( unsigned portLONG * ) pvParameters;\r
297 \r
298         for( ;; )\r
299         {\r
300                 if( lErrorOccurred == pdFALSE )\r
301                 {\r
302                         /* We have never seen an error so increment the counter. */\r
303                         ( *pulMemCheckTaskRunningCounter )++;\r
304                 }\r
305                 else\r
306                 {\r
307                         /* Reset the count so an error is detected by the \r
308                         prvCheckOtherTasksAreStillRunning() function. */\r
309                         *pulMemCheckTaskRunningCounter = mainCOUNT_INITIAL_VALUE;\r
310                 }\r
311 \r
312                 /* Allocate some memory - just to give the allocator some extra \r
313                 exercise.  This has to be in a critical section to ensure the\r
314                 task does not get deleted while it has memory allocated. */\r
315                 vTaskSuspendAll();\r
316                 {\r
317                         pvMem1 = pvPortMalloc( mainMEM_CHECK_SIZE_1 );\r
318                         if( pvMem1 == NULL )\r
319                         {\r
320                                 lErrorOccurred = pdTRUE;\r
321                         }\r
322                         else\r
323                         {\r
324                                 memset( pvMem1, 0xaa, mainMEM_CHECK_SIZE_1 );\r
325                                 vPortFree( pvMem1 );\r
326                         }\r
327                 }\r
328                 xTaskResumeAll();\r
329 \r
330                 /* Again - with a different size block. */\r
331                 vTaskSuspendAll();\r
332                 {\r
333                         pvMem2 = pvPortMalloc( mainMEM_CHECK_SIZE_2 );\r
334                         if( pvMem2 == NULL )\r
335                         {\r
336                                 lErrorOccurred = pdTRUE;\r
337                         }\r
338                         else\r
339                         {\r
340                                 memset( pvMem2, 0xaa, mainMEM_CHECK_SIZE_2 );\r
341                                 vPortFree( pvMem2 );\r
342                         }\r
343                 }\r
344                 xTaskResumeAll();\r
345 \r
346                 /* Again - with a different size block. */\r
347                 vTaskSuspendAll();\r
348                 {\r
349                         pvMem3 = pvPortMalloc( mainMEM_CHECK_SIZE_3 );\r
350                         if( pvMem3 == NULL )\r
351                         {\r
352                                 lErrorOccurred = pdTRUE;\r
353                         }\r
354                         else\r
355                         {\r
356                                 memset( pvMem3, 0xaa, mainMEM_CHECK_SIZE_3 );\r
357                                 vPortFree( pvMem3 );\r
358                         }\r
359                 }\r
360                 xTaskResumeAll();\r
361         }\r
362 }\r
363 /*-----------------------------------------------------------*/\r
364 \r
365 /*\r
366  * Called by the startup code.  Initial processor setup can be placed in this\r
367  * function.\r
368  */\r
369 void hw_initialise (void)\r
370 {\r
371 }\r
372 \r