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