USB Gadget iMX6UL开发板模拟U盘

iMX6UL开发板模拟U盘

参考资料:

  • 内核源码内:/{内核目录}/Documentation/usb/目录下
    gadget-testing.txt mass-storage.txt 文件
  • 网址:Backing Storage for the Mass Storage Gadget

步骤如下:

1、配置内核:

# make menuconfig
Device Drivers 
        -->USB support 
           -->USB Gadget Support 
               USB Gadget Drivers 
               USB functions configurable through configfs 
                 [*]       Mass storage 
                Gadget Zero (DEVELOPMENT  
           [*]     HNP Test Device  
                Mass Storage Gadget                                                                                              

保存配置,编译内核: make zImage
编译 内核模块: make modules
烧写镜像。。。。。

2、创建备份存储文件

#mkdir data
#cd data
#touch backing_file
root@qy_mx6ul:/data# dd bs=1M count=64 if=/dev/zero of=/data/backing_file      
64+0 records in
64+0 records out
67108864 bytes (67 MB) copied, 1.55831 s, 43.1 MB/s
root@qy_mx6ul:/data# 

这一步相当于创建一个磁盘驱动器。
接下来给磁盘驱动器分区:

root@qy_mx6ul:/data# fdisk backing_file 
Device contains neither a valid DOS partition table, nor Sun, SGI, OSF or GPT disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that the previous content
won't be recoverable.

Command (m for help): 
#增加分区
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): Value is out of range
Partition number (1-4): 1
First cylinder (1-8, default 1): Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-8, default 8): Using default value 8

Command (m for help): 
#查看已创建的分区
Command (m for help): p

Disk backing_file: 67 MB, 67108864 bytes
255 heads, 63 sectors/track, 8 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

       Device Boot      Start         End      Blocks  Id System
backing_file1               1           8       64228+ 83 Linux
#改分区类型
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): b
Changed system type of partition 1 to b (Win95 FAT32)
#把分区表写入硬盘并退出;
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table
fdisk: WARNING: rereading partition table failed, kernel still uses old table: Inappropriate ioctl for device

3、加载驱动模块

#首先加载相关模块
root@mx6ul:/linux-imx-4.1.15-r0/fs/configfs# ls | grep .ko     
configfs.ko
/linux-imx-4.1.15-r0/fs/configfs# insmod configfs.ko
/linux-imx-4.1.15-r0/fs/configfs# lsmod
Module                  Size  Used by
configfs               23785  0 
root@qy_mx6ul:/linux-imx-4.1.15-r0# cd drivers/usb/gadget/
root@qy_mx6ul:/linux-imx/drivers/usb/gadget# ls | grep .ko
libcomposite.ko
root@qy_mx6ul:/linux-imx/drivers/usb/gadget# insmod libcomposite.ko 
root@qy_mx6ul:/linux-imx/drivers/usb/gadget# lsmod
Module                  Size  Used by
libcomposite           47779  0 
configfs               23785  2 libcomposite
root@qy_mx6ul:/linux-imx/drivers/usb/gadget# cd function
root@qy_mx6ul:/linux-imx/drivers/usb/gadget/function# ls | grep .ko
u_ether.ko
u_serial.ko
usb_f_acm.ko
usb_f_ecm.ko
usb_f_ecm_subset.ko
usb_f_fs.ko
usb_f_mass_storage.ko
usb_f_ncm.ko
usb_f_obex.ko
usb_f_rndis.ko
usb_f_serial.ko
usb_f_ss_lb.ko
root@qy_mx6ul:/linux-imx/drivers/usb/gadget/function# insmod usb_f_mass_storage.ko
root@qy_mx6ul:/linux-imx/drivers/usb/gadget/function# cd ..
root@qy_mx6ul:/linux-imx/drivers/usb/gadget# cd legacy/
root@qy_mx6ul:/linux-imx/drivers/usb/gadget/legacy# ls | grep .ko
g_ether.ko
g_mass_storage.ko
g_ncm.ko
g_serial.ko
g_zero.ko
gadgetfs.ko
root@qy_mx6ul:/linux-imx/drivers/usb/gadget/legacy# modinfo g_mass_storage.ko     
。。。
depends:        usb_f_mass_storage,libcomposite
#因为模块有依赖关系,所以上面的步骤就是挨个添加所依赖的模块
root@qy_mx6ul:/linux-imx/drivers/usb/gadget/legacy# insmod g_mass_storage.ko file=/data/backing_file 
#将开发板连接到电脑,此时电脑会识别出一个磁盘
#到此成功!!!

最后:

上面的步骤参考的是文章开头 所列的文章,如果不清楚可以去网站查看。
用到的命令:
* dd
* fdisk
* insmod
* modinfo

你可能感兴趣的:(嵌入式)