plugging USB keyboard into AVR

Go To Last Post
14 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am interested in plugging a simple standard cheap USB keyboard into a simple standard cheap AVR.

For old purple-connector PS2 keyboards, this was not hard. There were two lines: data and clock.
When a key was pressed, the PS2 keyboard sent a message that was one to three bytes. The framework
of each of these bytes was eleven bits, 1 start, 8 data, 1 parity, and 1 stop. When the clock line
went from 0 volts to +5 volts, the data line held a valid bit.

I am at a complete loss as to how to do this with a USB keyboard. I know that the keyboard is
a Human-Interface-Device with a diffential pair of data lines. Data transfers at 1.2 mega bits per second.

I assume that:
1) When the keyboard is plugged in, or, more likely, when the AVR is powered-on or reset, a
sequence of bytes is sent from the AVR to the keyboard requesting that it identify itself
according to some pre-defined USB standard.

2) Every 1/10th or 1/20th of a second (50-100 milliseconds), the AVR sends a sequence of bytes
(a packet) to the keyboard requesting if any key has been pressed or released since end of the
last period. The keyboard sends a packet back to the AVR saying that there has been no key
pressed, or a packet or set of bytes with a scancode for each key that was either pressed or released
since the end of the last period.

How can I learn to do this? I looked on the web and very quickly got waterboarded with tons of
poorly-written incomprehensible pseudo-documentation about classes, descriptor, links, endpoints,
isochronousities, protocols, configuation descriptors, interrupt pipes that are not interrupts,
reports, etc..

As far as I can tell, all the USB/AVR verbage on the web is concerned with making the AVR appear
to Windows/Linus PC as a USB device. But I simply want the USB keyboard to send scancodes to the
AVR, and then convert the keypress to ASCII for display or as triggers to signal the AVR to do specific
actions.

Since the USB keyboards are only $5-10 and the AVRs are the same cost, there are probably a lot of
people who want to learn how to connect the two together, but are overwhelmed by the USB documentation.

Any suggestions on clear tutorials or source code? Should I be looking into a specialized USB AVR or
can I read a keyboard with a standard Mega328?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You need your AVR to work as a USB host. Keyboards and mice use low speed USB which is only 1.5Mbps which is in the range of a AVR to generate as projects like V-USB demonstrate. I havent seen anyone do a bit based usb host (V-USB is a usb device). Thus you would normally use a AT90USB chip as thee can be hosts. Dean's LUFA should be investigated to see if it talks to a keyboard.

As you've gathered, USB is a bit of a learning curve as there is a lot to know even for a 'simple' connection. Each layer is simple in itself, but there are many layers to navigate. Get one wrong and nothing happens. The best book I've found is this:

http://www.mindshare.com/shop/?c...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I am at a complete loss as to how to do this with a USB keyboard. I know that the keyboard is
a Human-Interface-Device with a diffential pair of data lines. Data transfers at 1.2 mega bits per second.

The AVR needs to operate USB "host mode". You can do this with AT90USB647 or AT90USB1287 because they operate "OTG" (On The Go) which is a limited form of host mode.

The only other option on any other AVR might be to see if V-USB offers an implementation of host mode (I seem to remember seeing this).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LUFA has a keyboard host demo.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
I havent seen anyone do a bit based usb host
Here you are for starters.

Warning: Grumpy Old Chuff. Reading this post may severely damage your mental health.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But you'll end up with software that has little time left for other things to do. But I guess you could rig up a tiny84 or so to convert an USB keyboard to a simple async serial one and use that as a coproc.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:
I am interested in plugging a simple standard cheap USB keyboard into a simple standard cheap AVR.
I've been interested in pursuing this too at some point, but haven't had time. Although what I'm going to suggest for your consideration is not a direct answer to your needs, I think it's worth mentioning....

