Author Topic: ADB to USB keyboard converter  (Read 41434 times)

0 Members and 1 Guest are viewing this topic.

Offline hasu

  • Thread Starter
  • Posts: 947
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter
« on: Wed, 12 January 2011, 11:53:40 »
I needed a ADB to USB keyboard converter to test a feel of ADB keyboards on actual usage. After 2days coding and several hours debug, I made this converter based on my keyboard firmware. If you are interested in ADB keyboard converter, try this.



WARNING
Key drop problem
http://geekhack.org/index.php?topic=14290.msg1064567#msg1064567

Finally this was fixed due to blargg's contribution. 2013/11/28
http://geekhack.org/index.php?topic=14290.msg1134847#msg1134847
https://github.com/tmk/tmk_keyboard/pull/80
https://github.com/tmk/tmk_keyboard/issues/14

UPDATE
2012/12/12 Extended keyboard support by blargg. This offers distinct codes for left/right modifier keys except for command(apple) key.
http://geekhack.org/index.php?topic=14290.msg736664#msg736664
2013/04/08 Fix issue: ADB-USB converter misses keystrokes (NOT FIXED COMPLETELY)
2013/07/22 Changed Pin usage: PD0, NOT PF0 now
2013/10/11 WIP: Fix ADB-USB converter misses keystrokes
2013/11/28 Fix key drop problem finally Thank you, blargg!


SOURCE CODE
https://github.com/tmk/tmk_keyboard/tree/master/converter/adb_usb



BUILD CONVERTER
You can use PJRC Teensy or other dev board with ATMega32U4.

Code: [Select]
ADB female socket from the front:
  ,--_--.                 
 / o4 3o \      1: DATA
| o2   1o |     2: Power SW
 -  ===  -      3: VCC
  `-___-'       4: GND

  • Connect keyboard DATA line to PD0 pin(configurable in config.h), not to mention VCC and GND as well.
  • Just 'make' to build firmware binary.
  • And program your AVR with the firmware.
DEBUG
You can use PJRC's 'hid_listen' command to see debug print.
http://www.pjrc.com/teensy/hid_listen.html


LOCKING CAPSLOCK
Many of old ADB keyboards have locking switch for Caps lock.
https://github.com/tmk/tmk_keyboard#mechanical-locking-support-for-capslock


Also you may want to remove locking pin from the push-lock switch to use it as a normal momentary switch.
http://www.youtube.com/watch?v=9wqnt2mGJ2Y



PULL-UP RESISTOR
You must have a external pull-up resistor(1K is recommended) on DATA line.
This converter uses AVR's internal pull-up, but it seems to be  too weak to communicate with ADB keyboard. The external pull-up resistor is strongly recommended required.

Code: [Select]
Keyboard       Conveter
               ,------.
5V------+------|VCC   |
        |      |      |
        R      |      |
        |      |      |
Signal--+------|PD0   |
               |      |
GND------------|GND   |
               `------'
R: 1K Ohm resistor
https://github.com/tmk/tmk_keyboard/wiki/FAQ#pull-up-resistor


ADB protocol Resource for Developer
This is code for ADB protocol:
https://github.com/tmk/tmk_keyboard/blob/master/protocol/adb.c
« Last Edit: Mon, 24 March 2014, 23:16:42 by hasu »
⚠ My Engrish is broken.    tmk keyboard firmware project(ADB,PS/2,X68K,PC98,M0110,HHKB,GH60)

Offline lowpoly

  • Posts: 2483
ADB to USB keyboard converter for Teensy
« Reply #1 on: Fri, 14 January 2011, 04:51:53 »
Hah, great.

Strange nobody replied. I thought the Griffin iMate was hard to get these days. The mod forum should have a better place in the forum overview, IMO.

Also, the iMate had issues with intl. 'boards.

So, Colemak converter for ps/2 'boards next? :-)
The Apple M0110 Today
Miniguru thread at GH, kit preview site at guru-board.com

