7

I have a whole bunch of Pi Zeros and Zero Ws. I've flashed SD cards with the latest Raspbian Jessie Lite, and I've followed the instructions at https://gist.github.com/gbaman/975e2db164b3ca2b51ae11e45e8fd40a with g_serial:

$ cat cmdline.txt
dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_serial

$ tail -n 5 config.txt
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
enable_uart=1

dtoverlay=dwc2

If I then plug the Pi into my laptop via a Micro USB OTG to USB Adapter (specifically, this one) the Pi appears to boot fine but there's no sign of any new serial port on the laptop - nothing appears in dmesg and no exciting new things in /dev/tty* or lsusb

I've tried:

  • Pi Zeros and Pi Zero Ws
  • Different host machines
  • Different OTG adapters (though all the same brand)

What have I missed? How can I debug further? (Or what might I be doing wrong?)

3 Answers 3

4

Setup for OTG Serial Gadget

Enabling the USB OTG Serial Gadget

Plug your miniSD card into your card reader and navigate to the card's /boot partition. This is the smaller of the two partitions.

Edit the cmdline.txt file and add this directly after rootwait

modules-load=dwc2,g_serial

Edit the config.txt file and add this directly after the last line in the file:

dtoverlay=dwc2

If you have a GNU/Linux host machine, you can simulate enabling the Serial Getty Service on what will be the Raspberry Pi's serial console.

sudo ln -s /lib/systemd/system/[email protected] /media/user/uuid-of-sd-card/etc/systemd/system/getty.target.wants/[email protected]

Log in to your Raspberry Pi through the OTG Serial Console using a GNU/Linux host machine

  1. Plug your modified miniSD card into your Pi.
  2. Plug in your power cable.
  3. Wait 60 seconds.
  4. Plug in your USB cable into your Raspberry Pi and your PC.
  5. Run sudo dmesg on your GNU/Linux machine and wait until you see the serial device register as something like ttyACM0
  6. Open a serial terminal program such as minicom.
  7. Change your serial device in the program to match your OTG registered Pi.
  8. Press Enter 2 or 3 times and wait a few seconds.

Success!

Raspbian GNU/Linux 9 raspberrypi ttyGS0
raspberrypi login: pi
Password: 
Linux raspberrypi 4.9.41+ #1023 Tue Aug 8 15:47:12 BST 2017 armv6l

The programs included with the Debian GNU/Linux system are free 
software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
pi@raspberrypi:~$

Enabling the RPi OTG USB Serial Console Service

In order to use the OTG Serial port as a console, you will need to enable the service on the RPi. If you have a GNU/Linux host machine, this can be done directly on the miniSD card as detailed above. However, it can also be done by booting the Raspberry Pi with keyboard and monitor attached. Then enable the serial getty console service:

sudo systemctl enable [email protected]

This last step is probably the one you are missing.

Debugging Raspberry Pi OTG Serial Gadget g_serial

On the Raspberry Pi

Connect monitor and keyboard and apply power to the Raspberry Pi.

Once booted login and drop into a root shell:

$ sudo -s

Check that the g_serial kernel module loaded:

# lsmod |grep g_serial
g_serial                3753  0
libcomposite           49619  2 g_serial,usb_f_acm

Check that the g_serial module configured properly:

# dmesg |grep g_serial
[    0.000000] Kernel command line: bcm2708_fb.fbwidth=640 bcm2708_fb.fbheight=480 bcm2708_fb.fbdepth=16 bcm2708_fb.fbswap=1 vc_mem.mem_base=0x1fa00000 vc_mem.mem_size=0x20000000  dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=PARTUUID=51c4565c-02 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules-load=dwc2,g_serial
[    4.765063] g_serial gadget: Gadget Serial v2.4
[    4.771694] g_serial gadget: g_serial ready
[    4.777739] dwc2 20980000.usb: bound driver g_serial
[    5.253329] g_serial gadget: high-speed config #2: CDC ACM config

Check that the g_serial special device file is present

# ls -l /dev/ttyGS*
crw------- 1 pi tty 245, 0 Sep  7 19:53 /dev/ttyGS0

Check that systemd started the console service on the g_serial ttyGS0

# systemctl status getty@ttyGS0
  ● [email protected] - Getty on ttyGS0
   Loaded: loaded (/lib/systemd/system/[email protected]; enabled; vendor preset: e
   Active: active (running) since Thu 2017-09-07 19:41:20 UTC; 10min ago
     Docs: man:agetty(8)
           man:systemd-getty-generator(8)
           http://0pointer.de/blog/projects/serial-console.html
 Main PID: 310 (login)
   CGroup: /system.slice/system-getty.slice/[email protected]
         ‣ 310 /bin/login --

Sep 07 19:41:20 raspberrypi systemd[1]: Started Getty on ttyGS0.

On the host GNU/Linux machine

Check that the host has registered the serial OTG device

sudo dmesg | grep cdc

[ 7887.020889] cdc_acm 1-1:2.0: ttyACM0: USB ACM device
[ 7887.021079] usbcore: registered new interface driver cdc_acm
[ 7887.021080] cdc_acm: USB Abstract Control Model driver for USB modems and ISDN adapters

Check that the host system has created the special device file for the OTG connected device

sudo ls -l /dev/ttyACM*
crw-rw---- 1 root dialout 166, 0 Nov 22 09:18 /dev/ttyACM0

Connect to the device using your favorite terminal program. I prefer minicom... However, your device has particular permissions and group assignment. If you wish to use it as a non-privileged user, you will need to make sure that your user is included in the group. In my example, the group is dialout... and my user can be added to that group using this command:

sudo usermod -aG dialout user

Hopefully this debugging and setup guide helps.

1

Try /dev/ttyACM0. My laptop is running Ubuntu, and, when plugging RPi 0 W, nothing fancy happened, but I managed to listen to it by that port: ACM0.

0
1

Check the cable you are using. It looks like you are trying to connect to the Pi from your PC with an OTG cable. You should use a regular USB-micro cable instead. Just connect the PC to the "USB" port on the Pi and don't connect anything to the "POWER" port. It will get enough power from the host.

I spent a lot of time myself debugging my config, when I was actually using a USB-micro cable that was only suitable for charging. You need a fully wired one. Test this by hooking up an other gadget (your Smartphone?) and see if the PC and that gadget see each other. I used my e-reader. Using the first cable, it just started charging. Using the second cable, the e-reader asked me wether I just wanted to charge or connect to my PC as a storage device. Needless to say, when I hooked up the second cable to my Pi Zero, it turned out my config was already working!

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.