USB Host controller project - Page 1
Forum section: electronics / projects

author: gmc
author_rank: Regular Contributor
timestamp: January 09, 2020, 10:38:20 am
content: I'm looking at starting a new project which will allow a PC usb keyboard to be connected to a microcontroller and this will interface with a retro Commodore 64 computer. The end goal is to use a normal usb keyboard in place of the C64 keyboard.The C64 keyboard is controlled via a 8x8 matrix. 8 rows and 8 columns.So I'm thinking of using a at90usb162 to read the keys from the USB keyboard and then interfacing into the C64 with a MT8816AE (8 x 16 Analog Switch Array)https://www.futurlec.com/Datasheet/Zarlink/MT8816AE.pdfDoes this sound do-able? I've seen a few examples of using the at90usb162 as a usb host controller and I think this will be the trickiest part of the project. The at90usb162 should also have enough pins to control the MT8816AE

author: SiliconWizard
author_rank: Super Contributor
timestamp: January 09, 2020, 03:48:18 pm
content: Are you sure this MCU can do USB host? I'm not so sure.

author: magic
author_rank: Super Contributor
timestamp: January 09, 2020, 04:50:22 pm
content: I'm absolutely sure it can't, unless somebody wrote a bitbanging implementation good for maybe low speed. But if you go up the line, there are AT90USB parts with OTG capability and they have more pins. They are expensive and some ARM may be cheaper.For the lowest effort solution use Raspberry PiAnd, frankly, look around because somebody may have already done similar project like translating USB to PS/2 or whatever.

author: SiliconWizard
author_rank: Super Contributor
timestamp: January 09, 2020, 04:56:36 pm
content: I'm pretty sure USB to PS/2 converters do exist (to use USB keyboards on old computers), and probably pretty cheap too. So all you'd need is one of these, and design just the PS/2 to C64 adapter, which should be pretty straightforward even on a very small MCU. Additional benefit, outside of simplicity, is that it could of course be used stand-alone with PS/2 keyboards, which can also still be found (a lot of used ones, and probably some new ones as well...)

author: oPossum
author_rank: Super Contributor
timestamp: January 09, 2020, 05:26:13 pm
content: CH559 has USB host and USB bootloader (no programmer needed)[note: not my video]

author: langwadt
author_rank: Super Contributor
timestamp: January 09, 2020, 05:49:31 pm
content: Quote from: SiliconWizard on January 09, 2020, 04:56:36 pmI believe most of those USB keyboard to PS/2 connector were passive and the keyboards supported both USB and PS/2 on the same wires. I don't know if "modern" keyboards still do that

author: gmc
author_rank: Regular Contributor
timestamp: January 09, 2020, 06:20:30 pm
content: Thanks for the replies. I wrongly assumed the at90usb162 would work as a usb host. I'll have a look at the other models that support OTG. Will also check out the CH559.There are other alternatives. I could use a RPi or a Arduino Due, or some other type of usb host shield but I would like to build everything on a single PCB so I can start selling a few devices. Going to be trickier if I have to get all these extra modules and solder them onto the PCB.

author: Nominal Animal
author_rank: Super Contributor
timestamp: January 09, 2020, 06:23:37 pm
content: Use a proper USB host controller instead. If you use MAX3421E, you can interface it to your microcontroller via SPI, and you can use or look at existing Arduino USB Host Shield libraries for software implementation.Note MAX3421E runs at 3.3V signal levels only. You can try it with the cheap Mini USB Host Shields on fleabay and elsewhere, just make sure you either use a 3.3V supply and signal level microcontroller, or use a level shifter and a 3.3V regulator for the SPI connection between MAX3421E and your microcontroller.If I was making my own board, I'd use AVR (I like ATmega32u4), 2+2 dual supply translating unidirectional transceivers (I dunno, maybe 74LVC1T?), MAX3421E, a 3.3V regulator, a 16 MHz crystal if the AVR wants it, and a smattering of passives, plus the MT8816AE of course.

author: GromBeestje
author_rank: Frequent Contributor
timestamp: January 09, 2020, 06:35:41 pm
content: The AT90USB647 and AT90USB1287 have host support.