Offline Soarer

  • * Moderator
  • Posts: 2672
  • Location: UK
ADB to USB keyboard converter for Teensy
« Reply #2 on: Fri, 14 January 2011, 06:20:26 »
Neat. :)

I would like to add ADB support to my adapter in the future, but I have lots of other tasks to complete first! One of them is implementing a key remapping UI ;)

And I recently bought a couple of the Belkin ADB adapters, which work ok, at least on US layout. But it seems they are not generally available either :/

Offline Findecanor

  • Posts: 2612
  • Location: Stockholm, Sweden
ADB to USB keyboard converter for Teensy
« Reply #3 on: Fri, 14 January 2011, 06:57:32 »
Superb! How does it handle latching Caps Lock, though?
"Smart" phones make people stupid.

My daily driver: Phantom (lubed Cherry MX Clear, thick lasered Cherry PBT, custom retro case, custom firmware).

Offline iMav

  • babysitter
  • Location: Columbus, WI
  • "En casa del herrero, cuchillo de palo"
ADB to USB keyboard converter for Teensy
« Reply #4 on: Fri, 14 January 2011, 07:26:10 »
Great job!  Love seeing this type of stuff.  How many days have you lost with your head down programming that teensy?  ;)

I may just have to pick up one of these and start playing as well.

Offline hasu

  • Thread Starter
  • Posts: 947
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #5 on: Sat, 15 January 2011, 01:08:18 »
Quote from: Findecanor;278417
Superb! How does it handle latching Caps Lock, though?


It doesn't nothing actually now.
At this time you must remove lock pin form the ALPS or replace with normal switch :)
Or if you don't care about Caps LED, you can remap to Shift.
⚠ My Engrish is broken.    tmk keyboard firmware project(ADB,PS/2,X68K,PC98,M0110,HHKB,GH60)

Offline kps

  • Posts: 607
ADB to USB keyboard converter for Teensy
« Reply #6 on: Sat, 15 January 2011, 12:12:08 »
Hasu, would you mind posting your hex file? I am trying to figure out what stupid thing I have done, since I see no keystrokes. The device is recognized as a keyboard, so I know it has been programmed correctly. I have tried two different computers, three different keyboards, and double-checked my wiring.


Quote from: hasu;278881
It doesn't nothing actually now.
At this time you must remove lock pin form the ALPS or replace with normal switch :)
Or if you don't care about Caps LED, you can remap to Shift.


This part should be simple; there is a specific USB code for locking Caps keys 130 rather than 57.

Offline hasu

  • Thread Starter
  • Posts: 947
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #7 on: Sat, 15 January 2011, 23:05:37 »
Quote from: kps;279038
Hasu, would you mind posting your hex file? I am trying to figure out what stupid thing I have done, since I see no keystrokes. The device is recognized as a keyboard, so I know it has been programmed correctly. I have tried two different computers, three different keyboards, and double-checked my wiring.

No, I don't mind at all.

Regarding your wiring, it seems to be OK but you may need to check ADB socket side again.
Following is my ADB socket pinouts, but I think yours is same as mine.

Code: [Select]

    Female socket from the front
    __________
    |        | <--- top
    | 4o  o3 |
    |2o    o1|
    |   ==   |
    |________| <--- bottom
      |    |   <--- 4pins

    Female socket from bottom

    ========== <--- front
    |        |
    |        |
    |2o    o1|
    |4o    o3|
    ---------- <--- back

    1: Data
    2: Power SW(low when press Power key)
    3: Vcc(5V)
    4: GND


   

Quote

This part should be simple; there is a specific USB code for locking Caps keys 130 rather than 57.


Thank you. I will try the keycode and fix my code.
⚠ My Engrish is broken.    tmk keyboard firmware project(ADB,PS/2,X68K,PC98,M0110,HHKB,GH60)

