How to configure SP7021 board as an USB mass storage device

After getting source files (after kernel 4.19) from git server of SP7021, follow the steps shown below.

1. Configure “USB_CONFIGFS” in kernel menuconfig

Device Drivers → USB support → USB Gadget Support → USB Gadget functions configurable through

configfs <M> → Mass storage [ * ]

After compiling, it will procude libcomposite.ko at linux/kernel/drivers/usb/gadget and

usb_f_mass_storage.ko at linux/kernel/drivers/usb/gadget/function.

 

2. Configure “USB Gadget precomposed configurations” in kernel menuconfig

Device Drivers → USB support → USB Gadget Support → USB Gadget precomposed configurations

<M> → Mass Storage Gadget <M>

After compiling, it will procude g_mass_storage.ko at linux/kernel/drivers/usb/gadget/legacy.

3. Configure “USB_GADGET_SP7021” in kernel menuconfig

Device Drivers → USB support → USB Gadget Support → USB Peripheral Controllers → Sunplus SP7021

Gemini USB Device Controller [ * ]

4. Configure “CONFIGFS_FS” in kernel menuconfig

File systems → Pseudo filesystems → Userspace-driven configuration filesystem <Module>

After compiling, it will produce configfs.ko at linux/kernel/fs/configfs.

5. Run “make all” to build the Linux image, update it to SP7021 and boot (eMMC).

6. Configure attributes and set up in user space

echo 0xff > /sys/module/sunplus_udc/parameters/dmsg

insmod /lib/modules/`uname - r`/kernel/fs/configfs/configfs.ko

insmod /lib/modules/`uname - r`/kernel/drivers/usb/gadget/libcomposite.ko

insmod /lib/modules/`uname - r`/kernel/drivers/usb/gadget/function/usb_f_mass_storage.ko

insmod /lib/modules/`uname - r`/kernel/drivers/usb/gadget/legacy/g_mass_storage.ko file=/

dev/mmcblk08p removable=1 ro=0

mount -t configfs none /sys/kernel/config

mkdir /sys/kernel/config/usb_gadget/g1

cd /sys/kernel/config/usb_gadget/g1

echo “64” > bMaxPacketSize0

echo “0x200” > bcdUSB

mkdir configs/c.1

echo 120 > configs/c.1/MaxPower

mkdir functions/mass_storage.usb0

ln -s functions/mass_storage.usb0 configs/c.1

echo “9c102800.usb” > UDC (9c102800.usb is at /sys/class/udc)

echo d > /sys/devices/platform/soc@B/9c102800.usb/udc_ctrl

7. Connect SP7021 DEMO board (via micro USB0) to an Ubuntu PC

The mass storage device of 3.8GB Volumn will show up. It is the root directory of SP7021.