攻城獅分享丨AM335x平臺(tái)如何通過udev固定4G模塊串口號(hào)
本文以飛凌嵌入式 AM335x平臺(tái)為例進(jìn)行講解。在使用 4G模塊的時(shí)候,在4G信號(hào)不太理想的環(huán)境下需要檢測(cè)網(wǎng)絡(luò),當(dāng)設(shè)備斷網(wǎng)后重啟4G模塊重新?lián)芴?hào)聯(lián)網(wǎng)。但系統(tǒng)長(zhǎng)期運(yùn)行后,重啟模塊會(huì)導(dǎo)致在/dev 目錄下的4G模塊生成的設(shè)備節(jié)點(diǎn)ttyusb序號(hào)改變,導(dǎo)致應(yīng)用程序撥號(hào)時(shí)出錯(cuò)。我們可以修改 udev規(guī)則文件解決這個(gè)問題。
udev 的規(guī)則文件里定義了很多變量,如KERNELS ,SUBSYSTEMS ,DRIVERS 等等,我們可以用這些變量中的幾個(gè)來確定我們的設(shè)備,并修改我們其中的NAME 和SYMLINK 變量,這樣我們就可以改變我們的設(shè)備在/dev 目錄下的名字和鏈接了。
使用udevadm info命令可以查詢udev數(shù)據(jù)庫中的設(shè)備信息。也可以從sysfs文件系統(tǒng)中查詢到設(shè)備的屬性以輔助創(chuàng)建udev規(guī)則。
udevadm info -q path -n /dev/ttyUSB0 可以查看ttyUSB0設(shè)備的系統(tǒng)設(shè)備路徑
root@ok335x:~# udevadm info -q path -n /dev/ttyUSB0
/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2/1-1.2:2.2/ttyUSB0/tty/ttyUSB0
udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB0) 查詢/dev/ttyUSB0設(shè)備,可以打印出設(shè)備總線的所有位置的父子關(guān)系,如下所示:
root@am335x:~# udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB4)
Udevadm info starts with the device specified by the devpath and then
walks up the chain of parent devices. It prints for every device
found, all possible attributes in the udev rules key format.
A rule to match, can be composed by the attributes of the device
and the attributes from one single parent device.
looking at device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2/1-1.2:2.2/ttyUSB0':
KERNEL=="ttyUSB0"
SUBSYSTEM=="usb-serial"
DRIVER=="option1"
ATTR{port_number}=="0"
looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2/1-1.2:2.2':
KERNELS=="1-1.2:2.2"
SUBSYSTEMS=="usb"
DRIVERS=="option"
ATTRS{bInterfaceNumber}=="02"
ATTRS{bAlternateSetting}==" 0"
ATTRS{bNumEndpoints}=="03"
ATTRS{bInterfaceClass}=="ff"
ATTRS{bInterfaceSubClass}=="06"
ATTRS{bInterfaceProtocol}=="10"
ATTRS{modalias}=="usb:v12D1p15C1d0102dc00dsc00dpFFicFFisc06ip10"
ATTRS{supports_autosuspend}=="1"
ATTRS{interface}=="Huawei Mobile Connect - Modem"
looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1/1-1.2':
KERNELS=="1-1.2"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{configuration}==""
ATTRS{bNumInterfaces}==" 7"
ATTRS{bConfigurationValue}=="2"
ATTRS{bmAttributes}=="a0"
ATTRS{bMaxPower}==" 2mA"
ATTRS{urbnum}=="26"
ATTRS{idVendor}=="12d1"
ATTRS{idProduct}=="15c1"
ATTRS{bcdDevice}=="0102"
ATTRS{bDeviceClass}=="00"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="ff"
ATTRS{bNumConfigurations}=="3"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{speed}=="480"
ATTRS{busnum}=="1"
ATTRS{devnum}=="6"
ATTRS{devpath}=="1.2"
ATTRS{version}==" 2.00"
ATTRS{maxchild}=="0"
ATTRS{quirks}=="0x0"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{authorized}=="1"
ATTRS{manufacturer}=="Huawei Technologies Co., Ltd."
ATTRS{product}=="HUAWEI Mobile V7R11"
ATTRS{serial}=="0123456789ABCDEF"
looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1/1-1':
KERNELS=="1-1"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{configuration}==""
ATTRS{bNumInterfaces}==" 1"
ATTRS{bConfigurationValue}=="1"
ATTRS{bmAttributes}=="e0"
ATTRS{bMaxPower}==" 2mA"
ATTRS{urbnum}=="100"
ATTRS{idVendor}=="0424"
ATTRS{idProduct}=="2514"
ATTRS{bcdDevice}=="0bb3"
ATTRS{bDeviceClass}=="09"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="02"
ATTRS{bNumConfigurations}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{speed}=="480"
ATTRS{busnum}=="1"
ATTRS{devnum}=="2"
ATTRS{devpath}=="1"
ATTRS{version}==" 2.00"
ATTRS{maxchild}=="4"
ATTRS{quirks}=="0x0"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{authorized}=="1"
looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1/usb1':
KERNELS=="usb1"
SUBSYSTEMS=="usb"
DRIVERS=="usb"
ATTRS{configuration}==""
ATTRS{bNumInterfaces}==" 1"
ATTRS{bConfigurationValue}=="1"
ATTRS{bmAttributes}=="e0"
ATTRS{bMaxPower}==" 0mA"
ATTRS{urbnum}=="26"
ATTRS{idVendor}=="1d6b"
ATTRS{idProduct}=="0002"
ATTRS{bcdDevice}=="0302"
ATTRS{bDeviceClass}=="09"
ATTRS{bDeviceSubClass}=="00"
ATTRS{bDeviceProtocol}=="01"
ATTRS{bNumConfigurations}=="1"
ATTRS{bMaxPacketSize0}=="64"
ATTRS{speed}=="480"
ATTRS{busnum}=="1"
ATTRS{devnum}=="1"
ATTRS{devpath}=="0"
ATTRS{version}==" 2.00"
ATTRS{maxchild}=="1"
ATTRS{quirks}=="0x0"
ATTRS{avoid_reset_quirk}=="0"
ATTRS{authorized}=="1"
ATTRS{manufacturer}=="Linux 3.2.0-dirty musb-hcd"
ATTRS{product}=="MUSB HDRC host driver"
ATTRS{serial}=="musb-hdrc.1"
ATTRS{authorized_default}=="1"
looking at parent device '/devices/platform/omap/musb-ti81xx/musb-hdrc.1':
KERNELS=="musb-hdrc.1"
SUBSYSTEMS=="platform"
DRIVERS=="musb-hdrc"
ATTRS{modalias}=="platform:musb-hdrc"
ATTRS{mode}=="a_host"
ATTRS{vbus}=="Vbus off, timeout 1100"
looking at parent device '/devices/platform/omap/musb-ti81xx':
KERNELS=="musb-ti81xx"
SUBSYSTEMS=="platform"
DRIVERS=="musb-ti81xx"
ATTRS{modalias}=="platform:musb-ti81xx"
looking at parent device '/devices/platform/omap':
KERNELS=="omap"
SUBSYSTEMS==""
DRIVERS==""
looking at parent device '/devices/platform':
KERNELS=="platform"
SUBSYSTEMS==""
DRIVERS==""
我們查看每個(gè)USB串口設(shè)備節(jié)點(diǎn)的信息,相應(yīng)設(shè)備信息如下:
root@am335x:~# udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB1)
root@am335x:~# udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB2)
root@am335x:~# udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB3)
root@am335x:~# udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB4)
我們通過觀察各個(gè)設(shè)備節(jié)點(diǎn)的信息,返現(xiàn)ATTRS{interface} 可以區(qū)別各個(gè)設(shè)備節(jié)點(diǎn)
所以我們將SUBSYSTEMS=="usb" ATTRS{interface}=="Huawei Mobile Connect - Modem" 作為過濾條件。
在/etc/udev/rules.d/local.rules 中加入以下內(nèi)容。將ttyUSB0-4 做相應(yīng)的軟鏈接為ttyusb0-4
ATTRS{interface}=="Huawei Mobile Connect - Serial B",SUBSYSTEMS=="usb",SYMLINK+="ttyusb4"
ATTRS{interface}=="Huawei Mobile Connect - Ctrl",SUBSYSTEMS=="usb",SYMLINK+="ttyusb3"
ATTRS{interface}=="Huawei Mobile Connect - Pcui",SUBSYSTEMS=="usb",SYMLINK+="ttyusb2"
ATTRS{interface}=="Huawei Mobile Connect - Application",SUBSYSTEMS=="usb",SYMLINK+="ttyusb1"
ATTRS{interface}=="Huawei Mobile Connect - Modem",SUBSYSTEMS=="usb",SYMLINK+="ttyusb0"
我們重啟系統(tǒng) 輸入查看設(shè)備節(jié)點(diǎn)
通過查看 ,完成了設(shè)備節(jié)點(diǎn)的軟鏈接
如果我們?cè)诰唧w應(yīng)用中 通過/dev/ttyusb2 進(jìn)行撥號(hào)上網(wǎng)。在實(shí)際長(zhǎng)期運(yùn)行過程中,我們的4g網(wǎng)絡(luò)守護(hù)進(jìn)程檢測(cè)到4g離網(wǎng)時(shí) ,會(huì)重啟4g模塊,有可能導(dǎo)致ttyUSB序號(hào)發(fā)生變化,如下
通過查看/dev/ttyUSB10 的設(shè)備信息與前期的設(shè)備信息對(duì)比發(fā)現(xiàn),多次反復(fù)4g模塊重啟,屬性為ATTRS{interface}=="Huawei Mobile Connect - Pcui"的接口在/dev 目錄下的設(shè)備節(jié)點(diǎn)由ttyUSB2變?yōu)?ttyUSB10,如下圖所示:
如果我們的應(yīng)用還繼續(xù)使用ttyUSB2,肯定會(huì)出問題的,但是我們使用USB串口所做的軟鏈接ttyusb2 ,就不會(huì)發(fā)生問題了。
相關(guān)產(chǎn)品 >
-
FET335xD核心板
飛凌AM335系列核心板基于TI RM Cortex-A8 架構(gòu) AM335x 微處理器設(shè)計(jì),AM335x核心板整板尺寸47*71mm;AM335X核心板芯片全部采用工業(yè)級(jí)用料;AM335x核心板引腳豐富,引出200pinCPU引腳,原生支持6路UART;AM335x核心板支持市面上主流的操作系統(tǒng),Linux-RT、Linux3.2。更多AM335x系列概述,AM335處理器特點(diǎn)請(qǐng)聯(lián)系在線客服
了解詳情 -
FET62xx-C核心板
FET6254-C核心板基于TI Sitara? AM62x系列工業(yè)級(jí)處理器設(shè)計(jì)。采用Arm Cortex A53架構(gòu),并集成了廣泛的接口,如2路支持TSN的千兆以太網(wǎng)、USB 2.0CAN-FD,AM6254核心板兼容AM62x全系列處理器,提供單核、雙核、四核可選,功能引腳完全兼容,飛凌嵌入式已經(jīng)適配AM6254 AM6231 AM6232三款芯片為您帶來靈活的成本組合方案,AM62x可應(yīng)用于廣泛的工業(yè)環(huán)境,如人機(jī)界面(HMI)、工業(yè)計(jì)算機(jī)、邊緣計(jì)算、零售自動(dòng)化、充電樁控制單元(TCU)、醫(yī)療設(shè)備等。
了解詳情