2 Copyright 2011-19 Jun WAKO <wakojun@gmail.com>
3 Copyright 2013 Shay Green <gblargg@gmail.com>
5 This software is licensed with a Modified BSD License.
6 All of this is supposed to be Free Software, Open Source, DFSG-free,
7 GPL-compatible, and OK to use in both free and proprietary applications.
8 Additions and corrections to this file are welcome.
11 Redistribution and use in source and binary forms, with or without
12 modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright
15 notice, this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in
19 the documentation and/or other materials provided with the
22 * Neither the name of the copyright holders nor the names of
23 contributors may be used to endorse or promote products derived
24 from this software without specific prior written permission.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 POSSIBILITY OF SUCH DAMAGE.
40 #include <util/delay.h>
42 #include <avr/interrupt.h>
47 // GCC doesn't inline functions normally
48 #define data_lo() (ADB_DDR |= (1<<ADB_DATA_BIT))
49 #define data_hi() (ADB_DDR &= ~(1<<ADB_DATA_BIT))
50 #define data_in() (ADB_PIN & (1<<ADB_DATA_BIT))
53 static inline void psw_lo(void);
54 static inline void psw_hi(void);
55 static inline bool psw_in(void);
58 static inline void attention(void);
59 static inline void place_bit0(void);
60 static inline void place_bit1(void);
61 static inline void send_byte(uint8_t data);
62 static inline uint16_t wait_data_lo(uint16_t us);
63 static inline uint16_t wait_data_hi(uint16_t us);
66 void adb_host_init(void)
68 ADB_PORT &= ~(1<<ADB_DATA_BIT);
76 bool adb_host_psw(void)
83 * Don't call this in a row without the delay, otherwise it makes some of poor controllers
84 * overloaded and misses strokes. Recommended interval is 12ms.
86 * Thanks a lot, blargg!
87 * <http://geekhack.org/index.php?topic=14290.msg1068919#msg1068919>
88 * <http://geekhack.org/index.php?topic=14290.msg1070139#msg1070139>
90 uint16_t adb_host_kbd_recv(uint8_t addr)
92 return adb_host_talk(addr, ADB_REG_0);
95 #ifdef ADB_MOUSE_ENABLE
96 __attribute__ ((weak))
97 void adb_mouse_init(void) {
101 __attribute__ ((weak))
102 void adb_mouse_task(void) {
107 // This sends Talk command to read data from register and returns length of the data.
108 uint8_t adb_host_talk_buf(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t len)
110 for (int8_t i =0; i < len; i++) buf[i] = 0;
114 send_byte((addr<<4) | ADB_CMD_TALK | reg);
115 place_bit0(); // Stopbit(0)
116 // TODO: Service Request(Srq):
117 // Device holds low part of comannd stopbit for 140-260us
120 // ......._ ______________________ ___ ............_ -------
122 // Command | | | | | Data bytes | |
123 // ........|___| | 140-260 |__| |_............|___|
124 // |stop0 | Tlt Stop-to-Start |start1| |stop0 |
126 // Command without data:
127 // ......._ __________________________
130 // ........|___| | 140-260 |
131 // |stop0 | Tlt Stop-to-Start |
134 // ......._ ______ ___ ............_ -------
135 // | 140-260 | | | | | |
136 // Command | Service Request | | | | Data bytes | |
137 // ........|___________________| |__| |_............|___|
138 // |stop0 | |start1| |stop0 |
139 // ......._ __________
141 // Command | Service Request |
142 // ........|___________________|
144 // This can be happened?
145 // ......._ ______________________ ___ ............_ -----
146 // | | | | | | 140-260 |
147 // Command | | | | | Data bytes | Service Request |
148 // ........|___| | 140-260 |__| |_............|_________________|
149 // |stop0 | Tlt Stop-to-Start |start1| |stop0 |
151 // "Service requests are issued by the devices during a very specific time at the
152 // end of the reception of the command packet.
153 // If a device in need of service issues a service request, it must do so within
154 // the 65 µs of the Stop Bit’s low time and maintain the line low for a total of 300 µs."
156 // "A device sends a Service Request signal by holding the bus low during the low
157 // portion of the stop bit of any command or data transaction. The device must lengthen
158 // the stop by a minimum of 140 J.lS beyond its normal duration, as shown in Figure 8-15."
159 // http://ww1.microchip.com/downloads/en/AppNotes/00591b.pdf
160 if (!wait_data_hi(500)) { // Service Request(310us Adjustable Keyboard): just ignored
165 if (!wait_data_lo(500)) { // Tlt/Stop to Start(140-260us)
167 return 0; // No data from device(not error);
171 if (!wait_data_hi(40)) {
176 if (!wait_data_lo(100)) {
182 uint8_t n = 0; // bit count
185 // |<- bit_cell_max(130) ->|
190 // | 130-lo | lo-hi |
193 uint8_t lo = (uint8_t) wait_data_hi(130);
195 goto error; // no more bit or after stop bit
197 uint8_t hi = (uint8_t) wait_data_lo(lo);
199 goto error; // stop bit extedned by Srq?
201 if (n/8 >= len) continue; // can't store in buf
204 if ((130 - lo) < (lo - hi)) {
215 uint16_t adb_host_talk(uint8_t addr, uint8_t reg)
219 len = adb_host_talk_buf(addr, reg, buf, 8);
220 if (len != 2) return 0;
221 return (buf[0]<<8 | buf[1]);
224 void adb_host_listen_buf(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t len)
228 send_byte((addr<<4) | ADB_CMD_LISTEN | reg);
229 place_bit0(); // Stopbit(0)
230 // TODO: Service Request
231 _delay_us(200); // Tlt/Stop to Start
232 place_bit1(); // Startbit(1)
233 for (int8_t i = 0; i < len; i++) {
235 //xprintf("%02X ", buf[i]);
237 place_bit0(); // Stopbit(0);
241 void adb_host_listen(uint8_t addr, uint8_t reg, uint8_t data_h, uint8_t data_l)
243 uint8_t buf[2] = { data_h, data_l };
244 adb_host_listen_buf(addr, reg, buf, 2);
247 void adb_host_flush(uint8_t addr)
251 send_byte((addr<<4) | ADB_CMD_FLUSH);
252 place_bit0(); // Stopbit(0)
253 _delay_us(200); // Tlt/Stop to Start
257 // send state of LEDs
258 void adb_host_kbd_led(uint8_t addr, uint8_t led)
261 // upper byte: not used
262 // lower byte: bit2=ScrollLock, bit1=CapsLock, bit0=NumLock
263 adb_host_listen(addr, 2, 0, led & 0x07);
268 static inline void psw_lo()
270 ADB_DDR |= (1<<ADB_PSW_BIT);
271 ADB_PORT &= ~(1<<ADB_PSW_BIT);
273 static inline void psw_hi()
275 ADB_PORT |= (1<<ADB_PSW_BIT);
276 ADB_DDR &= ~(1<<ADB_PSW_BIT);
278 static inline bool psw_in()
280 ADB_PORT |= (1<<ADB_PSW_BIT);
281 ADB_DDR &= ~(1<<ADB_PSW_BIT);
282 return ADB_PIN&(1<<ADB_PSW_BIT);
286 static inline void attention(void)
289 _delay_us(800-35); // bit1 holds lo for 35 more
293 static inline void place_bit0(void)
301 static inline void place_bit1(void)
309 static inline void send_byte(uint8_t data)
311 for (int i = 0; i < 8; i++) {
319 // These are carefully coded to take 6 cycles of overhead.
320 // inline asm approach became too convoluted
321 static inline uint16_t wait_data_lo(uint16_t us)
326 _delay_us(1 - (6 * 1000000.0 / F_CPU));
332 static inline uint16_t wait_data_hi(uint16_t us)
337 _delay_us(1 - (6 * 1000000.0 / F_CPU));
350 ADB - The Untold Story: Space Aliens Ate My Mouse
351 http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html
353 http://developer.apple.com/legacy/mac/library/documentation/mac/pdf/Devices/ADB_Manager.pdf
354 Service request(5-17)
355 Apple IIgs Hardware Reference Second Edition [Chapter6 p121]
356 ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf
358 http://72.0.193.250/Documentation/macppc/adbkeycodes/
359 http://m0115.web.fc2.com/m0115.jpg
360 [Inside Macintosh volume V, pages 191-192]
361 http://www.opensource.apple.com/source/IOHIDFamily/IOHIDFamily-421.18.3/IOHIDFamily/Cosmo_USB2ADB.c
363 http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf
364 ADB Overview & History
365 http://en.wikipedia.org/wiki/Apple_Desktop_Bus
366 Microchip Application Note: ADB device(with code for PIC16C)
367 http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062
368 AVR ATtiny2131 ADB to PS/2 converter(Japanese)
369 http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html
374 ADB female socket from the front:
380 |________| <--- bottom
384 ADB female socket from bottom:
386 ========== <--- front
394 2: Power SW(low when press Power key)
401 ADB command is 1byte and consists of 4bit-address, 2bit-command
402 type and 2bit-register. The commands are always sent by Host.
406 | | | |------------ address
407 | |-------- command type
411 ------------------------------------------------------
412 - - - - 0 0 0 0 Send Reset(reset all devices)
413 A A A A 0 0 0 1 Flush(reset a device)
414 - - - - 0 0 1 0 Reserved
415 - - - - 0 0 1 1 Reserved
416 - - - - 0 1 - - Reserved
417 A A A A 1 0 R R Listen(write to a device)
418 A A A A 1 1 R R Talk(read from a device)
420 The command to read keycodes from keyboard is 0x2C which
421 consist of keyboard address 2 and Talk against register 0.
428 0: application(keyboard uses this to store its data.)
430 2: application(keyboard uses this for LEDs and state of modifiers)
431 3: status and command
436 This is a minimum information for keyboard communication.
437 See "Resources" for detail.
441 ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~
443 |800us | |7 Command 0| | | |15-64 Data 0|Stopbit(0)
444 +Attention | | | +Startbit(1)
445 +Startbit(1) | +Tlt(140-260us)
456 bit0 low time: 60-70% of bit cell(42-91us)
457 bit1 low time: 30-40% of bit cell(21-52us)
458 bit cell time: 70-130us
459 [from Apple IIgs Hardware Reference Second Edition]
461 Criterion for bit0/1:
462 After 55us if line is low/high then bit is 0/1.
464 Attention & start bit:
465 Host asserts low in 560-1040us then places start bit(1).
468 Bus stays high in 140-260us then device places start bit(1).
471 Host asserts low in 2.8-5.2ms. All devices are forced to reset.
473 Service request from device(Srq):
474 Device can request to send at commad(Global only?) stop bit.
475 Requesting device keeps low for 140-260us at stop bit of command.
478 Keyboard Data(Register0)
479 This 16bit data can contains two keycodes and two released flags.
480 First keycode is palced in upper byte. When one keyocode is sent,
482 Release flag is 1 when key is released.
484 1514 . . . . . 8 7 6 . . . . . 0
485 | | | | | | | | | +-+-+-+-+-+-+- Keycode2
486 | | | | | | | | +--------------- Released2(1 when the key is released)
487 | +-+-+-+-+-+-+----------------- Keycode1
488 +------------------------------- Released1(1 when the key is released)
491 Scancode consists of 7bit keycode and 1bit release flag.
492 Device can send two keycodes at once. If just one keycode is sent
493 keycode1 contains it and keyocode2 is 0xFF.
496 You can read the state from PSW line(active low) however
497 the switch has a special scancode 0x7F7F, so you can
498 also read from Data line. It uses 0xFFFF for release scancode.
500 Keyboard LEDs & state of keys(Register2)
501 This register hold current state of three LEDs and nine keys.
502 The state of LEDs can be changed by sending Listen command.
504 1514 . . . . . . 7 6 5 . 3 2 1 0
505 | | | | | | | | | | | | | | | +- LED1(NumLock)
506 | | | | | | | | | | | | | | +--- LED2(CapsLock)
507 | | | | | | | | | | | | | +----- LED3(ScrollLock)
508 | | | | | | | | | | +-+-+------- Reserved
509 | | | | | | | | | +------------- ScrollLock
510 | | | | | | | | +--------------- NumLock
511 | | | | | | | +----------------- Apple/Command
512 | | | | | | +------------------- Option
513 | | | | | +--------------------- Shift
514 | | | | +----------------------- Control
515 | | | +------------------------- Reset/Power
516 | | +--------------------------- CapsLock
517 | +----------------------------- Delete
518 +------------------------------- Reserved
520 Address, Handler ID and bits(Register3)
521 1514131211 . . 8 7 . . . . . . 0
522 | | | | | | | | | | | | | | | |
523 | | | | | | | | +-+-+-+-+-+-+-+- Handler ID
524 | | | | +-+-+-+----------------- Address
525 | | | +------------------------- 0
526 | | +--------------------------- Service request enable(1 = enabled)
527 | +----------------------------- Exceptional event(alwyas 1 if not used)
528 +------------------------------- 0
531 bit cell time: 70-130us
532 low part of bit0: 60-70% of bit cell
533 low part of bit1: 30-40% of bit cell
535 bit cell time 70us 130us
536 --------------------------------------------
537 low part of bit0 42-49 78-91
538 high part of bit0 21-28 39-52
539 low part of bit1 21-28 39-52
540 high part of bit1 42-49 78-91
561 [from Apple IIgs Hardware Reference Second Edition]
564 Apple Standard Keyboard M0116: 0x01
565 Apple Extended Keyboard M0115: 0x02
566 Apple Extended Keyboard II M3501: 0x02
567 Apple Adjustable Keybaord: 0x10
569 http://lxr.free-electrons.com/source/drivers/macintosh/adbhid.c?v=4.4#L802