]> git.sur5r.net Git - freertos/blob - Demo/HCS12_GCC_banked/main.c
Add STM32 Primer demo. Remove the .lock file from the Eclipse demos.
[freertos] / Demo / HCS12_GCC_banked / main.c
1 \r
2 /*\r
3         FreeRTOS.org V4.6.1 - Copyright (C) 2003-2007 Richard Barry.\r
4 \r
5         This file is part of the FreeRTOS.org distribution.\r
6 \r
7         FreeRTOS.org is free software; you can redistribute it and/or modify\r
8         it under the terms of the GNU General Public License as published by\r
9         the Free Software Foundation; either version 2 of the License, or\r
10         (at your option) any later version.\r
11 \r
12         FreeRTOS.org is distributed in the hope that it will be useful,\r
13         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
15         GNU General Public License for more details.\r
16 \r
17         You should have received a copy of the GNU General Public License\r
18         along with FreeRTOS.org; if not, write to the Free Software\r
19         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
20 \r
21         A special exception to the GPL can be applied should you wish to distribute\r
22         a combined work that includes FreeRTOS.org, without being obliged to provide\r
23         the source code for any proprietary components.  See the licensing section \r
24         of http://www.FreeRTOS.org for full details of how and when the exception\r
25         can be applied.\r
26 \r
27         ***************************************************************************\r
28         See http://www.FreeRTOS.org for documentation, latest information, license \r
29         and contact details.  Please ensure to read the configuration and relevant \r
30         port sections of the online documentation.\r
31 \r
32         Also see http://www.SafeRTOS.com a version that has been certified for use\r
33         in safety critical systems, plus commercial licensing, development and\r
34         support options.\r
35         ***************************************************************************\r
36 */\r
37 \r
38 \r
39 /*\r
40  *\r
41  * main() creates all the demo application tasks, then starts the scheduler.\r
42  * The WEB      documentation provides more details of the demo application tasks.\r
43  *\r
44  * main.c also creates a task called "Check".  This only executes every three \r
45  * seconds but has the highest priority so is guaranteed to get processor time.  \r
46  * Its main function is to check that all the other tasks are still operational.\r
47  * Each task (other than the "flash" tasks) maintains a unique count that is \r
48  * incremented each time the task successfully completes its function.  Should \r
49  * any error occur within such a task the count is permanently halted.  The \r
50  * check task inspects the count of each task to ensure it has changed since\r
51  * the last time the check task executed.  If all the count variables have \r
52  * changed all the tasks are still executing error free, and the check task\r
53  * toggles the onboard LED.  Should any task contain an error at any time \r
54  * the LED toggle rate will change from 3 seconds to 500ms.\r
55  *\r
56  * This file also includes the functionality implemented within the \r
57  * standard demo application file integer.c.  This is done to demonstrate the\r
58  * use of an idle hook.  See the documentation within integer.c for the \r
59  * rationale of the integer task functionality.\r
60  * */\r
61 \r
62 /* Kernel includes. */\r
63 #include "FreeRTOS.h"\r
64 #include "task.h"\r
65 #include "queue.h"\r
66 \r
67 #include "cpu.h"\r
68 \r
69 /* special prototypes for memory-banked functions */\r
70 void vStartPolledQueueTasks( unsigned portBASE_TYPE uxPriority );\r
71 portBASE_TYPE xArePollingQueuesStillRunning( void );\r
72 \r
73 /* Demo application includes. */\r
74 #include "flash.h"\r
75 #include "PollQ.h"\r
76 #include "dynamic.h"\r
77 #include "partest.h"\r
78 #include "comtest2.h"\r
79 #include "BlockQ.h"\r
80 #include "integer.h"\r
81 #include "death.h"\r
82 \r
83 \r
84 /*-----------------------------------------------------------\r
85         Definitions.\r
86 -----------------------------------------------------------*/\r
87 \r
88 /* Priorities assigned to demo application tasks. */\r
89 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
90 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
91 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
92 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
93 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
94 #define mainDEATH_PRIORITY                      ( tskIDLE_PRIORITY + 1 )\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 then the toggle rate increases to mainERROR_CHECK_PERIOD. */\r
100 #define mainCHECK_TASK_LED                      ( 7 )\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 /* The constants used in the idle task calculation. */\r
105 #define intgCONST1                              ( ( portLONG ) 123 )\r
106 #define intgCONST2                              ( ( portLONG ) 234567 )\r
107 #define intgCONST3                              ( ( portLONG ) -3 )\r
108 #define intgCONST4                              ( ( portLONG ) 7 )\r
109 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
110 \r
111 \r
112 /* Baud rate used by the serial port tasks (ComTest tasks).\r
113 IMPORTANT:  The function COM0_SetBaudRateValue() which is generated by the\r
114 Processor Expert is used to set the baud rate.  As configured in the FreeRTOS\r
115 download this value must be one of the following:\r
116 \r
117 0 to configure for 38400 baud.\r
118 1 to configure for 19200 baud.\r
119 2 to configure for 9600 baud.\r
120 3 to configure for 4800 baud. */\r
121 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned portLONG ) 2 )\r
122 \r
123 /* LED used by the serial port tasks.  This is toggled on each character Tx,\r
124 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */\r
125 #define mainCOM_TEST_LED                                ( 3 )\r
126 \r
127 /*-----------------------------------------------------------\r
128         Local functions prototypes.\r
129 -----------------------------------------------------------*/\r
130 \r
131 /*\r
132  * The 'Check' task function.  See the explanation at the top of the file.\r
133  */\r
134 static void ATTR_BANK1 vErrorChecks( void* pvParameters );\r
135 \r
136 /*\r
137  * The idle task hook - in which the integer task is implemented.  See the\r
138  * explanation at the top of the file.\r
139  */\r
140 void ATTR_BANK0 vApplicationIdleHook( void );\r
141 \r
142 /*\r
143  * Checks the unique counts of other tasks to ensure they are still operational.\r
144  */\r
145 static portLONG ATTR_BANK0 prvCheckOtherTasksAreStillRunning( void );\r
146 \r
147 \r
148 \r
149 /*-----------------------------------------------------------\r
150         Local variables.\r
151 -----------------------------------------------------------*/\r
152 \r
153 /* A few tasks are defined within this file.  This flag is used to indicate\r
154 their status.  If an error is detected in one of the locally defined tasks then\r
155 this flag is set to pdTRUE. */\r
156 portBASE_TYPE xLocalError = pdFALSE;\r
157 \r
158 \r
159 /*-----------------------------------------------------------*/\r
160 \r
161 /* This is called from startup. */\r
162 int ATTR_BANK0 main ( void )\r
163 {\r
164         /* Start some of the standard demo tasks. */\r
165         vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
166         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
167         vStartDynamicPriorityTasks();\r
168         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
169         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
170         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
171         \r
172         /* Start the locally defined tasks.  There is also a task implemented as\r
173         the idle hook. */\r
174         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
175         \r
176         /* Must be the last demo created. */\r
177         vCreateSuicidalTasks( mainDEATH_PRIORITY );\r
178 \r
179         /* All the tasks have been created - start the scheduler. */\r
180         vTaskStartScheduler();\r
181         \r
182         /* Should not reach here! */\r
183         for( ;; );\r
184         return 0;\r
185 }\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 static void vErrorChecks( void *pvParameters )\r
189 {\r
190 portTickType xDelayPeriod = mainCHECK_PERIOD;\r
191 portTickType xLastWakeTime;\r
192 \r
193         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
194         functions correctly. */\r
195         xLastWakeTime = xTaskGetTickCount();\r
196 \r
197         for( ;; )\r
198         {\r
199                 /* Delay until it is time to execute again.  The delay period is \r
200                 shorter following an error. */\r
201                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );\r
202 \r
203                 /* Check all the demo application tasks are executing without \r
204                 error. If an error is found the delay period is shortened - this\r
205                 has the effect of increasing the flash rate of the 'check' task\r
206                 LED. */\r
207                 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )\r
208                 {\r
209                         /* An error has been detected in one of the tasks - flash faster. */\r
210                         xDelayPeriod = mainERROR_CHECK_PERIOD;\r
211                 }\r
212 \r
213                 /* Toggle the LED each cycle round. */\r
214                 vParTestToggleLED( mainCHECK_TASK_LED );\r
215         }\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 static portLONG prvCheckOtherTasksAreStillRunning( void )\r
220 {\r
221 portBASE_TYPE xAllTasksPassed = pdPASS;\r
222 \r
223         if( xArePollingQueuesStillRunning() != pdTRUE )\r
224         {\r
225                 xAllTasksPassed = pdFAIL;\r
226         }\r
227 \r
228         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
229         {\r
230                 xAllTasksPassed = pdFAIL;\r
231         }\r
232 \r
233         if( xAreComTestTasksStillRunning() != pdTRUE )\r
234         {\r
235                 xAllTasksPassed = pdFALSE;\r
236         }\r
237 \r
238         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
239         {\r
240                 xAllTasksPassed = pdFALSE;\r
241         }\r
242         \r
243         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
244         {\r
245                 xAllTasksPassed = pdFALSE;\r
246         }       \r
247 \r
248     if( xIsCreateTaskStillRunning() != pdTRUE )\r
249     {\r
250         xAllTasksPassed = pdFALSE;\r
251     }\r
252 \r
253         /* Also check the status flag for the tasks defined within this function. */\r
254         if( xLocalError != pdFALSE )\r
255         {\r
256                 xAllTasksPassed = pdFAIL;\r
257         }\r
258         \r
259         return xAllTasksPassed;\r
260 }\r
261 /*-----------------------------------------------------------*/\r
262 \r
263 void vApplicationIdleHook( void )\r
264 {\r
265 /* This variable is effectively set to a constant so it is made volatile to\r
266 ensure the compiler does not just get rid of it. */\r
267 volatile portLONG lValue;\r
268 \r
269         /* Keep performing a calculation and checking the result against a constant. */\r
270 \r
271         /* Perform the calculation.  This will store partial value in\r
272         registers, resulting in a good test of the context switch mechanism. */\r
273         lValue = intgCONST1;\r
274         lValue += intgCONST2;\r
275         lValue *= intgCONST3;\r
276         lValue /= intgCONST4;\r
277 \r
278         /* Did we perform the calculation correctly with no corruption? */\r
279         if( lValue != intgEXPECTED_ANSWER )\r
280         {\r
281                 /* Error! */\r
282                 portENTER_CRITICAL();\r
283                         xLocalError = pdTRUE;\r
284                 portEXIT_CRITICAL();\r
285         }\r
286 \r
287         /* Yield in case cooperative scheduling is being used. */\r
288         #if configUSE_PREEMPTION == 0\r
289         {\r
290                 taskYIELD();\r
291         }\r
292         #endif          \r
293 }\r
294 /*-----------------------------------------------------------*/\r
295 \r