7 #include "driver_timer.h"
9 #define TIMERDRV_CMD_EXPIRE 'e' /* ¥¿¥¤¥ÞËþλ */
10 #define TIMERDRV_CMD_START 's' /* ¥¿¥¤¥Þ¤Î¥¹¥¿¡¼¥È */
13 struct timerbuf *next;
14 kz_msgbox_id_t id; /* ¥¿¥¤¥ÞËþλ»þ¤Î¥á¥Ã¥»¡¼¥¸Á÷¿®Àè */
18 static struct timerreg {
19 struct timerbuf *timers; /* ¥¿¥¤¥Þ¡¦¥Ð¥Ã¥Õ¥¡¤Î¥ê¥ó¥¯¥ê¥¹¥È */
20 int index; /* ÍøÍѤ¹¤ë¥¿¥¤¥Þ¤ÎÈÖ¹æ */
23 void timerdrv_start(int msec)
26 req = kz_kmalloc(sizeof(*req));
27 req->id = MSGBOX_ID_TIMEXPIRE;
29 kz_send(MSGBOX_ID_TIMDRIVE, TIMERDRV_CMD_START, (char *)req);
34 kz_recv(MSGBOX_ID_TIMEXPIRE, NULL, NULL);
38 * °Ê²¼¤Ï³ä¹þ¤ß¥Ï¥ó¥É¥é¤Ç¤¢¤ê¡¤ÈóƱ´ü¤Ç¸Æ¤Ð¤ì¤ë¤Î¤Ç¡¤¥é¥¤¥Ö¥é¥ê´Ø¿ô¤Ê¤É¤ò
39 * ¸Æ¤Ó½Ð¤¹¾ì¹ç¤Ë¤ÏÃí°Õ¤¬É¬Íס¥
40 * ´ðËܤȤ·¤Æ¡¤°Ê²¼¤Î¤¤¤º¤ì¤«¤ËÅö¤Æ¤Ï¤Þ¤ë´Ø¿ô¤·¤«¸Æ¤Ó½Ð¤·¤Æ¤Ï¤¤¤±¤Ê¤¤¡¥
42 * ¡¦¥¹¥ì¥Ã¥É¤«¤é¸Æ¤Ð¤ì¤ë¤³¤È¤Ï̵¤¤´Ø¿ô¤Ç¤¢¤ë¡¥
43 * ¡¦¥¹¥ì¥Ã¥É¤«¤é¸Æ¤Ð¤ì¤ë¤³¤È¤¬¤¢¤ë¤¬¡¤³ä¹þ¤ß¶Ø»ß¤Ç¸Æ¤Ó½Ð¤·¤Æ¤¤¤ë¡¥
44 * ¤Þ¤¿Èó¥³¥ó¥Æ¥¥¹¥È¾õÂ֤ǸƤФì¤ë¤¿¤á¡¤¥·¥¹¥Æ¥à¡¦¥³¡¼¥ë¤ÏÍøÍѤ·¤Æ¤Ï¤¤¤±¤Ê¤¤¡¥
45 * (¥µ¡¼¥Ó¥¹¡¦¥³¡¼¥ë¤òÍøÍѤ¹¤ë¤³¤È)
47 static void timerdrv_intr(void)
49 struct timerreg *tim = &timerreg;
51 if (timer_is_expired(tim->index)) { /* ¥¿¥¤¥Þ³ä¹þ¤ß */
52 timer_cancel(tim->index);
53 kx_send(MSGBOX_ID_TIMDRIVE, TIMERDRV_CMD_EXPIRE, NULL);
57 static int timerdrv_init(void)
59 memset(&timerreg, 0, sizeof(timerreg));
60 timerreg.index = TIMER_DEFAULT_DEVICE;
64 /* ¥¹¥ì¥Ã¥É¤«¤é¤ÎÍ×µá¤ò½èÍý¤¹¤ë */
65 static int timerdrv_command(struct timerreg *tim, int cmd, char *p)
67 struct timerbuf *tmbuf;
68 struct timerbuf **tmbufp;
73 case TIMERDRV_CMD_EXPIRE: /* ¥¿¥¤¥ÞËþλ */
76 tim->timers = tmbuf->next;
77 kz_send(tmbuf->id, 0, NULL);
80 timer_start(tim->index, tim->timers->msec, 0);
84 case TIMERDRV_CMD_START: /* ¥¿¥¤¥Þ¤Î¥¹¥¿¡¼¥È */
85 req = (struct timerreq *)p;
87 tmbuf = kz_kmalloc(sizeof(*tmbuf));
90 tmbuf->msec = req->msec;
94 t = timer_gettime(tim->index);
97 for (tmbufp = &tim->timers;; tmbufp = &(*tmbufp)->next) {
98 if (*tmbufp == NULL) {
100 if (tmbufp == &tim->timers)
101 timer_start(tim->index, tim->timers->msec, 0);
104 msec = (*tmbufp)->msec - t;
105 if (msec < 0) msec = 0;
106 if (tmbuf->msec < msec) {
107 (*tmbufp)->msec = msec - tmbuf->msec;
108 tmbuf->next = *tmbufp;
110 timer_start(tim->index, tim->timers->msec, 0);
127 int driver_timer(int argc, char *argv[])
133 kz_setintr(SOFTVEC_TYPE_TIMINTR, timerdrv_intr); /* ³ä¹þ¤ß¥Ï¥ó¥É¥éÀßÄê */
136 kz_recv(MSGBOX_ID_TIMDRIVE, &cmd, &p);
137 timerdrv_command(&timerreg, cmd, p);