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

0 Members and 1 Guest are viewing this topic.

Offline arfink

  • Posts: 66
Re: ADB to USB keyboard converter
« Reply #150 on: Thu, 03 October 2013, 18:20:31 »
Quick question: the first page of the discussion here mentioned Data should be connected to PF0, while the Github indicates the default is D0. Which is it?

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #151 on: Thu, 03 October 2013, 18:29:08 »
Use PD0 now.
Pin usage changed recently.
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline arfink

  • Posts: 66
Re: ADB to USB keyboard converter
« Reply #152 on: Thu, 03 October 2013, 20:54:40 »
Well, I got everything compiled from the newest version on Git, and it seems that the IIgs keyboard is routinely dropping key strokes, especially double letters and sometimes even spaces.

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #153 on: Fri, 04 October 2013, 00:22:45 »
hmm, I also could confrm IIgs(Alps made in Japan) and M0116  drop key stroke sometime.

I found problems:
1) These keyboards are worse than normal 2KRO keyboard. They are *always* blocked when you press just two keys. This may be annoying if you are fast typer.

2) Pressing  same key sucessively tens to lose the stroke. Like I type 'jjj' and see 'jj' sometime.
But I'm not sure what causes this, switch defect, keyboard controller or my converter? Though I think my converter likely sitll has some bugs. I'll need to look into with scope or logic analyzer and reading the spec fully again. 

I'd like to see how iMate works with these boards, but I have none of that unfortunately. And I don't think I have time to debug this problem further soon.

Looks like these problems doesn't occur or seldom on Apple Extended keyboards M0115 and M3501.
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline tufty

  • Posts: 347
  • Location: French Alps
Re: ADB to USB keyboard converter
« Reply #154 on: Fri, 04 October 2013, 01:00:11 »
I'll be blunt - The key dropping is down to your converter, hasu. Connected through an iMate, my M0116 doesn't drop keys at all, and I'm a fast typist.

I believe the keyboards themselves only give 2 key rollover for normal alphabetic keys, and give full rollover for the control keys.  At least, that's what I'm seeing through the iMate, although it may well be that the iMate doesn't handle anything more than 2kro as well.  Not a problem for me, but I don't game...


Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #155 on: Fri, 04 October 2013, 01:19:58 »
tufty, interesting. Then my code sucks somewhere and misses signal.
I'll try to rewrite code from scratch when I got time for the curiousity.
Thanks.

This will not be fixed anytime soon,
For now I recommend iMate for daily usage.
« Last Edit: Fri, 04 October 2013, 01:28:25 by hasu »
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline arfink

  • Posts: 66
Re: ADB to USB keyboard converter
« Reply #156 on: Fri, 04 October 2013, 11:30:43 »
That's fine, for daily usage I'm just using an old IBM XT keyboard. So the IIgs is just a curiosity. I do know that when I used the IIgs keyboard with the IIgs I can hammer away on it as fast as I want, and it never misses a beat. So yeah, I think it's the converter.

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #157 on: Sun, 06 October 2013, 21:16:18 »
Hey, I've found myself doing more AVR programming lately and I realized that I now have the skills to write this ADB converter using interrupt-driven code that uses a hardware timer to measure durations. I've been using simavr which would be perfect for simulating ADB and testing the code in a sandbox. I now have an oscilloscope as well (with digital capture) so can debug any hardware issues. Just want to be sure that nobody else is working on this rewrite already.

I've since found a Belkin USB-ADB converter ($5 at a thrift shop) and it indeed works better than ours here, so it'd be nice to improve the one here. The Belkin one is apparently made by PI Engineering, the same people who make a Y-mouse PS2-USB keyboard/mouse adapter with similar esc-<letter> control codes.

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #158 on: Sun, 06 October 2013, 22:13:15 »
Great. I didn't start to fix yet.
If the community can get access to your reliable converter it is awsome.
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #159 on: Mon, 07 October 2013, 00:12:54 »
I'm studying the current code and it seems that the USB driver is bit-banged, and can't handle having interrupts disabled for more than a very short time (for obvious reasons). It's also almost a certainty that it keeps interrupts disabled when it's running. I assumed that this was meant to run on AVR devices with USB hardware, but maybe this project (tmk_keyboard) is meant to run on almost any AVR device.

