1

i plan to make a pi zero that can dynamically switch betweeb usb slave mode and "master mode"(I don't have a proper word for this) , and all i know is how to turn it into a slave device, but can any one tell me 1) can pi zero recieve signals from gpio and take action? 2) how can i reverse the pi into "master mode" programmatically

cuz i googled a lot and all i have got is tutorials on how to make the pi a slave device, but none on how to reverse that.

1 Answer 1

1

1) can pi zero recieve signals from gpio and take action?

Yes, you can find plenty of resources on Pi Exchange and other sites for using the RPi.GPIO library.

dynamically switch betweeb usb slave mode and "master mode"(I don't have a proper word for this)

2) how can i reverse the pi into "master mode" programmatically

In the context of USB, master vs slave mode is also referred to as host vs device (or peripheral) mode. In the dwc2 device tree overlay the OTG port dr_mode is set to "otg" by default. You could change that to "host" or "peripheral" with an alternate overlay, but not without rebooting.

For your intent, you probably want to stay in "otg" mode then enable or disable gadget drivers as needed. For example:

sudo modprobe g_zero      # enable a sample gadget

sudo modprobe -r g_zero   # disconnect

Or if you created a custom gadget via configfs, you can disable it with the following sequence:

sudo su
echo "" > /sys/kernel/config/usb_gadget/<your gadget name>/UDC
rm -rf /sys/kernel/config/usb_gadget/<your gadget name>
modprobe -r libcomposite

In both scenarios you might run into errors attempting to unload the module if it's busy. Sometimes this can be addressed by terminating an associated user process, unloading another module that was implicitly loaded (usb_f_<something>), or other approaches found by trial and error.

2
  • sorry that i may not be clear enough, but what i want is to disable the otg mode on the pi and reverse it back to a compute device, or a host. and i know that the pi can recieve signals from gpio, i just dun know is it still able to recieve gpio signals while acting as a otg device(e.g. a flash drive in my case)
    – mine storm
    Nov 9, 2017 at 6:19
  • You can. Acting as an OTG device isn't going to disable your GPIO pins. I think your question made sense.
    – jdonald
    Nov 9, 2017 at 6:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.