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