linux内核配置usb虚拟串口,Linux USB虚拟串口设备
Linux內核中usb設備側驅動程序分成3個層次:UDC驅動程序、Gadget API和Gadget驅動程序。UDC驅動程序(USB控制器)直接訪問硬件,控制USB設備和主機間的底層通信,向上層提供與硬件相關操作的回調函數。Gadget API是UDC驅動程序回調函數的簡單包裝,這部分程序內核都已經寫好。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.
內核版本:3.15 ? ? ? ? ? ? ? ? ? ?硬件:ATMEL ?SAMA5D3 ? ? ? ? ? ?編譯環境:ubuntu 12.04
1、配置內核
這里之所以都選擇為模塊的形式,是為了調試方便,有些模塊,比如U盤加載時還需要提供介質,就是說加載模塊時還需要參數,否則加載不上
Device Drivers ?--->
[*] USB support ?--->
? USB Gadget Support ?--->
? USB Gadget Drivers
< > ? ? USB functions configurable through configfs
< > ? ? Gadget Zero (DEVELOPMENT)
< > ? ? Ethernet Gadget (with CDC Ethernet support)
< > ? ? Network Control Model (NCM) support
< > ? ? Gadget Filesystem
< > ? ? Function Filesystem
< > ? ? Mass Storage Gadget
< > ? ? USB Gadget Target Fabric Module
? ? Serial Gadget (with CDC ACM and CDC OBEX support)
< > ? ? Printer Gadget
保存退出,編譯內核。在drivers/usb/gadget目錄下會生成以下驅動文件
-rw-rw-r-- 1 a_tu a_tu ?9710 ?3月 ?5 15:14 ./g_serial.ko
-rw-rw-r-- 1 a_tu a_tu 49319 ?3月 ?5 15:14 ./libcomposite.ko
-rw-rw-r-- 1 a_tu a_tu 11473 ?3月 ?5 15:14 ./usb_f_acm.ko
-rw-rw-r-- 1 a_tu a_tu ?9485 ?3月 ?5 15:14 ./usb_f_obex.ko
-rw-rw-r-- 1 a_tu a_tu ?8011 ?3月 ?5 15:14 ./usb_f_serial.ko
-rw-rw-r-- 1 a_tu a_tu 15970 ?3月 ?5 15:14 ./u_serial.ko
2、操作開發板
把編譯好的內核燒入開發板,加載生成的驅動文件,注意加載順序,否則會出現錯誤。
insmod ?u_serial.ko
insmod ?libcomposite.ko
insmod ? usb_f_serial.ko
insmod ?usb_f_obex.ko
insmod ?usb_f_acm.ko
insmod ?g_serial.ko
出現內核信息如下:
[ ? 27.310000] --------file=composite.c-------usb_composite_probe------1830
[ ? 27.320000] ------------402
[ ? 27.320000] -----usb_gadget_probe_driver------411
[ ? 27.330000] -------udc_bind_to_driver-----342
[ ? 27.330000] g_serial gadget: Gadget Serial v2.4
[ ? 27.340000] g_serial gadget: g_serial ready
[ ? 18.960000] g_serial gadget: high-speed config #2: CDC ACM config
說明加載成功。
此時你打開設備管理器會發現多了一個串口設備,
ELMO GMAS(COM10)
Win7操作系統可以自動加載基于udc標準的serial驅動。
對于WinXP操作系統,要使用ACM串口,需要WindowsXP機器上有gserial.inf和usbser.sys這兩個文件,其中gserial.inf的配置如下:
[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'
安裝成功后,在設備管理器中可以看到 “Gadget Serial (COM11) '這個端口。
開發板的/dev/下會出現/dev/ttyGS0這個設備。當然如果你怕這個設備重名,可以更改這個設備節點。操作如下:
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 251 ttyGS
mknod /dev/usb_serial c 254 0
以后可以直接按照串口通訊的方式打開該節點 /dev/usb_serial,進行數據讀寫操作。
3、通信測試
開發板上執行命令 ?cat /dev/usb_serial(當然你也可以編寫程序,打開設備讀寫),這里只是簡單的測試設備通信是否正常 ,pc機上打開串口調試助手,打開com10,通過字符串輸入框發送數據。此時發現開發板沒有收到數據,而串口助手卻收到自己發送的數據。這讓我很是納悶,折騰了2天,終于睡醒了。
又測試了另一通路:開發板給pc發。echo 12345 > /dev/usb_serial
這是完全正常的,通過串口助手收到的數據來看。每次收到的數據都有換行現象,會不會是數據沒有從緩沖區中刷出來?我就換了數據格式,發送簡單文件
111111111111111111
222222222222222222
222222222222222222
444444444444444444
此時開發板收到數據
# cat /dev/usb_serial
111111111111111111
222222222222222222
222222222222222222
444444444444444444
原來真的是 "\n" 在作怪!!!,如果你的文件只有一行,且沒有換行。那么你的開發板是收不到數據的,這些數據并沒有丟失,而是存儲在串口的緩沖區中,直到遇到 "\n",才會把數據一股腦發出來。
至此測試完畢。
本人水平有限,文章僅代表個人觀點,如有錯誤,請指正!!!
總結
以上是生活随笔為你收集整理的linux内核配置usb虚拟串口,Linux USB虚拟串口设备的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux文件权限umask,linux
- 下一篇: linux 提升cpu权限,CPUlim