OSDN Git Service

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