I'm designing a small device that will act as a USB HID keyboard, but it must also work with PS/2 hosts. I would really want to be able to use it with one of these nifty passive USB-to-PS/2 adapters that used to come with every USB keyboard (not sure if they still do, haven't bought a keyboard in many years!). Problem is, how can I detect if to use USB or PS/2 in my circuit?
Electrical connection
Since I'm using a PIC16F1454 which has a built-in 3.3V LDO, trancievers, and everything for USB my plan was to connect it like this:
I need to connect RA4/5 because D+ and D- can only be used as inputs if they are not used for USB communication. RA4 and RA5 would normally be tri-stated, thus not conflict with the USB, and if the PS/2 mode is enabled, D+ and D- would be disabled in a similar fashion.
I don't foresee any major problems because D+/D- are 3.3V and PS/2 is 5V. Because PS/2 is using an open-collector configuration, any mistake that causes 5V to go out on the USB D+/D- will have a very high impedance, and should not break anything.
Logical
Now, here's the main problem. I'm not sure how I would perform my initialization procedure to detect PS/2 or USB at startup.
One problem is that, from what I understand, there is not a single standard mapping between the USB D+/D- and the PS/2 Clock/Data pins in these passive adapters. Either D+ is connected to Clock and D- to Data, or vice versa. This also leads to the first real question: Is there a standard pin mapping? If not an official standard, perhaps a de facto standard? If there is not, I don't think there will be a problem detecting this in software.
The second question is, well, how do I actually detect if the host is talking to me using USB or PS/2? Preferably in a reliable way. Since every keyboard i have here works with one of those adapters without exception, it can't be that hard. Does the USB standard perhaps discuss this? Few adapters actually carry the USB logo, so I don't think this is something that the USB standard would encourage.
Best Answer
Rather than follow the above comment success will more likely detect if the two pins are pulled up to 5V. (Make sure you use an MCU that has 5V compatible GPIO pins). PS/2 does run as open collector but in the immediate interval after the power supply is up to valid level the interface will be still inactive and both of the PS/2 lines will be undriven and high at 5V. This may require you to add some extra circuitry to detect this as opposed to some lower USB signal levels.
What is the incentive to support PS/2 in this day and age?
Added Information
The data sheet for your PIC16F1454 indicates that the connection of the PS/2 signal lines into the USB D+ and D- may not be advisable. The Absolute Maximum Ratings section has this to say about the D+ and D- pins:
With this information in hand it is not recommended to subject the D+ and D- pins to over 3.3V + 0.5V = 3.8V and certainly NEVER over 4.6V.