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

0 Members and 1 Guest are viewing this topic.

Offline hasu

  • Thread Starter
  • Posts: 3490
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
TMK ADB to USB keyboard converter
« on: Wed, 12 January 2011, 11:53:40 »


You can buy preassembled TMK ADB-USB converter here




RESOURCES
ADB protocol:
https://github.com/tmk/tmk_keyboard/wiki/Apple-Desktop-Bus

Source code:
https://github.com/tmk/tmk_keyboard/tree/master/converter/adb_usb




KEYMAP EDITOR
You can edit keymap and download firmware with web browser.

TMK ADB-USB converter rev2(ATMega32u2)
TMK ADB-USB converter rev1 and converter with Teensy2.0 or ProMicro(ATMega32u4)

In the Keymap Editor physical key layout is based on Apple Extended Keyboard(AEK) but you can edit keymap of any other ADB keyboards.




BUILD FIRMWARE
https://github.com/tmk/tmk_keyboard/wiki#build-firmware

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


Use 'make' to build firmware for TMK ADB-USB converter rev.2.
Use 'make -f Makefile.rev1' to build firmware for TMK ADB-USB converter rev.1, Teensy2.0 or Pro Micro.

You are recommended to use Keymap Editor above, instead build yourself.





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

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

Connect keyboard DATA line to PD0(Pin3 on Pro Micro),  VCC to 5V(VCC on Pro Micro, not RAW) and GND as well.

PSW(Power SW) is not used in current firmware. You don't have to connect it. See this.


pull-up resistor
You must have an external pull-up resistor(1K Ohm) between VCC(5V) and DATA(PD0) line.

Check this wiki page: https://github.com/tmk/tmk_keyboard/wiki/FAQ#pull-up-resistor

1k ohm is strongly recommended.
You can use 1/10W or higher power rating of resistor. See this for resistor power rating.

Code: [Select]
Keyboard       Conveter
               ,------.
VCC---------+--|VCC   |
            |  |      |
            R  |      |
            |  |      |
DATA--------+--|PD0   |
               |      |
