2 Copyright 2011 Jun WAKO <wakojun@gmail.com>
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.
10 Redistribution and use in source and binary forms, with or without
11 modification, are permitted provided that the following conditions are met:
13 * Redistributions of source code must retain the above copyright
14 notice, this list of conditions and the following disclaimer.
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
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.
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.
39 #include <util/delay.h>
41 #include <avr/interrupt.h>
46 static inline void data_lo(void);
47 static inline void data_hi(void);
48 static inline bool data_in(void);
50 static inline void psw_lo(void);
51 static inline void psw_hi(void);
52 static inline bool psw_in(void);
55 static inline void attention(void);
56 static inline void place_bit0(void);
57 static inline void place_bit1(void);
58 static inline void send_byte(uint8_t data);
59 static inline bool read_bit(void);
60 static inline uint8_t read_byte(void);
61 static inline uint8_t wait_data_lo(uint16_t us);
62 static inline uint8_t wait_data_hi(uint8_t us);
65 void adb_host_init(void)
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);
80 bool adb_host_psw(void)
86 uint16_t adb_host_kbd_recv(void)
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
95 if (!read_bit()) { // Startbit(1)
97 dprintf("Startbit ERROR\n");
101 // ad hoc fix: without block inerrupt read wrong bit occasionally and get keys stuck
104 data = (data<<8) | read_byte();
105 uint8_t stop = read_bit(); // Stopbit(0)
109 dprintf("Stopbit ERROR\n");
115 void adb_host_listen(uint8_t cmd, uint8_t data_h, uint8_t data_l)
119 place_bit0(); // Stopbit(0)
120 _delay_us(200); // Tlt/Stop to Start
121 place_bit1(); // Startbit(1)
124 place_bit0(); // Stopbit(0);
127 // send state of LEDs
128 void adb_host_kbd_led(uint8_t led)
130 // Addr:Keyboard(0010), Cmd:Listen(10), Register2(10)
131 // send upper byte (not used)
132 // send lower byte (bit2: ScrollLock, bit1: CapsLock, bit0:
133 adb_host_listen(0x2A,0,led&0x07);
137 static inline void data_lo()
139 ADB_DDR |= (1<<ADB_DATA_BIT);
140 ADB_PORT &= ~(1<<ADB_DATA_BIT);
142 static inline void data_hi()
144 ADB_PORT |= (1<<ADB_DATA_BIT);
145 ADB_DDR &= ~(1<<ADB_DATA_BIT);
147 static inline bool data_in()
149 ADB_PORT |= (1<<ADB_DATA_BIT);
150 ADB_DDR &= ~(1<<ADB_DATA_BIT);
151 return ADB_PIN&(1<<ADB_DATA_BIT);
155 static inline void psw_lo()
157 ADB_DDR |= (1<<ADB_PSW_BIT);
158 ADB_PORT &= ~(1<<ADB_PSW_BIT);
160 static inline void psw_hi()
162 ADB_PORT |= (1<<ADB_PSW_BIT);
163 ADB_DDR &= ~(1<<ADB_PSW_BIT);
165 static inline bool psw_in()
167 ADB_PORT |= (1<<ADB_PSW_BIT);
168 ADB_DDR &= ~(1<<ADB_PSW_BIT);
169 return ADB_PIN&(1<<ADB_PSW_BIT);
173 static inline void attention(void)
180 static inline void place_bit0(void)
188 static inline void place_bit1(void)
196 static inline void send_byte(uint8_t data)
198 for (int i = 0; i < 8; i++) {
206 static inline bool read_bit(void)
210 // bit cell time: 70-130us
211 // low part of bit0: 60-70% of bit cell
212 // low part of bit1: 30-40% of bit cell
214 // bit cell time 70us 130us
215 // --------------------------------------------
216 // low part of bit0 42-49 78-91
217 // high part of bit0 21-28 39-52
218 // low part of bit1 21-28 39-52
219 // high part of bit1 42-49 78-91
224 // ____________~~~~~~
228 // ____________~~~~~~
233 // ______~~~~~~~~~~~~
237 // ______~~~~~~~~~~~~
241 // ________|~~~~~~~~~
243 // Read data line after 55us. If data line is low/high then bit is 0/1.
244 // This method might not work at <90us bit cell time.
246 // [from Apple IIgs Hardware Reference Second Edition]
248 wait_data_lo(75); // wait the start of bit cell at least 130ms(55+0+75)
251 wait_data_hi(36); // wait high part of bit cell at least 91ms(55+36)
255 static inline uint8_t read_byte(void)
258 for (int i = 0; i < 8; i++) {
266 static inline uint8_t wait_data_lo(uint16_t us)
268 while (data_in() && us) {
275 static inline uint8_t wait_data_hi(uint8_t us)
277 while (!data_in() && us) {
291 ADB - The Untold Story: Space Aliens Ate My Mouse
292 http://developer.apple.com/legacy/mac/library/#technotes/hw/hw_01.html
294 http://developer.apple.com/legacy/mac/library/documentation/mac/pdf/Devices/ADB_Manager.pdf
295 Service request(5-17)
296 Apple IIgs Hardware Reference Second Edition [Chapter6 p121]
297 ftp://ftp.apple.asimov.net/pub/apple_II/documentation/Apple%20IIgs%20Hardware%20Reference.pdf
299 http://72.0.193.250/Documentation/macppc/adbkeycodes/
300 http://m0115.web.fc2.com/m0115.jpg
301 [Inside Macintosh volume V, pages 191-192]
302 http://www.opensource.apple.com/source/IOHIDFamily/IOHIDFamily-421.18.3/IOHIDFamily/Cosmo_USB2ADB.c
304 http://kbdbabel.sourceforge.net/doc/kbd_signaling_pcxt_ps2_adb.pdf
305 ADB Overview & History
306 http://en.wikipedia.org/wiki/Apple_Desktop_Bus
307 Microchip Application Note: ADB device(with code for PIC16C)
308 http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en011062
309 AVR ATtiny2131 ADB to PS/2 converter(Japanese)
310 http://hp.vector.co.jp/authors/VA000177/html/KeyBoardA5DEA5CBA5A2II.html
315 ADB female socket from the front:
321 |________| <--- bottom
325 ADB female socket from bottom:
327 ========== <--- front
335 2: Power SW(low when press Power key)
342 ADB command is 1byte and consists of 4bit-address, 2bit-command
343 type and 2bit-register. The commands are always sent by Host.
347 | | | |------------ address
348 | |-------- command type
352 ------------------------------------------------------
353 - - - - 0 0 0 0 Send Request(reset all devices)
354 A A A A 0 0 0 1 Flush(reset a device)
355 - - - - 0 0 1 0 Reserved
356 - - - - 0 0 1 1 Reserved
357 - - - - 0 1 - - Reserved
358 A A A A 1 0 R R Listen(write to a device)
359 A A A A 1 1 R R Talk(read from a device)
361 The command to read keycodes from keyboard is 0x2C which
362 consist of keyboard address 2 and Talk against register 0.
369 0: application(keyobard uses this to store its data.)
371 2: application(keyboard uses this for LEDs and state of modifiers)
372 3: status and command
377 This is a minimum information for keyboard communication.
378 See "Resources" for detail.
382 ~~~~____________~~||||||||||||__~~~~~_~~|||||||||||||||__~~~~
384 |800us | |7 Command 0| | | |15-64 Data 0|Stopbit(0)
385 +Attention | | | +Startbit(1)
386 +Startbit(1) | +Tlt(140-260us)
397 bit0 low time: 60-70% of bit cell(42-91us)
398 bit1 low time: 30-40% of bit cell(21-52us)
399 bit cell time: 70-130us
400 [from Apple IIgs Hardware Reference Second Edition]
402 Criterion for bit0/1:
403 After 55us if line is low/high then bit is 0/1.
405 Attention & start bit:
406 Host asserts low in 560-1040us then places start bit(1).
409 Bus stays high in 140-260us then device places start bit(1).
412 Host asserts low in 2.8-5.2ms. All devices are forced to reset.
414 Service request from device(Srq):
415 Device can request to send at commad(Global only?) stop bit.
416 Requesting device keeps low for 140-260us at stop bit of command.
419 Keyboard Data(Register0)
420 This 16bit data can contains two keycodes and two released flags.
421 First keycode is palced in upper byte. When one keyocode is sent,
423 Release flag is 1 when key is released.
425 1514 . . . . . 8 7 6 . . . . . 0
426 | | | | | | | | | +-+-+-+-+-+-+- Keycode2
427 | | | | | | | | +--------------- Released2(1 when the key is released)
428 | +-+-+-+-+-+-+----------------- Keycode1
429 +------------------------------- Released1(1 when the key is released)
432 Scancode consists of 7bit keycode and 1bit release flag.
433 Device can send two keycodes at once. If just one keycode is sent
434 keycode1 contains it and keyocode2 is 0xFF.
437 You can read the state from PSW line(active low) however
438 the switch has a special scancode 0x7F7F, so you can
439 also read from Data line. It uses 0xFFFF for release scancode.
441 Keyboard LEDs & state of keys(Register2)
442 This register hold current state of three LEDs and nine keys.
443 The state of LEDs can be changed by sending Listen command.
445 1514 . . . . . . 7 6 5 . 3 2 1 0
446 | | | | | | | | | | | | | | | +- LED1(NumLock)
447 | | | | | | | | | | | | | | +--- LED2(CapsLock)
448 | | | | | | | | | | | | | +----- LED3(ScrollLock)
449 | | | | | | | | | | +-+-+------- Reserved
450 | | | | | | | | | +------------- ScrollLock
451 | | | | | | | | +--------------- NumLock
452 | | | | | | | +----------------- Apple/Command
453 | | | | | | +------------------- Option
454 | | | | | +--------------------- Shift
455 | | | | +----------------------- Control
456 | | | +------------------------- Reset/Power
457 | | +--------------------------- CapsLock
458 | +----------------------------- Delete
459 +------------------------------- Reserved