Given the constraints of v-usb, it might still be possible to use some hardware assist for ADB. I'm thinking input capture to time ADB input, and perhaps PWM in a one-shot mode to time output bits. This is assuming that USB doesn't tie things up for more than 100us or so. I know almost nothing about USB so this may be overly optimistic. I know the ADB protocol has timeout handling, but if we timed out reading from the keyboard, it probably won't send the key press events again.

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #160 on: Mon, 07 October 2013, 02:36:09 »
Some projects like keyboard controllers(under keyboards/*) and PS/2, Sun, X68 converters can run with V-USB, I think. But I don't test/use them with V-USB recently and not so eager to support it anymore.

I'm not sure but my codes use only a few interrupts of hadware USB engine(LUFA) and timer(timer.h). I think USB interrupts are not so critical so that USB tasks are almost processed with hardware and timer is also trivial. You will be able to use your interrupts and place critical sections mostly freely with LUFA stack.

It is great if the converter works with V-USB, so that we can make it from cheap MCU.
This is very challenging technically, but it may be possible as you described. IIRC V-USB blocks around 50us, not long as 100us.

But I think Atmega32u4(LUFA) is the first target to tackle.
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #161 on: Mon, 07 October 2013, 12:01:37 »
I was thinking that for V-USB, the ADB polling could be done between USB events. But you say it's not currently used so I won't waste time on that.

I looked at the timing on a scope and first off, USB transmission lasts 3us normally, sometimes as much as 15us. So it doesn't seem like it would mess up the current timing.

I'm getting bogged down trying to understand the source. On the older version I have, hid_listen never hears the device (even though keyboard works), and I'm not getting uart debug output working either.

I tried the current code but make fails:

$ cd converter/adb_usb
$ make

-------- begin --------
avr-gcc (GCC) 4.5.3
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


mkdir -p obj_adb_usb/protocol/pjrc
Compiling C: ../../protocol/pjrc/usb.c
avr-gcc -c -mmcu=at90usb1286       -gdwarf-2 -DF_CPU=16000000UL -DBOOTLOADER_SIZE=4096 -DPROTOCOL_PJRC -DNO_PRINT -DNO_DEBUG -DVERSION=unknown -Os -funsigned-char -funsigned-bitfields -ffunction-sections -fno-inline-small-functions -fpack-struct -fshort-enums -fno-strict-aliasing -Wall -Wstrict-prototypes -Wa,-adhlns=obj_adb_usb/protocol/pjrc/usb.lst -I. -I../.. -I../../protocol/pjrc -I../../protocol -I../../common -std=gnu99 -include config.h -MMD -MP -MF .dep/obj_adb_usb_protocol_pjrc_usb.o.d  ../../protocol/pjrc/usb.c -o obj_adb_usb/protocol/pjrc/usb.o
../../protocol/pjrc/usb.c: In function ‘__vector_11’:
../../protocol/pjrc/usb.c:892:15: error: ‘keyboard_report’ undeclared (first use in this function)
../../protocol/pjrc/usb.c:892:15: note: each undeclared identifier is reported only once for each function it appears in
make: *** [obj_adb_usb/protocol/pjrc/usb.o] Error 1

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #162 on: Mon, 07 October 2013, 19:26:30 »
It seems I broke something on PJRC stack with recent changes. Use Makefile.lufa to build.
I'd like to make LUFA default stack from now. But PJRC will be also supported for a while, I'll fix this problem later.

Build with LUFA and hit Power + h you'll get help on hid_listen.
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #163 on: Mon, 07 October 2013, 20:06:29 »
I split the core adb.c file out and made a new converter using the PJRC keyboard sources from the Teensy website. I'd like to reproduce any problems with this much simpler backend to figure whether the problem is the adb driver or all the translation going on. I noticed that neither PJRC sources on the site are as full-featured as the PJRC ones you use.

Using your full LUFA build, is there any reliable way to reproduce the problems?

EDIT: went through all my keyboards and all 5 extended ones are M3501. I have three smaller ones: two M0116 and an M0487 (IIgs keyboard). The M0487 shows issues like you mentioned with pressing the same key over and over quickly. It shows up my bare-bones tester that just runs the adb.c code and outputs over the hardware UART (so no interrupts to interfere). It doesn't occur when I poll the keyboard every 16 msec, but loses keys badly when I scan it continuously.

Polling every 1 msec causes it occasionally. Every 5 msec it's much more rare.
« Last Edit: Mon, 07 October 2013, 20:45:14 by blargg »

Offline sprit

  •  Post Editing Timeout
  • Posts: 2425
  • Location: The Geek Symphosion :::::: Mechanical Engineering ::: Alta Magna Uniforma :::::: 哀吾生之須臾羨長江之無窮 ::
Re: ADB to USB keyboard converter
« Reply #164 on: Mon, 07 October 2013, 20:50:46 »
I love this post!  for a long time
« Last Edit: Fri, 11 October 2013, 07:30:21 by sprit »

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #165 on: Mon, 07 October 2013, 21:35:30 »
EDIT: went through all my keyboards and all 5 extended ones are M3501. I have three smaller ones: two M0116 and an M0487 (IIgs keyboard). The M0487 shows issues like you mentioned with pressing the same key over and over quickly. It shows up my bare-bones tester that just runs the adb.c code and outputs over the hardware UART (so no interrupts to interfere). It doesn't occur when I poll the keyboard every 16 msec, but loses keys badly when I scan it continuously.

Polling every 1 msec causes it occasionally. Every 5 msec it's much more rare.

Wow, you are right :o
I confirmed that adding simple 5ms delay between scans make the problem gone!

Not sure about optimal value of scan rate now, but I believe you found the solution.
What's your recommendation? 16ms? I think it is fast enough even for fast typers.

It seems my code make the old controller high loaded unnecessarily...

Thanks blargg!
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #166 on: Mon, 07 October 2013, 22:39:04 »
I used 16ms delay tentativly and pushed the fix to the repository now.
With this commit it seems the problem is gone or mitigated dramatically to me.
https://github.com/tmk/tmk_keyboard/commit/e8e8f93bcf0cdc8d72d35662db5a1da4716762b8

tufty and arfink, can you try the latest source and test this fix?
« Last Edit: Mon, 07 October 2013, 22:40:38 by hasu »
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #167 on: Mon, 07 October 2013, 23:20:59 »
I'll have to try this finicky keyboard on some old Macs (PowerBook 540c and PowerMac 8500) and see if it works reliably, and what the ADB poll rates are on them.

I've also rewritten adb.c somewhat to hopefully be more robust, and I plan on offering it. The approach lends itself to easy conversion to use a timer rather than delay loops, and perhaps make it fully interrupt-driven. I'm going to add ADB simulation to simavr so I can run it through some virtual stress tests.

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #168 on: Tue, 08 October 2013, 00:20:38 »
Yeah, the interrupt driven is still more reliable approach, of course. Keep us updated.
Once your code works and makes available to us it would be great.

I'm looking forward to seeing your code! And it looks simavr is very useful tool, I must try it.

TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #169 on: Tue, 08 October 2013, 15:07:46 »
ADB polling periods
PowerBook 520c: 11.7 ms
PowerMac 8500 11.2 ms
Belkin ADB-USB adapter: 8.34 ms

Since this isn't for gaming, I'd say 11.5 ms is a good level. Clearly lower values are risky.

PowerBook 520c timings:
Attention: 800 us
bit cell time: 99 us

So the low-level timings you use are good in that regard.

I'm making progress using simavr for testing.

Another thing, I've changed my ADB code to never drive the data line high, and rely entirely on the pull-up resistor. This way there is never a risk of the AVR driving it high while the keyboard pulls it low. So init code becomes:

ADB_DDR &= ~(1<<bit);
ADB_PORT &= ~(1<<bit);

And then

static inline void data_lo( void ) { ADB_DDR |= data_mask; }
static inline void data_hi( void ) { ADB_DDR &= ~data_mask; }

This way the AVR pin behaves like a normal open-collector output: either floating or held low. We don't want to rely on the AVR's internal weak pull-up, since I think we've shown that it's too slow.

I looked at the rise time for no pull-up (hilarious), 10K (almost 10 us), and 1K (~150 ns), so a 1K pull-up is definitely desired, and seems to be roughly what the Mac uses.

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #170 on: Tue, 08 October 2013, 17:49:44 »
I wrote some code that tests the overhead of the PJRC basic USB keyboard driver's ISR, and found it to be only 5us. My Linux machine polls USB at 1kHz, so these missing 5us chunks occur every millisecond. I'm going to incorporate this into the simulator. My improved ADB code shouldn't be affected by this, though the current ADB code might be as it wasn't very tolerant of variations in timing in the simulator. With my improved code I don't think there's much need for an interrupt-based version, and the complexity could be a source of new bugs. Even if one were made, I'm not sure how to get PJRC's ISR to re-enable the timer interrupt so it can itself be interrupted by our ADB code. This doesn't rule out using a capture register when receiving from the keyboard, though.
« Last Edit: Tue, 08 October 2013, 17:51:38 by blargg »

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #171 on: Tue, 08 October 2013, 23:40:08 »
I did a preliminary input capture version for receiving from the keyboard and it does time it more accurately in the face of interrupts, but didn't perform any better than the basic rewrite I've done. And its send timing is still affected by interrupts, so it seems the only reasonable alternate version is a full interrupt-based one. But, I've been researching the LUFA USB driver and it seems that delaying interrupts isn't a problem with the hardware-based USB. Also, it apparently doesn't really support re-enabling interrupts in its interrupt handler, so an interrupt-based ADB would still get delayed. It sounds like disabling interrupts for the duration of ADB transfers would be fine for the hardware-based USB implementations. And assuming that LUFA is similar to the PJRC basic USB keyboard driver, the interrupt overhead doesn't mess up ADB timing enough to matter.

Here's the updated adb.c and the simple ADB-USB converter I wrote (with a makefile):

blargg-adb-usb-1.zip

The above can be used as a standalone ADB-USB converter (I'm using it right now to type this).

The new ADB code doesn't disable interrupts at all. You could do that around the calls to the adb code. I think if you're going to disable them, you could do it for the entire call, not just when the keyboard is talking.


Changes
-----
* Documented header and source a little more and added some symbolic names for error values.
* Rearranged functions in source to not need prototypes.
* Merged lots of functions where there was no loss in clarity.
* Added adb_host_kbd_modifiers() to get keyboard modifiers, just because it was only 3 lines of code and it could be useful to someone in the future for a keyboard driver.
* Added config.h support for setting ADB_PORT etc.
* Eliminated constant reconfiguring of ports every time they're used. We can set them up once in init and depend on them not being messed with. If someone else is screwing with our configuration, then that needs to be fixed, rather than us constantly reconfiguring them.
* Changed to using open-collector output and relying on external 1K pull-up resistor. This is how ADB devices do it and avoids possibility of us driving bus high while keyboard pulls it low.
* Moved bit-reading code into adb_host_talk so that we can report any bit timing errors rather than ignore them.
* Optimized code to be smaller and thus have less timing overhead.


Timing fix
----------
The biggest issue was wait_data_lo() and wait_data_hi(). Their loop delayed delayed 1 us in *addition* to the loop overhead, which was 1.5 us on its own due to gcc not inlining and the data_in() reconfiguring the port every time. This could have been throwing timing off so that it was just on the edge of working, failing when timing was just slightly off.

Now the code compensates so that loop is exactly 1 us (16 cycles). This allows return value to be used to calculate how long pulse was in the bit reading code. In addition, it logs every timing value so that we can look at them later and see how well within margins it's timing things, and be sure that the times it's measuring match the times of the actual pulses coming in. It just puts them into an array for examination by the calling code.

The new bit reading code times the low and high portions, then determines whether it's a 0 or 1 based on which was longer than the other, rather than looking at absolute times. In the simulator it's show itsely very robust to wide variations in timing.

The timing is still affected slightly by the overhead between calls to wait_*, but it's only a few us.
« Last Edit: Tue, 08 October 2013, 23:46:26 by blargg »

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #172 on: Wed, 09 October 2013, 00:54:56 »
excellent work! ill look into later.
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #173 on: Wed, 09 October 2013, 22:05:29 »
Small tweak to adb.c line 48:

// gcc is very unreliable for inlining, so use macros
#define data_lo() (ADB_DDR |= data_mask)
#define data_hi() (ADB_DDR &= ~data_mask)
#define data_in() (ADB_PIN & data_mask)

Making these macros ensures that they are inlined, something hard to get GCC to do consistently. This is important for timing.

Argh, I also see that I based my adb.c on the one from December 10, 2012, rather than the current one. So you'll have to add the adb_host_listen(0x2B,0x02,0x03) call to init (though I think it should be done by the caller).
« Last Edit: Thu, 10 October 2013, 01:52:07 by blargg »

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #174 on: Thu, 10 October 2013, 20:30:27 »
I added files from blargg's excellent wokrk into my repository, but it is not integrated and not default yet at this time. I need time to understand his work and code and after that I'll work on merging. You can use blargg's ADB protocol stack with 'make -f Makefile.blargg'.

Or his work itself is a complete ADB converter, you can just download zip archive from his post to use his ADB converter. His code is concise and skillful you may want to look through it.

Thanks.

Yeah, you are right. That command should be moved from adb.c. I'll fix it later.
« Last Edit: Thu, 10 October 2013, 20:33:00 by hasu »
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #175 on: Thu, 10 October 2013, 20:50:07 »
I'll see if I can learn how to use github to submit a patch myself. It's silly for me to make you try to grasp it all when I could do it in a few minutes.

Offline sprit

  •  Post Editing Timeout
  • Posts: 2425
  • Location: The Geek Symphosion :::::: Mechanical Engineering ::: Alta Magna Uniforma :::::: 哀吾生之須臾羨長江之無窮 ::
Re: ADB to USB keyboard converter
« Reply #176 on: Fri, 11 October 2013, 08:00:58 »
For my Apple II gs ?

38628-0

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #177 on: Fri, 11 October 2013, 23:09:11 »
I'll see if I can learn how to use github to submit a patch myself. It's silly for me to make you try to grasp it all when I could do it in a few minutes.

Thanks, but I know what I have to do: just replacement of my adb.c with yours.
The only problem is my ego, hehe; I'm hesitating to remove my own code from repository and trying to keep it somehow :D

I also saw your post in simavr ML, which said you dont want to be bothered by Git :) I can understand you! You've already done great job, I don't want to kill your time anymore.

sprit, I believe this converter also works on your IIgs, try it!
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #178 on: Fri, 11 October 2013, 23:41:00 »
Except the locking caps lock of course. I'll get around to that in a few days.

Quote
The only problem is my ego, hehe; I'm hesitating to remove my own code from repository and trying to keep it somehow

Never fear, I was facing the same thing with me wanting my coding style there, and how I rewrote adb.c almost completely even though it wasn't actually necessary. So today I got the simulated timing tester all working, so it tests the code to be within ADB timing tolerances, and verifies that it can receive from the keyboard at timing limits. Now I'm going to *minimally* modify your original adb.c code to pass the test, so that your style etc. is preserved. It's a good exercise for me to not try to redo everything unnecessarily.
« Last Edit: Fri, 11 October 2013, 23:42:36 by blargg »

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #179 on: Sun, 13 October 2013, 00:02:40 »
OK, here's my update of the current adb.c that you can just drop in. It's the fewest changes I could make and pass my tx/rx timing tests. I've kept it in your style. It was a good exercise to not do everything my way.

http://pastebin.com/RSK622dg

* data_* macros to ensure inlining
* data_* open-collector
* wait_data_* timing fix to exactly 1 usec per iter
* wait_data_* uint16_t on both for longer timeouts
* read_bit time both levels and compare, much more robust
* read_bit error reporting wouldn't work with just bool return value, so had to move into adb_host_kbd_recv()
* stop bit handled separately since its high portion doesn't end like other bits, and can have SRQ extension

Offline tufty

  • Posts: 347
  • Location: French Alps
Re: ADB to USB keyboard converter
« Reply #180 on: Sun, 13 October 2013, 00:46:06 »
blargg, hasu.

Awesome work.  Not had time to mess with this, but I'll see if I can make some time this week.

Simon

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #181 on: Sun, 13 October 2013, 15:21:09 »
I'm going to see whether I can get the ADB converter working on a USBasp 2.0 programmer, which is basically an atmega88 with zener diodes for voltage conversion to USB, a 12 MHz crystal, and connector to several pins. So I could put V-USB on it and the ADB code to have a really cheap converter (only a few dollars on eBay). I already got one of the programmers and it works great as a programmer, so the build quality is good. With a second one I can reprogram the first with the ADB and V-USB driver just by connecting the ISP cable between them.
« Last Edit: Sun, 13 October 2013, 19:53:40 by blargg »

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #182 on: Mon, 14 October 2013, 10:26:50 »
Thanks a lot. I merged your fix into repository.

OK, here's my update of the current adb.c that you can just drop in. It's the fewest changes I could make and pass my tx/rx timing tests. I've kept it in your style. It was a good exercise to not do everything my way.

http://pastebin.com/RSK622dg

* data_* macros to ensure inlining
* data_* open-collector
* wait_data_* timing fix to exactly 1 usec per iter
* wait_data_* uint16_t on both for longer timeouts
* read_bit time both levels and compare, much more robust
* read_bit error reporting wouldn't work with just bool return value, so had to move into adb_host_kbd_recv()
* stop bit handled separately since its high portion doesn't end like other bits, and can have SRQ extension


TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline hydrospell

  • * Destiny Supporter
  • Posts: 272
  • Location: Singapore
  • some keyboard thing
    • octobrain
Re: ADB to USB keyboard converter
« Reply #183 on: Tue, 29 October 2013, 11:46:32 »
Hi hasu, blargg and JeffreySung

First off thanks so much for making and contributing to this project - a real lifesaver when it comes to resurrecting old keyboards.

I'd like to ask what the difference is between keyboard/IIgs and converter/adb_usb? I am able to successfully make using blargg's Makefile in adb_usb, but I can't make at all with the keyboard/IIgs files. Here's my dump below.

So I'd like to know
  • Can adb_usb be used to drive the IIgs board? and what features will I be missing over the IIgs files?
  • How to fix the error I face?

Thanks in advance for your patience.

Code: [Select]
mkdir -p obj_IIgs_Standard
Compiling C: matrix.c
avr-gcc -c -mmcu=atmega32u4        -gdwarf-2 -DF_CPU=16000000UL -DPROTOCOL_PJRC -DMOUSEKEY_ENABLE -DEXTRAKEY_ENABLE -DNO_PRINT -DNO_DEBUG -DMOUSE_ENABLE -DVERSION=b9fe36f-dirty -Os -funsigned-char -funsigned-bitfields -ffunction-sections -fno-inline-small-functions -fpack-struct -fshort-enums -fno-strict-aliasing -Wall -Wstrict-prototypes -Wa,-adhlns=obj_IIgs_Standard/matrix.lst -I. -I../.. -I../../protocol/pjrc -I../../common -std=gnu99 -include config.h -MMD -MP -MF .dep/obj_IIgs_Standard_matrix.o.d  matrix.c -o obj_IIgs_Standard/matrix.o
matrix.c: In function 'matrix_scan':
matrix.c:126:10: warning: implicit declaration of function 'host_keyboard_leds' [-Wimplicit-function-declaration]
matrix.c: In function 'read_col':
matrix.c:271:11: error: 'PINA' undeclared (first use in this function)
matrix.c:271:11: note: each undeclared identifier is reported only once for each function it appears in
matrix.c: In function 'unselect_rows':
matrix.c:295:5: error: 'DDRA' undeclared (first use in this function)
matrix.c:296:5: error: 'PORTA' undeclared (first use in this function)
matrix.c: In function 'select_row':
matrix.c:354:7: error: 'DDRA' undeclared (first use in this function)
matrix.c:355:7: error: 'PORTA' undeclared (first use in this function)
matrix.c: At top level:
matrix.c:176:13: warning: 'matrix_has_ghost_in_row' is static but used in inline function 'matrix_has_ghost' which is not static [enabled by default]
make: *** [obj_IIgs_Standard/matrix.o] Error 1
⌨ Filco MJ2 MX Blues / M0116 Orange Alps / Homemade 60% MX Reds / M0110

Cherry MX art prints 20/50 still available!

Offline hydrospell

  • * Destiny Supporter
  • Posts: 272
  • Location: Singapore
  • some keyboard thing
    • octobrain
Re: ADB to USB keyboard converter
« Reply #184 on: Tue, 29 October 2013, 11:51:24 »
Oh frick just read the IIgs readme files :(

  • Replaceable keyboard controller for Apple IIgs(A9M0330)/Standard(M0116)
  • Teensy++ 2.0 required
  • Some signal bypass required
  • ADB keyboard doesn't use matrix for modifier keys. With virtual row for modifier keys, these keys are merged into key matrix using virtual row.

I've got a Teensy 2.0. Am I able to use adb_usb code to drive a IIgs keyboard then or do I have to get a Teensy++ 2.0 and build for that? (My keyboard will arrive only in the following couple of days)

Thanks.
⌨ Filco MJ2 MX Blues / M0116 Orange Alps / Homemade 60% MX Reds / M0110

Cherry MX art prints 20/50 still available!

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #185 on: Tue, 29 October 2013, 12:47:33 »
'keyboard/IIgs' is for replacing controller on inside of  keyboard with Teensy2.0++ and you need to mod your keyboard, while 'converter/adb_usb' is a converter for ADB keyboards including IIgs and you can keep keyboard intact.

You can convert your IIgs into USB keyboard with adb_usb and Teensy2.0. The converter doesn't require Teensy2.0++.
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline hydrospell

  • * Destiny Supporter
  • Posts: 272
  • Location: Singapore
  • some keyboard thing
    • octobrain
Re: ADB to USB keyboard converter
« Reply #186 on: Tue, 29 October 2013, 12:54:40 »
Thanks very much hasu. That's clearer to me now. So I just need to live with adb -> teensy -> usb (an additional attachment)

As long as I can start using the IIgs keyboard when I receive it I'm happy :)

May I ask what are the advantages of modding it with the Teensy 2.0++? Is the only benefit the more compact setup without having to have the adapter outside of the keyboard?

⌨ Filco MJ2 MX Blues / M0116 Orange Alps / Homemade 60% MX Reds / M0110

Cherry MX art prints 20/50 still available!

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #187 on: Wed, 30 October 2013, 00:43:21 »
I think you are right.
Not so different. The biggest difference is thier looking. jeffreysung's mod makes vintage IIgs into a complete modern USB keyboard. http://geekhack.org/index.php?topic=14290.msg304680#msg304680
As for their function what you can do with is almost same in practical usage or I can't come up with.
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline hydrospell

  • * Destiny Supporter
  • Posts: 272
  • Location: Singapore
  • some keyboard thing
    • octobrain
Re: ADB to USB keyboard converter
« Reply #188 on: Wed, 30 October 2013, 00:49:27 »
Thanks for the clear explanation hasu.
⌨ Filco MJ2 MX Blues / M0116 Orange Alps / Homemade 60% MX Reds / M0110

Cherry MX art prints 20/50 still available!

Offline hydrospell

  • * Destiny Supporter
  • Posts: 272
  • Location: Singapore
  • some keyboard thing
    • octobrain
Re: ADB to USB keyboard converter
« Reply #189 on: Sat, 02 November 2013, 16:12:22 »
sorry to bother you - I tried making the file but when I load it onto the teensy and usef hid_listen, all I get is

Code: [Select]
adb_host_kbd_recv: ERROR(FFFE)
adb_host_kbd_recv: ERROR(FFFE)
adb_host_kbd_recv: ERROR(FFFE)
adb_host_kbd_recv: ERROR(FFFE)
adb_host_kbd_recv: ERROR(FFFE)
adb_host_kbd_recv: ERROR(FFFE)
adb_host_kbd_recv: ERROR(FFFE)
adb_host_kbd_recv: ERROR(FFFE)
adb_host_kbd_recv: ERROR(FFFE)

This is even before I connect my keyboard.
Any ideas? Thanks.
⌨ Filco MJ2 MX Blues / M0116 Orange Alps / Homemade 60% MX Reds / M0110

Cherry MX art prints 20/50 still available!

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
ADB to USB keyboard converter
« Reply #190 on: Sat, 02 November 2013, 22:55:48 »
it looks like conection has something wrong, it has pull up resister?
edit: ah, keyboard isnt conected yet? see how it goes when conected.
« Last Edit: Sat, 02 November 2013, 23:01:47 by hasu »
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline hydrospell

  • * Destiny Supporter
  • Posts: 272
  • Location: Singapore
  • some keyboard thing
    • octobrain
Re: ADB to USB keyboard converter
« Reply #191 on: Sun, 03 November 2013, 01:03:35 »
I have connected my keyboard to the teensy like such

adb         teensy
1 (data)      D4
2 (PSW)      D5
3 (+5v)      VCC
4 (GND)      GND

I have this in my config.h

Code: [Select]
#define ADB_PORT        PORTD
#define ADB_PIN         PIND
#define ADB_DDR         DDRD
#define ADB_DATA_BIT    4
#define ADB_PSW_BIT     5       // optional

I have not soldered a resistor yet. Should I try soldering a resistor?

Do I connect resistor from D4 (my DATA port) to VCC?
⌨ Filco MJ2 MX Blues / M0116 Orange Alps / Homemade 60% MX Reds / M0110

Cherry MX art prints 20/50 still available!

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #192 on: Sun, 03 November 2013, 01:12:21 »
right, place resister like 1k ohm between vcc and data line.

psw line isnt used by default, you dont need in fact. but it wont harm anything.
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline hydrospell

  • * Destiny Supporter
  • Posts: 272
  • Location: Singapore
  • some keyboard thing
    • octobrain
Re: ADB to USB keyboard converter
« Reply #193 on: Sun, 03 November 2013, 10:47:14 »
hasu, I'm very happy to report that your firmware is working 100% despite my very bad soldering!  :) The resistor made it work perfectly. I was also able to map the keys correctly with your keymap.c. Very very happy with how this turned out and I can't thank you enough.

Thank you so much hasu.
⌨ Filco MJ2 MX Blues / M0116 Orange Alps / Homemade 60% MX Reds / M0110

Cherry MX art prints 20/50 still available!

Offline hydrospell

  • * Destiny Supporter
  • Posts: 272
  • Location: Singapore
  • some keyboard thing
    • octobrain
Re: ADB to USB keyboard converter
« Reply #194 on: Sun, 03 November 2013, 10:51:45 »
I would like to add that keystrokes are not dropped so far, and quick keystrokes are registered perfectly. I map ww to :w in my vim so I have to type w twice very quickly to trigger :w - no issues at all with this firmware and keyboard.

If there are any tests or observations you'd like me to do please feel free to get me to do so, I'll be more than happy to contribute back in any little way I can.

For the record I'm using

Stripped s-video cable head soldered to Teensy 2.0
adb_usb firmware by tmk/hasu
10k1k resistor from VCC to DATA line note: typo, I bought a 1k ohm resistor
Apple Desktop Bus keyboard (aka Apple IIgs keyboard)

Thanks!
« Last Edit: Tue, 05 November 2013, 21:28:16 by hydrospell »
⌨ Filco MJ2 MX Blues / M0116 Orange Alps / Homemade 60% MX Reds / M0110

Cherry MX art prints 20/50 still available!

Offline blargg

  • Posts: 45
    • My github
Re: ADB to USB keyboard converter
« Reply #195 on: Sun, 03 November 2013, 12:57:13 »
Great to hear that the revised ADB driver is working better for a demanding typist! It was most likely merely reducing the polling frequency.

And yeah, with my revisions to the ADB driver the pull-up resistor is required. This way rather than maybe working without it with one wondering why, it flat out requires it. Also, my tests on an oscilloscope show that a 1K is the better value rather than 10K, though 10K should be fine.

Hmmm, I wonder whether we could allow the absence of a pull-up resistor by tying several input pins together, all with weak internal pull-ups. This would allow people to build this without needing resistors. Datasheet lists internal pull-up from 20k to 50k, so 5 pins connected together would yield 4k to 10k pullup.

Yes, this works. 5 pins tied together with weak pull-ups gives a ~7k pullup on my Teensy, and ADB works. Given how many unused pins there are this might save someone having to locate a resistor, and make this require nothing more than the Teensy and an s-video cable cut in half. The main drawback of this is that you have to be sure the code doesn't configure the pull-up pins as outputs.
« Last Edit: Sun, 03 November 2013, 13:29:32 by blargg »

Offline hydrospell

  • * Destiny Supporter
  • Posts: 272
  • Location: Singapore
  • some keyboard thing
    • octobrain
Re: ADB to USB keyboard converter
« Reply #196 on: Sun, 03 November 2013, 12:59:03 »
I'm definitely not a demanding typist :)

Your work is much appreciated sir. Again, I reiterate my offer to do testing with my setup if required, with what I have listed as above.

Thanks!
⌨ Filco MJ2 MX Blues / M0116 Orange Alps / Homemade 60% MX Reds / M0110

Cherry MX art prints 20/50 still available!

Offline hasu

  • Thread Starter
  • Posts: 3122
  • Location: Tokyo, Japan
  • @tmk
    • tmk keyboard firmware project
Re: ADB to USB keyboard converter
« Reply #197 on: Mon, 04 November 2013, 08:46:01 »
hydrospell, thanks for your reporting and offer for further testing.
happy to hear your IIgs goes well now. Let me know if you run against any problem with your keyboard.

blargg, putting together multiple I/O  is very interesting idea! It will be useful for those who don't have pull-up resistor in hand :)
TMK products:HHKB Alt  ⌨ConvertersAlps64FC660C AltFC980C Alt

Offline thegagne

  • Posts: 69
  • Location: United States
Re: ADB to USB keyboard converter
« Reply #198 on: Mon, 04 November 2013, 09:30:04 »
So I have a few ADB Apple KB, and I want to make use of one, what's the best method at this point? Is this project mature? Should I just pull the controller and put a teensy in, or convert it? Find an old iMate?

Offline tufty

  • Posts: 347
  • Location: French Alps
Re: ADB to USB keyboard converter
« Reply #199 on: Mon, 04 November 2013, 10:05:21 »
Hasu's converter is certainly mature enough to use, but may* still drop keys on certain models (sorry hasu, been too busy to repatch my m0116 mappings into your code, I'll try and get round to testing again soon, I promise). 

If you don't have an iMate lying about, drop the cash on a teensy and give it a go.  Worst case, you can rip out all the ADB circuitry, get handy with the soldering iron, and use either hasu's or soarer's "raw matrix" drivers for the teensy.

Simon

* it did last time I tried, but I haven't tested recently, and there's been a major rehash of the ADB code since then.