OSDN Git Service

Added audio play test codes.
[kozos-expbrd/kozos_expbrd.git] / firm / sample / sample1 / os / task_input.c
1 #include "defines.h"
2 #include "kozos.h"
3 #include "task_display.h"
4 #include "lib.h"
5 #include "sw.h"
6 #include "re.h"
7 #include "intr.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 #define TCNT_COUNT_START()  (*H8_3069F_TSTR |= (1 << 0))
23 #define TCNT_COUNT_STOP()   (*H8_3069F_TSTR &= ~(1 << 0))
24 #define TCNT_COUNT_VALUE()  (((*H8_3069F_16TCNT0H) << 8) | ((*H8_3069F_16TCNT0L) << 0))
25 #define TCNT_COUNT_RESET()  \
26   do { \
27     *H8_3069F_16TCNT0H = 0; \
28     *H8_3069F_16TCNT0L = 0; \
29   } while (0)
30
31 #define TCR0_SETUP()        (*H8_3069F_16TCR0 = 0x03)
32 #define TMRINT_ENABLE()     (*H8_3069F_TISRC |= (1 << 4))
33 #define TMRINT_FLAG_CLEAR() (*H8_3069F_TISRC &= ~(1 << 0))
34 #define IRQ4_ENABLE() \
35   do { \
36     (*H8_3069F_ISCR |= (1 << 4)); \
37     (*H8_3069F_IER |= (1 << 4)); \
38   } while (0)
39
40 typedef enum {
41   WaitLeader,
42   FoundLeader,
43   Receiving,
44   Received
45 } ir_state_t;
46
47 static uint16 bitcnt = 0;
48 static uint16 tmrovf = 0;
49 static uint8 data[MAX_BIT_COUNT / 8];
50 static ir_state_t irs = WaitLeader;
51
52 static void remocon_intr_edge(void)
53 {
54   switch (irs) {
55     case WaitLeader:
56       TCNT_COUNT_RESET();
57       TCNT_COUNT_START();
58       bitcnt = 0;
59       tmrovf = 0;
60       irs = FoundLeader;
61       break;
62     case FoundLeader:
63       irs = Receiving;
64       break;
65     case Receiving:
66       if (bitcnt < MAX_BIT_COUNT) {
67         if ((bitcnt % 8) == 0) {
68           data[bitcnt / 8] = 0;
69         }
70         uint16 tmrval = TCNT_COUNT_VALUE();
71         if (tmrval > 0x1077) {
72           data[bitcnt / 8] |= (1 << (bitcnt % 8));
73         }
74         bitcnt++;
75       }
76       if (32 <= bitcnt) {
77         irs = Received;
78       }
79       break;
80     case Received:
81       TCNT_COUNT_STOP();
82       break;
83     default:
84       TCNT_COUNT_STOP();
85       break;
86   }
87   TCNT_COUNT_RESET();
88 }
89
90 static void remocon_intr_tovf(void)
91 {
92   TMRINT_FLAG_CLEAR();
93   tmrovf = 1;
94   irs = WaitLeader;
95   TCNT_COUNT_STOP();
96 }
97
98 int task_input(int argc, char *argv[])
99 {
100   int nx = 40, ny = 12;
101
102   /*
103    * Setup.
104    */
105   kz_setintr(SOFTVEC_TYPE_IR_EDGE, remocon_intr_edge);
106   kz_setintr(SOFTVEC_TYPE_IR_TOVF, remocon_intr_tovf);
107   TCR0_SETUP();
108   TMRINT_ENABLE();
109   IRQ4_ENABLE();
110
111   /*
112    * Display clear
113    */
114   display_clear();
115   display_draw_logo(  0, 0, 2);
116   display_draw_box(0, 0, 121, 31, 1);
117   display_draw_text(40, 4, "KOZOS EXPBRD #00");
118   display_draw_logo(nx, ny, 0);
119
120   uint16 prev = 0, curr = 0;
121   while (1) {
122     curr = re_read();
123     if (curr != prev) {
124       int dir = (int)(curr & 0xff) - (int)(prev & 0xff);
125       if (dir < 0) {
126         if (-32 < dir) {
127           display_led_write(1, 0);
128           display_led_toggle(0);
129         }
130         if (40 < nx) {
131           nx--;
132         }
133       } else {
134         if (dir < 32) {
135           display_led_write(0, 0);
136           display_led_toggle(1);
137         }
138         if (nx < 100) {
139           nx++;
140         }
141       }
142       prev = curr;
143
144       display_draw_box(nx + 20, ny, nx + 20, ny + 16, 0);
145       display_draw_logo(nx, ny, 0);
146       display_led_toggle(3);
147     }
148
149     if (tmrovf) {
150       if (bitcnt == 32) {
151 #if 0
152         int i;
153         for (i = 0; i < bitcnt; i++) {
154           if (data[i / 8] & (1 << (i % 8))) {
155             console_write("1");
156           } else {
157             console_write("0");
158           }
159         }
160         console_write("\n");
161 #endif
162         audio_pulse();
163         display_led_toggle(2);
164       }
165       bitcnt = 0;
166       tmrovf = 0;
167     }
168   }
169
170   return 0;
171 }
172