summaryrefslogtreecommitdiff
path: root/FreeRTOS/Demo/Tensilica_Simulator_Xplorer_XCC/main_full.c
blob: 51e4e15949a3fe5b707dbb756a4d613d7efb8bd0 (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
/*
 * FreeRTOS Kernel V10.4.1
 * 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.
 *
 * 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!
 */

/******************************************************************************
 * NOTE:  This file only contains the source code that is specific to the
 * basic demo.  Generic functions, such FreeRTOS hook functions, are defined
 * in main.c.
 ******************************************************************************
 *
 * 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:
 *
 * "Register tests":
 * prvRegTest1Task and prvRegTest2Task implement register tests. These functions
 * are just entry points and actual tests are written in the assembler file
 * regtest_xtensa.S. These tests populate core registers with a known set of
 * values and keep verifying them in a loop. Any corruption will indicate an
 * error in context switching mechanism.
 *
 * "Check" task:
 * This only executes every five seconds but has a high priority to ensure it
 * gets processor time. Its main function is to check that all the standard demo
 * tasks are still operational. While no errors have been discovered the check
 * task will print out "No errors", the current simulated tick time, free heap
 * size and the minimum free heap size so far. If an error is discovered in the
 * execution of a task then the check task will print out an appropriate error
 * message.
 */


/* Standard includes. */
#include <stdio.h>
#include <stdlib.h>

/* Kernel includes. */
#include <FreeRTOS.h>
#include <task.h>
#include <queue.h>
#include <timers.h>
#include <semphr.h>

/* Standard demo includes. */
#include "BlockQ.h"
#include "integer.h"
#include "semtest.h"
#include "PollQ.h"
#include "GenQTest.h"
#include "QPeek.h"
#include "recmutex.h"
#include "flop.h"
#include "TimerDemo.h"
#include "countsem.h"
#include "death.h"
#include "QueueSet.h"
#include "QueueOverwrite.h"
#include "EventGroupsDemo.h"
#include "IntSemTest.h"
#include "TaskNotify.h"
#include "QueueSetPolling.h"
#include "StaticAllocation.h"
#include "blocktim.h"
#include "AbortDelay.h"
#include "MessageBufferDemo.h"
#include "StreamBufferDemo.h"
#include "StreamBufferInterrupt.h"

/**
 * Priorities at which the tasks are created.
 */
#define mainCHECK_TASK_PRIORITY			( configMAX_PRIORITIES - 2 )
#define mainQUEUE_POLL_PRIORITY			( tskIDLE_PRIORITY + 1 )
#define mainSEM_TEST_PRIORITY			( tskIDLE_PRIORITY + 1 )
#define mainBLOCK_Q_PRIORITY			( tskIDLE_PRIORITY + 2 )
#define mainCREATOR_TASK_PRIORITY		( tskIDLE_PRIORITY + 3 )
#define mainFLASH_TASK_PRIORITY			( tskIDLE_PRIORITY + 1 )
#define mainINTEGER_TASK_PRIORITY		( tskIDLE_PRIORITY )
#define mainGEN_QUEUE_TASK_PRIORITY		( tskIDLE_PRIORITY )
#define mainFLOP_TASK_PRIORITY			( tskIDLE_PRIORITY )
#define mainQUEUE_OVERWRITE_PRIORITY	( tskIDLE_PRIORITY )

/**
 * Period used in timer tests.
 */
#define mainTIMER_TEST_PERIOD			( 50 )

/**
 * Parameters that are passed into the register check tasks solely for the
 * purpose of ensuring parameters are passed into tasks correctly.
 */
#define mainREG_TEST_TASK_1_PARAMETER	( ( void * ) 0x12345678 )
#define mainREG_TEST_TASK_2_PARAMETER	( ( void * ) 0x87654321 )

/**
 * Determines whether to enable interrupt queue tests.
 *
 * Interrupt queue tests are used to test interrupt nesting and enabling them
 * interferes with proper functioning of other tests. This macro controls
 * whether to enable interrupt queue tests or all other tests:
 *
 * * When mainENABLE_INT_QUEUE_TESTS is set to 1, interrupt queue tests are
 *   enabled and every other test is disabled.
 * * When mainENABLE_INT_QUEUE_TESTS is set to 0, interrupt queue tests are
 *   disabled and every other test is enabled.
 */
#define mainENABLE_INT_QUEUE_TESTS		( 0 )
/*-----------------------------------------------------------*/

/**
 * The task that periodically checks that all the standard demo tasks are
 * still executing and error free.
 */
static void prvCheckTask( void *pvParameters );

/**
 * Tasks that implement register tests.
 */
static void prvRegTest1Task( void *pvParameters );
static void prvRegTest2Task( void *pvParameters );

/**
 * Functions that implement register tests.
 *
 * These are implemented in assembler file regtest_xtensa.S.
 */
extern void vRegTest1( void );
extern void vRegTest2( void );
/*-----------------------------------------------------------*/

/**
 * The variable into which error messages are latched.
 */
static char *pcStatusMessage = "No errors";

/**
 * The following two variables are used to communicate the status of the
 * register check tasks to the check task.  If the variables keep incrementing,
 * then the register check tasks have not discovered any errors.  If a variable
 * stops incrementing, then an error has been found.
 */
volatile unsigned long ulRegTest1Counter = 0UL, ulRegTest2Counter = 0UL;

/**
 * The following variable is used to communicate whether the timers for the
 * IntQueue tests have been Initialized. This is needed to ensure that the queues
 * are accessed from the tick hook only after they have been created in the
 * interrupt queue test.
 */
volatile BaseType_t xTimerForQueueTestInitialized = pdFALSE;
/*-----------------------------------------------------------*/

int main_full( void )
{
	/* Start the check task as described at the top of this file. */
	xTaskCreate( prvCheckTask, "Check", configMINIMAL_STACK_SIZE, NULL, mainCHECK_TASK_PRIORITY, NULL );

	#if( mainENABLE_INT_QUEUE_TESTS == 0 )
	{
		/* Create the standard demo tasks. */
		vStartTaskNotifyTask();
		vStartBlockingQueueTasks( mainBLOCK_Q_PRIORITY );
		vStartSemaphoreTasks( mainSEM_TEST_PRIORITY );
		vStartPolledQueueTasks( mainQUEUE_POLL_PRIORITY );
		vStartIntegerMathTasks( mainINTEGER_TASK_PRIORITY );
		vStartGenericQueueTasks( mainGEN_QUEUE_TASK_PRIORITY );

		vStartQueuePeekTasks();
		vStartMathTasks( mainFLOP_TASK_PRIORITY );
		vStartRecursiveMutexTasks();
		vStartCountingSemaphoreTasks();
		vStartQueueSetTasks();

		vStartQueueOverwriteTask( mainQUEUE_OVERWRITE_PRIORITY );
		vStartEventGroupTasks();
		vStartInterruptSemaphoreTasks();
		vStartQueueSetPollingTask();
		vCreateBlockTimeTasks();

		#if( configUSE_PREEMPTION != 0  )
		{
			/* Don't expect these tasks to pass when preemption is not used. */
			vStartTimerDemoTask( mainTIMER_TEST_PERIOD );
		}
		#endif

		vCreateAbortDelayTasks();
		vStartMessageBufferTasks( configMINIMAL_STACK_SIZE );

		vStartStreamBufferTasks();
		vStartStreamBufferInterruptDemo();

		/* Create the register check tasks, as described at the top of this	file */
		xTaskCreate( prvRegTest1Task, "Reg1", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_1_PARAMETER, tskIDLE_PRIORITY, NULL );
		xTaskCreate( prvRegTest2Task, "Reg2", configMINIMAL_STACK_SIZE, mainREG_TEST_TASK_2_PARAMETER, tskIDLE_PRIORITY, NULL );

		/* The suicide tasks must be created last as they need to know how many
		 * tasks were running prior to their creation.  This then allows them to
		 * ascertain whether or not the correct/expected number of tasks are
		 * running at any given time. */
		vCreateSuicidalTasks( mainCREATOR_TASK_PRIORITY );
	}
	#else /* mainENABLE_INT_QUEUE_TESTS */
	{
		/* Start interrupt queue test tasks. */
		vStartInterruptQueueTasks();
	}
	#endif /* mainENABLE_INT_QUEUE_TESTS */

	/* Start the scheduler itself. */
	vTaskStartScheduler();

	/* Should never get here unless there was not enough heap space to create
	 * the idle and other system tasks. */
	return 0;
}
/*-----------------------------------------------------------*/

static void prvCheckTask( void *pvParameters )
{
TickType_t xNextWakeTime;
const TickType_t xCycleFrequency = pdMS_TO_TICKS( 5000UL );
static unsigned long ulLastRegTest1Value = 0, ulLastRegTest2Value = 0;

	/* Just to remove compiler warning. */
	( void ) pvParameters;

	/* Initialise xNextWakeTime - this only needs to be done once. */
	xNextWakeTime = xTaskGetTickCount();

	for( ;; )
	{
		/* Place this task in the blocked state until it is time to run again. */
		vTaskDelayUntil( &xNextWakeTime, xCycleFrequency );

		#if( mainENABLE_INT_QUEUE_TESTS == 0 )
		{
			/* Check the standard demo tasks are running without error. */
			#if( configUSE_PREEMPTION != 0 )
			{
				/* These tasks are only created when preemption is used. */
				if( xAreTimerDemoTasksStillRunning( xCycleFrequency ) != pdTRUE )
				{
					pcStatusMessage = "Error: TimerDemo";
				}
			}
			#endif

			if( xAreTaskNotificationTasksStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error:  Notification";
			}
			else if( xAreBlockingQueuesStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error: BlockQueue";
			}
			else if( xAreSemaphoreTasksStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error: SemTest";
			}
			else if( xArePollingQueuesStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error: PollQueue";
			}
			else if( xAreIntegerMathsTaskStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error: IntMath";
			}
			else if( xAreGenericQueueTasksStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error: GenQueue";
			}
			else if( xAreQueuePeekTasksStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error: QueuePeek";
			}
			else if( xAreMathsTaskStillRunning() != pdPASS )
			{
				pcStatusMessage = "Error: Flop";
			}
			else if( xAreRecursiveMutexTasksStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error: RecMutex";
			}
			else if( xAreCountingSemaphoreTasksStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error: CountSem";
			}
			else if( xAreQueueSetTasksStillRunning() != pdPASS )
			{
				pcStatusMessage = "Error: Queue set";
			}
			else if( xIsQueueOverwriteTaskStillRunning() != pdPASS )
			{
				pcStatusMessage = "Error: Queue overwrite";
			}
			else if( xAreEventGroupTasksStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error: EventGroup";
			}
			else if( xAreInterruptSemaphoreTasksStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error: IntSem";
			}
			else if( xAreQueueSetPollTasksStillRunning() != pdPASS )
			{
				pcStatusMessage = "Error: Queue set polling";
			}
			else if( xAreBlockTimeTestTasksStillRunning() != pdPASS )
			{
				pcStatusMessage = "Error: Block time";
			}
			else if( xAreAbortDelayTestTasksStillRunning() != pdPASS )
			{
				pcStatusMessage = "Error: Abort delay";
			}
			else if( xAreMessageBufferTasksStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error:  MessageBuffer";
			}
			else if( xAreStreamBufferTasksStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error:  StreamBuffer";
			}
			else if( xIsInterruptStreamBufferDemoStillRunning() != pdPASS )
			{
				pcStatusMessage = "Error: Stream buffer interrupt";
			}
			else if( xIsCreateTaskStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error: Death";
			}
			else if( ulLastRegTest1Value == ulRegTest1Counter )
			{
				pcStatusMessage = "Error: Reg Test 1";
			}
			else if( ulLastRegTest2Value == ulRegTest2Counter )
			{
				pcStatusMessage = "Error: Reg Test 2";
			}

			/* Update register test counters. */
			ulLastRegTest1Value = ulRegTest1Counter;
			ulLastRegTest2Value = ulRegTest2Counter;
		}
		#else /* mainENABLE_INT_QUEUE_TESTS */
		{
			if( xAreIntQueueTasksStillRunning() != pdTRUE )
			{
				pcStatusMessage = "Error: IntQueue";
			}
		}
		#endif /* mainENABLE_INT_QUEUE_TESTS */

		/* This is the only task that uses stdout so its ok to call printf()
		 * directly. Do not call printf from any other task. */
		printf( "%s - tick count %zu - free heap %zu - min free heap %zu\r\n", pcStatusMessage,
																			   xTaskGetTickCount(),
																			   xPortGetFreeHeapSize(),
																			   xPortGetMinimumEverFreeHeapSize() );
	}
}
/*-----------------------------------------------------------*/

/* Called by vApplicationTickHook(), which is defined in main.c. */
void vFullDemoTickHookFunction( void )
{
TaskHandle_t xTimerTask;

	#if( mainENABLE_INT_QUEUE_TESTS == 0 )
	{
		/* Exercise using task notifications from an interrupt. */
		xNotifyTaskFromISR();

		/* Write to a queue that is in use as part of the queue set demo to
		 * demonstrate using queue sets from an ISR. */
		vQueueSetAccessQueueSetFromISR();

		/* Call the periodic queue overwrite from ISR demo. */
		vQueueOverwritePeriodicISRDemo();

		/* Exercise event groups from interrupts. */
		vPeriodicEventGroupsProcessing();

		/* Exercise giving mutexes from an interrupt. */
		vInterruptSemaphorePeriodicTest();

		/* Queue set access from interrupt. */
		vQueueSetPollingInterruptAccess();

		/* Call the periodic timer test, which tests the timer API functions that
		 * can be called from an ISR. */
		#if( configUSE_PREEMPTION != 0 )
		{
			/* Only created when preemption is used. */
			vTimerPeriodicISRTests();
		}
		#endif

		/* Writes to stream buffer byte by byte to test the stream buffer trigger
		 * level functionality. */
		vPeriodicStreamBufferProcessing();

		/* Writes a string to a string buffer four bytes at a time to demonstrate
		 * a stream being sent from an interrupt to a task. */
		vBasicStreamBufferSendFromISR();
	}
	#else /* mainENABLE_INT_QUEUE_TESTS */
	{
		/* Access queues from interrupt. Make sure to access after the queues have
		 * been created. */
		if( xTimerForQueueTestInitialized == pdTRUE )
		{
			portYIELD_FROM_ISR( xFirstTimerHandler() );
		}
	}
	#endif /* mainENABLE_INT_QUEUE_TESTS */
}
/*-----------------------------------------------------------*/

static void prvRegTest1Task( void *pvParameters )
{
	/* Although the regtest task is written in assembly, its entry point is
	 * written in C for convenience of checking the task parameter is being
	 * passed in correctly. */
	if( pvParameters == mainREG_TEST_TASK_1_PARAMETER )
	{
		/* Start the part of the test that is written in assembly. */
		vRegTest1();
	}

	/* The following line will only execute if the task parameter is found to
	 * be incorrect. The check task will detect that the regtest loop counter is
	 * not being incremented and flag an error. */
	vTaskDelete( NULL );
}
/*-----------------------------------------------------------*/

static void prvRegTest2Task( void *pvParameters )
{
	/* Although the regtest task is written in assembly, its entry point is
	 * written in C for convenience of checking the task parameter is being
	 * passed in correctly. */
	if( pvParameters == mainREG_TEST_TASK_2_PARAMETER )
	{
		/* Start the part of the test that is written in assembly. */
		vRegTest2();
	}

	/* The following line will only execute if the task parameter is found to
	 * be incorrect. The check task will detect that the regtest loop counter is
	 * not being incremented and flag an error. */
	vTaskDelete( NULL );
}
/*-----------------------------------------------------------*/