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