os/kernelhwsrv/kerneltest/e32test/personality/example/main.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
// Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     2
// All rights reserved.
sl@0
     3
// This component and the accompanying materials are made available
sl@0
     4
// under the terms of the License "Eclipse Public License v1.0"
sl@0
     5
// which accompanies this distribution, and is available
sl@0
     6
// at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     7
//
sl@0
     8
// Initial Contributors:
sl@0
     9
// Nokia Corporation - initial contribution.
sl@0
    10
//
sl@0
    11
// Contributors:
sl@0
    12
//
sl@0
    13
// Description:
sl@0
    14
// e32test\personality\example\main.cpp
sl@0
    15
// Test code for example RTOS personality.
sl@0
    16
// 
sl@0
    17
//
sl@0
    18
sl@0
    19
#include <kernel/kern_priv.h>
sl@0
    20
#include <personality/example/personality.h>
sl@0
    21
sl@0
    22
#ifdef __cplusplus
sl@0
    23
extern "C" {
sl@0
    24
#endif
sl@0
    25
sl@0
    26
#define	OC_TASK				0
sl@0
    27
#define	L2_TASK				1
sl@0
    28
#define	RR_TASK				2
sl@0
    29
#define NONEXISTENT_TASK	3
sl@0
    30
#define TM_TASK				4
sl@0
    31
#define	TASK1				6
sl@0
    32
#define	TASK2				7
sl@0
    33
#define	TASK3				8
sl@0
    34
#define	TASK4				9
sl@0
    35
#define	L1_TASK				10
sl@0
    36
sl@0
    37
void oo_overall_control(void);
sl@0
    38
void l1_task_entry(void);
sl@0
    39
void l2_task_entry(void);
sl@0
    40
void rr_task_entry(void);
sl@0
    41
void tm_task_entry(void);
sl@0
    42
void task1_entry(void);
sl@0
    43
void task2_entry(void);
sl@0
    44
void task3_entry(void);
sl@0
    45
void task4_entry(void);
sl@0
    46
sl@0
    47
typedef void (*isr_entry)(unsigned);
sl@0
    48
sl@0
    49
extern int start_random_isr(isr_entry vector);
sl@0
    50
extern void stop_random_isr(void);
sl@0
    51
sl@0
    52
const taskinfo task_list[] =
sl@0
    53
	{
sl@0
    54
sl@0
    55
	/*		entry_pt,			priority, stack_size,	task_id, auto_start	*/
sl@0
    56
sl@0
    57
		{	&oo_overall_control,	120,	1024,		OC_TASK,	1	},
sl@0
    58
		{	&l2_task_entry,			236,	1024,		L2_TASK,	0	},
sl@0
    59
		{	&rr_task_entry,			224,	1024,		RR_TASK,	0	},
sl@0
    60
		{	&tm_task_entry,			240,	1024,		TM_TASK,	0	},
sl@0
    61
		{	&task1_entry,			112,	1024,		TASK1,		0	},
sl@0
    62
		{	&task2_entry,			112,	1024,		TASK2,		0	},
sl@0
    63
		{	&task3_entry,			112,	1024,		TASK3,		0	},
sl@0
    64
		{	&task4_entry,			112,	1024,		TASK4,		0	},
sl@0
    65
		{	&l1_task_entry,			244,	1024,		L1_TASK,	0	},
sl@0
    66
	/* terminator */
sl@0
    67
		{	0,						0,		0,			0,			0	}
sl@0
    68
	};
sl@0
    69
sl@0
    70
const poolinfo pool_list[] =
sl@0
    71
	{
sl@0
    72
	/*	block size,		block count	*/
sl@0
    73
		{	32,			256		},
sl@0
    74
		{	64,			256		},
sl@0
    75
		{	128,		128		},
sl@0
    76
		{	256,		64		},
sl@0
    77
		{	512,		32		},
sl@0
    78
	/* terminator */
sl@0
    79
		{	0,			0		}
sl@0
    80
	};
sl@0
    81
sl@0
    82
const int timer_count = 8;
sl@0
    83
const int semaphore_count = 2;
sl@0
    84
sl@0
    85
#define TM_TIMER		0
sl@0
    86
sl@0
    87
#define TM_INIT_DELAY	1000
sl@0
    88
#define TM_PERIOD		2
sl@0
    89
sl@0
    90
volatile unsigned next_random_id = 0;
sl@0
    91
volatile unsigned random_sem_signal_interval = 0;
sl@0
    92
volatile unsigned random_sem_signal_count = 0;
sl@0
    93
volatile unsigned random_send_interval = 0;
sl@0
    94
volatile unsigned random_send_count = 0;
sl@0
    95
volatile unsigned tmcount = 0;
sl@0
    96
volatile int t1func = 0;
sl@0
    97
volatile int t2func = 0;
sl@0
    98
volatile int t3func = 0;
sl@0
    99
volatile int t4func = 0;
sl@0
   100
sl@0
   101
#define TEST_SEM		0
sl@0
   102
#define	ISR_SEM			1
sl@0
   103
sl@0
   104
#define MSG_ID_INIT		1
sl@0
   105
#define MSG_ID_RUN		2
sl@0
   106
#define MSG_ID_RUN_P	3
sl@0
   107
#define	MSG_ID_RND_ISR	4
sl@0
   108
#define MSG_ID_DONE		5
sl@0
   109
#define	MSG_ID_DATA		6
sl@0
   110
#define	MSG_ID_FLUSH	7
sl@0
   111
#define MSG_ID_SEM_RPT	8
sl@0
   112
#define MSG_ID_RCV_RPT	9
sl@0
   113
#define MSG_ID_TM_RPT	10
sl@0
   114
sl@0
   115
typedef struct _run_msg
sl@0
   116
	{
sl@0
   117
	msghdr			header;
sl@0
   118
	int				task_id;
sl@0
   119
	unsigned		tmcount;
sl@0
   120
	int				parameter;
sl@0
   121
	} run_msg;
sl@0
   122
sl@0
   123
typedef struct _random_isr_msg
sl@0
   124
	{
sl@0
   125
	msghdr			header;
sl@0
   126
	unsigned		random_isr_number;
sl@0
   127
	unsigned		extra;
sl@0
   128
	} random_isr_msg;
sl@0
   129
sl@0
   130
typedef struct _data_msg
sl@0
   131
	{
sl@0
   132
	msghdr			header;
sl@0
   133
	int				length;
sl@0
   134
	unsigned char	checksum;
sl@0
   135
	unsigned char	data[1];
sl@0
   136
	} data_msg;
sl@0
   137
sl@0
   138
typedef struct _report_msg
sl@0
   139
	{
sl@0
   140
	msghdr			header;
sl@0
   141
	int				pad;
sl@0
   142
	unsigned		count;
sl@0
   143
	unsigned		ok_count;
sl@0
   144
	unsigned		bad_count;
sl@0
   145
	} report_msg;
sl@0
   146
sl@0
   147
void busy_wait(unsigned ticks)
sl@0
   148
	{
sl@0
   149
	unsigned t0 = tmcount;
sl@0
   150
	while ((tmcount - t0) < ticks)
sl@0
   151
		{}
sl@0
   152
	}
sl@0
   153
sl@0
   154
void send_run_signal()
sl@0
   155
	{
sl@0
   156
	run_msg* m = (run_msg*)alloc_mem_block(sizeof(run_msg));
sl@0
   157
	assert(m);
sl@0
   158
	m->header.msg_id = MSG_ID_RUN;
sl@0
   159
	m->task_id = current_task_id();
sl@0
   160
	m->tmcount = tmcount;
sl@0
   161
	int r = send_msg(OC_TASK, &m->header);
sl@0
   162
	assert(r == OK);
sl@0
   163
	}
sl@0
   164
sl@0
   165
void send_run_signal_p(int parameter)
sl@0
   166
	{
sl@0
   167
	run_msg* m = (run_msg*)alloc_mem_block(sizeof(run_msg));
sl@0
   168
	assert(m);
sl@0
   169
	m->header.msg_id = MSG_ID_RUN_P;
sl@0
   170
	m->task_id = current_task_id();
sl@0
   171
	m->tmcount = tmcount;
sl@0
   172
	m->parameter = parameter;
sl@0
   173
	int r = send_msg(OC_TASK, &m->header);
sl@0
   174
	assert(r == OK);
sl@0
   175
	}
sl@0
   176
sl@0
   177
void tsend_run_signal_p(int task_id, int parameter)
sl@0
   178
	{
sl@0
   179
	run_msg* m = (run_msg*)alloc_mem_block(sizeof(run_msg));
sl@0
   180
	assert(m);
sl@0
   181
	m->header.msg_id = MSG_ID_RUN_P;
sl@0
   182
	m->task_id = current_task_id();
sl@0
   183
	m->tmcount = tmcount;
sl@0
   184
	m->parameter = parameter;
sl@0
   185
	int r = send_msg(task_id, &m->header);
sl@0
   186
	assert(r == OK);
sl@0
   187
	}
sl@0
   188
sl@0
   189
void check_no_signal()
sl@0
   190
	{
sl@0
   191
	msghdr* m = NULL;
sl@0
   192
	int r = recv_msg(&m, NO_WAIT);
sl@0
   193
	assert(r == TIMED_OUT);
sl@0
   194
	}
sl@0
   195
sl@0
   196
unsigned check_for_signal(int task_id)
sl@0
   197
	{
sl@0
   198
	msghdr* m = NULL;
sl@0
   199
	int r = recv_msg(&m, NO_WAIT);
sl@0
   200
	assert(r == OK);
sl@0
   201
	assert(m->msg_id == MSG_ID_RUN);
sl@0
   202
	run_msg* rm = (run_msg*)m;
sl@0
   203
	assert(rm->task_id == task_id);
sl@0
   204
	unsigned tmc = rm->tmcount;
sl@0
   205
	free_mem_block(m);
sl@0
   206
	return tmc;
sl@0
   207
	}
sl@0
   208
sl@0
   209
int check_for_signal_p(int task_id, int task_id2, unsigned* pt)
sl@0
   210
	{
sl@0
   211
	msghdr* m = NULL;
sl@0
   212
	int r = recv_msg(&m, NO_WAIT);
sl@0
   213
	assert(r == OK);
sl@0
   214
	assert(m->msg_id == MSG_ID_RUN_P);
sl@0
   215
	run_msg* rm = (run_msg*)m;
sl@0
   216
	assert(rm->task_id == task_id);
sl@0
   217
	assert(m->sending_task_id == task_id2);
sl@0
   218
	r = rm->parameter;
sl@0
   219
	if (pt)
sl@0
   220
		*pt = rm->tmcount;
sl@0
   221
	free_mem_block(m);
sl@0
   222
	return r;
sl@0
   223
	}
sl@0
   224
sl@0
   225
int wait_for_signal_p(int task_id, unsigned* pt)
sl@0
   226
	{
sl@0
   227
	msghdr* m = NULL;
sl@0
   228
	int r = recv_msg(&m, WAIT_FOREVER);
sl@0
   229
	assert(r == OK);
sl@0
   230
	assert(m->msg_id == MSG_ID_RUN_P);
sl@0
   231
	run_msg* rm = (run_msg*)m;
sl@0
   232
	assert(rm->task_id == task_id);
sl@0
   233
	r = rm->parameter;
sl@0
   234
	if (pt)
sl@0
   235
		*pt = rm->tmcount;
sl@0
   236
	free_mem_block(m);
sl@0
   237
	return r;
sl@0
   238
	}
sl@0
   239
sl@0
   240
void resume_4(int t1, int t2, int t3, int t4)
sl@0
   241
	{
sl@0
   242
	if (t1>=0)
sl@0
   243
		assert(resume_task(t1)==OK);
sl@0
   244
	if (t2>=0)
sl@0
   245
		assert(resume_task(t2)==OK);
sl@0
   246
	if (t3>=0)
sl@0
   247
		assert(resume_task(t3)==OK);
sl@0
   248
	if (t4>=0)
sl@0
   249
		assert(resume_task(t4)==OK);
sl@0
   250
	}
sl@0
   251
sl@0
   252
void check_signal_4(int t1, int t2, int t3, int t4)
sl@0
   253
	{
sl@0
   254
	if (t1>=0)
sl@0
   255
		check_for_signal(t1);
sl@0
   256
	else
sl@0
   257
		check_no_signal();
sl@0
   258
	if (t2>=0)
sl@0
   259
		check_for_signal(t2);
sl@0
   260
	else
sl@0
   261
		check_no_signal();
sl@0
   262
	if (t3>=0)
sl@0
   263
		check_for_signal(t3);
sl@0
   264
	else
sl@0
   265
		check_no_signal();
sl@0
   266
	if (t4>=0)
sl@0
   267
		check_for_signal(t4);
sl@0
   268
	else
sl@0
   269
		check_no_signal();
sl@0
   270
	}
sl@0
   271
sl@0
   272
void check_for_multiple_signals(int task_id, int count)
sl@0
   273
	{
sl@0
   274
	unsigned t = check_for_signal(task_id);
sl@0
   275
	while (--count)
sl@0
   276
		{
sl@0
   277
		unsigned t2 = check_for_signal(task_id);
sl@0
   278
		assert(t2 - t >= 1);
sl@0
   279
		t = t2;
sl@0
   280
		}
sl@0
   281
	}
sl@0
   282
sl@0
   283
int flush_signals(void)
sl@0
   284
	{
sl@0
   285
	int c = 0;
sl@0
   286
	for (;;)
sl@0
   287
		{
sl@0
   288
		msghdr* m = NULL;
sl@0
   289
		int r = recv_msg(&m, NO_WAIT);
sl@0
   290
		if (r == TIMED_OUT)
sl@0
   291
			break;
sl@0
   292
		assert(r == OK);
sl@0
   293
		assert(m->msg_id == MSG_ID_RUN);
sl@0
   294
		free_mem_block(m);
sl@0
   295
		++c;
sl@0
   296
		}
sl@0
   297
	return c;
sl@0
   298
	}
sl@0
   299
sl@0
   300
void test_mem_pool(size_t size, int count, void** chain)
sl@0
   301
	{
sl@0
   302
	int i, fill;
sl@0
   303
	void *b, *bb, *c;
sl@0
   304
	c = *chain;
sl@0
   305
	for (i=0; i<count; ++i)
sl@0
   306
		{
sl@0
   307
		b = alloc_mem_block(size);
sl@0
   308
		assert(b != NULL);
sl@0
   309
		fill = (int)(size>>5);
sl@0
   310
		fill += 29;
sl@0
   311
		fill *= fill;
sl@0
   312
		fill &= 0xff;
sl@0
   313
		memset(b, fill, size);
sl@0
   314
		*(void**)b = c;
sl@0
   315
		((int*)b)[1] = (int)size;
sl@0
   316
		c = b;
sl@0
   317
		}
sl@0
   318
	bb = alloc_mem_block(size);
sl@0
   319
	assert(bb == NULL);
sl@0
   320
	*chain = c;
sl@0
   321
	}
sl@0
   322
sl@0
   323
void check_blocks(void* chain)
sl@0
   324
	{
sl@0
   325
	void* p = chain;
sl@0
   326
	while (p)
sl@0
   327
		{
sl@0
   328
		unsigned char *q, *qq;
sl@0
   329
		int size, fill, x;
sl@0
   330
		size = ((int*)p)[1];
sl@0
   331
		fill = (size>>5)+29;
sl@0
   332
		fill = (fill*fill)&0xff;
sl@0
   333
		q = (unsigned char*)p + sizeof(void*) + sizeof(int);
sl@0
   334
		qq = (unsigned char*)p + size;
sl@0
   335
		x = 0;
sl@0
   336
		while (q<qq)
sl@0
   337
			x |= (*q++ ^ fill);
sl@0
   338
		assert(x==0);
sl@0
   339
		p = *(void**)p;
sl@0
   340
		}
sl@0
   341
	}
sl@0
   342
sl@0
   343
int free_blocks(void* chain)
sl@0
   344
	{
sl@0
   345
	void* p = chain;
sl@0
   346
	int c = 0;
sl@0
   347
	while (p)
sl@0
   348
		{
sl@0
   349
		void* n = *(void**)p;
sl@0
   350
		free_mem_block(p);
sl@0
   351
		p = n;
sl@0
   352
		++c;
sl@0
   353
		}
sl@0
   354
	return c;
sl@0
   355
	}
sl@0
   356
sl@0
   357
void test_mem_mgr(void)
sl@0
   358
	{
sl@0
   359
	void* chain = NULL;
sl@0
   360
	const poolinfo* pi = pool_list;
sl@0
   361
	int nblocks = 0;
sl@0
   362
	int nfreed = 0;
sl@0
   363
	for (; pi->block_size; ++pi)
sl@0
   364
		{
sl@0
   365
		nblocks += pi->block_count;
sl@0
   366
		test_mem_pool(pi->block_size, pi->block_count, &chain);
sl@0
   367
		}
sl@0
   368
	check_blocks(chain);
sl@0
   369
	nfreed = free_blocks(chain);
sl@0
   370
	assert(nfreed == nblocks);
sl@0
   371
	chain = NULL;
sl@0
   372
	for (--pi; pi >= pool_list; --pi)
sl@0
   373
		test_mem_pool(pi->block_size, pi->block_count, &chain);
sl@0
   374
	check_blocks(chain);
sl@0
   375
	nfreed = free_blocks(chain);
sl@0
   376
	assert(nfreed == nblocks);
sl@0
   377
	chain = NULL;
sl@0
   378
	kprintf("Memory Manager Test OK");
sl@0
   379
	}
sl@0
   380
sl@0
   381
void test_suspend_1(void)
sl@0
   382
	{
sl@0
   383
	unsigned t1, t2, t3;
sl@0
   384
	int r;
sl@0
   385
	t1 = tmcount;
sl@0
   386
	delay(5*TM_PERIOD);
sl@0
   387
	t2 = tmcount;
sl@0
   388
	assert( ((int)t2)-((int)t1) >= 5 );
sl@0
   389
	r = suspend_task(TM_TASK);
sl@0
   390
	assert(r == OK);
sl@0
   391
	t1 = tmcount;
sl@0
   392
	delay(5*TM_PERIOD);
sl@0
   393
	t2 = tmcount;
sl@0
   394
	assert(t2==t1);
sl@0
   395
	r = resume_task(TM_TASK);
sl@0
   396
	assert(r == OK);
sl@0
   397
	t3 = tmcount;
sl@0
   398
	assert( ((int)t3)-((int)t2) >= 5 );
sl@0
   399
sl@0
   400
	r = suspend_task(TM_TASK);
sl@0
   401
	assert(r == OK);
sl@0
   402
	r = suspend_task(TM_TASK);
sl@0
   403
	assert(r == OK);
sl@0
   404
	t1 = tmcount;
sl@0
   405
	delay(5*TM_PERIOD);
sl@0
   406
	t2 = tmcount;
sl@0
   407
	assert(t2==t1);
sl@0
   408
	r = resume_task(TM_TASK);
sl@0
   409
	assert(r == OK);
sl@0
   410
	t3 = tmcount;
sl@0
   411
	assert(t3==t2);
sl@0
   412
	r = resume_task(TM_TASK);
sl@0
   413
	assert(r == OK);
sl@0
   414
	t3 = tmcount;
sl@0
   415
	assert( ((int)t3)-((int)t2) >= 5 );
sl@0
   416
sl@0
   417
	r = suspend_task(-1);
sl@0
   418
	assert(r == BAD_TASK_ID);
sl@0
   419
	r = suspend_task(300);
sl@0
   420
	assert(r == BAD_TASK_ID);
sl@0
   421
	r = suspend_task(NONEXISTENT_TASK);
sl@0
   422
	assert(r == BAD_TASK_ID);
sl@0
   423
	r = resume_task(-1);
sl@0
   424
	assert(r == BAD_TASK_ID);
sl@0
   425
	r = resume_task(300);
sl@0
   426
	assert(r == BAD_TASK_ID);
sl@0
   427
	r = resume_task(NONEXISTENT_TASK);
sl@0
   428
	assert(r == BAD_TASK_ID);
sl@0
   429
sl@0
   430
	kprintf("test_suspend_1 OK");
sl@0
   431
	}
sl@0
   432
sl@0
   433
void test_priority_scheduling(void)
sl@0
   434
	{
sl@0
   435
	int init_pri = get_task_priority(current_task_id());
sl@0
   436
	resume_4(TASK1, TASK2, TASK3, TASK4);
sl@0
   437
	delay(80*TM_PERIOD);
sl@0
   438
	check_for_multiple_signals(TASK1, 50);	// check no timeslicing
sl@0
   439
	assert(flush_signals()<=31);
sl@0
   440
	suspend_task(TASK1);
sl@0
   441
	delay(80*TM_PERIOD);
sl@0
   442
	check_for_multiple_signals(TASK2, 50);	// check no timeslicing
sl@0
   443
	assert(flush_signals()<=31);
sl@0
   444
	suspend_task(TASK2);
sl@0
   445
	delay(80*TM_PERIOD);
sl@0
   446
	check_for_multiple_signals(TASK3, 50);	// check no timeslicing
sl@0
   447
	assert(flush_signals()<=31);
sl@0
   448
	suspend_task(TASK3);
sl@0
   449
	delay(1);
sl@0
   450
	check_for_signal(TASK4);
sl@0
   451
	assert(flush_signals()<=1);
sl@0
   452
sl@0
   453
	t1func = 1;
sl@0
   454
	t2func = 1;
sl@0
   455
	t3func = 1;
sl@0
   456
	t4func = 1;
sl@0
   457
sl@0
   458
	resume_4(TASK1, TASK2, TASK3, TASK4);
sl@0
   459
	delay(10);
sl@0
   460
	flush_signals();
sl@0
   461
sl@0
   462
	resume_4(TASK3, TASK2, TASK4, TASK1);
sl@0
   463
	delay(10);
sl@0
   464
	check_signal_4(TASK3, TASK2, TASK4, TASK1);
sl@0
   465
	check_no_signal();
sl@0
   466
	resume_4(TASK1, TASK2, TASK3, TASK4);
sl@0
   467
	check_no_signal();	// all lower priority so don't run
sl@0
   468
	set_task_priority(TASK2, 255);		// higher than current task so run immediately
sl@0
   469
	check_for_signal(TASK2);
sl@0
   470
	set_task_priority(TASK4, 116);
sl@0
   471
	check_no_signal();	// all lower priority so don't run
sl@0
   472
	delay(10);
sl@0
   473
	check_for_signal(TASK4);
sl@0
   474
	check_for_signal(TASK1);
sl@0
   475
	check_for_signal(TASK3);
sl@0
   476
	set_task_priority(TASK1, 116);
sl@0
   477
	set_task_priority(TASK2, 116);
sl@0
   478
	set_task_priority(TASK3, 116);
sl@0
   479
	set_task_priority(TASK4, 116);
sl@0
   480
	resume_4(TASK1, TASK2, TASK3, TASK4);
sl@0
   481
	set_task_priority(current_task_id(), 112);	// drop current task priority
sl@0
   482
	assert(get_task_priority(current_task_id())==112);
sl@0
   483
	check_signal_4(TASK1, TASK2, TASK3, TASK4);
sl@0
   484
	set_task_priority(current_task_id(), init_pri);
sl@0
   485
	assert(get_task_priority(current_task_id())==init_pri);
sl@0
   486
	
sl@0
   487
	kprintf("test_priority_scheduling OK");
sl@0
   488
	}
sl@0
   489
sl@0
   490
unsigned sem_test(int task_id)
sl@0
   491
	{
sl@0
   492
	int r = semaphore_signal(TEST_SEM);
sl@0
   493
	assert(r==OK);
sl@0
   494
	return check_for_signal(task_id);
sl@0
   495
	}
sl@0
   496
sl@0
   497
unsigned sem_test_p(int task_id, int parameter)
sl@0
   498
	{
sl@0
   499
	unsigned t;
sl@0
   500
	int r = semaphore_signal(TEST_SEM);
sl@0
   501
	assert(r==OK);
sl@0
   502
	r = check_for_signal_p(task_id, task_id, &t);
sl@0
   503
	assert(r == parameter);
sl@0
   504
	return t;
sl@0
   505
	}
sl@0
   506
sl@0
   507
unsigned sem_test_pt(int task_id, int parameter)
sl@0
   508
	{
sl@0
   509
	unsigned t;
sl@0
   510
	int r = semaphore_signal(TEST_SEM);
sl@0
   511
	assert(r==OK);
sl@0
   512
	r = check_for_signal_p(task_id, task_id, &t);
sl@0
   513
	assert(r == parameter);
sl@0
   514
	return t;
sl@0
   515
	}
sl@0
   516
sl@0
   517
void test_semaphore(void)
sl@0
   518
	{
sl@0
   519
	unsigned t1, t2, t3;
sl@0
   520
	int r;
sl@0
   521
	int init_pri = get_task_priority(current_task_id());
sl@0
   522
	set_task_priority(TASK1, 128);
sl@0
   523
	set_task_priority(TASK2, 128);
sl@0
   524
	set_task_priority(TASK3, 128);
sl@0
   525
	set_task_priority(TASK4, 128);
sl@0
   526
	t1func = 2;
sl@0
   527
	t2func = 2;
sl@0
   528
	t3func = 2;
sl@0
   529
	t4func = 2;
sl@0
   530
	resume_4(TASK1, TASK2, TASK3, TASK4);
sl@0
   531
	delay(10);		// let tasks wait on semaphore
sl@0
   532
	check_no_signal();
sl@0
   533
	sem_test(TASK1);	// test they are released in same order
sl@0
   534
	sem_test(TASK2);
sl@0
   535
	sem_test(TASK3);
sl@0
   536
	sem_test(TASK4);
sl@0
   537
	check_no_signal();
sl@0
   538
	set_task_priority(TASK3, 132);	// test highest priority is released first
sl@0
   539
	sem_test(TASK3);
sl@0
   540
	sem_test(TASK3);
sl@0
   541
	suspend_task(TASK3);		// test suspended task doesn't contend for semaphore
sl@0
   542
	sem_test(TASK1);
sl@0
   543
	sem_test(TASK2);
sl@0
   544
	sem_test(TASK4);
sl@0
   545
	sem_test(TASK1);
sl@0
   546
	suspend_task(TASK2);
sl@0
   547
	sem_test(TASK4);
sl@0
   548
	sem_test(TASK1);
sl@0
   549
	sem_test(TASK4);
sl@0
   550
	set_task_priority(TASK2, 136);	// change priority while suspended
sl@0
   551
	sem_test(TASK1);
sl@0
   552
	sem_test(TASK4);
sl@0
   553
	sem_test(TASK1);
sl@0
   554
	resume_task(TASK2);
sl@0
   555
	sem_test(TASK2);
sl@0
   556
	sem_test(TASK2);	// test new highest priority task acquires semaphore first
sl@0
   557
	delay(100*TM_PERIOD);
sl@0
   558
	check_no_signal();	// check waits don't time out
sl@0
   559
sl@0
   560
	t2func = 3;			// switch over to timed waits for task 2
sl@0
   561
	t1 = sem_test(TASK2);			// get one last message of previous type
sl@0
   562
	delay(5*TM_PERIOD);
sl@0
   563
	t2 = sem_test_p(TASK2, OK);		// signal after half the timeout and check OK
sl@0
   564
	delay(11*TM_PERIOD);			// wait for > timeout
sl@0
   565
	r = check_for_signal_p(TASK2, TASK2, &t3);
sl@0
   566
	assert(r == TIMED_OUT);
sl@0
   567
	kprintf("t2-t1=%d t3-t2=%d", t2-t1, t3-t2);
sl@0
   568
	assert(t2-t1 >= 5);
sl@0
   569
	assert(t3-t2 >= 10);
sl@0
   570
	sem_test_p(TASK2, OK);
sl@0
   571
	resume_task(TASK3);
sl@0
   572
sl@0
   573
	set_task_priority(current_task_id(), 176);	// raise current task priority
sl@0
   574
	semaphore_signal(TEST_SEM);		// signal semaphore 4 times - should release all 4 waiting threads
sl@0
   575
	semaphore_signal(TEST_SEM);
sl@0
   576
	semaphore_signal(TEST_SEM);
sl@0
   577
	semaphore_signal(TEST_SEM);
sl@0
   578
	set_task_priority(current_task_id(), init_pri);	// let tasks run
sl@0
   579
	r = check_for_signal_p(TASK2, TASK2, NULL);
sl@0
   580
	assert(r == OK);
sl@0
   581
	check_for_signal(TASK3);
sl@0
   582
	check_for_signal(TASK4);
sl@0
   583
	check_for_signal(TASK1);
sl@0
   584
	set_task_priority(current_task_id(), 176);	// raise current task priority
sl@0
   585
	busy_wait(11);					// let semaphore wait time out
sl@0
   586
	t1func = 4;						// switch all threads over
sl@0
   587
	t2func = 4;						//
sl@0
   588
	t3func = 4;						//
sl@0
   589
	t4func = 4;						//
sl@0
   590
	semaphore_signal(TEST_SEM);		// signal semaphore 3 times - should release other 3 waiting threads
sl@0
   591
	semaphore_signal(TEST_SEM);
sl@0
   592
	semaphore_signal(TEST_SEM);
sl@0
   593
	set_task_priority(current_task_id(), init_pri);	// let tasks run
sl@0
   594
	r = check_for_signal_p(TASK2, TASK2, NULL);
sl@0
   595
	assert(r == TIMED_OUT);
sl@0
   596
	check_for_signal(TASK3);
sl@0
   597
	check_for_signal(TASK4);
sl@0
   598
	check_for_signal(TASK1);
sl@0
   599
sl@0
   600
	kprintf("test_semaphore OK");
sl@0
   601
	}
sl@0
   602
sl@0
   603
void test_message_queue(void)
sl@0
   604
	{
sl@0
   605
	unsigned t1, t2, t3, t4;
sl@0
   606
	int tid, p, r;
sl@0
   607
	int init_pri = get_task_priority(current_task_id());
sl@0
   608
	p = 0;
sl@0
   609
	t1 = 0;
sl@0
   610
	for (tid = TASK1; tid <= TASK4; ++tid)
sl@0
   611
		{
sl@0
   612
		for (p = 1; p; p<<=1)
sl@0
   613
			{
sl@0
   614
			tsend_run_signal_p(tid, p);
sl@0
   615
			r = check_for_signal_p(OC_TASK, tid, NULL);
sl@0
   616
			assert(r == p);
sl@0
   617
			}
sl@0
   618
		}
sl@0
   619
	check_no_signal();
sl@0
   620
	set_task_priority(current_task_id(), 176);	// raise current task priority
sl@0
   621
	set_task_priority(TASK4, 144);	// change task priorities while they are waiting
sl@0
   622
	set_task_priority(TASK3, 140);
sl@0
   623
	set_task_priority(TASK2, 136);
sl@0
   624
	set_task_priority(TASK1, 132);
sl@0
   625
	t1func = 5;	// switch task 1 to timed waits
sl@0
   626
	for (tid = TASK1; tid <= TASK4; ++tid)
sl@0
   627
		{
sl@0
   628
		for (p = 0; p<0x40000000; p+=(0x413b9cb+tid))
sl@0
   629
			{
sl@0
   630
			tsend_run_signal_p(tid, p);	// let multiple messages accumulate on the queues
sl@0
   631
			}
sl@0
   632
		}
sl@0
   633
	check_no_signal();
sl@0
   634
	set_task_priority(current_task_id(), init_pri);	// let tasks run
sl@0
   635
	kprintf("init_pri=%d",init_pri);
sl@0
   636
	for (tid = TASK4; tid >= TASK1; --tid)
sl@0
   637
		{
sl@0
   638
		for (p = 0; p<0x40000000; p+=(0x413b9cb+tid))
sl@0
   639
			{
sl@0
   640
			r = check_for_signal_p(OC_TASK, tid, &t1);
sl@0
   641
			assert(r == p);
sl@0
   642
			}
sl@0
   643
		}
sl@0
   644
sl@0
   645
	delay(5*TM_PERIOD);
sl@0
   646
	tsend_run_signal_p(TASK1, p);		// send after half timeout
sl@0
   647
	r = check_for_signal_p(OC_TASK, TASK1, &t2);
sl@0
   648
	assert(r == p);
sl@0
   649
	delay(11*TM_PERIOD);				// wait for > timeout
sl@0
   650
	tsend_run_signal_p(TASK1, ~p);		// send after timeout
sl@0
   651
	r = check_for_signal_p(TASK1, TASK1, &t3);
sl@0
   652
	assert(r == TIMED_OUT);
sl@0
   653
	kprintf("t2-t1=%d t3-t2=%d", t2-t1, t3-t2);
sl@0
   654
	assert(t2-t1 >= 5);
sl@0
   655
	assert(t3-t2 >= 10);
sl@0
   656
	r = check_for_signal_p(OC_TASK, TASK1, &t4);
sl@0
   657
	assert(r == ~p);
sl@0
   658
	assert(t4-t3 <= 1);
sl@0
   659
	t1func = 6;						// switch task 1 to timed semaphore wait
sl@0
   660
	t2func = 7;						// switch task 2 to timed queue wait
sl@0
   661
	t3func = 8;						//
sl@0
   662
	t4func = 8;						//
sl@0
   663
	for (tid = TASK1; tid <= TASK4; ++tid)
sl@0
   664
		{
sl@0
   665
		tsend_run_signal_p(tid, 0);
sl@0
   666
		r = check_for_signal_p(OC_TASK, tid, NULL);
sl@0
   667
		assert(r == 0);
sl@0
   668
		}
sl@0
   669
	check_no_signal();
sl@0
   670
sl@0
   671
	kprintf("test_message_queue OK");
sl@0
   672
	}
sl@0
   673
sl@0
   674
void random_isr(unsigned n)
sl@0
   675
	{
sl@0
   676
	random_isr_msg* m;
sl@0
   677
	unsigned extra = 1;
sl@0
   678
	unsigned count = 1;
sl@0
   679
	int r;
sl@0
   680
	if (!(n%11))
sl@0
   681
		++count;
sl@0
   682
	if (!(n%13))
sl@0
   683
		++count;
sl@0
   684
	while (count--)
sl@0
   685
		{
sl@0
   686
		m = (random_isr_msg*)alloc_mem_block(sizeof(random_isr_msg));
sl@0
   687
		m->header.msg_id = MSG_ID_RND_ISR;
sl@0
   688
		m->random_isr_number = n;
sl@0
   689
		extra *= n;
sl@0
   690
		m->extra = extra;
sl@0
   691
		r = send_msg(L1_TASK, &m->header);
sl@0
   692
		}
sl@0
   693
	if (random_sem_signal_count && !--random_sem_signal_count)
sl@0
   694
		{
sl@0
   695
		random_sem_signal_count = random_sem_signal_interval;
sl@0
   696
		semaphore_signal(ISR_SEM);
sl@0
   697
		}
sl@0
   698
	}
sl@0
   699
sl@0
   700
void flush_queue(msghdr** f, msghdr** l, msghdr* tm)
sl@0
   701
	{
sl@0
   702
	msghdr* m = *f;
sl@0
   703
	*f = NULL;
sl@0
   704
	*l = NULL;
sl@0
   705
	send_to_epoc(tm);
sl@0
   706
	while (m)
sl@0
   707
		{
sl@0
   708
		msghdr* n = m->next;
sl@0
   709
		send_to_epoc(m);
sl@0
   710
		m = n;
sl@0
   711
		}
sl@0
   712
	}
sl@0
   713
sl@0
   714
void l1_task_entry(void)
sl@0
   715
	{
sl@0
   716
	msghdr* first = NULL;
sl@0
   717
	msghdr* last = NULL;
sl@0
   718
	unsigned state = 0;
sl@0
   719
	unsigned extra_count = 0;
sl@0
   720
	unsigned extra_value = 0;
sl@0
   721
	assert(current_task_id() == L1_TASK);
sl@0
   722
	kprintf("L1_TASK running");
sl@0
   723
	for (;;)
sl@0
   724
		{
sl@0
   725
		msghdr* m = NULL;
sl@0
   726
		int r = recv_msg(&m, WAIT_FOREVER);
sl@0
   727
		assert(r == OK);
sl@0
   728
		switch (m->msg_id)
sl@0
   729
			{
sl@0
   730
			case MSG_ID_RND_ISR:
sl@0
   731
				{
sl@0
   732
				random_isr_msg* rm = (random_isr_msg*)m;
sl@0
   733
				assert(m->sending_task_id == TASK_ID_ISR);
sl@0
   734
				assert(rm->random_isr_number == next_random_id);
sl@0
   735
				if (state == 0)
sl@0
   736
					{
sl@0
   737
					extra_count = 0;
sl@0
   738
					if (!(next_random_id % 11))
sl@0
   739
						++extra_count;
sl@0
   740
					if (!(next_random_id % 13))
sl@0
   741
						++extra_count;
sl@0
   742
					extra_value = next_random_id;
sl@0
   743
					}
sl@0
   744
				else if (state > 0)
sl@0
   745
					{
sl@0
   746
					extra_value *= next_random_id;
sl@0
   747
					}
sl@0
   748
				assert(rm->extra == extra_value);
sl@0
   749
				if (++state > extra_count)
sl@0
   750
					state = 0;
sl@0
   751
				if (state == 0)
sl@0
   752
					++next_random_id;
sl@0
   753
				if (rm->random_isr_number == 0)
sl@0
   754
					send_msg(OC_TASK, m), m=NULL;
sl@0
   755
				if (state == 1 && extra_count == 2 && m)
sl@0
   756
					{
sl@0
   757
					flush_queue(&first, &last, m);
sl@0
   758
					m = NULL;
sl@0
   759
					}
sl@0
   760
				if (random_send_count && !--random_send_count)
sl@0
   761
					{
sl@0
   762
					random_send_count = random_send_interval;
sl@0
   763
					if (m)
sl@0
   764
						send_msg(TASK2, m), m=NULL;
sl@0
   765
					}
sl@0
   766
				break;
sl@0
   767
				}
sl@0
   768
			case MSG_ID_DATA:
sl@0
   769
				m->next = NULL;
sl@0
   770
				if (last)
sl@0
   771
					last->next = m;
sl@0
   772
				else
sl@0
   773
					first = m;
sl@0
   774
				last = m;
sl@0
   775
				m = NULL;
sl@0
   776
				break;
sl@0
   777
			case MSG_ID_FLUSH:
sl@0
   778
				flush_queue(&first, &last, m);
sl@0
   779
				m = NULL;
sl@0
   780
				break;
sl@0
   781
			default:
sl@0
   782
				kprintf("L1<-%08x",m->msg_id);
sl@0
   783
				break;
sl@0
   784
			}
sl@0
   785
		if (m)
sl@0
   786
			free_mem_block(m);
sl@0
   787
		}
sl@0
   788
	}
sl@0
   789
sl@0
   790
void l2_task_entry(void)
sl@0
   791
	{
sl@0
   792
	assert(current_task_id() == L2_TASK);
sl@0
   793
	kprintf("L2_TASK running");
sl@0
   794
	for (;;)
sl@0
   795
		{
sl@0
   796
		msghdr* m = NULL;
sl@0
   797
		int r = recv_msg(&m, WAIT_FOREVER);
sl@0
   798
		assert(r == OK);
sl@0
   799
		switch (m->msg_id)
sl@0
   800
			{
sl@0
   801
			case MSG_ID_DATA:
sl@0
   802
				{
sl@0
   803
				data_msg* dm = (data_msg*)m;
sl@0
   804
				int i;
sl@0
   805
				unsigned char cs = 0;
sl@0
   806
				for (i=0; i<dm->length; ++i)
sl@0
   807
					cs = (unsigned char)(cs + dm->data[i]);
sl@0
   808
				dm->checksum = cs;
sl@0
   809
				send_msg(L1_TASK, m);
sl@0
   810
				m=NULL;
sl@0
   811
				break;
sl@0
   812
				}
sl@0
   813
			default:
sl@0
   814
				kprintf("L2<-%08x",m->msg_id);
sl@0
   815
				break;
sl@0
   816
			}
sl@0
   817
		if (m)
sl@0
   818
			free_mem_block(m);
sl@0
   819
		}
sl@0
   820
	}
sl@0
   821
sl@0
   822
void rr_task_entry(void)
sl@0
   823
	{
sl@0
   824
	assert(current_task_id() == RR_TASK);
sl@0
   825
	kprintf("RR_TASK running");
sl@0
   826
	for (;;)
sl@0
   827
		{
sl@0
   828
		msghdr* m = NULL;
sl@0
   829
		int r = recv_msg(&m, WAIT_FOREVER);
sl@0
   830
		assert(r == OK);
sl@0
   831
		switch (m->msg_id)
sl@0
   832
			{
sl@0
   833
			case MSG_ID_DATA:
sl@0
   834
				send_msg(L2_TASK, m);
sl@0
   835
				m=NULL;
sl@0
   836
				break;
sl@0
   837
			default:
sl@0
   838
				kprintf("RR<-%08x",m->msg_id);
sl@0
   839
				break;
sl@0
   840
			}
sl@0
   841
		if (m)
sl@0
   842
			free_mem_block(m);
sl@0
   843
		}
sl@0
   844
	}
sl@0
   845
sl@0
   846
void tm_task_entry(void)
sl@0
   847
	{
sl@0
   848
	assert(current_task_id() == TM_TASK);
sl@0
   849
	kprintf("TM_TASK running");
sl@0
   850
	for (;;)
sl@0
   851
		{
sl@0
   852
		msghdr* m = NULL;
sl@0
   853
		int r = recv_msg(&m, WAIT_FOREVER);
sl@0
   854
		assert(r == OK);
sl@0
   855
		switch (m->msg_id)
sl@0
   856
			{
sl@0
   857
			case MSG_ID_TIMEOUT:
sl@0
   858
				tmcount = ((timer_msg*)m)->count;
sl@0
   859
				assert(m->sending_task_id == TASK_ID_ISR);
sl@0
   860
				if (!(tmcount & 255))
sl@0
   861
					{
sl@0
   862
					report_msg* rpt = (report_msg*)alloc_mem_block(sizeof(report_msg));
sl@0
   863
					rpt->header.msg_id = MSG_ID_TM_RPT;
sl@0
   864
					rpt->count = tmcount;
sl@0
   865
					rpt->ok_count = 0;
sl@0
   866
					rpt->bad_count = 0;
sl@0
   867
					send_to_epoc(&rpt->header);
sl@0
   868
					}
sl@0
   869
				break;
sl@0
   870
			default:
sl@0
   871
				kprintf("TM<-%08x",m->msg_id);
sl@0
   872
				break;
sl@0
   873
			}
sl@0
   874
		free_mem_block(m);
sl@0
   875
		}
sl@0
   876
	}
sl@0
   877
sl@0
   878
void generic_task(volatile int* f)
sl@0
   879
	{
sl@0
   880
	int r;
sl@0
   881
	msghdr* m;
sl@0
   882
	unsigned t1, t2;
sl@0
   883
	unsigned count = 0;
sl@0
   884
	unsigned ok_count = 0;
sl@0
   885
	unsigned bad_count = 0;
sl@0
   886
	while (*f==0)
sl@0
   887
		{
sl@0
   888
		send_run_signal();
sl@0
   889
		busy_wait(1);
sl@0
   890
		}
sl@0
   891
	while (*f==1)
sl@0
   892
		{
sl@0
   893
		send_run_signal();
sl@0
   894
		suspend_task(current_task_id());
sl@0
   895
		}
sl@0
   896
	while (*f==2)
sl@0
   897
		{
sl@0
   898
		r = semaphore_wait(TEST_SEM, WAIT_FOREVER);
sl@0
   899
		assert(r == OK);
sl@0
   900
		send_run_signal();
sl@0
   901
		}
sl@0
   902
	while (*f==3)
sl@0
   903
		{
sl@0
   904
		r = semaphore_wait(TEST_SEM, 10*TM_PERIOD);
sl@0
   905
		assert(r==OK || r==TIMED_OUT);
sl@0
   906
		send_run_signal_p(r);
sl@0
   907
		}
sl@0
   908
	while (*f==4)
sl@0
   909
		{
sl@0
   910
		r = recv_msg(&m, WAIT_FOREVER);
sl@0
   911
		assert(r==OK);
sl@0
   912
		assert(m->sending_task_id == OC_TASK);
sl@0
   913
		r = send_msg(OC_TASK, m);
sl@0
   914
		assert(r == OK);
sl@0
   915
		}
sl@0
   916
	while (*f==5)
sl@0
   917
		{
sl@0
   918
		r = recv_msg(&m, 10*TM_PERIOD);
sl@0
   919
		assert(r==OK || r==TIMED_OUT);
sl@0
   920
		if (r == OK)
sl@0
   921
			{
sl@0
   922
			assert(m->sending_task_id == OC_TASK);
sl@0
   923
			r = send_msg(OC_TASK, m);
sl@0
   924
			assert(r == OK);
sl@0
   925
			}
sl@0
   926
		else
sl@0
   927
			send_run_signal_p(r);
sl@0
   928
		}
sl@0
   929
	while (*f==6)
sl@0
   930
		{
sl@0
   931
		t1 = tick_count();
sl@0
   932
		r = semaphore_wait(ISR_SEM, 5);
sl@0
   933
		t2 = tick_count() - t1;
sl@0
   934
		if (r == TIMED_OUT && t2<5)
sl@0
   935
			{
sl@0
   936
			kprintf("SEM timed out too soon: %d", t2);
sl@0
   937
			++bad_count;
sl@0
   938
			}
sl@0
   939
		if (r == OK)
sl@0
   940
			++ok_count;
sl@0
   941
		++count;
sl@0
   942
		if (!(count & 0xff))
sl@0
   943
			{
sl@0
   944
			report_msg* rpt = (report_msg*)alloc_mem_block(sizeof(report_msg));
sl@0
   945
			rpt->header.msg_id = MSG_ID_SEM_RPT;
sl@0
   946
			rpt->count = count;
sl@0
   947
			rpt->ok_count = ok_count;
sl@0
   948
			rpt->bad_count = bad_count;
sl@0
   949
			send_to_epoc(&rpt->header);
sl@0
   950
			}
sl@0
   951
		}
sl@0
   952
	while (*f==7)
sl@0
   953
		{
sl@0
   954
		t1 = tick_count();
sl@0
   955
		r = recv_msg(&m, 5);
sl@0
   956
		t2 = tick_count() - t1;
sl@0
   957
		if (r == TIMED_OUT && t2<5)
sl@0
   958
			{
sl@0
   959
			kprintf("RECV timed out too soon: %d", t2);
sl@0
   960
			++bad_count;
sl@0
   961
			}
sl@0
   962
		if (r==OK)
sl@0
   963
			++ok_count, free_mem_block(m);
sl@0
   964
		++count;
sl@0
   965
		if (!(count & 0xff))
sl@0
   966
			{
sl@0
   967
			report_msg* rpt = (report_msg*)alloc_mem_block(sizeof(report_msg));
sl@0
   968
			rpt->header.msg_id = MSG_ID_RCV_RPT;
sl@0
   969
			rpt->count = count;
sl@0
   970
			rpt->ok_count = ok_count;
sl@0
   971
			rpt->bad_count = bad_count;
sl@0
   972
			send_to_epoc(&rpt->header);
sl@0
   973
			}
sl@0
   974
		}
sl@0
   975
	kprintf("Task %d finished", current_task_id());
sl@0
   976
	for(;;)
sl@0
   977
		suspend_task(current_task_id());
sl@0
   978
	}
sl@0
   979
sl@0
   980
void task1_entry(void)
sl@0
   981
	{
sl@0
   982
	assert(current_task_id() == TASK1);
sl@0
   983
	generic_task(&t1func);
sl@0
   984
	}
sl@0
   985
sl@0
   986
void task2_entry(void)
sl@0
   987
	{
sl@0
   988
	assert(current_task_id() == TASK2);
sl@0
   989
	generic_task(&t2func);
sl@0
   990
	}
sl@0
   991
sl@0
   992
void task3_entry(void)
sl@0
   993
	{
sl@0
   994
	assert(current_task_id() == TASK3);
sl@0
   995
	generic_task(&t3func);
sl@0
   996
	}
sl@0
   997
sl@0
   998
void task4_entry(void)
sl@0
   999
	{
sl@0
  1000
	assert(current_task_id() == TASK4);
sl@0
  1001
	generic_task(&t4func);
sl@0
  1002
	}
sl@0
  1003
sl@0
  1004
sl@0
  1005
sl@0
  1006
void oo_overall_control(void)
sl@0
  1007
	{
sl@0
  1008
	int r;
sl@0
  1009
	msghdr* m;
sl@0
  1010
	random_isr_msg* rm;
sl@0
  1011
	unsigned t1, t2, rss_interval;
sl@0
  1012
	kprintf("OC_TASK running");
sl@0
  1013
	assert(current_task_id() == OC_TASK);
sl@0
  1014
	resume_task(L2_TASK);
sl@0
  1015
	resume_task(RR_TASK);
sl@0
  1016
	resume_task(TM_TASK);
sl@0
  1017
	test_mem_mgr();
sl@0
  1018
sl@0
  1019
	kprintf("Wait for init msg");
sl@0
  1020
	r = recv_msg(&m, WAIT_FOREVER);
sl@0
  1021
	assert(r == OK);
sl@0
  1022
	assert(m->msg_id == MSG_ID_INIT);
sl@0
  1023
	assert(m->sending_task_id == TASK_ID_UNKNOWN);
sl@0
  1024
	free_mem_block(m);
sl@0
  1025
	kprintf("Received init msg");
sl@0
  1026
sl@0
  1027
	r = start_periodic_timer(TM_TIMER, TM_TASK, TM_INIT_DELAY, TM_PERIOD, NULL);
sl@0
  1028
	assert(r == OK);
sl@0
  1029
	delay(TM_INIT_DELAY-10);
sl@0
  1030
	assert(tmcount == 0);
sl@0
  1031
	delay(10*TM_PERIOD+20);
sl@0
  1032
	assert(tmcount > 0);
sl@0
  1033
	test_suspend_1();
sl@0
  1034
	test_priority_scheduling();
sl@0
  1035
	test_semaphore();
sl@0
  1036
	test_message_queue();
sl@0
  1037
sl@0
  1038
	resume_task(L1_TASK);
sl@0
  1039
	r = start_random_isr(&random_isr);
sl@0
  1040
	if (r != OK)
sl@0
  1041
		goto no_random_isr;
sl@0
  1042
sl@0
  1043
	r = recv_msg(&m, WAIT_FOREVER);
sl@0
  1044
	assert(r == OK);
sl@0
  1045
	assert(m->msg_id == MSG_ID_RND_ISR);
sl@0
  1046
	assert(m->sending_task_id == L1_TASK);
sl@0
  1047
	rm = (random_isr_msg*)m;
sl@0
  1048
	assert(rm->random_isr_number == 0);
sl@0
  1049
	free_mem_block(m);
sl@0
  1050
	t1 = next_random_id;
sl@0
  1051
	delay(1024);
sl@0
  1052
	t2 = next_random_id;
sl@0
  1053
	kprintf("%d random ISRs in 1024 ticks", t2-t1);
sl@0
  1054
	rss_interval = (5*(t2-t1)+512)/1024;
sl@0
  1055
	set_task_priority(TASK1, 196);	// needs to be higher than DfcThread1
sl@0
  1056
	set_task_priority(TASK2, 196);
sl@0
  1057
	random_sem_signal_interval = rss_interval;
sl@0
  1058
	random_sem_signal_count = rss_interval;
sl@0
  1059
	random_send_interval = rss_interval;
sl@0
  1060
	random_send_count = rss_interval;
sl@0
  1061
sl@0
  1062
no_random_isr:
sl@0
  1063
	m = (msghdr*)alloc_mem_block(sizeof(msghdr));
sl@0
  1064
	m->msg_id = MSG_ID_DONE;
sl@0
  1065
	send_to_epoc(m);
sl@0
  1066
	kprintf("All tests completed OK");
sl@0
  1067
	for (;;)
sl@0
  1068
		{
sl@0
  1069
		int r = recv_msg(&m, WAIT_FOREVER);
sl@0
  1070
		assert(r == OK);
sl@0
  1071
		switch (m->msg_id)
sl@0
  1072
			{
sl@0
  1073
			case MSG_ID_DATA:
sl@0
  1074
				send_msg(RR_TASK, m);
sl@0
  1075
				m=NULL;
sl@0
  1076
				break;
sl@0
  1077
			case MSG_ID_FLUSH:
sl@0
  1078
				send_msg(L1_TASK, m);
sl@0
  1079
				m=NULL;
sl@0
  1080
				break;
sl@0
  1081
			case MSG_ID_DONE:
sl@0
  1082
				stop_random_isr();
sl@0
  1083
				stop_timer(TM_TIMER);
sl@0
  1084
				suspend_task(L1_TASK);
sl@0
  1085
				suspend_task(L2_TASK);
sl@0
  1086
				suspend_task(RR_TASK);
sl@0
  1087
				suspend_task(TM_TASK);
sl@0
  1088
				suspend_task(TASK1);
sl@0
  1089
				suspend_task(TASK2);
sl@0
  1090
				suspend_task(TASK3);
sl@0
  1091
				suspend_task(TASK4);
sl@0
  1092
				break;
sl@0
  1093
			default:
sl@0
  1094
				kprintf("OC<-%08x",m->msg_id);
sl@0
  1095
				break;
sl@0
  1096
			}
sl@0
  1097
		if (m)
sl@0
  1098
			free_mem_block(m);
sl@0
  1099
		}
sl@0
  1100
	}
sl@0
  1101
sl@0
  1102
#ifdef __cplusplus
sl@0
  1103
}
sl@0
  1104
#endif