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