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