author: SiliconWizard
author_rank: Super Contributor
timestamp: January 09, 2020, 07:04:42 pm
content: Quote from: langwadt on January 09, 2020, 05:49:31 pmRight! I don't know for *all* more recent keyboards either, but keyboards supporting both USB and PS/2 are still sold.Now that we know the OP wants to probably make a product, I can understand this two-part solution could be clunky, although still usable, and limiting the potential buyers.So now the rest will largely depend on the OP's skills and the targeted cost. Any MCU supporting USB OTG should do it. Try and select one for which there's already example code to support USB HID devices.You can take a look a this:https://felis.github.io/USB_Host_Shield_2.0/(library that Nominal Animal talked about.)

author: langwadt
author_rank: Super Contributor
timestamp: January 09, 2020, 07:21:16 pm
content: Quote from: SiliconWizard on January 09, 2020, 07:04:42 pmif keyboards still support both USB and PS/2 the converter can be skipped, just use a USB connector directly for the PS/2

author: gmc
author_rank: Regular Contributor
timestamp: January 09, 2020, 07:46:40 pm
content: The MAX3421E looks very interesting. Need to put some costs together but it will either be a Atmel with OTG support, or a simpler Atmel in combination with the MAX3421.Great link you posted for the USB host shield 2.0

author: nigelwright7557
author_rank: Frequent Contributor
timestamp: January 09, 2020, 09:56:14 pm
content: There are numerous PIC microcontrollers that do USB hosting.

author: Yansi
author_rank: Super Contributor
timestamp: January 09, 2020, 11:06:23 pm
content: There are numerous STM32 microcontrollers that can do a USB host or two. Source code examples for USB HID (both keyboard and mouse) readily available.

author: Nominal Animal
author_rank: Super Contributor
timestamp: January 10, 2020, 10:11:14 am
content: The reason I suggested such a complicated arrangement of hardware (ATmega32U4, MT8816AE, MAX3421E, 3.3V regulator, SPI level shifter between 32u4 and MAX3421E, a crystal for the 32u4, and a smattering of passives), is that I know the needed software already exists -- with the exception of mapping keypresses and releases to control the MT8816AE, and that looks very straightforward. You can even build an Arduino test rig and try it right now, using a cheap Pro Micro clone (just remember to select Arduino Leonardo in the Arduino environment!), an USB host shield, a 3.3V regulator (breadboard module), 3.3V-5V level shifters between the clone and the USB host shield, plus a MT8816AE. All those are available on fleabay (I checked), and in DIP versions, so you can breadboard it.A 32-bit ARM microcontroller with USB host support would be electronically simpler (and cost much less in parts), as you'd probably only need some level shifters (they typically use 3.3V logic, and I believe the C64 uses 5V internally) to the MT8816AE and omit the USB host controller chip, but I believe the software side would need a lot more work, as the existing sources would not be usable as-is. (Unless, as Yansi said, there exists examples in the STM32 land and you use one; I would not know about those. I am only talking about the Arduino, and bare-metal/avr-gcc + avr-libc world here, without any hardware abstraction libraries. The Arduino libraries are surprisingly easy to modify to work on plain-metal avr-gcc, if bare-metal is your goal.)Because of this, I believe that the ATmega32u4 -based solution is "better" in terms of overall value, especially as the target audience is one that probably values the easy customization/modification of the board. There is nothing stopping one (but the fear/probability of others cloning the board) from keeping the custom device programmable in the Arduino environment, for example.(Of course, here we get into the business logic, which I fail. If the idea is to do this for fun and not for commercial gain, then the openness/ease of modification should be a win-win. If the idea is to make a small profit too, then.. well, I wouldn't know, my suggestion is utterly unlikely to yield that.)

author: gmc
author_rank: Regular Contributor
timestamp: January 10, 2020, 04:07:54 pm
content: I'm going to go with the max3421e route. Been using Arduino's for years and very familiar with programming them. There is also a decent library available which I have started looking through.I've ordered a MAX3421e and MT8816AE and will play around with them on my Arduino Uno. Once it all works will build everything on a PCB.As you said there are many ways to do this. A STM32 might be simpler with fewer components but might take me 10x longer to work out how to program itThe idea is to mainly to build a board for myself and a few friends, learn on the way (And hopefully not blow up my C64) and maybe sell off a few extras. Not looking to go big.

