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