设置usb0为host模式,支持u盘的热插拔。设置usb1为otg模式,支持设备作为u盘连接到电脑。
我对usb没有研究,但是linux内核对usb的支持非常强大,不需要修改代码,只需配置就可以实现。
首先,查看并设置usb的设备树,其中具体设置在am33xx.dtsi中。
am33xx.dtsi
usb: usb@47400000 {
compatible = "ti,am33xx-usb";
reg = <0x47400000 0x1000>;
ranges;
#address-cells = <1>;
#size-cells = <1>;
ti,hwmods = "usb_otg_hs";
status = "disabled";
usb_ctrl_mod: control@44e10000 {
compatible = "ti,am335x-usb-ctrl-module";
reg = <0x44e10620 0x10
0x44e10648 0x4>;
reg-names = "phy_ctrl", "wakeup";
status = "disabled";
};
usb0_phy: usb-phy@47401300 {
compatible = "ti,am335x-usb-phy";
reg = <0x47401300 0x100>;
reg-names = "phy";
status = "disabled";
ti,ctrl_mod = <&usb_ctrl_mod>;
};
usb0: usb@47401000 {
compatible = "ti,musb-am33xx";
status = "disabled";
reg = <0x47401400 0x400
0x47401000 0x200>;
reg-names = "mc", "control";
interrupts = <18>;
interrupt-names = "mc";
dr_mode = "otg";
mentor,multipoint = <1>;
mentor,num-eps = <16>;
mentor,ram-bits = <12>;
mentor,power = <500>;
phys = <&usb0_phy>;
dmas = <&cppi41dma 0 0 &cppi41dma 1 0
&cppi41dma 2 0 &cppi41dma 3 0
&cppi41dma 4 0 &cppi41dma 5 0
&cppi41dma 6 0 &cppi41dma 7 0
&cppi41dma 8 0 &cppi41dma 9 0
&cppi41dma 10 0 &cppi41dma 11 0
&cppi41dma 12 0 &cppi41dma 13 0
&cppi41dma 14 0 &cppi41dma 0 1
&cppi41dma 1 1 &cppi41dma 2 1
&cppi41dma 3 1 &cppi41dma 4 1
&cppi41dma 5 1 &cppi41dma 6 1
&cppi41dma 7 1 &cppi41dma 8 1
&cppi41dma 9 1 &cppi41dma 10 1
&cppi41dma 11 1 &cppi41dma 12 1
&cppi41dma 13 1 &cppi41dma 14 1>;
dma-names =
"rx1", "rx2", "rx3", "rx4", "rx5", "rx6", "rx7",
"rx8", "rx9", "rx10", "rx11", "rx12", "rx13",
"rx14", "rx15",
"tx1", "tx2", "tx3", "tx4", "tx5", "tx6", "tx7",
"tx8", "tx9", "tx10", "tx11", "tx12", "tx13",
"tx14", "tx15";
};
usb1_phy: usb-phy@47401b00 {
compatible = "ti,am335x-usb-phy";
reg = <0x47401b00 0x100>;
reg-names = "phy";
status = "disabled";
ti,ctrl_mod = <&usb_ctrl_mod>;
};
usb1: usb@47401800 {
compatible = "ti,musb-am33xx";
status = "disabled";
reg = <0x47401c00 0x400
0x47401800 0x200>;
reg-names = "mc", "control";
interrupts = <19>;
interrupt-names = "mc";
dr_mode = "otg";
mentor,multipoint = <1>;
mentor,num-eps = <16>;
mentor,ram-bits = <12>;
mentor,power = <500>;
phys = <&usb1_phy>;
dmas = <&cppi41dma 15 0 &cppi41dma 16 0
&cppi41dma 17 0 &cppi41dma 18 0
&cppi41dma 19 0 &cppi41dma 20 0
&cppi41dma 21 0 &cppi41dma 22 0
&cppi41dma 23 0 &cppi41dma 24 0
&cppi41dma 25 0 &cppi41dma 26 0
&cppi41dma 27 0 &cppi41dma 28 0
&cppi41dma 29 0 &cppi41dma 15 1
&cppi41dma 16 1 &cppi41dma 17 1
&cppi41dma 18 1 &cppi41dma 19 1
&cppi41dma 20 1 &cppi41dma 21 1
&cppi41dma 22 1 &cppi41dma 23 1
&cppi41dma 24 1 &cppi41dma 25 1
&cppi41dma 26 1 &cppi41dma 27 1
&cppi41dma 28 1 &cppi41dma 29 1>;
dma-names =
"rx1", "rx2", "rx3", "rx4", "rx5", "rx6", "rx7",
"rx8", "rx9", "rx10", "rx11", "rx12", "rx13",
"rx14", "rx15",
"tx1", "tx2", "tx3", "tx4", "tx5", "tx6", "tx7",
"tx8", "tx9", "tx10", "tx11", "tx12", "tx13",
"tx14", "tx15";
};
am335x-evm.dts
&usb {
status = "okay";
control@44e10000 {
status = "okay";
};
usb-phy@47401300 {
status = "okay";
};
usb-phy@47401b00 {
status = "okay";
};
usb@47401000 {
status = "okay";
dr_mode = "host";
};
usb@47401800 {
status = "okay";
};
dma-controller@07402000 {
status = "okay";
};
};
我们需要做的就是使能usb并且设置正确的模式,默认模式是otg。am33xx.dtsi中关于usb的设置不需要任何修改。当然,还需要验证这些对应的驱动是否配置上,方法如之前所讲。
Device Drivers --->
[*] USB support --->
<*> USB Gadget Support --->
<M> USB Gadget Drivers
etc/mdev.conf增加usb热插拔的支持
#hotplug usb disk
sd[a-z][0-9] 0:0 666 @(/etc/hotplug/insert.sh $MDEV $SUBSYSTEM)
sd[a-z] 0:0 666 $(/etc/hotplug/remove.sh $MDEV $SUBSYSTEM)
mmcblk[0-9]p[0-9] 0:0 666 @(/etc/hotplug/insert.sh $MDEV $SUBSYSTEM)
mmcblk[0-9] 0:0 666 $(/etc/hotplug/remove.sh $MDEV $SUBSYSTEM)
创建etc/hotplug目录,并在该目录下创建insert.sh和remove.sh脚本
insert.sh
#! /bin/sh
if [ -n "$1" ] ; then
if [ -b /dev/$1 ]; then
if [ ! -d /media ]; then
mkdir -p /media
fi
if [ ! -d /media/$1 ]; then
mkdir -p /media/$1
fi
mount /dev/$1 /media/$1
fi
fi
echo "AAAA:insert:" > /dev/console
echo $1 > /dev/console
remove.sh
#! /bin/sh
MOUNTS=$(mount | grep $1 | cut -d' ' -f3)
umount $MOUNTS
rm -rf $MOUNTS
echo "AAAA:remove:$MOUNTS" > /dev/console
echo $1 > /dev/console
另外,还需在etc/init.d目录下的初始化脚本中加入
/bin/echo /sbin/mdev > /proc/sys/kernel/hotplug
mdev -s
Device Drivers --->
[*] USB support --->
<*> USB Gadget Support --->
<M> Mass Storage Gadget
制作一个10M的镜像
dd if=/dev/zero of=disk.img bs=1M count=10
格式化为FAT32模式
mkdosfs disk.img
把该disk.img拷贝到根文件系统的opt目录下,在etc目录下的初始化脚本中增加
mount -t vfat -o sync /opt/disk.img /mnt
/sbin/modprobe g_mass_storage file=/dev/loop0 removable=1
如此设置后,开机就可以把设备连接到电脑,当做u盘来使用,大小为10M。