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