]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Flshlite/main.c
4a87a3dbe93cc45af3ba74a704cb1a8ebe72bcfd
[freertos] / FreeRTOS / Demo / Flshlite / main.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 /*\r
29  * Creates all the demo application tasks, then starts the scheduler.\r
30  *\r
31  * Main. c also creates a task called "Print".  This only executes every five \r
32  * seconds but has the highest priority so is guaranteed to get processor time.  \r
33  * Its main function is to check that all the other tasks are still operational.  \r
34  * Nearly all the tasks in the demo application maintain a unique count that is \r
35  * incremented each time the task successfully completes its function.  Should any \r
36  * error occur within the task the count is permanently halted.  The print task \r
37  * checks the count of each task to ensure it has changed since the last time the \r
38  * print task executed.  If any count is found not to have changed the print task\r
39  * displays an appropriate message, halts, and flashes the on board LED rapidly.\r
40  * If all the tasks are still incrementing their unique counts the print task\r
41  * displays an "OK" message.\r
42  *\r
43  * The LED flash tasks do not maintain a count as they already provide visual\r
44  * feedback of their status.\r
45  *\r
46  * The print task blocks on the queue into which messages that require displaying\r
47  * are posted.  It will therefore only block for the full 5 seconds if no messages\r
48  * are posted onto the queue.\r
49  *\r
50  * Main. c also provides a demonstration of how the trace visualisation utility can\r
51  * be used, and how the scheduler can be stopped.\r
52  *\r
53  * On the Flashlite it is preferable not to try to write to the console during\r
54  * real time operation.  The built in LED is toggled every cycle of the print task\r
55  * that does not encounter any errors, so the console IO may be removed if required.\r
56  * The build in LED will start flashing rapidly if any task reports an error.\r
57  */\r
58 \r
59 /*\r
60 Changes from V1.01:\r
61 \r
62         + Previously, if an error occurred in a task the on board LED was stopped from\r
63           toggling.  Now if an error occurs the check task enters an infinite loop,\r
64           toggling the LED rapidly.\r
65 \r
66 Changes from V1.2.3\r
67 \r
68         + The integer and comtest tasks are now used when the cooperative scheduler \r
69           is being used.  Previously they were only used with the preemptive\r
70           scheduler.\r
71 \r
72 Changes from V1.2.5\r
73 \r
74         + Made the communications RX task a higher priority.\r
75 \r
76 Changes from V2.0.0\r
77 \r
78         + Delay periods are now specified using variables and constants of\r
79           TickType_t rather than unsigned long.\r
80 */\r
81 \r
82 #include <stdlib.h>\r
83 #include <conio.h>\r
84 #include "FreeRTOS.h"\r
85 #include "task.h"\r
86 #include "partest.h"\r
87 #include "serial.h"\r
88 \r
89 /* Demo file headers. */\r
90 #include "BlockQ.h"\r
91 #include "PollQ.h"\r
92 #include "death.h"\r
93 #include "flash.h"\r
94 #include "integer.h"\r
95 #include "print.h"\r
96 #include "comtest.h"\r
97 #include "fileio.h"\r
98 #include "semtest.h"\r
99 \r
100 /* Priority definitions for all the tasks in the demo application. */\r
101 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
102 #define mainCREATOR_TASK_PRIORITY               ( tskIDLE_PRIORITY + 3 )\r
103 #define mainPRINT_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 5 )\r
104 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
105 #define mainQUEUE_BLOCK_PRIORITY                ( tskIDLE_PRIORITY + 3 )\r
106 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 3 )\r
107 #define mainSEMAPHORE_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )\r
108 \r
109 #define mainPRINT_STACK_SIZE            ( ( unsigned short ) 256 )\r
110 #define mainDEBUG_LOG_BUFFER_SIZE       ( ( unsigned short ) 20480 )\r
111 \r
112 /* Constant definitions for accessing the build in LED on the Flashlite 186. */\r
113 #define mainLED_REG_DIR                         ( ( unsigned short ) 0xff78 )\r
114 #define mainLED_REG                             ( ( unsigned short ) 0xff7a )\r
115 \r
116 /* If an error is detected in a task then the vErrorChecks() task will enter\r
117 an infinite loop flashing the LED at this rate. */\r
118 #define mainERROR_FLASH_RATE            ( ( TickType_t ) 100 / portTICK_PERIOD_MS )\r
119 \r
120 /* Task function for the "Print" task as described at the top of the file. */\r
121 static void vErrorChecks( void *pvParameters );\r
122 \r
123 /* Function that checks the unique count of all the other tasks as described at\r
124 the top of the file. */\r
125 static void prvCheckOtherTasksAreStillRunning( void );\r
126 \r
127 /* Functions to setup and use the built in LED on the Flashlite 186 board. */\r
128 static void prvToggleLED( void );\r
129 static void prvInitLED( void );\r
130 \r
131 /* Key presses can be used to start/stop the trace visualisation utility or stop\r
132 the scheduler. */\r
133 static void     prvCheckForKeyPresses( void );\r
134 \r
135 /* Buffer used by the trace visualisation utility. */\r
136 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];\r
137 \r
138 /*-----------------------------------------------------------*/\r
139 short main( void )\r
140 {\r
141         /* Initialise hardware and utilities. */\r
142         vParTestInitialise();\r
143         vPrintInitialise();\r
144         prvInitLED();\r
145 \r
146         /* CREATE ALL THE DEMO APPLICATION TASKS. */\r
147 \r
148         vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );\r
149         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
150         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
151         vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );\r
152         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
153         vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );\r
154 \r
155         /* Create the "Print" task as described at the top of the file. */\r
156         xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );\r
157 \r
158         /* This task has to be created last as it keeps account of the number of tasks\r
159         it expects to see running. */\r
160         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
161 \r
162         /* Set the scheduler running.  This function will not return unless a task\r
163         calls vTaskEndScheduler(). */\r
164         vTaskStartScheduler();\r
165 \r
166         return 1;\r
167 }\r
168 /*-----------------------------------------------------------*/\r
169 \r
170 static void vErrorChecks( void *pvParameters )\r
171 {\r
172 TickType_t xExpectedWakeTime;\r
173 const TickType_t xPrintRate = ( TickType_t ) 5000 / portTICK_PERIOD_MS;\r
174 const long lMaxAllowableTimeDifference = ( long ) 0;\r
175 TickType_t xWakeTime;\r
176 long lTimeDifference;\r
177 const char *pcReceivedMessage;\r
178 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";\r
179 \r
180         /* Stop warnings. */\r
181     ( void ) pvParameters;\r
182 \r
183         /* Loop continuously, blocking, then checking all the other tasks are still\r
184         running, before blocking once again.  This task blocks on the queue of messages\r
185         that require displaying so will wake either by its time out expiring, or a\r
186         message becoming available. */\r
187         for( ;; )\r
188         {\r
189                 /* Calculate the time we will unblock if no messages are received\r
190                 on the queue.  This is used to check that we have not blocked for too long. */\r
191                 xExpectedWakeTime = xTaskGetTickCount();\r
192                 xExpectedWakeTime += xPrintRate;\r
193 \r
194                 /* Block waiting for either a time out or a message to be posted that\r
195                 required displaying. */\r
196                 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );\r
197 \r
198                 /* Was a message received? */\r
199                 if( pcReceivedMessage == NULL )\r
200                 {\r
201                         /* A message was not received so we timed out, did we unblock at the\r
202                         expected time? */\r
203                         xWakeTime = xTaskGetTickCount();\r
204 \r
205                         /* Calculate the difference between the time we unblocked and the\r
206                         time we should have unblocked. */\r
207                         if( xWakeTime > xExpectedWakeTime )\r
208                         {\r
209                                 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );\r
210                         }\r
211                         else\r
212                         {\r
213                                 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );\r
214                         }\r
215 \r
216                         if( lTimeDifference > lMaxAllowableTimeDifference )\r
217                         {\r
218                                 /* We blocked too long - create a message that will get\r
219                                 printed out the next time around. */\r
220                                 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );\r
221                         }\r
222 \r
223                         /* Check the other tasks are still running, just in case. */\r
224                         prvCheckOtherTasksAreStillRunning();\r
225                 }\r
226                 else\r
227                 {\r
228                         /* We unblocked due to a message becoming available.  Send the message\r
229                         for printing. */\r
230                         vDisplayMessage( pcReceivedMessage );\r
231                 }\r
232 \r
233                 /* Key presses are used to invoke the trace visualisation utility, or\r
234                 end the program. */\r
235                 prvCheckForKeyPresses();\r
236         }\r
237 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */\r
238 /*-----------------------------------------------------------*/\r
239 \r
240 static void      prvCheckForKeyPresses( void )\r
241 {\r
242         #ifdef USE_STDIO\r
243 \r
244         short sIn;\r
245 \r
246         \r
247                 taskENTER_CRITICAL();\r
248                         sIn = kbhit();\r
249                 taskEXIT_CRITICAL();\r
250 \r
251                 if( sIn )\r
252                 {\r
253                         unsigned long ulBufferLength;\r
254 \r
255                         /* Key presses can be used to start/stop the trace utility, or end the\r
256                         program. */\r
257                         sIn = getch();\r
258                         switch( sIn )\r
259                         {\r
260                                 /* Only define keys for turning on and off the trace if the trace\r
261                                 is being used. */\r
262                                 #if configUSE_TRACE_FACILITY == 1\r
263                                         case 't' :      vTaskList( pcWriteBuffer );\r
264                                                                 vWriteMessageToDisk( pcWriteBuffer );\r
265                                                                 break;\r
266 \r
267                                         /* The legacy trace is no longer supported.  Use FreeRTOS+Trace instead.\r
268                                         case 's' :      vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );\r
269                                                                 break;\r
270 \r
271                                         case 'e' :      ulBufferLength = ulTaskEndTrace();\r
272                                                                 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );\r
273                                                                 break;*/\r
274                                 #endif\r
275 \r
276                                 default  :      vTaskEndScheduler();\r
277                                                         break;\r
278                         }\r
279                 }\r
280 \r
281         #else\r
282                 ( void ) pcWriteBuffer;\r
283         #endif\r
284 }\r
285 /*-----------------------------------------------------------*/\r
286 \r
287 static void prvCheckOtherTasksAreStillRunning( void )\r
288 {\r
289 short sErrorHasOccurred = pdFALSE;\r
290 \r
291         if( xAreComTestTasksStillRunning() != pdTRUE )\r
292         {\r
293                 vDisplayMessage( "Com test count unchanged!\r\n" );\r
294                 sErrorHasOccurred = pdTRUE;\r
295         }\r
296 \r
297         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
298         {\r
299                 vDisplayMessage( "Integer maths task count unchanged!\r\n" );\r
300                 sErrorHasOccurred = pdTRUE;\r
301         }\r
302 \r
303         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
304         {\r
305                 vDisplayMessage( "Blocking queues count unchanged!\r\n" );\r
306                 sErrorHasOccurred = pdTRUE;\r
307         }\r
308 \r
309         if( xArePollingQueuesStillRunning() != pdTRUE )\r
310         {\r
311                 vDisplayMessage( "Polling queue count unchanged!\r\n" );\r
312                 sErrorHasOccurred = pdTRUE;\r
313         }\r
314 \r
315         if( xIsCreateTaskStillRunning() != pdTRUE )\r
316         {\r
317                 vDisplayMessage( "Incorrect number of tasks running!\r\n" );\r
318                 sErrorHasOccurred = pdTRUE;\r
319         }\r
320 \r
321         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
322         {\r
323                 vDisplayMessage( "Semaphore take count unchanged!\r\n" );\r
324                 sErrorHasOccurred = pdTRUE;\r
325         }\r
326 \r
327         if( sErrorHasOccurred == pdFALSE )\r
328         {\r
329                 vDisplayMessage( "OK " );\r
330                 /* Toggle the LED if everything is okay so we know if an error occurs even if not\r
331                 using console IO. */\r
332                 prvToggleLED();\r
333         }\r
334         else\r
335         {\r
336                 for( ;; )\r
337                 {\r
338                         /* An error has occurred in one of the tasks.  Don't go any further and\r
339                         flash the LED rapidly in case console IO is not being used. */\r
340                         prvToggleLED();\r
341                         vTaskDelay( mainERROR_FLASH_RATE );\r
342                 }\r
343         }\r
344 }\r
345 /*-----------------------------------------------------------*/\r
346 \r
347 static void prvInitLED( void )\r
348 {\r
349 unsigned short usPortDirection;\r
350 const unsigned short usLEDOut = 0x400;\r
351 \r
352         /* Set the LED bit to an output. */\r
353 \r
354         usPortDirection = inpw( mainLED_REG_DIR );\r
355         usPortDirection &= ~usLEDOut;\r
356         outpw( mainLED_REG_DIR, usPortDirection );\r
357 }\r
358 /*-----------------------------------------------------------*/\r
359 \r
360 static void prvToggleLED( void )\r
361 {\r
362 static short sLED = pdTRUE;\r
363 unsigned short usLEDState;\r
364 const unsigned short usLEDBit = 0x400;\r
365 \r
366         /* Flip the state of the LED. */\r
367         usLEDState = inpw( mainLED_REG );\r
368         if( sLED )\r
369         {\r
370                 usLEDState &= ~usLEDBit;\r
371         }\r
372         else\r
373         {\r
374                 usLEDState |= usLEDBit;\r
375         }\r
376         outpw( mainLED_REG, usLEDState );\r
377 \r
378         sLED = !sLED;\r
379 }\r
380 \r
381 \r