OSDN Git Service

Step 10 added.
[kozos-expbrd/kozos_expbrd.git] / firm / junk / 10 / 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_TSTR |= (1 << 0);
41       bitcnt = 0;
42       tmrovf = 0;
43       irs = FoundLeader;
44       break;
45     case FoundLeader:
46       irs = Receiving;
47       break;
48     case Receiving:
49       if (bitcnt < MAX_BIT_COUNT) {
50         if ((bitcnt % 8) == 0) {
51           data[bitcnt / 8] = 0;
52         }
53         uint16 tmrval =
54           ((*H8_3069F_16TCNT0H) << 8) |
55           ((*H8_3069F_16TCNT0L) << 0);
56         if (tmrval > 0x1077) {
57           data[bitcnt / 8] |= (1 << (bitcnt % 8));
58         }
59         bitcnt++;
60       }
61       if (32 <= bitcnt) {
62         irs = Received;
63       }
64       break;
65     case Received:
66       *H8_3069F_TSTR &= ~(1 << 0);
67       break;
68     default:
69       break;
70   }
71   *H8_3069F_16TCNT0H = 0;
72   *H8_3069F_16TCNT0L = 0;
73 }
74
75 static void remocon_intr_tovf(void)
76 {
77   *H8_3069F_TISRC &= ~(1 << 0);
78   tmrovf = 1;
79   *H8_3069F_TSTR &= ~(1 << 0);
80 }
81
82 int task_ir(int argc, char *argv[])
83 {
84   kz_setintr(SOFTVEC_TYPE_IR_EDGE, remocon_intr_edge);
85   kz_setintr(SOFTVEC_TYPE_IR_TOVF, remocon_intr_tovf);
86   *H8_3069F_ISCR |= (1 << 4);
87   *H8_3069F_IER |= (1 << 4);
88
89   *H8_3069F_TISRC |= (1 << 4);
90   *H8_3069F_16TCR0 = 0x03;
91
92   while (1) {
93     if (tmrovf) {
94       if (bitcnt == 32) {
95 #if 0
96         int i;
97         for (i = 0; i < bitcnt; i++) {
98           if (data[i / 8] & (1 << (i % 8))) {
99             console_write("1");
100           } else {
101             console_write("0");
102           }
103         }
104         console_write("\n");
105 #endif
106         audio_pulse();
107         leddrv_toggle(2);
108       }
109       bitcnt = 0;
110       tmrovf = 0;
111       irs = WaitLeader;
112     }
113   }
114
115   return 0;
116 }
117