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