linux内核配置usb虚拟串口,Linux USB虚拟串口设备
Linux內(nèi)核中usb設(shè)備側(cè)驅(qū)動(dòng)程序分成3個(gè)層次:UDC驅(qū)動(dòng)程序、Gadget API和Gadget驅(qū)動(dòng)程序。UDC驅(qū)動(dòng)程序(USB控制器)直接訪問硬件,控制USB設(shè)備和主機(jī)間的底層通信,向上層提供與硬件相關(guān)操作的回調(diào)函數(shù)。Gadget API是UDC驅(qū)動(dòng)程序回調(diào)函數(shù)的簡(jiǎn)單包裝,這部分程序內(nèi)核都已經(jīng)寫好。Gadget驅(qū)動(dòng)程序具體控制USB設(shè)備功能的實(shí)現(xiàn),使設(shè)備表現(xiàn)出“U盤”、“虛擬串口”等特性。
簡(jiǎn)單看個(gè)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.
內(nèi)核版本:3.15 ? ? ? ? ? ? ? ? ? ?硬件:ATMEL ?SAMA5D3 ? ? ? ? ? ?編譯環(huán)境:ubuntu 12.04
1、配置內(nèi)核
這里之所以都選擇為模塊的形式,是為了調(diào)試方便,有些模塊,比如U盤加載時(shí)還需要提供介質(zhì),就是說加載模塊時(shí)還需要參數(shù),否則加載不上
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
保存退出,編譯內(nèi)核。在drivers/usb/gadget目錄下會(huì)生成以下驅(qū)動(dòng)文件
-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、操作開發(fā)板
把編譯好的內(nèi)核燒入開發(fā)板,加載生成的驅(qū)動(dòng)文件,注意加載順序,否則會(huì)出現(xiàn)錯(cuò)誤。
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
出現(xiàn)內(nèi)核信息如下:
[ ? 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
說明加載成功。
此時(shí)你打開設(shè)備管理器會(huì)發(fā)現(xiàn)多了一個(gè)串口設(shè)備,
ELMO GMAS(COM10)
Win7操作系統(tǒng)可以自動(dòng)加載基于udc標(biāo)準(zhǔn)的serial驅(qū)動(dòng)。
對(duì)于WinXP操作系統(tǒng),要使用ACM串口,需要WindowsXP機(jī)器上有g(shù)serial.inf和usbser.sys這兩個(gè)文件,其中g(shù)serial.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'
安裝成功后,在設(shè)備管理器中可以看到 “Gadget Serial (COM11) '這個(gè)端口。
開發(fā)板的/dev/下會(huì)出現(xiàn)/dev/ttyGS0這個(gè)設(shè)備。當(dāng)然如果你怕這個(gè)設(shè)備重名,可以更改這個(gè)設(shè)備節(jié)點(diǎn)。操作如下:
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
以后可以直接按照串口通訊的方式打開該節(jié)點(diǎn) /dev/usb_serial,進(jìn)行數(shù)據(jù)讀寫操作。
3、通信測(cè)試
開發(fā)板上執(zhí)行命令 ?cat /dev/usb_serial(當(dāng)然你也可以編寫程序,打開設(shè)備讀寫),這里只是簡(jiǎn)單的測(cè)試設(shè)備通信是否正常 ,pc機(jī)上打開串口調(diào)試助手,打開com10,通過字符串輸入框發(fā)送數(shù)據(jù)。此時(shí)發(fā)現(xiàn)開發(fā)板沒有收到數(shù)據(jù),而串口助手卻收到自己發(fā)送的數(shù)據(jù)。這讓我很是納悶,折騰了2天,終于睡醒了。
又測(cè)試了另一通路:開發(fā)板給pc發(fā)。echo 12345 > /dev/usb_serial
這是完全正常的,通過串口助手收到的數(shù)據(jù)來看。每次收到的數(shù)據(jù)都有換行現(xiàn)象,會(huì)不會(huì)是數(shù)據(jù)沒有從緩沖區(qū)中刷出來?我就換了數(shù)據(jù)格式,發(fā)送簡(jiǎn)單文件
111111111111111111
222222222222222222
222222222222222222
444444444444444444
此時(shí)開發(fā)板收到數(shù)據(jù)
# cat /dev/usb_serial
111111111111111111
222222222222222222
222222222222222222
444444444444444444
原來真的是 "\n" 在作怪!!!,如果你的文件只有一行,且沒有換行。那么你的開發(fā)板是收不到數(shù)據(jù)的,這些數(shù)據(jù)并沒有丟失,而是存儲(chǔ)在串口的緩沖區(qū)中,直到遇到 "\n",才會(huì)把數(shù)據(jù)一股腦發(fā)出來。
至此測(cè)試完畢。
本人水平有限,文章僅代表個(gè)人觀點(diǎn),如有錯(cuò)誤,請(qǐng)指正!!!
總結(jié)
以上是生活随笔為你收集整理的linux内核配置usb虚拟串口,Linux USB虚拟串口设备的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux文件权限umask,linux
- 下一篇: linux 提升cpu权限,CPUlim