OSDN Git Service

(none)
[hos/hos-v4a.git] / test / twai_sem / lpc2000 / sample.c
1 /** 
2  *  Sample program for Hyper Operating System V4 Advance
3  *
4  * @file  sample.c
5  * @brief %jp{サンプルプログラム}%en{Sample program}
6  *
7  * Copyright (C) 1998-2006 by Project HOS
8  * http://sourceforge.jp/projects/hos/
9  */
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include "kernel.h"
14 #include "kernel_id.h"
15 #include "uart.h"
16
17
18
19 /** %jp{初期化ハンドラ} */
20 void Sample_Initialize(VP_INT exinf)
21 {
22         /* %jp{UART初期化} */
23         Uart_Initialize();
24
25         /* %jp{タスク起動} */
26         act_tsk(TSKID_WAIT);
27         act_tsk(TSKID_SIGNAL);
28 }
29
30
31 /** %jp{乱数取得} */
32 int GetRand(void)
33 {
34         int r;
35
36         wai_sem(SEMID_RAND);
37         r = rand();
38         sig_sem(SEMID_RAND);
39         
40         return r;
41 }
42
43
44
45 /** %jp{待ちタスク} */
46 void Sample_WaitTask(VP_INT exinf)
47 {
48         TMO     tmo;
49         ER      ercd;
50
51         for ( ; ; )
52         {
53                 /* 待ち時間を決める */
54                 tmo = GetRand() % 5;
55                 
56                 /* 待つ */
57                 ercd = twai_sem(SEMID_TEST, tmo);
58                 switch ( ercd )
59                 {
60                 case E_OK:
61                         Uart_PutString("OK\n");
62                         break;
63                 
64                 case E_TMOUT:
65                         Uart_PutString("TMOUT\n");
66                         break;
67
68                 default:
69                         Uart_PutString("error!\n");
70                         for ( ; ; ) ;
71                 }
72         }
73 }
74
75
76 /** %jp{sig_semタスク} */
77 void Sample_SignalTask(VP_INT exinf)
78 {
79         RELTIM tim;
80         
81         for ( ; ; )
82         {
83                 tim = GetRand() % 5;
84                 dly_tsk(tim);
85                 
86                 sig_sem(SEMID_TEST);
87         }
88 }
89
90
91
92 /* end of file */