Using RPi4 B as USB slave and master

I want to use Raspberry Pi 4B as a USB slave and master at the same time. It should connect to a Windows machine via any USB ports OR USB-C port and appear as a Serial device to the Windows machine while be connected to a different USB port to another device and read from it. I have tried it without BalenaOS and works fine using the steps here: Serial Gadget | Turning your Raspberry Pi Zero into a USB Gadget | Adafruit Learning System

I can’t seem to find any documentation about it.

How can I achieve this with Balena OS?

Hello @g_sarthak_kelapure thanks for your message and welcome to the balena community?

First of all, what did you try? Did you configure the dtoverlays in balenaCloud? What errors did you get when testing?

Thanks!

I have tried updating the dtoverlays but it doesn’t show up as a serial port on Windows/Linux machine.

I had to follow steps in here do a manual run of

modprobe -a dwc2 g_serial --first-time

which seems like should be done with configuration options on Balena Cloud

Note: my dtoverlays look like

"vc4-kms-v3d","dwc2","dr_mode=peripheral"

@g_sarthak_kelapure could you please share the definition of the service on the docker-compose or Dockerfile template?

Thanks!

The Dockerfile just called a script startup.sh which runs the

modprobe -a dwc2 g_serial --first-time

command as above.

Nothing different than what has been mentioned in the above forum link.

Additionally @mpous I also see that the modprobe command cannot be found in my container. I can run it in host and it works fine but cannot run from my container as below

interface:
    build: ./pos-interface
    privileged: true
    restart: always
    network_mode: host
    labels:
      io.balena.features.dbus: '1'
      io.balena.features.firmware: '1'
      io.balena.features.kernel-modules: '1'
      io.balena.features.supervisor-api: '1'
      io.balena.features.balena-api: '1'
    volumes:
      - 'resin-data:/data'
    environment:
      - LOG_LEVEL=DEBUG
    depends_on:
      - broker

comes up with error as below
/startup.sh: 11: modprobe: not found

@mpous any update on this?
Sorry for the trouble.

Hello Sarthak,

The steps you’re following from the guide are correct. As far as running modprobe in your container, you’ll need to install it before you can use it inside your application. Even though it’s present in the host OS, the container is isolated from that filesystem.

For example, in an Alpine based container image, you’d run apk add --update kmod. This could also be added as a RUN instruction to your Dockerfile.

Let us know if you run into any more trouble.

1 Like

Thank you for letting me know. It hasn’t worked

my docker-compose.yml:

machine-init:
    privileged: true
    restart: "no"
    tty: true
    build:  ./machine-init

Dockerfile as below

FROM alpine:latest

# Install the modprobe utility
RUN apk add --no-cache kmod

# Copy the startup script
COPY startup.sh ./startup.sh

RUN chmod +x ./startup.sh

# Run the startup script
CMD ["./startup.sh"]

startup.sh as below:

#!/bin/sh

echo "Setting up USB connection"
# Execute the modprobe command
modprobe -a dwc2 g_serial --first-time

if [ -c "/dev/ttyGS0" ]; then
  echo "modprobe command success, /dev/ttyGS0 exists"
else
  echo "modprobe failed"
fi

# Exit the container
exit 0

Fails with the errors

modprobe: WARNING: Module dwc2 not found in directory /lib/modules/5.15.92-v8
modprobe: WARNING: Module g_serial not found in directory /lib/modules/5.15.92-v8

Could you try adding the io.balena.features.kernel-modules label, as documented here?