Use USB pins as GPIO?

Does anyone know if it’s possible to change the USB Data+ and Data- pins on the micro USB port to function as GPIO pins instead from a games code, I know that you can access the rest of the GPIO pins by taking off the back of the case, but I was hoping it would be possible to program a game to use a moddified usb cable to add periferals or function as a link cable without opening the console.

A cursory look at the datasheet seems to suggest that the D+ and D- pins used for usb are not regular gpio so I don’t think they can be directly addressed by the micro (keep in mind the usb functionality isn’t bitbanged, but done in hardware for this chip).

Thanks for the reply! Shame I can’t configure it as GPIO, I’ll keep researching to see what else can be done with the USB controller.

There’s some very limited output control possible:

You can Pull up D+ or D- (to 3.3V) by selecting Highspeed or lowspeed mode and enable/disable the pullup by Attaching and DETACHing the USB controller.

But this is kind of a hack.

You’d be better off just communicating with peripherals using serial communication over USB if it’s possible to do so.

Perhaps even make a chip that can translate commands over serial into GPIO state changes (e.g. 1 bit for ‘high/low’ and 7 bits for selecting a port).

Not ideal, but probably the easiest solution.

How does one go about using serial over USB ?

For which end of the connection?

From the Arduboy end you use the Serial class.

There’s a little bit of documentation here:
https://www.arduino.cc/reference/en/language/functions/communication/serial/

Unfortunately it’s not particularly good, it leaves out the types.

To check if Serial is connected you do if(Serial).
To wait until Serial is connected you can do while(Serial).
To find out how much data is available you can do int amount = Serial.available();.
To read a byte you can do int value = Serial.read();.
To peek a byte (i.e. read it without consuming it) you can do int value = Serial.peek();.
To write a byte you can do Serial.write(value);.

You can also use all the print and println functions like you’d be able to on the Arduboy2 object,
but that’s probably not much use for GPIO.


The other endpoint would depend on what the hardware setup is.
If it’s another Arduino chip doing the reading then it’ll be the same API.

Essentially the other chip will just have to do something like:

while(true)
{
	int available = Serial.available();
	if(available > 0)
	{
		int value = Serial.read();
		// Interpret the value and decide what GPIO state to change
	}
}

(That’s a really simplified version, but it illustrates the basic idea - wait for input, handle input.)

Unfortunately the Atmega32u4 has no host support so so it can not initate communications

Yes, the other endpoint would have to host, hence the suggestion of making a ‘chip’ that can translate.
(Perhaps I should have used a word other than ‘chip’ but I can’t think of anything adequate offhand. Board? Piece of hardware? Doohicky?)

There are USB host solutions you could use (i.e. MAX3421E > https://www.hpmuseum.org/forum/thread-654.html), but I would not go that way, it is complex and prone to errors.

What you could use is the ICSP header (soldering wires to the pads in the back -> Bring your Arduboy back from the dead (burn bootloader) step 2), for example MISO and MOSI (gpio 11 and 12 of arduino respectively). Those behave as you would expect (digitalWrite, digitalRead, etc...)