]> git.gir.st - tmk_keyboard.git/blob - keyboard/hhkb_rn42/rn42/rn42.c
756285d7bb265a9342d9f2695070e3dd34ea6356
[tmk_keyboard.git] / keyboard / hhkb_rn42 / rn42 / rn42.c
1 #include <avr/io.h>
2 #include "host.h"
3 #include "host_driver.h"
4 #include "serial.h"
5 #include "rn42.h"
6 #include "print.h"
7 #include "timer.h"
8 #include "wait.h"
9
10
11 /* Host driver */
12 static uint8_t keyboard_leds(void);
13 static void send_keyboard(report_keyboard_t *report);
14 static void send_mouse(report_mouse_t *report);
15 static void send_system(uint16_t data);
16 static void send_consumer(uint16_t data);
17
18 host_driver_t rn42_driver = {
19 keyboard_leds,
20 send_keyboard,
21 send_mouse,
22 send_system,
23 send_consumer
24 };
25
26
27 void rn42_init(void)
28 {
29 // JTAG disable for PORT F. write JTD bit twice within four cycles.
30 MCUCR |= (1<<JTD);
31 MCUCR |= (1<<JTD);
32
33 // PF7: BT connection control(high: connect, low: disconnect)
34 rn42_autoconnect();
35
36 // PF6: linked(input without pull-up)
37 DDRF &= ~(1<<6);
38 PORTF |= (1<<6);
39
40 // PF1: RTS(low: allowed to send, high: not allowed)
41 DDRF &= ~(1<<1);
42 PORTF &= ~(1<<1);
43
44 // PD5: CTS(low: allow to send, high:not allow)
45 DDRD |= (1<<5);
46 PORTD &= ~(1<<5);
47
48 serial_init();
49 }
50
51 int16_t rn42_getc(void)
52 {
53 return serial_recv2();
54 }
55
56 const char *rn42_gets(uint16_t timeout)
57 {
58 static char s[24];
59 uint16_t t = timer_read();
60 uint8_t i = 0;
61 int16_t c;
62 while (i < 23 && timer_elapsed(t) < timeout) {
63 if ((c = rn42_getc()) != -1) {
64 if ((char)c == '\r') continue;
65 if ((char)c == '\n') break;
66 s[i++] = c;
67 }
68 }
69 s[i] = '\0';
70 return s;
71 }
72
73 void rn42_putc(uint8_t c)
74 {
75 serial_send(c);
76 }
77
78 void rn42_puts(char *s)
79 {
80 while (*s)
81 serial_send(*s++);
82 }
83
84 bool rn42_autoconnecting(void)
85 {
86 // GPIO6 for control connection(high: auto connect, low: disconnect)
87 // Note that this needs config: SM,4(Auto-Connect DTR Mode)
88 return (PORTF & (1<<7) ? true : false);
89 }
90
91 void rn42_autoconnect(void)
92 {
93 // hi to auto connect
94 DDRF |= (1<<7);
95 PORTF |= (1<<7);
96 }
97
98 void rn42_disconnect(void)
99 {
100 // low to disconnect
101 DDRF |= (1<<7);
102 PORTF &= ~(1<<7);
103 }
104
105 bool rn42_rts(void)
106 {
107 // low when RN-42 is powered and ready to receive
108 return PINF&(1<<1);
109 }
110
111 void rn42_cts_hi(void)
112 {
113 // not allow to send
114 PORTD |= (1<<5);
115 }
116
117 void rn42_cts_lo(void)
118 {
119 // allow to send
120 PORTD &= ~(1<<5);
121 }
122
123 bool rn42_linked(void)
124 {
125 // RN-42 GPIO2
126 // Hi-Z: Not powered
127 // High: Linked
128 // Low: Connecting
129 return PINF&(1<<6);
130 }
131
132
133 static uint8_t leds = 0;
134 static uint8_t keyboard_leds(void) { return leds; }
135 void rn42_set_leds(uint8_t l) { leds = l; }
136
137 static void send_keyboard(report_keyboard_t *report)
138 {
139 // wake from deep sleep
140 /*
141 PORTD |= (1<<5); // high
142 wait_ms(5);
143 PORTD &= ~(1<<5); // low
144 */
145
146 serial_send(0xFD); // Raw report mode
147 serial_send(9); // length
148 serial_send(1); // descriptor type
149 serial_send(report->mods);
150 serial_send(0x00);
151 serial_send(report->keys[0]);
152 serial_send(report->keys[1]);
153 serial_send(report->keys[2]);
154 serial_send(report->keys[3]);
155 serial_send(report->keys[4]);
156 serial_send(report->keys[5]);
157 }
158
159 static void send_mouse(report_mouse_t *report)
160 {
161 // wake from deep sleep
162 /*
163 PORTD |= (1<<5); // high
164 wait_ms(5);
165 PORTD &= ~(1<<5); // low
166 */
167
168 serial_send(0xFD); // Raw report mode
169 serial_send(5); // length
170 serial_send(2); // descriptor type
171 serial_send(report->buttons);
172 serial_send(report->x);
173 serial_send(report->y);
174 serial_send(report->v);
175 }
176
177 static void send_system(uint16_t data)
178 {
179 // Table 5-6 of RN-BT-DATA-UB
180 // 81,82,83 scan codes can be used?
181 }
182
183
184 static uint16_t usage2bits(uint16_t usage)
185 {
186 switch (usage) {
187 case AC_HOME: return 0x01;
188 case AL_EMAIL: return 0x02;
189 case AC_SEARCH: return 0x04;
190 //case AL_KBD_LAYOUT: return 0x08; // Apple virtual keybaord toggle
191 case AUDIO_VOL_UP: return 0x10;
192 case AUDIO_VOL_DOWN: return 0x20;
193 case AUDIO_MUTE: return 0x40;
194 case TRANSPORT_PLAY_PAUSE: return 0x80;
195 case TRANSPORT_NEXT_TRACK: return 0x100;
196 case TRANSPORT_PREV_TRACK: return 0x200;
197 case TRANSPORT_STOP: return 0x400;
198 case TRANSPORT_STOP_EJECT: return 0x800;
199 //case return 0x1000; // Fast forward
200 //case return 0x2000; // Rewind
201 //case return 0x4000; // Stop/eject
202 //case return 0x8000; // Internet browser
203 };
204 return 0;
205 }
206
207 static void send_consumer(uint16_t data)
208 {
209 uint16_t bits = usage2bits(data);
210 serial_send(0xFD); // Raw report mode
211 serial_send(3); // length
212 serial_send(3); // descriptor type
213 serial_send(bits&0xFF);
214 serial_send((bits>>8)&0xFF);
215 }
216
217
218 /* Null driver for config_mode */
219 static uint8_t config_keyboard_leds(void);
220 static void config_send_keyboard(report_keyboard_t *report);
221 static void config_send_mouse(report_mouse_t *report);
222 static void config_send_system(uint16_t data);
223 static void config_send_consumer(uint16_t data);
224
225 host_driver_t rn42_config_driver = {
226 config_keyboard_leds,
227 config_send_keyboard,
228 config_send_mouse,
229 config_send_system,
230 config_send_consumer
231 };
232
233 static uint8_t config_keyboard_leds(void) { return leds; }
234 static void config_send_keyboard(report_keyboard_t *report) {}
235 static void config_send_mouse(report_mouse_t *report) {}
236 static void config_send_system(uint16_t data) {}
237 static void config_send_consumer(uint16_t data) {}
Imprint / Impressum