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