Offline hasu

  • Thread Starter
  • Posts: 947
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #8 on: Sun, 16 January 2011, 00:04:22 »
I tried the "Locking Caps Lock"(0x82) instead of Caps Lock(0x39) in Windows and it failed to handle the key. I didn't check it on other OSes, but Windows fails at least.

Windows has no translation table for "Locking Caps Lock", thus we can use the key unfortunately :(

See this document for details:
http://www.microsoft.com/whdc/archive/scancode.mspx
⚠ My Engrish is broken.    tmk keyboard firmware project(ADB,PS/2,X68K,PC98,M0110,HHKB,GH60)

Offline Soarer

  • * Moderator
  • Posts: 2672
  • Location: UK
ADB to USB keyboard converter for Teensy
« Reply #9 on: Sun, 16 January 2011, 07:38:31 »
That's a messy problem :(

I suppose the only solution is to send two packets for each change in state of the caps lock - one with the key down, and then one with the key up a millisecond or so later.

That appears to be how the Belkin adapter handles it.

What a hassle though!

Offline hasu

  • Thread Starter
  • Posts: 947
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #10 on: Sun, 16 January 2011, 08:17:58 »
Quote from: hasu;279286
No, I don't mind at all.

kps,
I misinterpreted your first sentence :)
Sorry. I attached my hex.



Quote from: Soarer;279367
That's a messy problem :(

I suppose the only solution is to send two packets for each change in state of the caps lock - one with the key down, and then one with the key up a millisecond or so later.

That appears to be how the Belkin adapter handles it.

What a hassle though!


Belkin solution will be a problem for someone who want to remap the key.
But it may be good for most people.

I prefer switch conversion solution though it is not for everyone.
⚠ My Engrish is broken.    tmk keyboard firmware project(ADB,PS/2,X68K,PC98,M0110,HHKB,GH60)

Offline Soarer

  • * Moderator
  • Posts: 2672
  • Location: UK
ADB to USB keyboard converter for Teensy
« Reply #11 on: Sun, 16 January 2011, 08:32:14 »
Quote from: hasu;279388
Belkin solution will be a problem for someone who want to remap the key.
But it may be good for most people.

I prefer switch conversion solution though it is not for everyone.


Yes, that would be better.

But... I have a Macally MK96, and it already has a non-locking switch for caps lock! Its keyboard firmware must simulate the locking action, I guess. I don't know how many other ADB keyboards do a similar thing, but the only way to get a normal key action with them would be to replace the keyboard controller (which is what I will do :) ).

Offline kps

  • Posts: 607
ADB to USB keyboard converter for Teensy
« Reply #12 on: Sun, 16 January 2011, 12:33:21 »
Quote from: hasu;279388
kps, I misinterpreted your first sentence :)


Sorry, my fault for using idiomatic English.

Thank you; that has helped me narrow down my problem somewhat.

I hacked in some ugly code to send a Shift directly when the power key is pressed, and that worked. With a logic probe, I see "chatter" on the ADB line on the other port of the keyboard, so I think my connections are sound. I suspect my problem may be either noise or timing related, since (by using the LED on the Teensy) it seems I never get past the if (read_bit()) // Stopbit(0) in adb_host_kbd_recv().

Offline hasu

  • Thread Starter
  • Posts: 947
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #13 on: Mon, 17 January 2011, 09:38:47 »
Quote from: kps;279449

I hacked in some ugly code to send a Shift directly when the power key is pressed, and that worked. With a logic probe, I see "chatter" on the ADB line on the other port of the keyboard, so I think my connections are sound. I suspect my problem may be either noise or timing related, since (by using the LED on the Teensy) it seems I never get past the if (read_bit()) // Stopbit(0) in adb_host_kbd_recv().



My code is very primitive and have not been checked its signal conformity
by logic analyzer nor oscilloscope, thus it could lose the sync under some circumstances.

