]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/HCS12_GCC_banked/main.c
Update version number to 9.0.0rc2.
[freertos] / FreeRTOS / Demo / HCS12_GCC_banked / main.c
1 \r
2 /*\r
3     FreeRTOS V9.0.0rc2 - Copyright (C) 2016 Real Time Engineers Ltd.\r
4     All rights reserved\r
5 \r
6     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
7 \r
8     This file is part of the FreeRTOS distribution.\r
9 \r
10     FreeRTOS is free software; you can redistribute it and/or modify it under\r
11     the terms of the GNU General Public License (version 2) as published by the\r
12     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
13 \r
14     ***************************************************************************\r
15     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
16     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
17     >>!   obliged to provide the source code for proprietary components     !<<\r
18     >>!   outside of the FreeRTOS kernel.                                   !<<\r
19     ***************************************************************************\r
20 \r
21     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
22     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
23     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
24     link: http://www.freertos.org/a00114.html\r
25 \r
26     ***************************************************************************\r
27      *                                                                       *\r
28      *    FreeRTOS provides completely free yet professionally developed,    *\r
29      *    robust, strictly quality controlled, supported, and cross          *\r
30      *    platform software that is more than just the market leader, it     *\r
31      *    is the industry's de facto standard.                               *\r
32      *                                                                       *\r
33      *    Help yourself get started quickly while simultaneously helping     *\r
34      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
35      *    tutorial book, reference manual, or both:                          *\r
36      *    http://www.FreeRTOS.org/Documentation                              *\r
37      *                                                                       *\r
38     ***************************************************************************\r
39 \r
40     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
41     the FAQ page "My application does not run, what could be wrong?".  Have you\r
42     defined configASSERT()?\r
43 \r
44     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
45     embedded software for free we request you assist our global community by\r
46     participating in the support forum.\r
47 \r
48     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
49     be as productive as possible as early as possible.  Now you can receive\r
50     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
51     Ltd, and the world's leading authority on the world's leading RTOS.\r
52 \r
53     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
54     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
55     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
56 \r
57     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
58     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
59 \r
60     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
61     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
62     licenses offer ticketed support, indemnification and commercial middleware.\r
63 \r
64     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
65     engineered and independently SIL3 certified version for use in safety and\r
66     mission critical applications that require provable dependability.\r
67 \r
68     1 tab == 4 spaces!\r
69 */\r
70 \r
71 \r
72 /*\r
73  *\r
74  * main() creates all the demo application tasks, then starts the scheduler.\r
75  * The WEB      documentation provides more details of the demo application tasks.\r
76  *\r
77  * main.c also creates a task called "Check".  This only executes every three \r
78  * seconds but has the highest priority so is guaranteed to get processor time.  \r
79  * Its main function is to check that all the other tasks are still operational.\r
80  * Each task (other than the "flash" tasks) maintains a unique count that is \r
81  * incremented each time the task successfully completes its function.  Should \r
82  * any error occur within such a task the count is permanently halted.  The \r
83  * check task inspects the count of each task to ensure it has changed since\r
84  * the last time the check task executed.  If all the count variables have \r
85  * changed all the tasks are still executing error free, and the check task\r
86  * toggles the onboard LED.  Should any task contain an error at any time \r
87  * the LED toggle rate will change from 3 seconds to 500ms.\r
88  *\r
89  * This file also includes the functionality implemented within the \r
90  * standard demo application file integer.c.  This is done to demonstrate the\r
91  * use of an idle hook.  See the documentation within integer.c for the \r
92  * rationale of the integer task functionality.\r
93  * */\r
94 \r
95 /* Kernel includes. */\r
96 #include "FreeRTOS.h"\r
97 #include "task.h"\r
98 #include "queue.h"\r
99 \r
100 #include "cpu.h"\r
101 \r
102 /* special prototypes for memory-banked functions */\r
103 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority );\r
104 portBASE_TYPE xArePollingQueuesStillRunning( void );\r
105 \r
106 /* Demo application includes. */\r
107 #include "flash.h"\r
108 #include "PollQ.h"\r
109 #include "dynamic.h"\r
110 #include "partest.h"\r
111 #include "comtest2.h"\r
112 #include "BlockQ.h"\r
113 #include "integer.h"\r
114 #include "death.h"\r
115 \r
116 \r
117 /*-----------------------------------------------------------\r
118         Definitions.\r
119 -----------------------------------------------------------*/\r
120 \r
121 /* Priorities assigned to demo application tasks. */\r
122 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
123 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
124 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
125 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
126 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
127 #define mainDEATH_PRIORITY                      ( tskIDLE_PRIORITY + 1 )\r
128 \r
129 /* LED that is toggled by the check task.  The check task periodically checks\r
130 that all the other tasks are operating without error.  If no errors are found\r
131 the LED is toggled with mainCHECK_PERIOD frequency.  If an error is found \r
132 then the toggle rate increases to mainERROR_CHECK_PERIOD. */\r
133 #define mainCHECK_TASK_LED                      ( 7 )\r
134 #define mainCHECK_PERIOD                        ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
135 #define mainERROR_CHECK_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS )\r
136 \r
137 /* The constants used in the idle task calculation. */\r
138 #define intgCONST1                              ( ( long ) 123 )\r
139 #define intgCONST2                              ( ( long ) 234567 )\r
140 #define intgCONST3                              ( ( long ) -3 )\r
141 #define intgCONST4                              ( ( long ) 7 )\r
142 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
143 \r
144 \r
145 /* Baud rate used by the serial port tasks (ComTest tasks).\r
146 IMPORTANT:  The function COM0_SetBaudRateValue() which is generated by the\r
147 Processor Expert is used to set the baud rate.  As configured in the FreeRTOS\r
148 download this value must be one of the following:\r
149 \r
150 0 to configure for 38400 baud.\r
151 1 to configure for 19200 baud.\r
152 2 to configure for 9600 baud.\r
153 3 to configure for 4800 baud. */\r
154 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 2 )\r
155 \r
156 /* LED used by the serial port tasks.  This is toggled on each character Tx,\r
157 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */\r
158 #define mainCOM_TEST_LED                                ( 3 )\r
159 \r
160 /*-----------------------------------------------------------\r
161         Local functions prototypes.\r
162 -----------------------------------------------------------*/\r
163 \r
164 /*\r
165  * The 'Check' task function.  See the explanation at the top of the file.\r
166  */\r
167 static void ATTR_BANK1 vErrorChecks( void* pvParameters );\r
168 \r
169 /*\r
170  * The idle task hook - in which the integer task is implemented.  See the\r
171  * explanation at the top of the file.\r
172  */\r
173 void ATTR_BANK0 vApplicationIdleHook( void );\r
174 \r
175 /*\r
176  * Checks the unique counts of other tasks to ensure they are still operational.\r
177  */\r
178 static long ATTR_BANK0 prvCheckOtherTasksAreStillRunning( void );\r
179 \r
180 \r
181 \r
182 /*-----------------------------------------------------------\r
183         Local variables.\r
184 -----------------------------------------------------------*/\r
185 \r
186 /* A few tasks are defined within this file.  This flag is used to indicate\r
187 their status.  If an error is detected in one of the locally defined tasks then\r
188 this flag is set to pdTRUE. */\r
189 portBASE_TYPE xLocalError = pdFALSE;\r
190 \r
191 \r
192 /*-----------------------------------------------------------*/\r
193 \r
194 /* This is called from startup. */\r
195 int ATTR_BANK0 main ( void )\r
196 {\r
197         /* Start some of the standard demo tasks. */\r
198         vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
199         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
200         vStartDynamicPriorityTasks();\r
201         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
202         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
203         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
204         \r
205         /* Start the locally defined tasks.  There is also a task implemented as\r
206         the idle hook. */\r
207         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
208         \r
209         /* Must be the last demo created. */\r
210         vCreateSuicidalTasks( mainDEATH_PRIORITY );\r
211 \r
212         /* All the tasks have been created - start the scheduler. */\r
213         vTaskStartScheduler();\r
214         \r
215         /* Should not reach here! */\r
216         for( ;; );\r
217         return 0;\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 static void vErrorChecks( void *pvParameters )\r
222 {\r
223 TickType_t xDelayPeriod = mainCHECK_PERIOD;\r
224 TickType_t xLastWakeTime;\r
225 \r
226         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
227         functions correctly. */\r
228         xLastWakeTime = xTaskGetTickCount();\r
229 \r
230         for( ;; )\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                 /* Check all the demo application tasks are executing without \r
237                 error. If an error is found the delay period is shortened - this\r
238                 has the effect of increasing the flash rate of the 'check' task\r
239                 LED. */\r
240                 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )\r
241                 {\r
242                         /* An error has been detected in one of the tasks - flash faster. */\r
243                         xDelayPeriod = mainERROR_CHECK_PERIOD;\r
244                 }\r
245 \r
246                 /* Toggle the LED each cycle round. */\r
247                 vParTestToggleLED( mainCHECK_TASK_LED );\r
248         }\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 static long prvCheckOtherTasksAreStillRunning( void )\r
253 {\r
254 portBASE_TYPE xAllTasksPassed = pdPASS;\r
255 \r
256         if( xArePollingQueuesStillRunning() != pdTRUE )\r
257         {\r
258                 xAllTasksPassed = pdFAIL;\r
259         }\r
260 \r
261         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
262         {\r
263                 xAllTasksPassed = pdFAIL;\r
264         }\r
265 \r
266         if( xAreComTestTasksStillRunning() != pdTRUE )\r
267         {\r
268                 xAllTasksPassed = pdFALSE;\r
269         }\r
270 \r
271         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
272         {\r
273                 xAllTasksPassed = pdFALSE;\r
274         }\r
275         \r
276         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
277         {\r
278                 xAllTasksPassed = pdFALSE;\r
279         }       \r
280 \r
281     if( xIsCreateTaskStillRunning() != pdTRUE )\r
282     {\r
283         xAllTasksPassed = pdFALSE;\r
284     }\r
285 \r
286         /* Also check the status flag for the tasks defined within this function. */\r
287         if( xLocalError != pdFALSE )\r
288         {\r
289                 xAllTasksPassed = pdFAIL;\r
290         }\r
291         \r
292         return xAllTasksPassed;\r
293 }\r
294 /*-----------------------------------------------------------*/\r
295 \r
296 void vApplicationIdleHook( void )\r
297 {\r
298 /* This variable is effectively set to a constant so it is made volatile to\r
299 ensure the compiler does not just get rid of it. */\r
300 volatile long lValue;\r
301 \r
302         /* Keep performing a calculation and checking the result against a constant. */\r
303 \r
304         /* Perform the calculation.  This will store partial value in\r
305         registers, resulting in a good test of the context switch mechanism. */\r
306         lValue = intgCONST1;\r
307         lValue += intgCONST2;\r
308         lValue *= intgCONST3;\r
309         lValue /= intgCONST4;\r
310 \r
311         /* Did we perform the calculation correctly with no corruption? */\r
312         if( lValue != intgEXPECTED_ANSWER )\r
313         {\r
314                 /* Error! */\r
315                 portENTER_CRITICAL();\r
316                         xLocalError = pdTRUE;\r
317                 portEXIT_CRITICAL();\r
318         }\r
319 \r
320         /* Yield in case cooperative scheduling is being used. */\r
321         #if configUSE_PREEMPTION == 0\r
322         {\r
323                 taskYIELD();\r
324         }\r
325         #endif          \r
326 }\r
327 /*-----------------------------------------------------------*/\r
328 \r