]> git.gir.st - tmk_keyboard.git/blob - protocol/adb.c
Merge branch 'adb_fix'
[tmk_keyboard.git] / protocol / adb.c
1 /*
2 Copyright 2011 Jun WAKO <wakojun@gmail.com>
3
4 This software is licensed with a Modified BSD License.
5 All of this is supposed to be Free Software, Open Source, DFSG-free,
6 GPL-compatible, and OK to use in both free and proprietary applications.
7 Additions and corrections to this file are welcome.
8
9
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
12
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
15
16 * Redistributions in binary form must reproduce the above copyright
17 notice, this list of conditions and the following disclaimer in
18 the documentation and/or other materials provided with the
19 distribution.
20
21 * Neither the name of the copyright holders nor the names of
22 contributors may be used to endorse or promote products derived
23 from this software without specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include <stdbool.h>
39 #include <util/delay.h>
40 #include <avr/io.h>
41 #include <avr/interrupt.h>
42 #include "adb.h"
43
44
45 static inline void data_lo(void);
46 static inline void data_hi(void);
47 static inline bool data_in(void);
48 #ifdef ADB_PSW_BIT
49 static inline void psw_lo(void);
50 static inline void psw_hi(void);
51 static inline bool psw_in(void);
52 #endif
53
54 static inline void attention(void);
55 static inline void place_bit0(void);
56 static inline void place_bit1(void);
57 static inline void send_byte(uint8_t data);
58 static inline bool read_bit(void);
59 static inline uint8_t read_byte(void);
60 static inline uint8_t wait_data_lo(uint16_t us);
61 static inline uint8_t wait_data_hi(uint8_t us);
62
63
64 void adb_host_init(void)
65 {
66 DDRF |= (1<<1);
67 data_hi();
68 #ifdef ADB_PSW_BIT
69 psw_hi();
70 #endif
71
72 // Enable keyboard left/right modifier distinction
73 // Addr:Keyboard(0010), Cmd:Listen(10), Register3(11)
74 // upper byte: reserved bits 0000, device address 0010
75 // lower byte: device handler 00000011
76 adb_host_listen(0x2B,0x02,0x03);
77 }
78
79 #ifdef ADB_PSW_BIT
80 bool adb_host_psw(void)
81 {
82 return psw_in();
83 }
84 #endif
85
86 uint16_t adb_host_kbd_recv(void)
87 {
88 uint16_t data = 0;
89 attention();
90 send_byte(0x2C); // Addr:Keyboard(0010), Cmd:Talk(11), Register0(00)
91 place_bit0(); // Stopbit(0)
92 if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us)
93 return 0; // No data to send
94 }
95 if (!read_bit()) { // Startbit(1)
96 // Service Request
97 return -2;
98 }
99
100 // ad hoc fix: without block inerrupt read wrong bit occasionally and get keys stuck
101 cli();
102 data = read_byte();
103 data = (data<<8) | read_byte();
104 uint8_t stop = read_bit(); // Stopbit(0)
105 sei();
106
107 if (stop) {
108 return -3;
109 }
110 return data;
111 }
112
113 void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l)
114 {
115 attention();
116 send_byte(cmd);
117 place_bit0(); // Stopbit(0)
118 _delay_us(200); // Tlt/Stop to Start
119 place_bit1(); // Startbit(1)
120 send_byte(data_h);
121 send_byte(data_l);
122 place_bit0(); // Stopbit(0);
123 }
124
125 // send state of LEDs
126 void adb_host_kbd_led(uint8_t led)
127 {
128 // Addr:Keyboard(0010), Cmd:Listen(10), Register2(10)
129 // send upper byte (not used)
130 // send lower byte (bit2: ScrollLock, bit1: CapsLock, bit0:
131 adb_host_listen(0x2A,0,led&0x07);
132 }
133
134
135 static inline void data_lo()
136 {
137 ADB_DDR |= (1<<ADB_DATA_BIT);
138 ADB_PORT &= ~(1<<ADB_DATA_BIT);
139 }
140 static inline void data_hi()
141 {
142 ADB_PORT |= (1<<ADB_DATA_BIT);
143 ADB_DDR &= ~(1<<ADB_DATA_BIT);
144 }
145 static inline bool data_in()
146 {
147 ADB_PORT |= (1<<ADB_DATA_BIT);
148 ADB_DDR &= ~(1<<ADB_DATA_BIT);
149 return ADB_PIN&(1<<ADB_DATA_BIT);
150 }
151
152 #ifdef ADB_PSW_BIT
153 static inline void psw_lo()
154 {
155 ADB_DDR |= (1<<ADB_PSW_BIT);
156 ADB_PORT &= ~(1<<ADB_PSW_BIT);
157 }
158 static inline void psw_hi()
159 {
160 ADB_PORT |= (1<<ADB_PSW_BIT);
161 ADB_DDR &= ~(1<<ADB_PSW_BIT);
162 }
163 static inline bool psw_in()
164 {
165 ADB_PORT |= (1<<ADB_PSW_BIT);
166 ADB_DDR &= ~(1<<ADB_PSW_BIT);
167 return ADB_PIN&(1<<ADB_PSW_BIT);
168 }
169 #endif
170
171 static inline void attention(void)
172 {
173 data_lo();
174 _delay_us(700);
175 place_bit1();
176 }
177
178 static inline void place_bit0(void)
179 {
180 data_lo();
181 _delay_us(65);
182 data_hi();
183 _delay_us(35);
184 }
185
186 static inline void place_bit1(void)
187 {
188 data_lo();
189 _delay_us(35);
190 data_hi();
191 _delay_us(65);
192 }
193
194 static inline void send_byte(uint8_t data)
195 {
196 for (int i = 0; i < 8; i++) {
197 if (data&(0x80>>i))
198 place_bit1();
199 else
200 place_bit0();
201 }
202 }
203
204 static inline bool read_bit(void)
205 {
206 // ADB Bit Cells
207 //
208 // bit cell time: 70-130us
209 // low part of bit0: 60-70% of bit cell
210 // low part of bit1: 30-40% of bit cell
211 //
212 // bit cell time 70us 130us
213 // --------------------------------------------
214 // low part of bit0 42-49 78-91
215 // high part of bit0 21-28 39-52
216 // low part of bit1 21-28 39-52
217 // high part of bit1 42-49 78-91
218 //
219 //
220 // bit0:
221 // 70us bit cell:
222 // ____________~~~~~~
223 // 42-49 21-28
224 //
225 // 130us bit cell:
226 // ____________~~~~~~
227 // 78-91 39-52
228 //
229 // bit1:
230 // 70us bit cell:
231 // ______~~~~~~~~~~~~
232 // 21-28 42-49
233 //
234 // 130us bit cell:
235 // ______~~~~~~~~~~~~
236 // 39-52 78-91
237 //
238 // read:
239 // ________|~~~~~~~~~
240 // 55us
241 // Read data line after 55us. If data line is low/high then bit is 0/1.
242 // This method might not work at <90us bit cell time.
243 //
244 // [from Apple IIgs Hardware Reference Second Edition]
245 bool bit;
246 wait_data_lo(75); // wait the start of bit cell at least 130ms(55+0+75)
247 _delay_us(55);
248 bit = data_in();
249 wait_data_hi(36); // wait high part of bit cell at least 91ms(55+36)
250 return bit;
251 }
252
253 static inline uint8_t read_byte(void)
254 {
255 uint8_t data = 0;
256 for (int i = 0; i < 8; i++) {
257 data <<= 1;
258 if (read_bit())
259 data = data | 1;
260 }
261 return data;
262 }
263
264 static inline uint8_t wait_data_lo(uint16_t us)
265 {
266 while (data_in() && us) {
267 _delay_us(1);
268 us--;
269 }
270 return us;
271 }
272
273 static inline uint8_t wait_data_hi(uint8_t us)
274 {
275 while (!data_in() && us) {
276 _delay_us(1);
277 us--;
278 }
279 return us;
280 }
281
282
283 /*
284 ADB Protocol
285 ============
286
287 Resources
288 ---------
289 ADB - The Untold Story: Space Aliens Ate My Mouse
290 http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html
291 ADB Manager
292 http://developer.apple.com/legacy/mac/library/documentation/mac/pdf/Devices/ADB_Manager.pdf
293 Service request(5-17)
294 Apple IIgs Hardware Reference Second Edition [Chapter6 p121]
295 ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf
296 ADB Keycode
297 http://72.0.193.250/Documentation/macppc/adbkeycodes/
298 http://m0115.web.fc2.com/m0115.jpg
299 [Inside Macintosh volume V, pages 191-192]
300 http://www.opensource.apple.com/source/IOHIDFamily/IOHIDFamily-421.18.3/IOHIDFamily/Cosmo_USB2ADB.c
301 ADB Signaling
302 http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf
303 ADB Overview & History
304 http://en.wikipedia.org/wiki/Apple_Desktop_Bus
305 Microchip Application Note: ADB device(with code for PIC16C)
306 http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062
307 AVR ATtiny2131 ADB to PS/2 converter(Japanese)
308 http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html
309
310
311 Pinouts
312 -------
313 ADB female socket from the front:
314 __________
315 | | <--- top
316 | 4o o3 |
317 |2o o1|
318 | == |
319 |________| <--- bottom
320 | | <--- 4pins
321
322
323 ADB female socket from bottom:
324
325 ========== <--- front
326 | |
327 | |
328 |2o o1|
329 |4o o3|
330 ---------- <--- back
331
332 1: Data
333 2: Power SW(low when press Power key)
334 3: Vcc(5V)
335 4: GND
336
337
338 Commands
339 --------
340 ADB command is 1byte and consists of 4bit-address, 2bit-command
341 type and 2bit-register. The commands are always sent by Host.
342
343 Command format:
344 7 6 5 4 3 2 1 0
345 | | | |------------ address
346 | |-------- command type
347 | |---- register
348
349 bits commands
350 ------------------------------------------------------
351 - - - - 0 0 0 0 Send Request(reset all devices)
352 A A A A 0 0 0 1 Flush(reset a device)
353 - - - - 0 0 1 0 Reserved
354 - - - - 0 0 1 1 Reserved
355 - - - - 0 1 - - Reserved
356 A A A A 1 0 R R Listen(write to a device)
357 A A A A 1 1 R R Talk(read from a device)
358
359 The command to read keycodes from keyboard is 0x2C which
360 consist of keyboard address 2 and Talk against register 0.
361
362 Address:
363 2: keyboard
364 3: mice
365
366 Registers:
367 0: application(keyobard uses this to store its data.)
368 1: application
369 2: application(keyboard uses this for LEDs and state of modifiers)
370 3: status and command
371
372
373 Communication
374 -------------
375 This is a minimum information for keyboard communication.
376 See "Resources" for detail.
377
378 Signaling:
379
380 ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~
381
382 |800us | |7 Command 0| | | |15-64 Data 0|Stopbit(0)
383 +Attention | | | +Startbit(1)
384 +Startbit(1) | +Tlt(140-260us)
385 +stopbit(0)
386
387 Bit cells:
388
389 bit0: ______~~~
390 65 :35us
391
392 bit1: ___~~~~~~
393 35 :65us
394
395 bit0 low time: 60-70% of bit cell(42-91us)
396 bit1 low time: 30-40% of bit cell(21-52us)
397 bit cell time: 70-130us
398 [from Apple IIgs Hardware Reference Second Edition]
399
400 Criterion for bit0/1:
401 After 55us if line is low/high then bit is 0/1.
402
403 Attention & start bit:
404 Host asserts low in 560-1040us then places start bit(1).
405
406 Tlt(Stop to Start):
407 Bus stays high in 140-260us then device places start bit(1).
408
409 Global reset:
410 Host asserts low in 2.8-5.2ms. All devices are forced to reset.
411
412 Service request from device(Srq):
413 Device can request to send at commad(Global only?) stop bit.
414 Requesting device keeps low for 140-260us at stop bit of command.
415
416
417 Keyboard Data(Register0)
418 This 16bit data can contains two keycodes and two released flags.
419 First keycode is palced in upper byte. When one keyocode is sent,
420 lower byte is 0xFF.
421 Release flag is 1 when key is released.
422
423 1514 . . . . . 8 7 6 . . . . . 0
424 | | | | | | | | | +-+-+-+-+-+-+- Keycode2
425 | | | | | | | | +--------------- Released2(1 when the key is released)
426 | +-+-+-+-+-+-+----------------- Keycode1
427 +------------------------------- Released1(1 when the key is released)
428
429 Keycodes:
430 Scancode consists of 7bit keycode and 1bit release flag.
431 Device can send two keycodes at once. If just one keycode is sent
432 keycode1 contains it and keyocode2 is 0xFF.
433
434 Power switch:
435 You can read the state from PSW line(active low) however
436 the switch has a special scancode 0x7F7F, so you can
437 also read from Data line. It uses 0xFFFF for release scancode.
438
439 Keyboard LEDs & state of keys(Register2)
440 This register hold current state of three LEDs and nine keys.
441 The state of LEDs can be changed by sending Listen command.
442
443 1514 . . . . . . 7 6 5 . 3 2 1 0
444 | | | | | | | | | | | | | | | +- LED1(NumLock)
445 | | | | | | | | | | | | | | +--- LED2(CapsLock)
446 | | | | | | | | | | | | | +----- LED3(ScrollLock)
447 | | | | | | | | | | +-+-+------- Reserved
448 | | | | | | | | | +------------- ScrollLock
449 | | | | | | | | +--------------- NumLock
450 | | | | | | | +----------------- Apple/Command
451 | | | | | | +------------------- Option
452 | | | | | +--------------------- Shift
453 | | | | +----------------------- Control
454 | | | +------------------------- Reset/Power
455 | | +--------------------------- CapsLock
456 | +----------------------------- Delete
457 +------------------------------- Reserved
458
459 END_OF_ADB
460 */
Imprint / Impressum