summaryrefslogtreecommitdiff
path: root/FreeRTOS/Demo/MB91460_Softune/SRC/main.c
blob: 33230d465d400490b6a9d3f4ccc1af807b730041 (plain)
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
 * FreeRTOS V202104.00
 * Copyright (C) 2020 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.
 *
 * 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.  The WEB
 * documentation provides more details of the demo application tasks.
 *
 * In addition to the standard demo tasks, the follow demo specific tasks are
 * create:
 *
 * The "Check" task.  This only executes every three 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.  Most tasks maintain
 * a unique count that is incremented each time the task successfully completes
 * its function.  Should any error occur within such a task the count is
 * permanently halted.  The check task inspects the count of each task to ensure
 * it has changed since the last time the check task executed.  If all the count
 * variables have changed all the tasks are still executing error free, and the
 * check task toggles the onboard LED.  Should any task contain an error at any time
 * the LED toggle rate will change from 3 seconds to 500ms.
 *
 * The "Register Check" tasks.  These tasks fill the CPU registers with known
 * values, then check that each register still contains the expected value 0 the
 * discovery of an unexpected value being indicative of an error in the RTOS
 * context switch mechanism.  The register check tasks operate at low priority
 * so are switched in and out frequently.
 *
 * The "Trace Utility" task.  This can be used to obtain trace and debug
 * information via UART5.
 */


/* Hardware specific includes. */
#include "mb91467d.h"
#include "vectors.h"
#include "watchdog.h"

/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"

/* Demo app includes. */
#include "flash.h"
#include "integer.h"
#include "comtest2.h"
#include "semtest.h"
#include "BlockQ.h"
#include "dynamic.h"
#include "flop.h"
#include "GenQTest.h"
#include "QPeek.h"
#include "blocktim.h"
#include "death.h"
#include "taskutility.h"
#include "partest.h"
#include "crflash.h"

/* Demo task priorities. */
#define mainWATCHDOG_TASK_PRIORITY		( tskIDLE_PRIORITY + 5 )
#define mainCHECK_TASK_PRIORITY			( tskIDLE_PRIORITY + 4 )
#define mainUTILITY_TASK_PRIORITY		( tskIDLE_PRIORITY )
#define mainSEM_TEST_PRIORITY			( tskIDLE_PRIORITY + 3 )
#define mainCOM_TEST_PRIORITY			( tskIDLE_PRIORITY + 2 )
#define mainQUEUE_BLOCK_PRIORITY		( tskIDLE_PRIORITY + 2 )
#define mainDEATH_PRIORITY 				( tskIDLE_PRIORITY + 1 )
#define mainLED_TASK_PRIORITY			( tskIDLE_PRIORITY + 1 )
#define mainGENERIC_QUEUE_PRIORITY		( tskIDLE_PRIORITY )

/* Baud rate used by the COM test tasks. */
#define mainCOM_TEST_BAUD_RATE			( ( unsigned long ) 19200 )

/* The frequency at which the 'Check' tasks executes.  See the comments at the
top of the page.  When the system is operating error free the 'Check' task
toggles an LED every three seconds.  If an error is discovered in any task the
rate is increased to 500 milliseconds.  [in this case the '*' characters on the
LCD represent LEDs]*/
#define mainNO_ERROR_CHECK_DELAY		( ( TickType_t ) 3000 / portTICK_PERIOD_MS  )
#define mainERROR_CHECK_DELAY			( ( TickType_t ) 500 / portTICK_PERIOD_MS  )

/* The total number of LEDs available. */
#define mainNO_CO_ROUTINE_LEDs	( 8 )

/* The first LED used by the comtest tasks. */
#define mainCOM_TEST_LED		( 0x05 )

/* The LED used by the check task. */
#define mainCHECK_TEST_LED		( 0x07 )

/* The number of interrupt levels to use. */
#define mainINTERRUPT_LEVELS	( 31 )

/* The number of 'flash' co-routines to create - each toggles a different LED. */
#define mainNUM_FLASH_CO_ROUTINES	( 8 )

/*---------------------------------------------------------------------------*/

/*
 * The function that implements the Check task.  See the comments at the head
 * of the page for implementation details.
 */
static void prvErrorChecks( void *pvParameters );

/*
 * Called by the Check task.  Returns pdPASS if all the other tasks are found
 * to be operating without error - otherwise returns pdFAIL.
 */
