注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

时间记录器

记录我的Linux、Android学习之路

 
 
 

日志

 
 

Linux gadget驱动应用 之 USB串口  

2012-12-02 16:02:09|  分类: USB相关 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
Linux内核中usb设备侧驱动程序分成3个层次:UDC驱动程序、Gadget APIGadget驱动程序。UDC驱动程序(USB控制器)直接访问硬件,控制USB设备和主机间的底层通信,向上层提供与硬件相关操作的回调函数。Gadget APIUDC驱动程序回调函数的简单包装,这部分程序内核都已经写好。Gadget驱动程序具体控制USB设备功能的实现,使设备表现出“U盘”、“虚拟串口”等特性。

   简单看个usb 虚拟串口例子

Overview
--------
The gadget serial driver is a Linux USB gadget driver, a USB device
side driver.  It runs on a Linux system that has USB device side
hardware; for example, a PDA, an embedded Linux system, or a PC
with a USB development card.
 
The gadget serial driver talks over USB to either a CDC ACM driver
or a generic USB serial driver running on a host PC.
 
   Host
   --------------------------------------
  | Host-Side   CDC ACM       USB Host   |
  | Operating |   or        | Controller |   USB
  | System    | Generic USB | Driver     |--------
  | (Linux or | Serial      | and        |        |
  | Windows)    Driver        USB Stack  |        |
   --------------------------------------         |
                                                  |
                                                  |
                                                  |
   Gadget                                         |
   --------------------------------------         |
  | Gadget                   USB Periph. |        |
  | Device-Side |  Gadget  | Controller  |        |
  | Linux       |  Serial  | Driver      |--------
  | Operating   |  Driver  | and         |
  | System                   USB Stack   |
   --------------------------------------

On the device-side Linux system, the gadget serial driver looks
like a serial device.
 
On the host-side system, the gadget serial device looks like a
CDC ACM compliant class device or a simple vendor specific device
with bulk in and bulk out endpoints, and it is treated similarly
to other serial devices.
 
The host side driver can potentially be any ACM compliant driver
or any driver that can talk to a device with a simple bulk in/out
interface.  Gadget serial has been tested with the Linux ACM driver,
the Windows usbser.sys ACM driver, and the Linux USB generic serial
driver.
 
With the gadget serial driver and the host side ACM or generic
serial driver running, you should be able to communicate between
the host and the gadget side systems as if they were connected by a
serial cable.
 
The gadget serial driver only provides simple unreliable data
communication.  It does not yet handle flow control or many other
features of normal serial devices.

 

usb-serial功能体验

硬件:at91sam9263

内核:2.6.27

 

<*>   USB Gadget Support  --->
            USB Peripheral Controller (AT91 USB Device Port)  --->
             AT91 USB Device Port
    <M>   USB Gadget Drivers

    <M>       Gadget Zero(DEVELOPMENT) 
    <M>       Ethernet Gadget (with CDC Ethernet support)
    [*]         RNDIS support

    <M>       Gadget Filesystem(EXPERIMENTAL) 

    <M>       File-backed Storage Gadget 

    <M>       Serial Gadget 

    <M>       Printer Gadget 

    <M>       CDC Composite Device(Ethernet and ACM) 

  

简单解释下

  Gadget Zero, 类似于 dummy hcd, 该驱动用于测试 udc 驱动。它会帮助您通过 USB-IF 测试。

  Ethernet Gadget, 该驱动模拟以太网网口,它支持多种运行方式:

  CDC Ethernet: usb 规范规定的 Communications Device Class “Ethernet Model” protocol。

  RNDIS: 微软公司对 CDC Ethernet 的变种实现。

   Gadget Filesystem:提供一个基于API的文件系统,可以在用户空间访问

  File-backed Storage Gadget最常见的 U 盘功能实现。

  Serial Gadget:虚拟串口

   Printer Gadget:打印机功能

 

这里之所以都选择为模块的形式,是为了调试方便,有些模块,比如U盘加载时还需要提供介质,就是说加载模块时还需要参数,否则加载不上

编译内核 make uImage

编译模块 make modules

然后把

# insmod g_serial.ko
g_serial gadget: Gadget Serial v2.4
g_serial gadget: g_serial ready

通过连接板子和电脑

板子上提示

# g_serial gadget: full speed config #2: CDC ACM config

 

说明开发板上已经加载成功。

电脑上同时也会出现提示新硬件

 

驱动Inf文件如下

The "gserial.inf" file is given here.
 
-------------------- CUT HERE --------------------
[Version]
Signature="$Windows NT$"
Class=Ports
ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}
Provider=%LINUX%
DriverVer=08/17/2004,0.0.2.0
; Copyright (C) 2004 Al Borchers (alborchers@steinerpoint.com)
 
[Manufacturer]
%LINUX%=GSerialDeviceList
 
[GSerialDeviceList]
%GSERIAL%=GSerialInstall, USB\VID_0525&PID_A4A7
 
[DestinationDirs]
DefaultDestDir=10,System32\Drivers
 
[GSerialInstall]
CopyFiles=GSerialCopyFiles
AddReg=GSerialAddReg
 
[GSerialCopyFiles]
usbser.sys
 
[GSerialAddReg]
HKR,,DevLoader,,*ntkern
HKR,,NTMPDriver,,usbser.sys
HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"

[GSerialInstall.Services]
AddService = usbser,0x0002,GSerialService
 
[GSerialService]
DisplayName = %GSERIAL_DISPLAY_NAME%
ServiceType = 1                  ; SERVICE_KERNEL_DRIVER
StartType = 3                    ; SERVICE_DEMAND_START
ErrorControl = 1                 ; SERVICE_ERROR_NORMAL
ServiceBinary = %10%\System32\Drivers\usbser.sys
LoadOrderGroup = Base
 
[Strings]
LINUX = "Linux"
GSERIAL = "Gadget Serial"
GSERIAL_DISPLAY_NAME = "USB Gadget Serial Driver"
-------------------- CUT HERE --------------------
 
The "usbser.sys" file comes with various versions of Windows.
For example, it can be found on Windows XP typically in
 
  C:\WINDOWS\Driver Cache\i386\driver.cab

 

安装成功后就会发现电脑多了一个串口。操作这个串口就可以给板子读写数据了。

 

测试方法:

先查看

# cat /proc/devices
Character devices:
  1 mem
  2 pty
  3 ttyp
  4 /dev/vc/0
  4 tty
  4 ttyS
  5 /dev/tty
  5 /dev/console
  5 /dev/ptmx
  7 vcs
 10 misc
 13 input
 90 mtd
128 ptm
136 pts
153 spi
254 ttyGS
 
Block devices:
  1 ramdisk
  7 loop
  8 sd
 31 mtdblock
 65 sd
 66 sd
 67 sd
 68 sd
 69 sd
 70 sd
 71 sd
128 sd
129 sd
130 sd
131 sd
132 sd
133 sd
134 sd
135 sd
179 mmc
#

然后

# mknod /dev/usb_serial c 254 0

然后

# cat /dev/usb_serial 

在PC上给串口发送数据,就会显示在板子上。

# echo 12345 > /dev/usb_serial 

会发现串口收到字符"12345"

 

至此测试完毕

  评论这张
 
阅读(4149)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017