]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/HCS12_CodeWarrior_banked/main.c
9e16bcaf2a12559b5650bf2f7f4d87b1f13e7caf
[freertos] / FreeRTOS / Demo / HCS12_CodeWarrior_banked / main.c
1 \r
2 /*\r
3  * FreeRTOS Kernel V10.2.1\r
4  * Copyright (C) 2019 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
5  *\r
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
7  * this software and associated documentation files (the "Software"), to deal in\r
8  * the Software without restriction, including without limitation the rights to\r
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
10  * the Software, and to permit persons to whom the Software is furnished to do so,\r
11  * subject to the following conditions:\r
12  *\r
13  * The above copyright notice and this permission notice shall be included in all\r
14  * copies or substantial portions of the Software.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 \r
30 /*\r
31  *\r
32  * vMain() is effectively the demo application entry point.  It is called by\r
33  * the main() function generated by the Processor Expert application.  \r
34  *\r
35  * vMain() creates all the demo application tasks, then starts the scheduler.\r
36  * The WEB      documentation provides more details of the demo application tasks.\r
37  *\r
38  * Main.c also creates a task called "Check".  This only executes every three \r
39  * seconds but has the highest priority so is guaranteed to get processor time.  \r
40  * Its main function is to check that all the other tasks are still operational.\r
41  * Each task (other than the "flash" tasks) maintains a unique count that is \r
42  * incremented each time the task successfully completes its function.  Should \r
43  * any error occur within such a task the count is permanently halted.  The \r
44  * check task inspects the count of each task to ensure it has changed since\r
45  * the last time the check task executed.  If all the count variables have \r
46  * changed all the tasks are still executing error free, and the check task\r
47  * toggles the onboard LED.  Should any task contain an error at any time \r
48  * the LED toggle rate will change from 3 seconds to 500ms.\r
49  *\r
50  * This file also includes the functionality implemented within the \r
51  * standard demo application file integer.c.  This is done to demonstrate the\r
52  * use of an idle hook.  See the documentation within integer.c for the \r
53  * rationale of the integer task functionality.\r
54  * */\r
55 \r
56 /* Kernel includes. */\r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 #include "queue.h"\r
60 \r
61 /* Demo application includes. */\r
62 #include "flash.h"\r
63 #include "PollQ.h"\r
64 #include "dynamic.h"\r
65 #include "partest.h"\r
66 #include "comtest2.h"\r
67 #include "BlockQ.h"\r
68 #include "integer.h"\r
69 #include "death.h"\r
70 \r
71 \r
72 /*-----------------------------------------------------------\r
73         Definitions.\r
74 -----------------------------------------------------------*/\r
75 \r
76 /* Priorities assigned to demo application tasks. */\r
77 #define mainFLASH_PRIORITY                      ( tskIDLE_PRIORITY + 2 )\r
78 #define mainCHECK_TASK_PRIORITY         ( tskIDLE_PRIORITY + 3 )\r
79 #define mainQUEUE_POLL_PRIORITY         ( tskIDLE_PRIORITY + 1 )\r
80 #define mainCOM_TEST_PRIORITY           ( tskIDLE_PRIORITY + 2 )\r
81 #define mainBLOCK_Q_PRIORITY            ( tskIDLE_PRIORITY + 2 )\r
82 #define mainDEATH_PRIORITY                      ( tskIDLE_PRIORITY + 1 )\r
83 \r
84 /* LED that is toggled by the check task.  The check task periodically checks\r
85 that all the other tasks are operating without error.  If no errors are found\r
86 the LED is toggled with mainCHECK_PERIOD frequency.  If an error is found \r
87 then the toggle rate increases to mainERROR_CHECK_PERIOD. */\r
88 #define mainCHECK_TASK_LED                      ( 7 )\r
89 #define mainCHECK_PERIOD                        ( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )\r
90 #define mainERROR_CHECK_PERIOD          ( ( TickType_t ) 500 / portTICK_PERIOD_MS )\r
91 \r
92 /* The constants used in the idle task calculation. */\r
93 #define intgCONST1                              ( ( long ) 123 )\r
94 #define intgCONST2                              ( ( long ) 234567 )\r
95 #define intgCONST3                              ( ( long ) -3 )\r
96 #define intgCONST4                              ( ( long ) 7 )\r
97 #define intgEXPECTED_ANSWER             ( ( ( intgCONST1 + intgCONST2 ) * intgCONST3 ) / intgCONST4 )\r
98 \r
99 \r
100 /* Baud rate used by the serial port tasks (ComTest tasks).\r
101 IMPORTANT:  The function COM0_SetBaudRateValue() which is generated by the\r
102 Processor Expert is used to set the baud rate.  As configured in the FreeRTOS\r
103 download this value must be one of the following:\r
104 \r
105 0 to configure for 38400 baud.\r
106 1 to configure for 19200 baud.\r
107 2 to configure for 9600 baud.\r
108 3 to configure for 4800 baud. */\r
109 #define mainCOM_TEST_BAUD_RATE                  ( ( unsigned long ) 2 )\r
110 \r
111 /* LED used by the serial port tasks.  This is toggled on each character Tx,\r
112 and mainCOM_TEST_LED + 1 is toggles on each character Rx. */\r
113 #define mainCOM_TEST_LED                                ( 3 )\r
114 \r
115 /*-----------------------------------------------------------\r
116         Local functions prototypes.\r
117 -----------------------------------------------------------*/\r
118 \r
119 /*\r
120  * The 'Check' task function.  See the explanation at the top of the file.\r
121  */\r
122 static void vErrorChecks( void* pvParameters );\r
123 \r
124 /*\r
125  * The idle task hook - in which the integer task is implemented.  See the\r
126  * explanation at the top of the file.\r
127  */\r
128 void vApplicationIdleHook( void );\r
129 \r
130 /*\r
131  * Checks the unique counts of other tasks to ensure they are still operational.\r
132  */\r
133 static long prvCheckOtherTasksAreStillRunning( void );\r
134 \r
135 \r
136 \r
137 /*-----------------------------------------------------------\r
138         Local variables.\r
139 -----------------------------------------------------------*/\r
140 \r
141 /* A few tasks are defined within this file.  This flag is used to indicate\r
142 their status.  If an error is detected in one of the locally defined tasks then\r
143 this flag is set to pdTRUE. */\r
144 portBASE_TYPE xLocalError = pdFALSE;\r
145 \r
146 \r
147 /*-----------------------------------------------------------*/\r
148 \r
149 /* \r
150  * This is called from the main() function generated by the Processor Expert.\r
151  */\r
152 void vMain( void )\r
153 {\r
154         /* Start some of the standard demo tasks. */\r
155         vStartLEDFlashTasks( mainFLASH_PRIORITY );\r
156         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
157         vStartDynamicPriorityTasks();\r
158         vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED );\r
159         vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );\r
160         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
161                 \r
162         /* Start the locally defined tasks.  There is also a task implemented as\r
163         the idle hook. */\r
164         xTaskCreate( vErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );\r
165         \r
166         /* Must be the last demo created. */\r
167         vCreateSuicidalTasks( mainDEATH_PRIORITY );\r
168 \r
169         /* All the tasks have been created - start the scheduler. */\r
170         vTaskStartScheduler();\r
171         \r
172         /* Should not reach here! */\r
173         for( ;; );\r
174 }\r
175 /*-----------------------------------------------------------*/\r
176 \r
177 static void vErrorChecks( void *pvParameters )\r
178 {\r
179 TickType_t xDelayPeriod = mainCHECK_PERIOD;\r
180 TickType_t xLastWakeTime;\r
181 \r
182         /* Initialise xLastWakeTime to ensure the first call to vTaskDelayUntil()\r
183         functions correctly. */\r
184         xLastWakeTime = xTaskGetTickCount();\r
185 \r
186         for( ;; )\r
187         {\r
188                 /* Delay until it is time to execute again.  The delay period is \r
189                 shorter following an error. */\r
190                 vTaskDelayUntil( &xLastWakeTime, xDelayPeriod );\r
191 \r
192                 /* Check all the demo application tasks are executing without \r
193                 error. If an error is found the delay period is shortened - this\r
194                 has the effect of increasing the flash rate of the 'check' task\r
195                 LED. */\r
196                 if( prvCheckOtherTasksAreStillRunning() == pdFAIL )\r
197                 {\r
198                         /* An error has been detected in one of the tasks - flash faster. */\r
199                         xDelayPeriod = mainERROR_CHECK_PERIOD;\r
200                 }\r
201 \r
202                 /* Toggle the LED each cycle round. */\r
203                 vParTestToggleLED( mainCHECK_TASK_LED );\r
204         }\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 static long prvCheckOtherTasksAreStillRunning( void )\r
209 {\r
210 portBASE_TYPE xAllTasksPassed = pdPASS;\r
211 \r
212         if( xArePollingQueuesStillRunning() != pdTRUE )\r
213         {\r
214                 xAllTasksPassed = pdFAIL;\r
215         }\r
216 \r
217         if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )\r
218         {\r
219                 xAllTasksPassed = pdFAIL;\r
220         }\r
221 \r
222         if( xAreComTestTasksStillRunning() != pdTRUE )\r
223         {\r
224                 xAllTasksPassed = pdFALSE;\r
225         }\r
226 \r
227         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
228         {\r
229                 xAllTasksPassed = pdFALSE;\r
230         }\r
231         \r
232         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
233         {\r
234                 xAllTasksPassed = pdFALSE;\r
235         }       \r
236 \r
237     if( xIsCreateTaskStillRunning() != pdTRUE )\r
238     {\r
239         xAllTasksPassed = pdFALSE;\r
240     }\r
241 \r
242         /* Also check the status flag for the tasks defined within this function. */\r
243         if( xLocalError != pdFALSE )\r
244         {\r
245                 xAllTasksPassed = pdFAIL;\r
246         }\r
247         \r
248         return xAllTasksPassed;\r
249 }\r
250 /*-----------------------------------------------------------*/\r
251 \r
252 void vApplicationIdleHook( void )\r
253 {\r
254 /* This variable is effectively set to a constant so it is made volatile to\r
255 ensure the compiler does not just get rid of it. */\r
256 volatile long lValue;\r
257 \r
258         /* Keep performing a calculation and checking the result against a constant. */\r
259 \r
260         /* Perform the calculation.  This will store partial value in\r
261         registers, resulting in a good test of the context switch mechanism. */\r
262         lValue = intgCONST1;\r
263         lValue += intgCONST2;\r
264         lValue *= intgCONST3;\r
265         lValue /= intgCONST4;\r
266 \r
267         /* Did we perform the calculation correctly with no corruption? */\r
268         if( lValue != intgEXPECTED_ANSWER )\r
269         {\r
270                 /* Error! */\r
271                 portENTER_CRITICAL();\r
272                         xLocalError = pdTRUE;\r
273                 portEXIT_CRITICAL();\r
274         }\r
275 \r
276         /* Yield in case cooperative scheduling is being used. */\r
277         #if configUSE_PREEMPTION == 0\r
278         {\r
279                 taskYIELD();\r
280         }\r
281         #endif          \r
282 }\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 \r
286 \r