OSDN Git Service

Step 10 added.
[kozos-expbrd/kozos_expbrd.git] / firm / 08 / os / remocon.c
1 #include "remocon.h"
2 #include "defines.h"
3 #include "kozos.h"
4 #include "intr.h"
5 #include "led.h"
6 #include "lib.h"
7 #include "consdrv.h"
8
9 #define H8_3069F_ISCR       ((volatile uint8 *)0xFEE014)
10 #define H8_3069F_IER        ((volatile uint8 *)0xFEE015)
11 #define H8_3069F_ISR        ((volatile uint8 *)0xFEE016)
12
13 #define H8_3069F_TSTR       ((volatile uint8 *)0xFFFF60)
14 #define H8_3069F_TISRC      ((volatile uint8 *)0xFFFF66)
15 #define H8_3069F_16TCR0     ((volatile uint8 *)0xFFFF68)
16 #define H8_3069F_TIOR0      ((volatile uint8 *)0xFFFF69)
17 #define H8_3069F_16TCNT0H   ((volatile uint8 *)0xFFFF6A)
18 #define H8_3069F_16TCNT0L   ((volatile uint8 *)0xFFFF6B)
19
20 #define MAX_BIT_COUNT (64)
21
22 typedef enum {
23   WaitLeader,
24   FoundLeader,
25   Receiving,
26   Received
27 } ir_state_t;
28
29 static uint16 bitcnt = 0;
30 static uint16 tmrovf = 0;
31 static uint8 data[MAX_BIT_COUNT / 8];
32 static ir_state_t irs = WaitLeader;
33
34 static void remocon_intr_edge(void)
35 {
36   switch (irs) {
37     case WaitLeader:
38       *H8_3069F_16TCNT0H = 0;
39       *H8_3069F_16TCNT0L = 0;
40       *H8_3069F_TSTR |= (1 << 0);
41       bitcnt = 0;
42       tmrovf = 0;
43       irs = FoundLeader;
44       break;
45     case FoundLeader:
46       *H8_3069F_16TCNT0H = 0;
47       *H8_3069F_16TCNT0L = 0;
48       irs = Receiving;
49       break;
50     case Receiving:
51       if (bitcnt < MAX_BIT_COUNT) {
52         if ((bitcnt % 8) == 0) {
53           data[bitcnt / 8] = 0;
54         }
55         uint16 tmrval =
56           ((*H8_3069F_16TCNT0H) << 8) |
57           ((*H8_3069F_16TCNT0L) << 0);
58         if (tmrval > 0x1077) {
59           data[bitcnt / 8] |= (1 << (bitcnt % 8));
60         }
61         bitcnt++;
62       }
63       *H8_3069F_16TCNT0H = 0;
64       *H8_3069F_16TCNT0L = 0;
65       if (32 <= bitcnt) {
66         irs = Received;
67       }
68       break;
69     case Received:
70       break;
71     default:
72       break;
73   }
74
75 }
76
77 static void remocon_intr_tovf(void)
78 {
79   *H8_3069F_TISRC &= ~(1 << 0);
80   tmrovf = 1;
81   *H8_3069F_TSTR &= ~(1 << 0);
82 }
83
84 /* \e$B%3%s%=!<%k$X$NJ8;zNs=PNO$r%3%s%=!<%k!&%I%i%$%P$K0MMj$9$k\e(B */
85 static void send_write(char *str)
86 {
87   char *p;
88   int len;
89   len = strlen(str);
90   p = kz_kmalloc(len + 2);
91   p[0] = '0';
92   p[1] = CONSDRV_CMD_WRITE;
93   memcpy(&p[2], str, len);
94   kz_send(MSGBOX_ID_CONSOUTPUT, len + 2, p);
95 }
96
97 int remocon_main(int argc, char *argv[])
98 {
99   kz_setintr(SOFTVEC_TYPE_IR_EDGE, remocon_intr_edge);
100   kz_setintr(SOFTVEC_TYPE_IR_TOVF, remocon_intr_tovf);
101   *H8_3069F_ISCR |= (1 << 4);
102   *H8_3069F_IER |= (1 << 4);
103
104   *H8_3069F_TISRC |= (1 << 4);
105   *H8_3069F_16TCR0 = 0x03;
106
107   while (1) {
108     if (tmrovf) {
109       if (bitcnt > 0) {
110         if (bitcnt == 32) {
111           int i;
112           for (i = 0; i < bitcnt; i++) {
113             if (data[i / 8] & (1 << (i % 8))) {
114               send_write("1");
115             } else {
116               send_write("0");
117             }
118           }
119           send_write("\n");
120         }
121         bitcnt = 0;
122       }
123       tmrovf = 0;
124       irs = WaitLeader;
125     }
126   }
127
128   return 0;
129 }
130