author: SiliconWizard
author_rank: Super Contributor
timestamp: January 10, 2020, 04:15:38 pm
content: Quote from: Nominal Animal on January 10, 2020, 10:11:14 amFully agree with this. And I don't like Arduino much, but here the existing *working* code base is a very big plus.Disclaimer: I haven't looked thoroughly at the library itself, but it should work on several MCUs that are supported by the Arduino environment I guess? So you're not even necessarily stuck to using an Atmega if you want something a bit more powerful, or cheaper, or whatever?The software side of this should not be underestimated. Sure it only takes a few seconds to just write: "any MCU with OTG will do". It's another story to get a working, robust solution, that will work with all keyboards without weird compatibility issues and that will not require hours of debugging. Anyone thinking that random example code (even code that's from the vendor! it's often pure crap!) will do is IMO sadly mistaken.

author: Nominal Animal
author_rank: Super Contributor
timestamp: January 10, 2020, 05:28:15 pm
content: Quote from: SiliconWizard on January 10, 2020, 04:15:38 pmYes; thegithub page for the USB Host Shield 2.0 librarysays, paraphrased: all official Arduino AVR boards, Arduino Due, Intel Galileo, Intel Galileo 2, Intel Edison, Teensy 2.0/2.0++/LC/3.x, Balanduino, Sanguino, Black Widdow, RedBearLab nRF51822, Digilent chipKIT, STM32F4, ESP8266, and ESP32.That obviously includes clones like Pro Micros (clones of Arduino Pro Micro or SparkFun Pro Micro, normally with an Arduino Leonardo bootloader, and should be treated as an Arduino Leonardo in the Arduino environment).

author: SiliconWizard
author_rank: Super Contributor
timestamp: January 10, 2020, 06:01:41 pm
content: Quote from: Nominal Animal on January 10, 2020, 05:28:15 pmA handful indeed. With the ones supporting Bluetooth, you could even design the adapter so that it can also work with Bluetooth keyboards.

author: Nominal Animal
author_rank: Super Contributor
timestamp: January 10, 2020, 06:35:50 pm
content: Not a bad idea! If you used ESP32, you could support both USB and Bluetooth in the same device! ESP32's are certainly cheap enough.(You would need to use 3.3V to 5V voltage shifter between the ESP32 and the MT8816AE, though, as ESP32 uses 3.3V logic levels. Standard level shifters work, all the signals are one-directional. No level shifter needed between ESP32 and the USB Host Shield 2.0. I do believe this is an even better suggestion than my ATmega32u4 -based one.)Note: I haven't used Bluetooth HID, but AIUI it's just a wrapper around the USB HID protocol, so it should not be too much extra work to support both. I would also have both interfaces their own state, so that one could use two keyboard simultaneously. (That is, MT8816AE would be controlled by every press and release event on either keyboard; you do not need to remember the MT8816AE states.)Also, I only just noticed that the USB Host Shield 2.0 library even has akeyboard example; surely useful here.Oh, and an additional note: do consider using the extra pins left over for optionally controlling the joystick ports as well.

author: dreamwatcher
author_rank: Newbie
timestamp: May 27, 2022, 03:24:51 pm
content: I am really late to the party with this post.However, I have been working on just this type pf project for a few years.https://github.com/Hartland/C64-KeyboardThis project includes both PS/2 and USB keyboards.For USB, as I suggest in the README, use a SEEED XIAO, though the project does include a DUE, it is WAY overkill for the project.For PS/2 use an UNO or Nano.Perhaps you are already well along the way on your own project and my project can help with some issues you are having.

author: ebclr
author_rank: Super Contributor
timestamp: May 28, 2022, 11:03:17 am
content: http://www.wch-ic.com/products/CH549.htmlhttps://www.electrodragon.com/product/ch559-mini-dev-board-ch55x-series/

author: robert.rozee
author_rank: Frequent Contributor
timestamp: May 31, 2022, 01:37:16 pm
content: you may also like to have a look at the CH376, available on a small PCB for around $5 on ebay:https://www.ebay.com/itm/165027473834while the main target is reading USB flash drives, it appears to also produce results from USB keyboards and mouses. it can connect directly to a TTL serial port, and requires to be sent commands and returns packets of data containing keypress information - NOT ASCII codes; some interpretation is required. for projects using it see:https://github.com/djuseeq/Ch376msc/issues/13and:http://www.electronicsfaq.com/2016/11/interface-usb-mouse-to-your-arduino.htmli have a couple of the ebay boards on order, and am hoping to be able to come up with the incantations necessary to configure it and then repeatedly prompt for reports of keyboard state (up to 6-key rollover, shift states, etc). there appears to be a wch-ic.com forum somewhere where folks have been able to obtain useful guidance in english.cheers,rob :-)