Another possibility I come up with is  port damage of your Teensy.
My Teensy actually lost one port by latch-up while prototyping/debug :)
If your DATA line port is suspicious, you should try another port.
⚠ My Engrish is broken.    tmk keyboard firmware project(ADB,PS/2,X68K,PC98,M0110,HHKB,GH60)

Offline kps

  • Posts: 607
ADB to USB keyboard converter for Teensy
« Reply #14 on: Mon, 17 January 2011, 10:52:09 »
Quote from: hasu;279835
My code is very primitive and have not been checked its signal conformity by logic analyzer nor oscilloscope, thus it could lose the sync under some circumstances.


I may get out those tools next weekend. My logic analyzer is ancient (HP1615A, 1979) but should be adequate for ADB.

Quote
Another possibility I come up with is  port damage of your Teensy.
My Teensy actually lost one port by latch-up while prototyping/debug :)
If your DATA line port is suspicious, you should try another port.


I don't think that is the case, but I will check.

I do own an iMate, so this is not vital to me, but it might help other people later.

Later, I want to try using one of the cheap PS3 dongles with an AT90USB162. That, plus half an s-video cable, would make an ADB converter for under $10.

Offline jeffreysung

  • Posts: 11
ADB to USB keyboard converter for Teensy
« Reply #15 on: Fri, 11 February 2011, 02:30:41 »

This week I got Teensy++ 2.0 and tried. As a result I found problems.

1. If ADB cabe is long, OSX couldn't detect keyboard. (In fact, it does but couldn't detect any key strokes) I have short cable which is included in Apple adjustable keyboard and three other normal cables. Short cable works good but others not.
2. key stroke is recognized multiple time.

These problems (I Think) caused by my environment...

I changed adb/config.h (I moved port and bit)

#define ADB_PORT        PORTB
#define ADB_PIN         PINB
#define ADB_DDR         DDRB
#define ADB_DATA_BIT    6
#define ADB_PSW_BIT     5       // optional

I should change ADB_DDR/ADB_PIN as above right ?
« Last Edit: Fri, 11 February 2011, 02:42:54 by jeffreysung »

Offline hasu

  • Thread Starter
  • Posts: 947
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #16 on: Sat, 12 February 2011, 03:03:55 »
jeffreysung,
Thank you for your reporting!
I don't use this converter daily, so it is still very immature.
But I am happy to improve this project, any feedback is welcome.

Quote from: jeffreysung;293270

This week I got Teensy++ 2.0 and tried. As a result I found problems.

1. If ADB cabe is long, OSX couldn't detect keyboard. (In fact, it does but couldn't detect any key strokes) I have short cable which is included in Apple adjustable keyboard and three other normal cables. Short cable works good but others not.
2. key stroke is recognized multiple time.

These problems (I Think) caused by my environment...

My code(adb.c) which process ADB signal is very primitive. It use polling & delay method to check the DATA line activity, while it should use interrupt routine.  I think it is prone to lose sync with keyboard under some condition(signal with loose edge or interference by other interrupt code). This perhaps causes your problems.

I will rewrite adb.c using pin chajnge interrupt at an early day.


Quote

I changed adb/config.h (I moved port and bit)

#define ADB_PORT        PORTB
#define ADB_PIN         PINB
#define ADB_DDR         DDRB
#define ADB_DATA_BIT    6
#define ADB_PSW_BIT     5       // optional

I should change ADB_DDR/ADB_PIN as above right ?

You are right. It is OK to use PB6.
⚠ My Engrish is broken.    tmk keyboard firmware project(ADB,PS/2,X68K,PC98,M0110,HHKB,GH60)

Offline kps

  • Posts: 607
ADB to USB keyboard converter for Teensy
« Reply #17 on: Sat, 12 February 2011, 12:56:17 »
Quote from: jeffreysung;293270
1. If ADB cabe is long, OSX couldn't detect keyboard.


Ah. I just tried this. (Last time I used a normal coiled ADB cable.)  I made a 3-inch cable from an S-video cable. With this, a IIgs keyboard worked (except for the duplicates) and an Extended partly worked.

