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