OSDN Git Service

Moved the junk codes to junk directory.
[kozos-expbrd/kozos_expbrd.git] / firm / junk / 02 / os / timerdrv.c
1 #include "defines.h"
2 #include "kozos.h"
3 #include "intr.h"
4 #include "interrupt.h"
5 #include "timer.h"
6 #include "lib.h"
7 #include "timerdrv.h"
8
9 struct timerbuf {
10   struct timerbuf *next;
11   kz_msgbox_id_t id; /* ¥¿¥¤¥ÞËþλ»þ¤Î¥á¥Ã¥»¡¼¥¸Á÷¿®Àè */
12   int msec;
13 };
14
15 static struct timerreg {
16   struct timerbuf *timers; /* ¥¿¥¤¥Þ¡¦¥Ð¥Ã¥Õ¥¡¤Î¥ê¥ó¥¯¥ê¥¹¥È */
17   int index; /* ÍøÍѤ¹¤ë¥¿¥¤¥Þ¤ÎÈÖ¹æ */
18 } timerreg;
19
20 /*
21  * °Ê²¼¤Ï³ä¹þ¤ß¥Ï¥ó¥É¥é¤Ç¤¢¤ê¡¤ÈóƱ´ü¤Ç¸Æ¤Ð¤ì¤ë¤Î¤Ç¡¤¥é¥¤¥Ö¥é¥ê´Ø¿ô¤Ê¤É¤ò
22  * ¸Æ¤Ó½Ð¤¹¾ì¹ç¤Ë¤ÏÃí°Õ¤¬É¬Íס¥
23  * ´ðËܤȤ·¤Æ¡¤°Ê²¼¤Î¤¤¤º¤ì¤«¤ËÅö¤Æ¤Ï¤Þ¤ë´Ø¿ô¤·¤«¸Æ¤Ó½Ð¤·¤Æ¤Ï¤¤¤±¤Ê¤¤¡¥
24  * ¡¦ºÆÆþ²Äǽ¤Ç¤¢¤ë¡¥
25  * ¡¦¥¹¥ì¥Ã¥É¤«¤é¸Æ¤Ð¤ì¤ë¤³¤È¤Ï̵¤¤´Ø¿ô¤Ç¤¢¤ë¡¥
26  * ¡¦¥¹¥ì¥Ã¥É¤«¤é¸Æ¤Ð¤ì¤ë¤³¤È¤¬¤¢¤ë¤¬¡¤³ä¹þ¤ß¶Ø»ß¤Ç¸Æ¤Ó½Ð¤·¤Æ¤¤¤ë¡¥
27  * ¤Þ¤¿Èó¥³¥ó¥Æ¥­¥¹¥È¾õÂ֤ǸƤФì¤ë¤¿¤á¡¤¥·¥¹¥Æ¥à¡¦¥³¡¼¥ë¤ÏÍøÍѤ·¤Æ¤Ï¤¤¤±¤Ê¤¤¡¥
28  * (¥µ¡¼¥Ó¥¹¡¦¥³¡¼¥ë¤òÍøÍѤ¹¤ë¤³¤È)
29  */
30 static void timerdrv_intr(void)
31 {
32   struct timerreg *tim = &timerreg;
33
34   if (timer_is_expired(tim->index)) { /* ¥¿¥¤¥Þ³ä¹þ¤ß */
35     timer_cancel(tim->index);
36     kx_send(MSGBOX_ID_TIMDRIVE, TIMERDRV_CMD_EXPIRE, NULL);
37   }
38 }
39
40 static int timerdrv_init(void)
41 {
42   memset(&timerreg, 0, sizeof(timerreg));
43   timerreg.index = TIMER_DEFAULT_DEVICE;
44   return 0;
45 }
46
47 /* ¥¹¥ì¥Ã¥É¤«¤é¤ÎÍ×µá¤ò½èÍý¤¹¤ë */
48 static int timerdrv_command(struct timerreg *tim, int cmd, char *p)
49 {
50   struct timerbuf *tmbuf;
51   struct timerbuf **tmbufp;
52   struct timerreq *req;
53   int t, msec;
54
55   switch (cmd) {
56   case TIMERDRV_CMD_EXPIRE: /* ¥¿¥¤¥ÞËþλ */
57     tmbuf = tim->timers;
58     if (tmbuf) {
59       tim->timers = tmbuf->next;
60       kz_send(tmbuf->id, 0, NULL);
61       kz_kmfree(tmbuf);
62       if (tim->timers)
63         timer_start(tim->index, tim->timers->msec, 0);
64     }
65     break;
66
67   case TIMERDRV_CMD_START: /* ¥¿¥¤¥Þ¤Î¥¹¥¿¡¼¥È */
68     req = (struct timerreq *)p;
69
70     tmbuf = kz_kmalloc(sizeof(*tmbuf));
71     tmbuf->next = NULL;
72     tmbuf->id   = req->id;
73     tmbuf->msec = req->msec;
74
75     t = 0;
76     if (tim->timers) {
77       t = timer_gettime(tim->index);
78     }
79
80     for (tmbufp = &tim->timers;; tmbufp = &(*tmbufp)->next) {
81       if (*tmbufp == NULL) {
82         *tmbufp = tmbuf;
83         if (tmbufp == &tim->timers)
84           timer_start(tim->index, tim->timers->msec, 0);
85         break;
86       }
87       msec = (*tmbufp)->msec - t;
88       if (msec < 0) msec = 0;
89       if (tmbuf->msec < msec) {
90         (*tmbufp)->msec = msec - tmbuf->msec;
91         tmbuf->next = *tmbufp;
92         *tmbufp = tmbuf;
93         timer_start(tim->index, tim->timers->msec, 0);
94         break;
95       }
96       t = 0;
97       tmbuf->msec -= msec;
98     }
99
100     kz_kmfree(p);
101     break;
102
103   default:
104     break;
105   }
106
107   return 0;
108 }
109
110 int timerdrv_main(int argc, char *argv[])
111 {
112   int cmd;
113   char *p;
114
115   timerdrv_init();
116   kz_setintr(SOFTVEC_TYPE_TIMINTR, timerdrv_intr); /* ³ä¹þ¤ß¥Ï¥ó¥É¥éÀßÄê */
117
118   while (1) {
119     kz_recv(MSGBOX_ID_TIMDRIVE, &cmd, &p);
120     timerdrv_command(&timerreg, cmd, p);
121   }
122
123   return 0;
124 }