]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Flshlite/main.c
Update version number in preparation for official V8.2.0 release.
[freertos] / FreeRTOS / Demo / Flshlite / main.c
1 /*\r
2     FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13         ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18         ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40         the FAQ page "My application does not run, what could be wrong?".  Have you\r
41         defined configASSERT()?\r
42 \r
43         http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44         embedded software for free we request you assist our global community by\r
45         participating in the support forum.\r
46 \r
47         http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48         be as productive as possible as early as possible.  Now you can receive\r
49         FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50         Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /*\r
71  * Creates all the demo application tasks, then starts the scheduler.\r
72  *\r
73  * Main. c also creates a task called "Print".  This only executes every five \r
74  * seconds but has the highest priority so is guaranteed to get processor time.  \r
75  * Its main function is to check that all the other tasks are still operational.  \r
76  * Nearly all the tasks in the demo application maintain a unique count that is \r
77  * incremented each time the task successfully completes its function.  Should any \r
78  * error occur within the task the count is permanently halted.  The print task \r
79  * checks the count of each task to ensure it has changed since the last time the \r
80  * print task executed.  If any count is found not to have changed the print task\r
81  * displays an appropriate message, halts, and flashes the on board LED rapidly.\r
82  * If all the tasks are still incrementing their unique counts the print task\r
83  * displays an "OK" message.\r
84  *\r
85  * The LED flash tasks do not maintain a count as they already provide visual\r
86  * feedback of their status.\r
87  *\r
88  * The print task blocks on the queue into which messages that require displaying\r
89  * are posted.  It will therefore only block for the full 5 seconds if no messages\r
90  * are posted onto the queue.\r
91  *\r
92  * Main. c also provides a demonstration of how the trace visualisation utility can\r
93  * be used, and how the scheduler can be stopped.\r
94  *\r
95  * On the Flashlite it is preferable not to try to write to the console during\r
96  * real time operation.  The built in LED is toggled every cycle of the print task\r
97  * that does not encounter any errors, so the console IO may be removed if required.\r
98  * The build in LED will start flashing rapidly if any task reports an error.\r
99  */\r
100 \r
101 /*\r
102 Changes from V1.01:\r
103 \r
104         + Previously, if an error occurred in a task the on board LED was stopped from\r
105           toggling.  Now if an error occurs the check task enters an infinite loop,\r
106           toggling the LED rapidly.\r
107 \r
108 Changes from V1.2.3\r
109 \r
110         + The integer and comtest tasks are now used when the cooperative scheduler \r
111           is being used.  Previously they were only used with the preemptive\r
112           scheduler.\r
113 \r
114 Changes from V1.2.5\r
115 \r
116         + Made the communications RX task a higher priority.\r
117 \r
118 Changes from V2.0.0\r
119 \r
120         + Delay periods are now specified using variables and constants of\r
121           TickType_t rather than unsigned long.\r
122 */\r
123 \r
124 #include <stdlib.h>\r
125 #include <conio.h>\r
126 #include "FreeRTOS.h"\r
127 #include "task.h"\r
128 #include "partest.h"\r
129 #include "serial.h"\r
130 \r
131 /* Demo file headers. */\r
132 #include "BlockQ.h"\r
133 #include "PollQ.h"\r
134 #include "death.h"\r
135 #include "flash.h"\r
136 #include "integer.h"\r
137 #include "print.h"\r
138 #include "comtest.h"\r
139 #include "fileio.h"\r
140 #include "semtest.h"\r
141 \r
142 /* Priority definitions for all the tasks in the demo application. */\r
143 #define mainLED_TASK_PRIORITY                   ( tskIDLE_PRIORITY + 1 )\r
144 #define mainCREATOR_TASK_PRIORITY               ( tskIDLE_PRIORITY + 3 )\r
145 #define mainPRINT_TASK_PRIORITY                 ( tskIDLE_PRIORITY + 5 )\r
146 #define mainQUEUE_POLL_PRIORITY                 ( tskIDLE_PRIORITY + 2 )\r
147 #define mainQUEUE_BLOCK_PRIORITY                ( tskIDLE_PRIORITY + 3 )\r
148 #define mainCOM_TEST_PRIORITY                   ( tskIDLE_PRIORITY + 3 )\r
149 #define mainSEMAPHORE_TASK_PRIORITY             ( tskIDLE_PRIORITY + 1 )\r
150 \r
151 #define mainPRINT_STACK_SIZE            ( ( unsigned short ) 256 )\r
152 #define mainDEBUG_LOG_BUFFER_SIZE       ( ( unsigned short ) 20480 )\r
153 \r
154 /* Constant definitions for accessing the build in LED on the Flashlite 186. */\r
155 #define mainLED_REG_DIR                         ( ( unsigned short ) 0xff78 )\r
156 #define mainLED_REG                             ( ( unsigned short ) 0xff7a )\r
157 \r
158 /* If an error is detected in a task then the vErrorChecks() task will enter\r
159 an infinite loop flashing the LED at this rate. */\r
160 #define mainERROR_FLASH_RATE            ( ( TickType_t ) 100 / portTICK_PERIOD_MS )\r
161 \r
162 /* Task function for the "Print" task as described at the top of the file. */\r
163 static void vErrorChecks( void *pvParameters );\r
164 \r
165 /* Function that checks the unique count of all the other tasks as described at\r
166 the top of the file. */\r
167 static void prvCheckOtherTasksAreStillRunning( void );\r
168 \r
169 /* Functions to setup and use the built in LED on the Flashlite 186 board. */\r
170 static void prvToggleLED( void );\r
171 static void prvInitLED( void );\r
172 \r
173 /* Key presses can be used to start/stop the trace visualisation utility or stop\r
174 the scheduler. */\r
175 static void     prvCheckForKeyPresses( void );\r
176 \r
177 /* Buffer used by the trace visualisation utility. */\r
178 static char pcWriteBuffer[ mainDEBUG_LOG_BUFFER_SIZE ];\r
179 \r
180 /*-----------------------------------------------------------*/\r
181 short main( void )\r
182 {\r
183         /* Initialise hardware and utilities. */\r
184         vParTestInitialise();\r
185         vPrintInitialise();\r
186         prvInitLED();\r
187 \r
188         /* CREATE ALL THE DEMO APPLICATION TASKS. */\r
189 \r
190         vStartComTestTasks( mainCOM_TEST_PRIORITY, serCOM2, ser38400 );\r
191         vStartIntegerMathTasks( tskIDLE_PRIORITY );\r
192         vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );\r
193         vStartBlockingQueueTasks( mainQUEUE_BLOCK_PRIORITY );\r
194         vStartLEDFlashTasks( mainLED_TASK_PRIORITY );\r
195         vStartSemaphoreTasks( mainSEMAPHORE_TASK_PRIORITY );\r
196 \r
197         /* Create the "Print" task as described at the top of the file. */\r
198         xTaskCreate( vErrorChecks, "Print", mainPRINT_STACK_SIZE, NULL, mainPRINT_TASK_PRIORITY, NULL );\r
199 \r
200         /* This task has to be created last as it keeps account of the number of tasks\r
201         it expects to see running. */\r
202         vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );\r
203 \r
204         /* Set the scheduler running.  This function will not return unless a task\r
205         calls vTaskEndScheduler(). */\r
206         vTaskStartScheduler();\r
207 \r
208         return 1;\r
209 }\r
210 /*-----------------------------------------------------------*/\r
211 \r
212 static void vErrorChecks( void *pvParameters )\r
213 {\r
214 TickType_t xExpectedWakeTime;\r
215 const TickType_t xPrintRate = ( TickType_t ) 5000 / portTICK_PERIOD_MS;\r
216 const long lMaxAllowableTimeDifference = ( long ) 0;\r
217 TickType_t xWakeTime;\r
218 long lTimeDifference;\r
219 const char *pcReceivedMessage;\r
220 const char * const pcTaskBlockedTooLongMsg = "Print task blocked too long!\r\n";\r
221 \r
222         /* Stop warnings. */\r
223     ( void ) pvParameters;\r
224 \r
225         /* Loop continuously, blocking, then checking all the other tasks are still\r
226         running, before blocking once again.  This task blocks on the queue of messages\r
227         that require displaying so will wake either by its time out expiring, or a\r
228         message becoming available. */\r
229         for( ;; )\r
230         {\r
231                 /* Calculate the time we will unblock if no messages are received\r
232                 on the queue.  This is used to check that we have not blocked for too long. */\r
233                 xExpectedWakeTime = xTaskGetTickCount();\r
234                 xExpectedWakeTime += xPrintRate;\r
235 \r
236                 /* Block waiting for either a time out or a message to be posted that\r
237                 required displaying. */\r
238                 pcReceivedMessage = pcPrintGetNextMessage( xPrintRate );\r
239 \r
240                 /* Was a message received? */\r
241                 if( pcReceivedMessage == NULL )\r
242                 {\r
243                         /* A message was not received so we timed out, did we unblock at the\r
244                         expected time? */\r
245                         xWakeTime = xTaskGetTickCount();\r
246 \r
247                         /* Calculate the difference between the time we unblocked and the\r
248                         time we should have unblocked. */\r
249                         if( xWakeTime > xExpectedWakeTime )\r
250                         {\r
251                                 lTimeDifference = ( long ) ( xWakeTime - xExpectedWakeTime );\r
252                         }\r
253                         else\r
254                         {\r
255                                 lTimeDifference = ( long ) ( xExpectedWakeTime - xWakeTime );\r
256                         }\r
257 \r
258                         if( lTimeDifference > lMaxAllowableTimeDifference )\r
259                         {\r
260                                 /* We blocked too long - create a message that will get\r
261                                 printed out the next time around. */\r
262                                 vPrintDisplayMessage( &pcTaskBlockedTooLongMsg );\r
263                         }\r
264 \r
265                         /* Check the other tasks are still running, just in case. */\r
266                         prvCheckOtherTasksAreStillRunning();\r
267                 }\r
268                 else\r
269                 {\r
270                         /* We unblocked due to a message becoming available.  Send the message\r
271                         for printing. */\r
272                         vDisplayMessage( pcReceivedMessage );\r
273                 }\r
274 \r
275                 /* Key presses are used to invoke the trace visualisation utility, or\r
276                 end the program. */\r
277                 prvCheckForKeyPresses();\r
278         }\r
279 } /*lint !e715 !e818 pvParameters is not used but all task functions must take this form. */\r
280 /*-----------------------------------------------------------*/\r
281 \r
282 static void      prvCheckForKeyPresses( void )\r
283 {\r
284         #ifdef USE_STDIO\r
285 \r
286         short sIn;\r
287 \r
288         \r
289                 taskENTER_CRITICAL();\r
290                         sIn = kbhit();\r
291                 taskEXIT_CRITICAL();\r
292 \r
293                 if( sIn )\r
294                 {\r
295                         unsigned long ulBufferLength;\r
296 \r
297                         /* Key presses can be used to start/stop the trace utility, or end the\r
298                         program. */\r
299                         sIn = getch();\r
300                         switch( sIn )\r
301                         {\r
302                                 /* Only define keys for turning on and off the trace if the trace\r
303                                 is being used. */\r
304                                 #if configUSE_TRACE_FACILITY == 1\r
305                                         case 't' :      vTaskList( pcWriteBuffer );\r
306                                                                 vWriteMessageToDisk( pcWriteBuffer );\r
307                                                                 break;\r
308 \r
309                                         /* The legacy trace is no longer supported.  Use FreeRTOS+Trace instead.\r
310                                         case 's' :      vTaskStartTrace( pcWriteBuffer, mainDEBUG_LOG_BUFFER_SIZE );\r
311                                                                 break;\r
312 \r
313                                         case 'e' :      ulBufferLength = ulTaskEndTrace();\r
314                                                                 vWriteBufferToDisk( pcWriteBuffer, ulBufferLength );\r
315                                                                 break;*/\r
316                                 #endif\r
317 \r
318                                 default  :      vTaskEndScheduler();\r
319                                                         break;\r
320                         }\r
321                 }\r
322 \r
323         #else\r
324                 ( void ) pcWriteBuffer;\r
325         #endif\r
326 }\r
327 /*-----------------------------------------------------------*/\r
328 \r
329 static void prvCheckOtherTasksAreStillRunning( void )\r
330 {\r
331 short sErrorHasOccurred = pdFALSE;\r
332 \r
333         if( xAreComTestTasksStillRunning() != pdTRUE )\r
334         {\r
335                 vDisplayMessage( "Com test count unchanged!\r\n" );\r
336                 sErrorHasOccurred = pdTRUE;\r
337         }\r
338 \r
339         if( xAreIntegerMathsTaskStillRunning() != pdTRUE )\r
340         {\r
341                 vDisplayMessage( "Integer maths task count unchanged!\r\n" );\r
342                 sErrorHasOccurred = pdTRUE;\r
343         }\r
344 \r
345         if( xAreBlockingQueuesStillRunning() != pdTRUE )\r
346         {\r
347                 vDisplayMessage( "Blocking queues count unchanged!\r\n" );\r
348                 sErrorHasOccurred = pdTRUE;\r
349         }\r
350 \r
351         if( xArePollingQueuesStillRunning() != pdTRUE )\r
352         {\r
353                 vDisplayMessage( "Polling queue count unchanged!\r\n" );\r
354                 sErrorHasOccurred = pdTRUE;\r
355         }\r
356 \r
357         if( xIsCreateTaskStillRunning() != pdTRUE )\r
358         {\r
359                 vDisplayMessage( "Incorrect number of tasks running!\r\n" );\r
360                 sErrorHasOccurred = pdTRUE;\r
361         }\r
362 \r
363         if( xAreSemaphoreTasksStillRunning() != pdTRUE )\r
364         {\r
365                 vDisplayMessage( "Semaphore take count unchanged!\r\n" );\r
366                 sErrorHasOccurred = pdTRUE;\r
367         }\r
368 \r
369         if( sErrorHasOccurred == pdFALSE )\r
370         {\r
371                 vDisplayMessage( "OK " );\r
372                 /* Toggle the LED if everything is okay so we know if an error occurs even if not\r
373                 using console IO. */\r
374                 prvToggleLED();\r
375         }\r
376         else\r
377         {\r
378                 for( ;; )\r
379                 {\r
380                         /* An error has occurred in one of the tasks.  Don't go any further and\r
381                         flash the LED rapidly in case console IO is not being used. */\r
382                         prvToggleLED();\r
383                         vTaskDelay( mainERROR_FLASH_RATE );\r
384                 }\r
385         }\r
386 }\r
387 /*-----------------------------------------------------------*/\r
388 \r
389 static void prvInitLED( void )\r
390 {\r
391 unsigned short usPortDirection;\r
392 const unsigned short usLEDOut = 0x400;\r
393 \r
394         /* Set the LED bit to an output. */\r
395 \r
396         usPortDirection = inpw( mainLED_REG_DIR );\r
397         usPortDirection &= ~usLEDOut;\r
398         outpw( mainLED_REG_DIR, usPortDirection );\r
399 }\r
400 /*-----------------------------------------------------------*/\r
401 \r
402 static void prvToggleLED( void )\r
403 {\r
404 static short sLED = pdTRUE;\r
405 unsigned short usLEDState;\r
406 const unsigned short usLEDBit = 0x400;\r
407 \r
408         /* Flip the state of the LED. */\r
409         usLEDState = inpw( mainLED_REG );\r
410         if( sLED )\r
411         {\r
412                 usLEDState &= ~usLEDBit;\r
413         }\r
414         else\r
415         {\r
416                 usLEDState |= usLEDBit;\r
417         }\r
418         outpw( mainLED_REG, usLEDState );\r
419 \r
420         sLED = !sLED;\r
421 }\r
422 \r
423 \r