osを作る その13

#概要

osを作る。
avrosを使ってみた。

#サンプルコード

```

#define task_number		3
#define task1			1
#define task2			2
#define task3			3

void k_Request(int taskid);
void k_Trigger(int taskid);
void k_Exit(void);
void k_Suspend(int taskid);
void k_Resume(void);
int k_Trigger(void);
void k_Wait(int time);

static int Current;
static int Timer_Flag;
static int TC[2 * task_number + 2];

void k_Request(int taskid) {
	cli();
	TC[2 * taskid] = TC[2 * taskid] | 0x80;
	sei();
}

void k_Trigger(int taskid) {
	cli();
	TC[2 * taskid] = TC[2 * taskid] | 0x80;
}

void k_Exit(void) {
	cli();
	TC[2 * Current] = 0;
	TC[2 * Current + 1] = 0;
}

void k_Suspend(int taskid) {
	cli();
	TC[2 * Current] = (TC[2 * Current] & 0xE0) | 0x40;
	TC[2 * Current + 1] = taskid;
}

void k_Resume(void) {
	int i,
		errflag;
	cli();
	errflag = 1;
	i = 1;
	while(i <= task_number)
	{
		if (((TC[2 * i] & 0x40) == 0) || (TC[2 * i + 1] != Current))
		{
			i++;
			continue;
		}
		else
		{
			TC[2 * i] = (TC[2 * i] & 0xA0) + 1;
			errflag = 0;
			break;
		}
	}
	sei();
}

int k_Trigger(void) {
	return (TC[2 * Current] & 0x03);
}

void k_Wait(int time) {
	cli();
	TC[2 * Current] = (TC[2 * Current] & 0xC0) + 0x20;
	TC[2 * Current + 1] = time;
	Timer_Flag = 1;
	sei();
}

void task_1(void) {
	Serial.print("task-1  ");
	Serial.println(millis());
	k_Wait(20);
}

void task_2(void) {
	Serial.print("task-2  ");
	Serial.println(millis());
	k_Wait(10);
}

void task_3(void) {
	Serial.print("task-3  ");
	Serial.println(millis());
}

void setup(void) {
	int i;
	cli();
	for (i = 1; i <= task_number; i++)
	{
		TC[2 * i] = 0;
		TC[2 * i + 1] = 0;
	}
	Serial.begin(9600);
	while (!Serial)
		continue;
	cli();
	TCCR1A = 0;
	TCCR1B = 0;
	TCNT1 = 0;
	OCR1A = 1999;
	TCCR1B |= (1 << WGM12);
	TCCR1B |= (1 << CS11);
	TIMSK1 |= (1 << OCIE1A);
	Serial.println("ok");
	k_Request(task1);
	k_Request(task2);
	k_Request(task3);
	while (1)
	{
		cli();
		i = 1;
		while (i <= task_number)
		{
			if ((TC[2 * i] & 0xE0) == 0x80)
			{
				Current = i;
				switch (i)
				{
				case 1:
				{
					sei();
					task_1();
					break;
				}
				case 2:
				{
					sei();
					task_2();
					break;
				}
				case 3:
				{
					sei();
					task_3();
					break;
				}
				default:
					break;
				}
				i = 1;
			}
			else
			{
				i++;
			}
		}
		sei();
	}
}
ISR(TIMER1_COMPA_vect) {
	int i;
	if (Timer_Flag != 0)
	{
		Timer_Flag = 0;
		for (i = 1; i <= task_number; i++)
		{
			if (TC[2 * i] & 0x20)
			{
				TC[2 * i + 1] = TC[2 * i + 1] - 1;
				if (TC[2 * i + 1] == 0)
				{
					TC[2 * i] = (TC[2 * i] & 0xDD) + 2;
				}
				else
				{
					Timer_Flag = 1;
				}
			}
		}
	}
}





```

#結果


