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