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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
|
/*
FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
***************************************************************************
* *
* FreeRTOS provides completely free yet professionally developed, *
* robust, strictly quality controlled, supported, and cross *
* platform software that has become a de facto standard. *
* *
* Help yourself get started quickly and support the FreeRTOS *
* project by purchasing a FreeRTOS tutorial book, reference *
* manual, or both from: http://www.FreeRTOS.org/Documentation *
* *
* Thank you! *
* *
***************************************************************************
This file is part of the FreeRTOS distribution.
FreeRTOS is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License (version 2) as published by the
Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
>>! NOTE: The modification to the GPL is included to allow you to distribute
>>! a combined work that includes FreeRTOS without being obliged to provide
>>! the source code for proprietary components outside of the FreeRTOS
>>! kernel.
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. Full license text is available from the following
link: http://www.freertos.org/a00114.html
1 tab == 4 spaces!
***************************************************************************
* *
* Having a problem? Start by reading the FAQ "My application does *
* not run, what could be wrong?" *
* *
* http://www.FreeRTOS.org/FAQHelp.html *
* *
***************************************************************************
http://www.FreeRTOS.org - Documentation, books, training, latest versions,
license and Real Time Engineers Ltd. contact details.
http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
including FreeRTOS+Trace - an indispensable productivity tool, a DOS
compatible FAT file system, and our tiny thread aware UDP/IP stack.
http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
licenses offer ticketed support, indemnification and middleware.
http://www.SafeRTOS.com - High Integrity Systems also provide a safety
engineered and independently SIL3 certified version for use in safety and
mission critical applications that require provable dependability.
1 tab == 4 spaces!
*/
/* ****************************************************************************
* This project includes a lot of tasks and tests and is therefore complex.
* If you would prefer a much simpler project to get started with then select
* the 'low power' demo by setting configCREATE_LOW_POWER_DEMO to 1 in
* FreeRTOSConfig.h. When configCREATE_LOW_POWER_DEMO is set to 1 main() will
* call main_low_power() instead of main_full().
* ****************************************************************************
*
* Creates all the demo application tasks, then starts the scheduler. The web
* documentation provides more details of the standard demo application tasks,
* which provide no particular functionality but do provide a good example of
* how to use the FreeRTOS API.
*
* In addition to the standard demo tasks, the following tasks and tests are
* defined and/or created within this file:
*
* "Reg test" tasks - These fill the registers with known values, then
* repeatedly check that each register still contains its expected value for
* the lifetime of the tasks. Each task uses different values. The tasks run
* with very low priority so get preempted very frequently. A check variable
* is incremented on each iteration of the test loop. A register containing an
* unexpected value is indicative of an error in the context switching
* mechanism and will result in a branch to a null loop - which in turn will
* prevent the check variable from incrementing any further and allow the check
* timer (described below) to determine that an error has occurred. The nature
* of the reg test tasks necessitates that they are written in assembly code.
*
* "Check Timer" and Callback Function - The check timer period is initially
* set to three seconds. The check timer callback function checks that all the
* standard demo tasks are not only still executing, but are executing without
* reporting any errors. If the check timer discovers that a task has either
* stalled, or reported an error, then it changes its own period from the
* initial three seconds, to just 200ms. The check timer callback function
* also toggles LED 0 each time it is called. This provides a visual
* indication of the system status: If the LED toggles every three seconds,
* then no issues have been discovered. If the LED toggles every 200ms, then
* an issue has been discovered with at least one task.
*
* *NOTE 1* The CPU must be in Supervisor mode when the scheduler is started.
* The PowerON_Reset_PC() supplied in resetprg.c with this demo has
* Change_PSW_PM_to_UserMode() commented out to ensure this is the case.
*/
/* Standard includes. */
#include <string.h>
/* Hardware specific includes. */
#include "iodefine.h"
/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
#include "semphr.h"
/* Standard demo includes. */
#include "partest.h"
#include "death.h"
#include "blocktim.h"
#include "GenQTest.h"
#include "recmutex.h"
/* The code in this file is only built when configCREATE_LOW_POWER_DEMO is set
to 0, otherwise the code in main_low_power.c is used. */
#if configCREATE_LOW_POWER_DEMO == 0
/* Values that are passed into the reg test tasks using the task parameter.
The tasks check that the values are passed in correctly. */
#define mainREG_TEST_1_PARAMETER ( 0x12121212UL )
#define mainREG_TEST_2_PARAMETER ( 0x12345678UL )
/* Priorities at which the standard demo tasks are created. */
#define mainGEN_QUEUE_TASK_PRIORITY ( tskIDLE_PRIORITY )
#define mainCREATOR_TASK_PRIORITY ( tskIDLE_PRIORITY + 3 )
/* The LED toggled by the check timer. */
#define mainCHECK_LED ( 0 )
/* The period at which the check timer will expire, in ms, provided no errors
have been reported by any of the standard demo tasks. ms are converted to the
equivalent in ticks using the portTICK_RATE_MS constant. */
#define mainCHECK_TIMER_PERIOD_MS ( 3000UL / portTICK_RATE_MS )
/* The period at which the check timer will expire, in ms, if an error has been
reported in one of the standard demo tasks. ms are converted to the equivalent
in ticks using the portTICK_RATE_MS constant. */
#define mainERROR_CHECK_TIMER_PERIOD_MS ( 200UL / portTICK_RATE_MS )
/* A block time of zero simple means "Don't Block". */
#define mainDONT_BLOCK ( 0UL )
/*
* The reg test tasks as described at the top of this file.
*/
static void prvRegTest1Task( void *pvParameters );
static void prvRegTest2Task( void *pvParameters );
/*
* The actual implementation of the reg test functionality, which, because of
* the direct register access, have to be in assembly.
*/
static void prvRegTest1Implementation( void ) __attribute__(( naked ));
static void prvRegTest2Implementation( void ) __attribute__(( naked ));
/*
* The check timer callback function, as described at the top of this file.
*/
static void prvCheckTimerCallback( xTimerHandle xTimer );
/*-----------------------------------------------------------*/
/* Variables that are incremented on each iteration of the reg test tasks -
provided the tasks have not reported any errors. The check timer inspects these
variables to ensure they are still incrementing as expected. If a variable
stops incrementing then it is likely that its associated task has stalled. */
unsigned long ulRegTest1CycleCount = 0UL, ulRegTest2CycleCount = 0UL;
/* The check timer. This uses prvCheckTimerCallback() as its callback
function. */
static xTimerHandle xCheckTimer = NULL;
/*-----------------------------------------------------------*/
void main_full( void )
{
/* Start the reg test tasks which test the context switching mechanism. */
xTaskCreate( prvRegTest1Task, "RegTst1", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_1_PARAMETER, tskIDLE_PRIORITY, NULL );
xTaskCreate( prvRegTest2Task, "RegTst2", configMINIMAL_STACK_SIZE, ( void * ) mainREG_TEST_2_PARAMETER, tskIDLE_PRIORITY, NULL );
/* Create the standard demo tasks. */
vCreateBlockTimeTasks();
vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );
vStartRecursiveMutexTasks();
/* The suicide tasks must be created last as they need to know how many
tasks were running prior to their creation in order to ascertain whether
or not the correct/expected number of tasks are running at any given time. */
vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
/* Create the software timer that performs the 'check' functionality,
as described at the top of this file. */
xCheckTimer = xTimerCreate( ( const signed char * ) "CheckTimer",/* A text name, purely to help debugging. */
( mainCHECK_TIMER_PERIOD_MS ), /* The timer period, in this case 5000ms (5s). */
pdTRUE, /* This is an auto-reload timer, so xAutoReload is set to pdTRUE. */
( void * ) 0, /* The ID is not used, so can be set to anything. */
prvCheckTimerCallback /* The callback function that inspects the status of all the other tasks. */
);
configASSERT( xCheckTimer );
/* Start the check timer. It will actually start when the scheduler is
started. */
xTimerStart( xCheckTimer, mainDONT_BLOCK );
/* Start the tasks running. */
vTaskStartScheduler();
/* If all is well execution will never reach here as the scheduler will be
running. If this null loop is reached then it is likely there was
insufficient FreeRTOS heap available for the idle task and/or timer task to
be created. See http://www.freertos.org/a00111.html. */
for( ;; );
}
/*-----------------------------------------------------------*/
static void prvCheckTimerCallback( xTimerHandle xTimer )
{
static long lChangedTimerPeriodAlready = pdFALSE, lErrorStatus = pdPASS;
static volatile unsigned long ulLastRegTest1CycleCount = 0UL, ulLastRegTest2CycleCount = 0UL;
/* Remove compiler warnings about unused parameters. */
( void ) xTimer;
/* Check the standard demo tasks are running without error. */
if( xAreGenericQueueTasksStillRunning() != pdTRUE )
{
lErrorStatus = pdFAIL;
}
else if( xIsCreateTaskStillRunning() != pdTRUE )
{
lErrorStatus = pdFAIL;
}
else if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
{
lErrorStatus = pdFAIL;
}
else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
{
lErrorStatus = pdFAIL;
}
/* Check the reg test tasks are still cycling. They will stop incrementing
their loop counters if they encounter an error. */
if( ulRegTest1CycleCount == ulLastRegTest1CycleCount )
{
lErrorStatus = pdFAIL;
}
if( ulRegTest2CycleCount == ulLastRegTest2CycleCount )
{
lErrorStatus = pdFAIL;
}
/* Remember the loop counter values this time around so they can be checked
again the next time this callback function executes. */
ulLastRegTest1CycleCount = ulRegTest1CycleCount;
ulLastRegTest2CycleCount = ulRegTest2CycleCount;
/* Toggle the check LED to give an indication of the system status. If
the LED toggles every three seconds then everything is ok. A faster toggle
indicates an error. */
vParTestToggleLED( mainCHECK_LED );
/* Was an error detected this time through the callback execution? */
if( lErrorStatus != pdPASS )
{
if( lChangedTimerPeriodAlready == pdFALSE )
{
lChangedTimerPeriodAlready = pdTRUE;
/* This call to xTimerChangePeriod() uses a zero block time.
Functions called from inside of a timer callback function must
*never* attempt to block. */
xTimerChangePeriod( xCheckTimer, ( mainERROR_CHECK_TIMER_PERIOD_MS ), mainDONT_BLOCK );
}
}
}
/*-----------------------------------------------------------*/
/* This function is explained in the comments at the top of this file. */
static void prvRegTest1Task( void *pvParameters )
{
if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_1_PARAMETER )
{
/* The parameter did not contain the expected value. */
for( ;; )
{
/* Stop the tick interrupt so its obvious something has gone wrong. */
taskDISABLE_INTERRUPTS();
}
}
/* This is an inline asm function that never returns. */
prvRegTest1Implementation();
}
/*-----------------------------------------------------------*/
/* This function is explained in the comments at the top of this file. */
static void prvRegTest2Task( void *pvParameters )
{
if( ( ( unsigned long ) pvParameters ) != mainREG_TEST_2_PARAMETER )
{
/* The parameter did not contain the expected value. */
for( ;; )
{
/* Stop the tick interrupt so its obvious something has gone wrong. */
taskDISABLE_INTERRUPTS();
}
}
/* This is an inline asm function that never returns. */
prvRegTest2Implementation();
}
/*-----------------------------------------------------------*/
/* This function is explained in the comments at the top of this file. */
static void prvRegTest1Implementation( void )
{
__asm volatile
(
/* Set each register to a known value. */
" MOV.L #0x33333333, R15 \n\t"
" MVTACHI R15 \n\t"
" MOV.L #0x44444444, R15 \n\t"
" MVTACLO R15 \n\t"
" MOV.L #1, R1 \n\t"
" MOV.L #2, R2 \n\t"
" MOV.L #3, R3 \n\t"
" MOV.L #4, R4 \n\t"
" MOV.L #5, R5 \n\t"
" MOV.L #6, R6 \n\t"
" MOV.L #7, R7 \n\t"
" MOV.L #8, R8 \n\t"
" MOV.L #9, R9 \n\t"
" MOV.L #10, R10 \n\t"
" MOV.L #11, R11 \n\t"
" MOV.L #12, R12 \n\t"
" MOV.L #13, R13 \n\t"
" MOV.L #14, R14 \n\t"
" MOV.L #15, R15 \n\t"
" \n\t"
/* Loop, checking each iteration that each register still contains the
expected value. */
"TestLoop1: \n\t"
" \n\t"
/* Push the registers that are going to get clobbered. */
" PUSHM R14-R15 \n\t"
" \n\t"
/* Increment the loop counter to show this task is still getting CPU
time. */
" MOV.L #_ulRegTest1CycleCount, R14 \n\t"
" MOV.L [ R14 ], R15 \n\t"
" ADD #1, R15 \n\t"
" MOV.L R15, [ R14 ] \n\t"
" \n\t"
/* Yield to extend the text coverage. Set the bit in the ITU SWINTR
register. */
" MOV.L #1, R14 \n\t"
" MOV.L #0872E0H, R15 \n\t"
" MOV.B R14, [R15] \n\t"
" NOP \n\t"
" NOP \n\t"
" \n\t"
/* Check the accumulator value. */
" MVFACHI R15 \n\t"
" CMP #0x33333333, R15 \n\t"
" BNE RegTest2Error \n\t"
" MVFACMI R15 \n\t"
" CMP #0x33334444, R15 \n\t"
" BNE RegTest2Error \n\t"
" \n\t"
/* Restore the clobbered registers. */
" POPM R14-R15 \n\t"
" \n\t"
/* Now compare each register to ensure it still contains the value that
was set before this loop was entered. */
" CMP #1, R1 \n\t"
" BNE RegTest1Error \n\t"
" CMP #2, R2 \n\t"
" BNE RegTest1Error \n\t"
" CMP #3, R3 \n\t"
" BNE RegTest1Error \n\t"
" CMP #4, R4 \n\t"
" BNE RegTest1Error \n\t"
" CMP #5, R5 \n\t"
" BNE RegTest1Error \n\t"
" CMP #6, R6 \n\t"
" BNE RegTest1Error \n\t"
" CMP #7, R7 \n\t"
" BNE RegTest1Error \n\t"
" CMP #8, R8 \n\t"
" BNE RegTest1Error \n\t"
" CMP #9, R9 \n\t"
" BNE RegTest1Error \n\t"
" CMP #10, R10 \n\t"
" BNE RegTest1Error \n\t"
" CMP #11, R11 \n\t"
" BNE RegTest1Error \n\t"
" CMP #12, R12 \n\t"
" BNE RegTest1Error \n\t"
" CMP #13, R13 \n\t"
" BNE RegTest1Error \n\t"
" CMP #14, R14 \n\t"
" BNE RegTest1Error \n\t"
" CMP #15, R15 \n\t"
" BNE RegTest1Error \n\t"
" \n\t"
/* All comparisons passed, start a new iteration of this loop. */
" BRA TestLoop1 \n\t"
" \n\t"
/* A compare failed, just loop here so the loop counter stops
incrementing causing the check timer to indicate the error. */
"RegTest1Error: \n\t"
" BRA RegTest1Error "
);
}
/*-----------------------------------------------------------*/
/* This function is explained in the comments at the top of this file. */
static void prvRegTest2Implementation( void )
{
__asm volatile
(
/* Set each register to a known value. */
" MOV.L #0x11111111, R15 \n\t"
" MVTACHI R15 \n\t"
" MOV.L #0x22222222, R15 \n\t"
" MVTACLO R15 \n\t"
" MOV.L #100, R1 \n\t"
" MOV.L #200, R2 \n\t"
" MOV.L #300, R3 \n\t"
" MOV.L #400, R4 \n\t"
" MOV.L #500, R5 \n\t"
" MOV.L #600, R6 \n\t"
" MOV.L #700, R7 \n\t"
" MOV.L #800, R8 \n\t"
" MOV.L #900, R9 \n\t"
" MOV.L #1000, R10 \n\t"
" MOV.L #1001, R11 \n\t"
" MOV.L #1002, R12 \n\t"
" MOV.L #1003, R13 \n\t"
" MOV.L #1004, R14 \n\t"
" MOV.L #1005, R15 \n\t"
" \n\t"
/* Loop, checking each iteration that each register still contains the
expected value. */
"TestLoop2: \n\t"
" \n\t"
/* Push the registers that are going to get clobbered. */
" PUSHM R14-R15 \n\t"
" \n\t"
/* Increment the loop counter to show this task is still getting CPU
time. */
" MOV.L #_ulRegTest2CycleCount, R14 \n\t"
" MOV.L [ R14 ], R15 \n\t"
" ADD #1, R15 \n\t"
" MOV.L R15, [ R14 ] \n\t"
" \n\t"
/* Check the accumulator value. */
" MVFACHI R15 \n\t"
" CMP #0x11111111, R15 \n\t"
" BNE RegTest2Error \n\t"
" MVFACMI R15 \n\t"
" CMP #0x11112222, R15 \n\t"
" BNE RegTest2Error \n\t"
" \n\t"
/* Restore the clobbered registers. */
" POPM R14-R15 \n\t"
" \n\t"
/* Now compare each register to ensure it still contains the value that
was set before this loop was entered. */
" CMP #100, R1 \n\t"
" BNE RegTest2Error \n\t"
" CMP #200, R2 \n\t"
" BNE RegTest2Error \n\t"
" CMP #300, R3 \n\t"
" BNE RegTest2Error \n\t"
" CMP #400, R4 \n\t"
" BNE RegTest2Error \n\t"
" CMP #500, R5 \n\t"
" BNE RegTest2Error \n\t"
" CMP #600, R6 \n\t"
" BNE RegTest2Error \n\t"
" CMP #700, R7 \n\t"
" BNE RegTest2Error \n\t"
" CMP #800, R8 \n\t"
" BNE RegTest2Error \n\t"
" CMP #900, R9 \n\t"
" BNE RegTest2Error \n\t"
" CMP #1000, R10 \n\t"
" BNE RegTest2Error \n\t"
" CMP #1001, R11 \n\t"
" BNE RegTest2Error \n\t"
" CMP #1002, R12 \n\t"
" BNE RegTest2Error \n\t"
" CMP #1003, R13 \n\t"
" BNE RegTest2Error \n\t"
" CMP #1004, R14 \n\t"
" BNE RegTest2Error \n\t"
" CMP #1005, R15 \n\t"
" BNE RegTest2Error \n\t"
" \n\t"
/* All comparisons passed, start a new iteration of this loop. */
" BRA TestLoop2 \n\t"
" \n\t"
/* A compare failed, just loop here so the loop counter stops
incrementing causing the check timer to indicate the error. */
"RegTest2Error: \n\t"
" BRA RegTest2Error "
);
}
/*-----------------------------------------------------------*/
#endif /* configCREATE_LOW_POWER_DEMO */
|