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