static short prvCheckOtherTasksAreStillRunning( void );

/*
 * Setup the microcontroller as used by this demo.
 */
static void prvSetupHardware( void );

/*
 * Tasks that test the context switch mechanism by filling the CPU registers
 * with known values then checking that each register contains the value
 * expected.  Each of the two tasks use different values, and as low priority
 * tasks, get swapped in and out regularly.
 */
static void vFirstRegisterTestTask( void *pvParameters );
static void vSecondRegisterTestTask( void *pvParameters );

/*---------------------------------------------------------------------------*/

/* The variable that is set to true should an error be found in one of the
register test tasks. */
unsigned long ulRegTestError = pdFALSE;

/*---------------------------------------------------------------------------*/

/* Start all the demo application tasks, then start the scheduler. */
void main(void)
{
	/* Initialise the hardware ready for the demo. */
	prvSetupHardware();

	/* Start the standard demo application tasks. */
	vStartLEDFlashTasks( mainLED_TASK_PRIORITY );
	vStartIntegerMathTasks( tskIDLE_PRIORITY );
	vAltStartComTestTasks( mainCOM_TEST_PRIORITY, mainCOM_TEST_BAUD_RATE, mainCOM_TEST_LED - 1 );
	vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
	vStartBlockingQueueTasks ( mainQUEUE_BLOCK_PRIORITY );
	vStartDynamicPriorityTasks();
	vStartMathTasks( tskIDLE_PRIORITY );
	vStartGenericQueueTasks( mainGENERIC_QUEUE_PRIORITY );
	vStartQueuePeekTasks();
	vCreateBlockTimeTasks();
	vStartFlashCoRoutines( mainNUM_FLASH_CO_ROUTINES );

	/* Start the 'Check' task which is defined in this file. */
	xTaskCreate( prvErrorChecks, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );

	/* Start the 'Register Test' tasks as described at the top of this file. */
	xTaskCreate( vFirstRegisterTestTask, "Reg1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
	xTaskCreate( vSecondRegisterTestTask, "Reg2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );

	/* Start the task that write trace information to the UART. */
	vUtilityStartTraceTask( mainUTILITY_TASK_PRIORITY );

	/* If we are going to service the watchdog from within a task, then create
	the task here. */
	#if WATCHDOG == WTC_IN_TASK
		vStartWatchdogTask( mainWATCHDOG_TASK_PRIORITY );
	#endif

	/* The suicide tasks must be started last as they record the number of other
	tasks that exist within the system.  The value is then used to ensure at run
	time the number of tasks that exists is within expected bounds. */
	vCreateSuicidalTasks( mainDEATH_PRIORITY );

	/* Now start the scheduler.  Following this call the created tasks should
	be executing. */
	vTaskStartScheduler( );

	/* vTaskStartScheduler() will only return if an error occurs while the
	idle task is being created. */
	for( ;; );
}
/*-----------------------------------------------------------*/

static void prvErrorChecks( void *pvParameters )
{
TickType_t xDelayPeriod = mainNO_ERROR_CHECK_DELAY, xLastExecutionTime;

	/* Initialise xLastExecutionTime so the first call to vTaskDelayUntil()
	works correctly. */
	xLastExecutionTime = xTaskGetTickCount();

	/* Cycle for ever, delaying then checking all the other tasks are still
	operating without error. */
	for( ;; )
	{
		/* Wait until it is time to check again.  The time we wait here depends
		on whether an error has been detected or not.  When an error is
		detected the time is shortened resulting in a faster LED flash rate. */
		/* Perform this check every mainCHECK_DELAY milliseconds. */
		vTaskDelayUntil( &xLastExecutionTime, xDelayPeriod );

		/* See if the other tasks are all ok. */
		if( prvCheckOtherTasksAreStillRunning() != pdPASS )
		{
			/* An error occurred in one of the tasks so shorten the delay
			period - which has the effect of increasing the frequency of the
			LED toggle. */
			xDelayPeriod = mainERROR_CHECK_DELAY;
		}

		/* Flash! */
		vParTestToggleLED( mainCHECK_TEST_LED );
	}
}
/*-----------------------------------------------------------*/

static short prvCheckOtherTasksAreStillRunning( void )
{
portBASE_TYPE lReturn = pdPASS;

	/* The demo tasks maintain a count that increments every cycle of the task
	provided that the task has never encountered an error.  This function
	checks the counts maintained by the tasks to ensure they are still being
	incremented.  A count remaining at the same value between calls therefore
	indicates that an error has been detected. */

	if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
	{
		lReturn = pdFAIL;
	}

	if( xAreComTestTasksStillRunning() != pdTRUE )
	{
		lReturn = pdFAIL;
	}

	if( xAreSemaphoreTasksStillRunning() != pdTRUE )
	{
		lReturn = pdFAIL;
	}

	if( xAreBlockingQueuesStillRunning() != pdTRUE )
	{
		lReturn = pdFAIL;
	}

	if( xAreDynamicPriorityTasksStillRunning() != pdTRUE )
	{
		lReturn = pdFAIL;
	}

	if( xAreMathsTaskStillRunning() != pdTRUE )
	{
		lReturn = pdFAIL;
	}

	if( xIsCreateTaskStillRunning() != pdTRUE )
	{
		lReturn = pdFAIL;
	}

	if( xAreBlockTimeTestTasksStillRunning() != pdTRUE )
	{
		lReturn = pdFAIL;
	}

	if ( xAreGenericQueueTasksStillRunning() != pdTRUE )
	{
		lReturn = pdFAIL;
	}

	if ( xAreQueuePeekTasksStillRunning() != pdTRUE )
	{
		lReturn = pdFAIL;
	}

	/* Have the register test tasks found any errors? */
	if( ulRegTestError != pdFALSE )
	{
		lReturn = pdFAIL;
	}

	return lReturn;
}
/*-----------------------------------------------------------*/

static void prvSetupHardware( void )
{
	/* Allow all interrupt levels. */
	__set_il( mainINTERRUPT_LEVELS );

	/* Initialise interrupts. */
	InitIrqLevels();

	/* Initialise the ports used by the LEDs. */
	vParTestInitialise();

	/* If we are going to use the watchdog, then initialise it now. */
	#if WATCHDOG != WTC_NONE
		InitWatchdog();
	#endif
}
/*-----------------------------------------------------------*/

/* Idle hook function. */
#if configUSE_IDLE_HOOK == 1
	void vApplicationIdleHook( void )
	{
		/* Are we using the idle task to kick the watchdog?  See watchdog.h
		for watchdog kicking options. Note this is for demonstration only
		and is not a suggested method of servicing the watchdog in a real
		application. */
		#if WATCHDOG == WTC_IN_IDLE
			Kick_Watchdog();
		#endif

		vCoRoutineSchedule();
	}
#else
	#if WATCHDOG == WTC_IN_IDLE
		#error configUSE_IDLE_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the idle task hook.
	#endif
#endif

/*-----------------------------------------------------------*/

/* Tick hook function. */
#if configUSE_TICK_HOOK == 1
	void vApplicationTickHook( void )
	{
		/* Are we using the tick to kick the watchdog?  See watchdog.h
		for watchdog kicking options.  Note this is for demonstration
		only and is not a suggested method of servicing the watchdog in
		a real application. */
		#if WATCHDOG == WTC_IN_TICK
			Kick_Watchdog();
		#endif
	}
#else
	#if WATCHDOG == WTC_IN_TICK
		#error configUSE_TICK_HOOK must be set to 1 in FreeRTOSConfig.h if the watchdog is being cleared in the tick hook.
	#endif
#endif
/*-----------------------------------------------------------*/

static void vFirstRegisterTestTask( void *pvParameters )
{
extern volatile unsigned long ulCriticalNesting;

	/* Fills the registers with known values (different to the values
	used in vSecondRegisterTestTask()), then checks that the registers still
	all contain the expected value.  This is done to test the context save
	and restore mechanism as this task is swapped onto and off of the CPU. */

	for( ;; )
	{
		#pragma asm
			;Load known values into each register.
			LDI	#0x11111111, R0
			LDI	#0x22222222, R1
			LDI	#0x33333333, R2
			LDI #0x44444444, R3
			LDI	#0x55555555, R4
			LDI	#0x66666666, R5
			LDI	#0x77777777, R6
			LDI	#0x88888888, R7
			LDI	#0x99999999, R8
			LDI	#0xaaaaaaaa, R9
			LDI	#0xbbbbbbbb, R10
			LDI	#0xcccccccc, R11
			LDI	#0xdddddddd, R12

			;Check each register still contains the expected value.
			LDI #0x11111111, R13
			CMP R13, R0
			BNE First_Set_Error

			LDI #0x22222222, R13
			CMP R13, R1
			BNE First_Set_Error

			LDI #0x33333333, R13
			CMP R13, R2
			BNE First_Set_Error

			LDI #0x44444444, R13
			CMP R13, R3
			BNE First_Set_Error

			LDI #0x55555555, R13
			CMP R13, R4
			BNE First_Set_Error

			LDI #0x66666666, R13
			CMP R13, R5
			BNE First_Set_Error

			LDI #0x77777777, R13
			CMP R13, R6
			BNE First_Set_Error

			LDI #0x88888888, R13
			CMP R13, R7
			BNE First_Set_Error

			LDI #0x99999999, R13
			CMP R13, R8
			BNE First_Set_Error

			LDI #0xaaaaaaaa, R13
			CMP R13, R9
			BNE First_Set_Error

			LDI #0xbbbbbbbb, R13
			CMP R13, R10
			BNE First_Set_Error

			LDI #0xcccccccc, R13
			CMP R13, R11
			BNE First_Set_Error

			LDI #0xdddddddd, R13
			CMP R13, R12
			BNE First_Set_Error

			BRA First_Start_Next_Loop

		First_Set_Error:

			; Latch that an error has occurred.
			LDI #_ulRegTestError, R0
			LDI #0x00000001, R1
			ST R1, @R0


		First_Start_Next_Loop:


		#pragma endasm
	}
}
/*-----------------------------------------------------------*/

static void vSecondRegisterTestTask( void *pvParameters )
{
extern volatile unsigned long ulCriticalNesting;

	/* Fills the registers with known values (different to the values
	used in vFirstRegisterTestTask()), then checks that the registers still
	all contain the expected value.  This is done to test the context save
	and restore mechanism as this task is swapped onto and off of the CPU. */

	for( ;; )
	{
		#pragma asm
			;Load known values into each register.
			LDI	#0x11111111, R1
			LDI	#0x22222222, R2
			INT #40H
			LDI	#0x33333333, R3
			LDI #0x44444444, R4
			LDI	#0x55555555, R5
			LDI	#0x66666666, R6
			LDI	#0x77777777, R7
			LDI	#0x88888888, R8
			LDI	#0x99999999, R9
			INT #40H
			LDI	#0xaaaaaaaa, R10
			LDI	#0xbbbbbbbb, R11
			LDI	#0xcccccccc, R12
			LDI	#0xdddddddd, R0

			;Check each register still contains the expected value.
			LDI #0x11111111, R13
			CMP R13, R1
			BNE Second_Set_Error

			LDI #0x22222222, R13
			CMP R13, R2
			BNE Second_Set_Error

			LDI #0x33333333, R13
			CMP R13, R3
			BNE Second_Set_Error

			LDI #0x44444444, R13
			CMP R13, R4
			BNE Second_Set_Error

			LDI #0x55555555, R13
			CMP R13, R5
			BNE Second_Set_Error

			INT #40H

			LDI #0x66666666, R13
			CMP R13, R6
			BNE Second_Set_Error

			LDI #0x77777777, R13
			CMP R13, R7
			BNE Second_Set_Error

			LDI #0x88888888, R13
			CMP R13, R8
			BNE Second_Set_Error

			LDI #0x99999999, R13
			CMP R13, R9
			BNE Second_Set_Error

			INT #40H

			LDI #0xaaaaaaaa, R13
			CMP R13, R10
			BNE Second_Set_Error

			LDI #0xbbbbbbbb, R13
			CMP R13, R11
			BNE Second_Set_Error

			LDI #0xcccccccc, R13
			CMP R13, R12
			BNE Second_Set_Error

			LDI #0xdddddddd, R13
			CMP R13, R0
			BNE Second_Set_Error

			BRA Second_Start_Next_Loop

		Second_Set_Error:

			; Latch that an error has occurred.
			LDI #_ulRegTestError, R0
			LDI #0x00000001, R1
			ST R1, @R0


		Second_Start_Next_Loop:


		#pragma endasm
	}
}
/*-----------------------------------------------------------*/