GND------------|GND   |
               `------'

R: 1K Ohm resistor

- You may use 4.7k or less resister if you don't have 1k in hand. But it may not work with longer curled cable or daisy-chained multiple devices. 
- See this issue. 4.7k doesn't work in some cases like lenghty cable, curled cable or multipule devices. They required 1k. https://github.com/tmk/tmk_keyboard/issues/683
- 10k ohm didn't work for Turbo Mouse 5 with vintage coiled ADB cable, which is expected to have quite a bit capacitance. 4.7k did work. 2022-03-23
- Kensington Thinking mouse and NeXT mouser didn't work with 10k while they works with 4.7k. https://geekhack.org/index.php?topic=14290.msg3117619#msg3117619
- Griffin iMate and Apple IIGS has even 470 ohm pull-up resistor. https://geekhack.org/index.php?topic=14290.msg3117720#msg3117720




DEBUG
You can use hid_listen to see debug prints.

Debut Outputs
RRR
The "R" on debug output means that the conveter didn't get any respnse to a command within specific term(500ms).
https://github.com/tmk/tmk_keyboard/blob/66e97371bacb1ad55096f0407b47f0a73fadc6c7/tmk_core/protocol/adb.c#L160-L164

This happens when connecting keyboard with converter already powered(hotplug) because keyobard can't response to the converter during its startup. It is not error in this case. But in many other cases the "R" means that there is something wrong on your converter, keyboard or wiring setup.


Sigrok ADB decoder
Cheap $10-20 Logic Analyzer that is compatible with sigrok works well for this job.

Get one and try. 1MHz sample rate is actually good enough for ADB protocol.
You can use this deocoder with sigrok PulseView to see ADB commands and data..

https://github.com/tmk/tmk_keyboard/wiki/Signal-Capture-for-debug#sigrok



TROUBLE WITH YOUR DIY CONVERTER?
You are using Pro Micro?
Confirm it is a 5V version with 16MHz crystal. Make solder bridge on J1 and use VCC for 5V power, not RAW. MUST check this page also.

I'm never willing to recommend you to use Pro Micro unless you are already familiar to it and have skill to solve problems yourself. Pro Micro is bad choice especially if this is your first time project. Alternatives are PJRC Teensy2.0 and TMK converter, of course!

Please don't ask help for problem specific to Pro Micro here.

Don't ask help before placing pull-up resistor and trying solder on J1!

More
1K Ohm pull-up resistor You must have an external pull-up resistor(1K Ohm) between VCC(5V) and DATA(PD0) line. 1K ohm is strongly recommended. You may use 4.7K, 10K or any of 1K-10K Ohm resister, if you don't have 1K in hand.

In most cases your wiring is wrong Check your wiring yourself again and again, one hundred times! And try swapping wires at your own risk, note that your wiring are already wrong at this point probably :D It won't break your keyboard unless you are extremely unlucky.

Now you need help? Don't ask help before trying things above!
OK. If you are not sure about your wiring, post pics of your hardware and wriring this really save our time. Don't save your time by omitting this. Shots of controller side and ADB connector/keyboard side would be helpful. Use TMK default prebuilt firmware to test. Also debug outputs from hid_listen command would be useful.

[rant]
Many people said they are sure their wiring was fine first but it became clear they failed to do that in the end many times in this thread :( Don't omit pull-up resistor with your laziness, you better have it absolutely.
Don't take advantage of generosity of kind and helpful members(yes, including me, of course  ;))! Their time is more precious than your time, use your time to learn and look into your problem first.
[/rant]





UPDATE
2012/12/12 Extended keyboard support by blargg
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!
2015/04/28 Mouse is supported by mek-apelsin
2015/12/18 Request for help: extended mouse support
2016/06/04 Added LIMITATION section
2016/06/04 Apple Adjustable Keyboard media keys are supported now
2016/09/03 Keymap editor(unimap) is available now
2018/01/19 Fix lag problem on ADB-USB converter
2019/06/23 Added Apple Extended Mouse Protocol and Kensignton Turbo Mouse support
2020-02-13 Updated firmware to fix AEK support
2021-01-24 Change default mapping for 'keypad ='
2021-11-17 Hotplug of keyboard support
2022-04-12 Various pointing devices support
2022-05-05 JIS keyboard support
2022-05-21  sigrok ADB decoder
2022-05-30 Implement 'Address Resolution '
2023-03-12 Fix Remote Wakeup
2023-10-26 Add support for Microspeed MacTrac 2.0(old firmware)




TESTED DEVICES

Check also: https://github.com/tmk/tmk_keyboard/issues/736

Keyboard:
- Apple Extended Keyboard M0115
- Apple Extended Keyboard II M3501
- Apple Standard Keyboard M0116, M0118
- Apple Adjustable Keyboard M1242
- Apple Desktop Bus Keyboard(Apple II GS Keyboard) 658-4081
- NeXT ADB Keyboard ANSI
- NeXT ADB Keyboard German


Mouse:
- Apple Desktop Bus Mouse G5431 Taiwan(1)
- Apple Desktop Bus Mouse II M2706 Taiwan(1)
- Apple Desktop Bus Mouse II M2706 Ireland(?) - See ISSUE below.
- Apple Desktop Bus Mouse II M2706 Malaysia
- Apple Classic Mouse protocol (1, 2)                                                                                 
- Apple Extended Mouse protocol (4)
- Kensington Turbo Mouse 5 #64210 and Thinking Mouse (0x32)                                                           
- Macally 2-button Mouse (0x42)
- Logitech MouseMan/TrackMan Proprietary protocol (0x4C*)                                                             
- Logitech MouseMan/TrackMan Extended protocol (0x4D*)
- NeXT ADB Mouse(1) - See ISSUE below.

TEST REQUIRED
- Micrspeed MacTrac (0x2F, 0x5F) - Not confirmed
- Contour Design Countour Mouse (0x66) - Not confirmed                                                                 
- Mouse Systems A3 Mouse/Trackball (0x03) - Not confirmed                                                             
- CH Products Tracball Pro/DT225 (0x42) - Not confirmed

https://github.com/tmk/tmk_keyboard/wiki/Apple-Desktop-Bus#mouse   




LIMITATION
- Hotplug of keyboard is not fully supported, you will have to connect keyboard with the converter first, then plug-in USB port.

Hotplug of keyboard and mouse is supported now. 2021-11-17




AEK left/right modifiers discrimination, ISO layout and Adjustable keyboard media keys
On ADB Standard keyboards(M0116) left and right corresponding modifiers are logically indentical and can not be discriminated one another by the converter. This is ADB keyboard limitation, not converter's.
Apple Extended keyboard(M0115/M3501) can discriminate them except for 'Command' key.



ISO keys
The ISO keys should be correctly supported.

iMate and uKey have problem on some keys reportedly.
https://geekhack.org/index.php?topic=4185.msg61270#msg61270
https://github.com/randyrants/sharpkeys/issues/225#issue-537872871


Power key
In default configuration Power key works as 'Magic command' trigger key when it is held.
https://github.com/tmk/tmk_keyboard#magic-commands

You can remap the Power key somehow but there is a limitation.
https://geekhack.org/index.php?topic=14290.msg3100399#msg3100399

You may need to change thse line in config.h if 'Magic command' feature interferes.
https://github.com/tmk/tmk_keyboard/blob/7b31d1b9d9f0d9c7d1e4b553e583381552cd8778/converter/adb_usb/config.h#L54-L57


Keypad =
The key is assigned to keycode 'EQL'(usual equal) on default keymap.

Note that keycode 'PEQL'(keypad equal) works on Linux and MacOS but doesn't on Windows. If you are on Windows you will need to use 'EQL'.



Locking CapsLock
Many of old ADB keyboards have mechanically locking switch for Caps lock and TMK supports them.
https://github.com/tmk/tmk_keyboard#mechanical-locking-support

Or you may want to remove locking pin from the push-lock switch to use it as a normal momentary switch.

Caps lock key of Apple adjustable keyboard is not mechanical locking switch but it emulates locking switch behaviour. You will have problem when using the key as control, for example.






Extended mouse protocol support
- Kensington Turbo Mouse 5(by hasu)

Do you have other multiple button mouse to support? Check this issue on github and ask help in this thread.
https://github.com/tmk/tmk_keyboard/issues/274




ISSUES

Check also: https://github.com/tmk/tmk_keyboard/issues?q=is%3Aissue+ADB

- Apple Desktop Bus Mouse II(M2706) assembled in Ireland doesn't work due to initialization failure. Meanwhile, M2706 assembled in Taiwan works.
https://geekhack.org/index.php?topic=14290.msg2873443#msg2873443
https://geekhack.org/index.php?topic=14290.msg2875515#msg2875515

Malaysia one also works. With 'Address Resolution' fix(2022-05) Ireland one work now perhaps.(Test needed)
https://github.com/tmk/tmk_keyboard/issues/748#issue-1396848868

- NeXT ADB Mouse has problem at startup.
https://geekhack.org/index.php?topic=14290.msg3119277#msg3119277
« Last Edit: Wed, 25 October 2023, 22:07:06 by hasu »

Offline lowpoly

  • Posts: 1749
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? :-)

Miniguru thread at GH // The Apple M0110 Today

Offline Soarer

  • * Moderator
  • Posts: 1918
  • 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: 5064
  • Location: Koriko
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?
🍉

Offline iMav

  • geekhack creator/founder
  • Location: Valley City, ND
  • "Τα εργαλεία σας είναι σημαντικά."
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: 3490
  • 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.

Offline kps

  • Posts: 410
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: 3490
  • 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.

Offline hasu

  • Thread Starter
  • Posts: 3490
  • 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

Offline Soarer

  • * Moderator
  • Posts: 1918
  • 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: 3490
  • 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.

Offline Soarer

  • * Moderator
  • Posts: 1918
  • 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: 410
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: 3490
  • 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.

Offline kps

  • Posts: 410
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: 13
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: 3490
  • 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.

Offline kps

  • Posts: 410
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: 13
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: 3490
  • 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 »

Offline jeffreysung

  • Posts: 13
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: 1918
  • 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: 13
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: 1918
  • 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: 3490
  • 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 :)

Offline Soarer

  • * Moderator
  • Posts: 1918
  • 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: 13
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: 13
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: 3490
  • 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.

Offline kps

  • Posts: 410
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.

Offline kps

  • Posts: 410
ADB to USB keyboard converter for Teensy
« Reply #30 on: Sun, 06 March 2011, 10:24:02 »
I have tried several different keyboards, and got duplicate key reports with all of them.

Quote
TThis is an  Apple  Desktop Bus keyboard, modell A9M0330.
This  is ann Apple Keyboardd,  model  M0116.
This is a squishy Apple Keyboard II,  mmodel  MM0487..
This  is  ann Apple   Extended  Keyboard,, model M0115.
TThis  is an  Appple Extennded Keyboard  II, mmodel M3501.
Thhis is an Applle Adjustable Keyboard, modell MM1242.

woody

  •  Guest
ADB to USB keyboard converter for Teensy
« Reply #31 on: Sun, 06 March 2011, 11:36:39 »
kps, do you happen to have extra A9M0330 for sale?

Offline jeffreysung

  • Posts: 13
ADB to USB keyboard converter for Teensy
« Reply #32 on: Sun, 06 March 2011, 20:12:10 »
Quote from: hasu;304861
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.

1.I have two "2 adb to usb" converters and there is no problems with these.
2.I have AEKII/IIgs/Design/Adjustable keyboards and all the same problem with converter.
3.I think there is a different in handling USB interrupt between OSX and Windows. Windows doesn't have problem.
4.If I set teensy( ++ 2.0) as low speed device, this multiple stroke problem is gone. There should be a timing mismatch between this firmware and OSX.
   a. As I wrote before, if I insert delay in keyscan function, the occurrence of this multiple stroke reduced.
   b. If I set as low speed device, USB interrupt/FIFO speed would go lower and this prevent our problem.
   c. With low speed device setting, USB debugging has some problem. It it too low to print debug information on screen.


To set Teensy++ 2.0 as low speed device, you should modify some functions and headers.

usb.c, usb_init
UDCON = 0; --> UDCON = 0x04;  (for Teensy++2.0, set LSM bit.)

usb_debyg.h
#define DEBUG_TX_SIZE 32 --> 8  (All endpoint size should be 8byte)

usb_keyboard.h
#define KDB2_SIZE 16 --> 8 (the same with above)

Offline hasu

  • Thread Starter
  • Posts: 3490
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #33 on: Sun, 06 March 2011, 20:36:22 »
Quote from: kps;306178
I have tried several different keyboards, and got duplicate key reports with all of them.

Thanks.
It is on OS X? You see this problem on WIndows or Linux too?

I have no problem during 20-30min my daily usage. Hmm...
Disclosure: I am slow typer 40-50wpm.

I tried this attached firmware on Teensy2.0 and latest firmware on Teensy2.0/Teensy++2.0. My pull-up resistor is 10K Ohm.

Cables: coiled cable and straight cable.
Keyboards: AEKII(M3501) and Apple keyboard(M0116)
OS: WIndows 7 64bit and Ubuntu 10.04


Quote
1.I have two "2 adb to usb" converters and there is no problems with these.
2.I have AEKII/IIgs/Design/Adjustable keyboards and all the same problem with converter.
3.I think there is a different in handling USB interrupt between OSX and Windows. Windows doesn't have problem.
Thanks, jeffreysung.
Finally, I understood that it is a problem specific to OS X.
Unfortunately I have no OSX to try :(

My USB stack(based on PJRC's code) seems to have some faults.
« Last Edit: Sun, 06 March 2011, 20:45:00 by hasu »

Offline kps

  • Posts: 410
ADB to USB keyboard converter for Teensy
« Reply #34 on: Mon, 07 March 2011, 09:12:12 »
Quote from: hasu;306544
It is on OS X? You see this problem on WIndows or Linux too?


It was OS X. I tried again on Windows (with an AEK) and saw two instances of doubled characters in a half page of text.

For my tests I was using yesterday's firmware (did a 'git pull' before testing), 10K pullups, and a 1m straight cable (half an S-video cable).

Quote
My USB stack(based on PJRC's code) seems to have some faults.


I am not sure that is so. I notice in my tests above that 12 of 14 doubles occur at the beginning or end of words. Obviously the software does not know about words, but my fingers do. I wonder whether the ADB keyboards might actually be sending a second event if the key is still down when it is polled again.

Offline jeffreysung

  • Posts: 13
ADB to USB keyboard converter for Teensy
« Reply #35 on: Mon, 07 March 2011, 11:26:04 »
Quote from: kps;306815

I am not sure that is so. I notice in my tests above that 12 of 14 doubles occur at the beginning or end of words. Obviously the software does not know about words, but my fingers do. I wonder whether the ADB keyboards might actually be sending a second event if the key is still down when it is polled again.


According to my test (USB controller directly attached to IIgs), this multiple stroke might be caused with interrupt handling. The source code looks good and USB interrupt handling functions are based on sample code of pjrc.com.

1. When I debugged adb adapter, key stroke scanning has no problem and USB report(data structure for sending keys to USB host) has no problem.
2. The problem is adb adapter has only one key data but it seems that USB host get the data multiple time.
3. This situation could be occurred if interrupt flag is handled wrong. Think about this. USB host requested to device for new data. Device had sent one data(right data). With some problem, device think there is more data in FIFO and send data at next request. (USB host search new data by polling devices)
4. I think that there could be a problems in key scan turn around time. The simplified actions of firmware is repeating this procedure "key scan - if there is modified key send it FIFO". In repeating this procedure, there should be some problems (especially interrupt handling).
5. In the case of USB controller(directly attached to IIgs), everything from keyscan to storing data to FIFO is perfect.

Offline hasu

  • Thread Starter
  • Posts: 3490
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #36 on: Tue, 08 March 2011, 00:20:18 »
After browse of some codes of Darwin, I found a clue of the problem.
OS X seems to request SET_IDLE(24ms) to keyboard, while Windows requests SET_IDLE(0ms). I think Linux also requests 0ms.

HID SET_IDLE request inhibits device to report keys during given interval if key state is not changed. 0ms means device must not report without change.
I assume this difference causes the problem appeared only in OS X.

Bug codes run only when idle interval runs out. On Windows and Linux the codes with bug never run, on the other hand the problem occasionally occurs on OS X.

This bug is very timing issue around interrupt handlers. I will fix this problem but it may take some time.



This is the codelet from Apple.
OS X requests 24ms on most keyboards, but 0ms on Apple's. why?
If I found right clue, changing VENDOR_ID to '0x05AC'(Apple) in config.h will disappear the problem.

http://www.opensource.apple.com/source/IOUSBFamily/IOUSBFamily-417.4.1/IOUSBHIDDriver/Classes/IOUSBHIDDriver.cpp

Code: [Select]
   // For Keyboards, set the idle millecs to 24 or to 0 if from Apple
    //
    if ( (_interface->GetInterfaceClass() == kUSBHIDClass) &&
         (_interface->GetInterfaceSubClass() == kUSBHIDBootInterfaceSubClass) &&
         (_interface->GetInterfaceProtocol() == kHIDKeyboardInterfaceProtocol) )
    {
        if (_device->GetVendorID() == kIOUSBVendorIDAppleComputer)
        {
            SetIdleMillisecs(0);
        }
        else
        {
            SetIdleMillisecs(24);
        }
    }
« Last Edit: Tue, 08 March 2011, 00:23:53 by hasu »

Offline jeffreysung

  • Posts: 13
ADB to USB keyboard converter for Teensy
« Reply #37 on: Tue, 08 March 2011, 00:39:15 »
Wow.. awesome!!! there is no multiple key stroke!!!!

Thanks. Hasu

I attach screenshot.


Offline hasu

  • Thread Starter
  • Posts: 3490
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #38 on: Wed, 09 March 2011, 20:27:21 »
Quote from: jeffreysung;307403
Wow.. awesome!!! there is no multiple key stroke!!!!


jeffreysung,
Thanks for your feedback.

I pushed fix for the problem to github now. I think this fix avoids multiple key stroke.

Offline b0bb3r5

  • Posts: 2
ADB to USB keyboard converter for Teensy
« Reply #39 on: Sat, 12 March 2011, 11:56:29 »
i have a old m0110a how would i go about getting this to work with a teensy? i have a teensy, the keyboard, electronics...etc and id really love to get some help converting it

Offline kps

  • Posts: 410
ADB to USB keyboard converter for Teensy
« Reply #40 on: Sat, 12 March 2011, 13:33:25 »
Quote from: b0bb3r5;310514
i have a old m0110a how would i go about getting this to work with a teensy? i have a teensy, the keyboard, electronics...etc and id really love to get some help converting it


The M0110A is not an ADB keyboard, so you should probably start a separate thread for that.

Offline hasu

  • Thread Starter
  • Posts: 3490
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #41 on: Thu, 01 September 2011, 00:06:35 »
I finally added locking capslock key support and this is default now.
Someone sent me a patch for this feature. Thank you!

I think most ADB keyboards has a locking capslock in either case whether physical push-lock or software locking.
So this feature is good for people using their keyboard as is, while some who replace caps with non-locking switch
for remapping will want to disable this feature with commenting out following macro in config.h.
Code: [Select]

/* Locking Caps Lock support */
#define MATRIX_HAS_LOCKING_CAPS

Offline nodnerb

  • Posts: 8
  • Location: Sydney
ADB to USB keyboard converter for Teensy
« Reply #42 on: Sat, 29 October 2011, 00:36:38 »
Hi Hasu,

Your firmware is some great work... works out of the box for my Apple-branded keyboards!

I had some problems with a Datadesk Smartboard, and it wasn't until I got it on an oscilloscope that I found out why...
For some reason, this keyboard sends a '1' as a stop bit (which is against the ADB standard... how did this keyboard ever work on a Mac?).
Also, the timing of the signals is a bit screwy,
__~~~ after 35 us for '1'
___~~ after 56 us for a '0' (This should be 65 us!) - this one is causing random bit-swapping with the default 55 us delay before reading the bit value.
So, for this board, I've changed the read delay to 45 us and disregard the value of the stop bit.

Just got things working a bit on Friday, we'll see how the board works next week.

My keyboard has a sticky "B" key - don't know what I can do about this just yet...

Thank you for providing the firmware for us!

I'm going to look into your source code to see how the layer switching works.

Cheers,
nodnerb
Ergodox, Kinesis contoured (ps/2), Sanwa vertical mouse

Offline hasu

  • Thread Starter
  • Posts: 3490
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #43 on: Sat, 29 October 2011, 07:59:33 »
nodnerb,
Thank you for your feedback! This is good to know.


Quote from: nodnerb;441636

I had some problems with a Datadesk Smartboard, and it wasn't until I got it on an oscilloscope that I found out why...
For some reason, this keyboard sends a '1' as a stop bit (which is against the ADB standard... how did this keyboard ever work on a Mac?).
Also, the timing of the signals is a bit screwy,
__~~~ after 35 us for '1'
___~~ after 56 us for a '0' (This should be 65 us!) - this one is causing random bit-swapping with the default 55 us delay before reading the bit value.
So, for this board, I've changed the read delay to 45 us and disregard the value of the stop bit.

Your keyboard might be compliant for ADB spec though it is not good implementation without enough margin.
If my code could read a bit exactly after 55us it seems to has no problem.  My code intends to read a bit after 55us delay,
but delay time is not so accurate because the code is written in C as you know. It may be imposed a few excess micro seconds in addition to the 55us.


Quote from: nodnerb;441636

I'm going to look into your source code to see how the layer switching works.

Ah, it is a very confusing and chaotic part of my firmware :(
and its broken English comment doesn't work even for me :)

Offline CyberDave

  • Posts: 33
ADB to USB keyboard converter for Teensy
« Reply #44 on: Wed, 21 December 2011, 03:41:54 »
Thanks so much for this! I'm modding an Apple Extended Keyboard II with an internal Teensy adapter as a Christmas gift for my brother.


It's working great, except for one thing...the power button isn't doing anything on my Mac. Yes, it is wired up correctly...the Teensy is seeing the key up/down, but seemingly not doing anything. Here's what hid_listen.mac shows for debugging output:


Code: [Select]
r/c 01234567
00: 00000000
01: 00000000
02: 00000000
03: 00000000
04: 00000000
05: 00000000
06: 00000000
07: 00000000
08: 00000000
09: 00000000
0A: 00000000
0B: 00000000
0C: 00000000
0D: 00000000
0E: 00000000
0F: 00000001
ignore keycode: B0
keys: 00 00 00 00 00 00  mods: 00
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
ignore keycode: B0
adb_host_kbd_recv: FFFF


I'm not sure what the "ignore keycode" is all about. Will take another look tomorrow when I'm not half asleep. Until then, any ideas?

Offline hasu

  • Thread Starter
  • Posts: 3490
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #45 on: Wed, 21 December 2011, 11:25:05 »
Hi, CyberDave. Thanks for your feedback.

Quote from: CyberDave;475045
Thanks so much for this! I'm modding an Apple Extended Keyboard II with an internal Teensy adapter as a Christmas gift for my brother.

Wow, AEKII for gift?! Your brother is so lucky :)


Quote
It's working great, except for one thing...the power button isn't doing anything on my Mac. Yes, it is wired up correctly...the Teensy is seeing the key up/down, but seemingly not doing anything. Here's what hid_listen.mac shows for debugging output:


Oops, this is a bug. I fixed this bug and pushed the commit to github, try latest sources.

With default keymap you can send a power down event to OS with the power button, Mac's reaction to the event depends on your OS setting. I think Mac will display a sleep/shutdown dialog or sleep immediately without warn.
By pushing power button you will be able to wake your sleeping Mac as well.

Offline CyberDave

  • Posts: 33
ADB to USB keyboard converter for Teensy
« Reply #46 on: Wed, 21 December 2011, 18:42:47 »
Quote from: hasu;475239
Hi, CyberDave. Thanks for your feedback.

Wow, AEKII for gift?! Your brother is so lucky :)

Oops, this is a bug. I fixed this bug and pushed the commit to github, try latest sources.

With default keymap you can send a power down event to OS with the power button, Mac's reaction to the event depends on your OS setting. I think Mac will display a sleep/shutdown dialog or sleep immediately without warn.
By pushing power button you will be able to wake your sleeping Mac as well.

He's an editor at a small book publisher, and an aspiring writer. We've been Mac users since we were kids...I've Retr0brighted the keyboard and cleaned it up. I hope he gets a kick out of seeing it restored to its original platinum glory (and that I can get the thing finished before I head home for the holidays (currently I need to figure out how I'm going to install the USB port in this thing so it can look as much like a stock keyboard as possible).

Thanks for the quick fix...I will test it out tonight. Ideally, I'd like it to just pop up the Mac's regular shutdown dialog box. Wake from sleep would be a nice side benefit as well.

Offline CyberDave

  • Posts: 33
ADB to USB keyboard converter for Teensy
« Reply #47 on: Thu, 22 December 2011, 02:44:17 »
Update: the power button now brings up the Mac's standard sleep/restart/shutdown dialog, and also wakes my MacBook Pro from sleep. The keyboard is reassembled and looks great. Now I just have to get to work on another one or two of these for myself...

hasu, this is awesome. If you're ever in Seattle, the first beer or two is on me.

Offline hasu

  • Thread Starter
  • Posts: 3490
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter for Teensy
« Reply #48 on: Thu, 22 December 2011, 21:02:39 »
Cool. Happy Christmas!

Offline blargg

  • Posts: 45
    • My github
ADB to USB keyboard converter for Teensy
« Reply #49 on: Tue, 10 January 2012, 14:58:24 »
HUGE thanks for this! I've been using an old PowerMac with ADB Extended Keyboard for the last 15 years for coding, and hate all the cheap keyboards for my newer USB-based machines. I just got a Teensy++ 2.0 and figured I'd try this as the first project. Code built without a hitch, wiring just as shown on the page, and it works perfectly. I've saved up several Apple Extended Keyboard IIs and this will make my slow transition to newer machines so much more pleasant. Thank you!