OSDN Git Service

Added task ID variables.
[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_START();
57       bitcnt = 0;
58       tmrovf = 0;
59       irs = FoundLeader;
60       break;
61     case FoundLeader:
62       irs = Receiving;
63       break;
64     case Receiving:
65       if (bitcnt < MAX_BIT_COUNT) {
66         if ((bitcnt % 8) == 0) {
67           data[bitcnt / 8] = 0;
68         }
69         uint16 tmrval = TCNT_COUNT_VALUE();
70         if (tmrval > 0x1077) {
71           data[bitcnt / 8] |= (1 << (bitcnt % 8));
72         }
73         bitcnt++;
74       }
75       if (32 <= bitcnt) {
76         irs = Received;
77       }
78       break;
79     case Received:
80       break;
81     default:
82       break;
83   }
84   TCNT_COUNT_RESET();
85 }
86
87 static void remocon_intr_tovf(void)
88 {
89   TMRINT_FLAG_CLEAR();
90   tmrovf = 1;
91   irs = WaitLeader;
92   TCNT_COUNT_STOP();
93 }
94
95 int task_input(int argc, char *argv[])
96 {
97   int nx = 40, ny = 12;
98
99   /*
100    * Setup.
101    */
102   kz_setintr(SOFTVEC_TYPE_IR_EDGE, remocon_intr_edge);
103   kz_setintr(SOFTVEC_TYPE_IR_TOVF, remocon_intr_tovf);
104   TCR0_SETUP();
105   TMRINT_ENABLE();
106   IRQ4_ENABLE();
107
108   uint16 prev = 0, curr = 0;
109   while (1) {
110     /*
111      * \e$B%m!<%?%j!<%(%s%3!<%@F~NO=hM}\e(B
112      */
113     curr = re_read();
114     if (curr != prev) {
115       int dir = (int)(curr & 0xff) - (int)(prev & 0xff);
116       if (dir < 0) {
117         if (-32 < dir) {
118           display_led_write(1, 0);
119           display_led_toggle(0);
120         }
121         if (0 < nx) {
122           nx--;
123         }
124       } else {
125         if (dir < 32) {
126           display_led_write(0, 0);
127           display_led_toggle(1);
128         }
129         if (nx < 100) {
130           nx++;
131         }
132       }
133       prev = curr;
134
135       display_draw_box(nx + 20, ny, nx + 20, ny + 16, 0);
136       display_draw_logo(nx, ny, 0);
137       display_led_toggle(3);
138     }
139
140     /*
141      * \e$B%j%b%3%sF~NO=hM}\e(B
142      */
143     if (tmrovf) {
144       if (bitcnt == 32) {
145         putxval(data[0], 2);
146         putxval(data[1], 2);
147         putxval(data[2], 2);
148         putxval(data[3], 2);
149         puts("\n");
150         display_led_toggle(2);
151       }
152       bitcnt = 0;
153       tmrovf = 0;
154     }
155
156     /*
157      * \e$B%9%$%C%AF~NO=hM}\e(B
158      */
159     // @todo
160
161     /*
162      *
163      */
164     kz_wait();
165   }
166
167   return 0;
168 }
169