```
sk-尞k
task-1  0
task-2  0
task-3  0
task-3  0
task-3  0
task-3  2
task-3  13
task-2  25
task-1  37
task-2  50
task-3  62
task-1  75
task-2  88
task-3  100
task-1  113
task-2  126
task-3  141
task-1  154
task-2  167
task-3  181
task-1  194
task-2  208
task-3  222
task-1  235
task-2  248
task-3  262
task-1  276
task-2  289
task-3  303
task-1  316
task-2  330
task-3  344
task-1  357
task-2  370
task-3  384
task-1  398
task-2  411
task-3  424
task-1  438
task-2  451
task-3  465
task-1  479
task-2  492
task-3  505
task-1  519
task-2  533
task-3  546
task-1  560
task-2  573
task-3  586
task-1  601
task-2  614
task-3  627
task-1  641
task-2  654
task-3  668
task-1  681
task-2  695
task-3  708
task-1  721
task-2  736
task-3  749
task-1  762
task-2  776
task-3  789
task-1  803
task-2  817
task-3  830
task-1  843
task-2  857
task-3  871
task-1  884
task-2  898
task-3  911
task-1  924
task-2  939
task-3  952
task-1  965
task-2  978
task-3  992
task-1  1006
task-2  1020
task-3  1035
task-1  1049
task-2  1064
task-3  1079
task-1  1093
task-2  1107
task-3  1122
task-1  1137
task-2  1152
task-3  1166
task-1  1180
task-2  1196
task-3  1210
task-1  1224
task-2  1239
task-3  1253
task-1  1268
task-2  1283
task-3  1297
task-1  1311
task-2  1326
task-3  1341
task-1  1355
task-2  1370
task-3  1384
task-1  1399
task-2  1414
task-3  1428
task-1  1442
task-2  1457
task-3  1472
task-1  1486
task-2  1501
task-3  1515
task-1  1530
task-2  1545
task-3  1559
task-1  1573
task-2  1588
task-3  1603
task-1  1617
task-2  1632
task-3  1646
task-1  1661
task-2  1676
task-3  1690
task-1  1704
task-2  1719
task-3  1734
task-1  1748
task-2  1763
task-3  1777
task-1  1792
task-2  1807
task-3  1821
task-1  1836
task-2  1850
task-3  1865
task-1  1880
task-2  1894
task-3  1908
task-1  1923
task-2  1938
task-3  1952
task-1  1967
task-2  1981
task-3  1996
task-1  2011
task-2  2025
task-3  2039
task-1  2054
task-2  2069
task-3  2083
task-1  2098
task-2  2112
task-3  2127
task-1  2142
task-2  2156
task-3  2170
task-1  2185
task-2  2200
task-3  2214
task-1  2229
task-2  2243
task-3  2257
task-1  2273
task-2  2287
task-3  2301
task-1  2316
task-2  2331
task-3  2345
task-1  2360
task-2  2374
task-3  2388
task-1  2404
task-2  2418
task-3  2433
task-1  2447
task-2  2462
task-3  2477
task-1  2491
task-2  2505
task-3  2520
task-1  2535
task-2  2549
task-3  2564
task-1  2578
task-2  2593
task-3  2608
task-1  2622
task-2  2636
task-3  2651
task-1  2666
task-2  2680
task-3  2695
task-1  2709
task-2  2723
task-3  2739
task-1  2753
task-2  2767
task-3  2782
task-1  2797
task-2  2811
task-3  2826
task-1  2840
task-2  2854
task-3  2870
task-1  2884
task-2  2898
task-3  2913
task-1  2928
task-2  2942
task-3  2957
task-1  2971
task-2  2985
task-3  3001
task-1  3015
task-2  3030
task-3  3044
task-1  3059
task-2  3074
task-3  3088
task-1  3102
task-2  3117
task-3  3132
task-1  3146
task-2  3161
task-3  3175
task-1  3189
task-2  3205
task-3  3219
task-1  3233
task-2  3248
task-3  3263
task-1  3277
task-2  3292
task-3  3306
task-1  3320
task-2  3336
task-3  3350
task-1  3364
task-2  3379
task-3  3394
task-1  3408
task-2  3423
task-3  3437
task-1  3451
task-2  3467
task-3  3481
task-1  3495
task-2  3510
task-3  3525
task-1  3539
task-2  3554
task-3  3568
task-1  3582
task-2  3598
task-3  3612
task-1  3627
task-2  3641
task-3  3655
task-1  3671
task-2  3685
task-3  3699
task-1  3714
task-2  3729
task-3  3743
task-1  3758
task-2  3772
task-3  3786
task-1  3802
task-2  3816
task-3  3830
task-1  3845
task-2  3860
task-3  3874
task-1  3889
task-2  3903
task-3  3917
task-1  3933
task-2  3947
task-3  3961
task-1  3976
task-2  3991
task-3  4005
task-1  4020
task-2  4034
task-3  4048
task-1  4064
task-2  4078
task-3  4092
task-1  4107
task-2  4121
task-3  4136
task-1  4151
task-2  4165
task-3  4179
task-1  4195
task-2  4209
task-3  4224
task-1  4238
task-2  4252
task-3  4268
task-1  4282
task-2  4296
task-3  4311
task-1  4326
task-2  4340
task-3  4355
task-1  4369
task-2  4383
task-3  4399
task-1  4413
task-2  4427
task-3  4442
task-1  4457
task-2  4471
task-3  4486
task-1  4500
task-2  4514
task-3  4530
task-1  4544
task-2  4558
task-3  4573
task-1  4587
task-2  4602
task-3  4617
task-1  4631
task-2  4645
task-3  4661
task-1  4675
task-2  4689
task-3  4704
task-1  4718
task-2  4733
task-3  4748
task-1  4762
task-2  4776
task-3  4792
task-1  4806
task-2  4820
task-3  4835
task-1  4849
task-2  4865
task-3  4879
task-1  4893
task-2  4908
task-3  4923
task-1  4937
task-2  4952
task-3  4966
task-1  4980
tas

```

以上。