I purchased an AT90USBKEY2 (v2 of original AT90USBKEY) for US$30 to play around with. The board has an AT90USB1287 MCU, so, as Cliff said, it can perform USB OTG. In fact, the AT90USBKEY2 PCB has a USB-mini-B female socket and comes with 2 USB cables:

  • a conventional one with USB-mini-B male and USB-A male (to plug into a PC's USB socket, when using the AT90USBKEY2 as a USB device)
  • a rather unusual one with USB-mini-B male and USB-A female (to plug a USB device, like a keyboard or a mouse, into the PCB)
Obviously, it's that latter cable that would be used when using the AT90USBKEY2 in host mode.

Unfortunately, I've had no time to mess with my AT90USBKEY2 other than to verify that the hardware is working. But it seems like a convenient (albeit not as cheap as a simple AVR MCU) choice for what you're attempting.

That's all I can recommend on the hardware side. I wish I had more to offer on the software side. All I can say now is that I plan to experiment with LUFA some day.

Aside:

  • Originally, I understood that the AT90USBKEY2 was the same as the original AT90USBKEY, just different packaging and ROHS. But, if the image of the original on Mouser's website is accurate, then the original did not have the JTAG header (just holes) whereas my v2 came with the 2x5 header soldered in place.
  • The PCB comes with no headers for the port connections and, as described in several spots on this forum, their pin spacing is maddeningly uncommon, essentially forcing you to solder bare wires to the holes unless you can find that "magical" header.
Edit: Added URL for AT90USBKEY now that Atmel's website is out of "maintenance mode".

Last Edited: Sat. Jun 30, 2012 - 05:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

USB keyboards are supposed to have a PS-2 mode so that they will work with older computers. Not shoure how to make it work. I think it happens on power up. It has to be very simple, because old computers won't do anything special.

Google ought to tell you how. I would just try a USB keyboard with a PS2 interface. It may well work.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ka7ehk wrote:
USB keyboards are supposed to have a PS-2 mode so that they will work with older computers. Not [sure] how to make it work. I think it happens on power up. It has to be very simple, because old computers won't do anything special.
I'm by no means an expert on this, but my understanding was that some USB devices (like mice/trackballs) which come with one of those ubiquitous USB->PS/2 adapters are smart enough to speak both USB and PS/2 protocol and somehow figure out early on what PC port they're connected to (USB or PS/2), like Jim is saying above. I haven't owned very many USB keyboards (2, IIRC) but I suppose they probably come with one of those adapters too these days. I would 2nd Jim's suggestion to give it a try, especially if your USB keyboard came with such an adapter.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Those adapters are just wiring, there's no electronics in them.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jayjay1974 wrote:
Those adapters are just wiring, there's no electronics in them.
Yeah, I know that. I'm not that dumb! :) But I can see where I might have made that clearer.

What I'm saying is that USB devices that come with such an adapter will have the firmware in the device (not the adapter!) that allows it to figure out whether to speak USB or PS/2 protocol to the host, depending on which port it's connected to.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Right. The USB interface in the keyboard detected which mode it is to operate in. IIRC, it happens according to which lines are in what state on keyboard power-up. It may be that if one line is high and one is low, its USB, and if both are low (or both high, depending on how PS2 is during idle state), its PS2. That simple.

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd be curious how ubiquitous ps2 support is at this point...I haven't had a keyboard come with one of those adapters in years.  That said, it looks like all it takes is a couple 10k pullups on the d+/d- lines and the keyboard should start up in ps2 mode if it supports it.

 

Edit:  Aaand I just noticed that this thread is ancient.  Strange that it was showing on the first page for me...I swear I didn't go digging!

Last Edited: Mon. May 18, 2015 - 05:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Rezurok wrote:
I'd be curious how ubiquitous ps2 support is at this point

I think it's well on the way out.

 

I have a KVM switch which needs PS/2 mouse & KB. When I got it (probably around the time of the OP in this thread), I just bought the 1st mouse I found with one of those adaptors - and it just worked.

 

I recently had to replace the mouse so, again, I just bought the 1st one I found - and it didn't work!

 

sad

 

I then had to go back and look carefully for mice that specifically stated, "USB and PS/2"

 

Quote:
... the keyboard should start up in ps2 mode if it supports it.

but it looks like the default answer to "if it supports it" is now "NO!"

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. May 18, 2015 - 06:19 PM