Then I did a little searching and found a PIC application note on ADB. Based on that, I added a pullup to the ADB data line. With the pullup, both keyboards work, even with a long cable.

Offline jeffreysung

  • Posts: 11
ADB to USB keyboard converter for Teensy
« Reply #18 on: Sat, 12 February 2011, 19:50:21 »
Quote from: kps;294046
Ah. I just tried this. (Last time I used a normal coiled ADB cable.)  I made a 3-inch cable from an S-video cable. With this, a IIgs keyboard worked (except for the duplicates) and an Extended partly worked.

Then I did a little searching and found a PIC application note on ADB. Based on that, I added a pullup to the ADB data line. With the pullup, both keyboards work, even with a long cable.


ATMEL supports (maybe) both pull up/down. This could be very useful in weak signal but pull up/down is a little bit slow compared to normal signaling. Thanks for useful info.

Offline hasu

  • Thread Starter
  • Posts: 947
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #19 on: Sun, 13 February 2011, 03:02:09 »
kps,

I confirmed the problem on coiled cable, and pull-up resistor solved it.  With coiled cable the converter couldn't get signal from keyboard and  didn't recognize any key stroke. I couldn't see the key duplicate  problem.

AVR's internal pull-up seems to be too weak to communicate with ADB  keyboards, we may need external pull-up resistor instead in many case  though some cable including my  straight cable doesn't need it.

Thanks.

My pull-up resistor:
« Last Edit: Sun, 13 February 2011, 04:12:27 by hasu »
⚠ My Engrish is broken.    tmk keyboard firmware project(ADB,PS/2,X68K,PC98,M0110,HHKB,GH60)

Offline jeffreysung

  • Posts: 11
ADB to USB keyboard converter for Teensy
« Reply #20 on: Mon, 14 February 2011, 01:36:39 »
Quote from: hasu;294316
kps,


AVR's internal pull-up seems to be too weak to communicate with ADB  keyboards, we may need external pull-up resistor instead in many case  though some cable including my  straight cable doesn't need it.


It is the other around.

There is internal pull-up resistor inside and if you add resistor outside pin, total resistance goes down(1/Rt = 1/Rin + 1/Rout) and this could explain why coiled or long cable couldn't work with.
The ADB cable is like a attenuator and because of internal (big) pull up resistor, signal couldn't swing between +5V and 0V properly (means couldn't go down to ground level).

By adding a external resistor, total pull up resistance goes down, and signal could swing more properly.

Offline Soarer

  • * Moderator
  • Posts: 2672
  • Location: UK
ADB to USB keyboard converter for Teensy
« Reply #21 on: Mon, 14 February 2011, 08:24:19 »
Partly, you are saying the same thing: a big resistor = a weak pull-up.

But the voltage range of the signal is not affected. The capacitance of the cable makes the signal slow to rise from 0V to +V, since that capacitance has to charge up via the pull-up resistor.

Offline jeffreysung

  • Posts: 11
ADB to USB keyboard converter for Teensy
« Reply #22 on: Tue, 15 February 2011, 22:24:30 »
I fount a strange thing.

Up to now, I tested this board with my macbookpro/iMac. It always shows multiple stroke from time to time. When I connected to Windows PC, there is no multiple strokes.

Is there anybody who tried with Mac ?

Offline Soarer

  • * Moderator
  • Posts: 2672
  • Location: UK
ADB to USB keyboard converter for Teensy
« Reply #23 on: Wed, 16 February 2011, 09:07:40 »
I played around with external pull-up resistors on my PS2 adapter (the principle is the same).

This is with about 5m of cable in all, so pretty much a worst case scenario.

Internal pull-up:

6042-0

External 2k2 pull-up - better, but still a bit slow to rise and a fair bit of crosstalk between the clock and data signals:

6044-1

External 1k pull-up - seems to be the sweet spot, much cleaner signals in every respect:

6046-2

I think it affects the ADB adapter more because the timing is critical - if you have a slow rise on the signal it moves the switching point in time.
« Last Edit: Sun, 14 October 2012, 10:49:01 by Soarer »

Offline hasu

  • Thread Starter
  • Posts: 947
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #24 on: Wed, 16 February 2011, 22:55:18 »
jeffreysung,
hmm, it is a weird behaviour.  I can't come up with the cause now and I have no healthy mac to try:(

Soarer,
It is nice to see real wave forms!
I have ordered my first oscilloscope a few days ago. I can hardly wait for my Rigol :)
⚠ My Engrish is broken.    tmk keyboard firmware project(ADB,PS/2,X68K,PC98,M0110,HHKB,GH60)

Offline Soarer

  • * Moderator
  • Posts: 2672
  • Location: UK
ADB to USB keyboard converter for Teensy
« Reply #25 on: Thu, 17 February 2011, 06:40:34 »
Excellent! I don't know how I managed before I got my 'scope :-)

I have just ordered my first logic analyzer (although a cheap one!), an Open Workbench Logic Sniffer. So, similar suspense here :-)

Offline jeffreysung

  • Posts: 11
ADB to USB keyboard converter for Teensy
« Reply #26 on: Wed, 23 February 2011, 00:49:40 »
Multiple key stroke problem solved.

in adb/matrix.c

just add delay statement before if (codes == 0 ) {

like this.

    key0 = codes>>8;
    key1 = codes&0xFF;

   _delay_us(300);
    if (codes == 0) {                           // no keys
        return 0;
    } else if (key0 == 0xFF && key1 != 0xFF) {  // error
 


I found this problem in making USB controller for Apple Desktop Bus Keyboard(a.k.a. IIgs keyboard).

Why 300us ? This is experimental value. I tried from 250us to 500us and 300us is the most stable value.
(With 300us, there is little multiple stroke issue.)

and I don't know why this problem is occurred only with OSX. : (

Offline jeffreysung

  • Posts: 11
ADB to USB keyboard converter for Teensy
« Reply #27 on: Thu, 03 March 2011, 21:06:40 »
The upper solution could solve almost multiple key stroke problem but not all.

By setting USB device as low speed device, this problem could be solved.(I didn't tried)

I made USB controller for Apple desktop bus keyboard(IIgs) with Teensy++2.0 and this multiple key stroke problem exists also. I tried in many ways but I couldn't find what is wrong but I could expect there is some timing issue between USB host and firmware. As a work around, by setting this controller as low speed device this problem has gone.

Here is my work picture

Offline hasu

  • Thread Starter
  • Posts: 947
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #28 on: Fri, 04 March 2011, 09:01:14 »
jeffreysung,
Nice USB receptacle setting!


Your problem is not bouncing. right?
I have not experienced the multiple key stroke problem. I tried AEKII, m0116 and some Macways, but it was not a long time use.
Unfortunately I have no IIgs to try. (It is one of keyboards I want to own! :)

Do you have other ADB keyboard than IIgs?
I want to know whether the problem is specific to IIgs or not.

Do you have iMate or other converter?
I want to know whether the problem is caused by my converter or not.
⚠ My Engrish is broken.    tmk keyboard firmware project(ADB,PS/2,X68K,PC98,M0110,HHKB,GH60)

Offline kps

  • Posts: 607
ADB to USB keyboard converter for Teensy
« Reply #29 on: Fri, 04 March 2011, 09:17:19 »
Quote from: hasu;304861
Do you have other ADB keyboard than IIgs?
I want to know whether the problem is specific to IIgs or not.


I do have most models of Apple ADB keyboard, including the IIgs. I will test several this weekend.

Quote
Do you have iMate or other converter?
I want to know whether the problem is caused by my converter or not.


I have an iMate, and have never seen duplicate keys with it.