简介
随着工业发展,嵌入式设备接入网络的需求日益增多,在没有有线或者无线以太网的环境下,直接通过3G/4G Modem连接运营商网络来接入互联网不失为一个好方法,本文就着重介绍基于ARM平台的嵌入式设备在Embedded Linux下使用3G/4G Modem的方法。
目前市面上的3G/4G modem和主机的连接方式主要有串口、USB和mini-PCIE,串口模块比较传统且使用比较简单,而mini-PCIE在工业设备中目前应用还不广泛,因此本文使用USB 3G/4G modem来进行演示。
硬件准备
嵌入式平台基于Toradex Colibri i.MX6工业产品级ARM核心板搭配Colibri开发载板,Embedded linux采用Toradex官方发布兼容Yocto Project 的Linux V2.5版本。
3G Modem使用华为 E303s WCDMA Modem,支持中国联通的3G网络
Linux Kernel准备
关于Linux kernel源代码下载及基本编译更新操作请参考这里,本文不再赘述,只简单列出操作如下:
a). 下载Linux kernel源代码
b). 下载安装Toolchain,并link到 “ ~/gcc-linaro “ 目录
$ wget http://releases.linaro.org/14.11/components/toolchain/binaries/arm-linux-gnueabihf/gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf.tar.xz
$ tar xvf gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf.tar.xz
$ ln -s gcc-linaro-4.9-2014.11-x86_64_arm-linux-gnueabihf gcc-linaro
c). 设置环境变量
$ export ARCH=arm
$ export PATH=~/gcc-linaro/bin/:$PATH
$ export CROSS_COMPILE=arm-linux-gnueabihf-
d). 配置
$ make colibri_imx6_defconfig
$ make menuconfig //添加下面列出功能
--------------------
[*] Network device support --->
<*> PPP (point-to-point protocol) support
<*> PPP BSD-Compress compression
<*> PPP Deflate compression
[*] PPP filtering
<*> PPP MPPE compression (encryption)
[*] PPP multilink support
<*> PPP over Ethernet
<*> PPP support for async serial ports
<*> PPP support for sync tty ports
[*] USB support --->
<*> USB Modem (CDC ACM) support
[*] USB support --->
<*> USB Serial Converter support --->
<*> USB driver for GSM and CDMA modems
--------------------
e). 重新编译kernel image
$ make -j3 uImage LOADADDR=10008000 2>&1 | tee build.log
f). 升级目标板
根据本章节开始提供的参考升级目标板到新的kernel image
相关软件准备
1). PPP
Toradex image默认已经安装,如果其他ARM平台image也是基于Yocto/OpenEmbedded环境编译,可以在local.conf文件中添加如下选项后编译
-----------------
IMAGE_INSTALL_append = " ppp"
-----------------
2). usb-modesiwtch-2.3.0
a). 需要libusb-1.0.x,先从这里下载源文件并解压
b). 使用上一章节配置的ToolChain如下设置交叉编译libusb
$ sudo CC="/home/xxx/gcc-linaro/bin/arm-linux-gnueabihf-gcc" CXX="/home/xxx/gcc-linaro/bin/arm-linux-gnueabihf-g++" ./configure --host=arm-linux-gnueabihf --prefix=/home/xxx/gcc-linaro --disable-udev
$ make
$ make install
c). 从这里下载usb-modesiwtch并解压
d). 如下修改usb-modesiwtch 目录下Makefile
----------------
CC = ~/gcc-linaro/bin/arm-linux-gnueabihf-gcc
CFLAGS += -Wall -I/home/xxx/gcc-linaro/include -L/home/xxx/gcc-linaro/lib
----------------
e).交叉编译usb-modesiwtch
$ make
f). 从这里下载usb-modeswitch-data,和上面步骤编译出的usb-modesiwtch可执行文件一起上传到目标板
配置Modem连接网络
目前比较新的3G/4G都支持Ethernet模式和Fallback模式,而老一些的模块只支持Fallback模式,请根据所需模块情况自行参考下面操作。
1). Ethernet模式
a). 将E303s 3G Modem连接目标板,由于Modem上包含为了给Windows系统提供驱动而部署的存储分区,Linux默认将设备识别成USB Mass Storage设备
root@colibri-imx6:/home# lsusb
Bus 001 Device 004: ID 12d1:1f01 Huawei Technologies Co., Ltd. E353/E3131 (Mass storage mode)
……
b). 如下修改usb-modeswitch-data-20160112/usb_modeswitch.d目录下的12d1:1f01文件
-----------------------
# Huawei E303s Ethernet Mode
DefaultVendor=0x12d1
DefaultProduct=0x1f01
TargetVendor=0x12d1
TargetProduct=0x14db
MessageEndPoint = "0x01"
MessageContent="55534243123456780000000000000a11062000000000000100000000000000"
------------------------
c). 使用usb-modeswitch工具将modem设置为Ethernet mode
root@colibri-imx6:/home# ./usb_modeswitch -c 12d1:1f01
……
root@colibri-imx6:/home# [ 840.027676] usb 1-1.1: USB disconnect, device number 4
[ 840.284299] usb 1-1.1: new high-speed USB device number 6 using ci_hdrc
[ 840.427810] cdc_ether 1-1.1:1.0 eth1: register 'cdc_ether' at usb-ci_hdrc.0-1.1, CDC Ethernet Device, 58:2c:80:13:92:63
lsusb查看
root@colibri-imx6:/home# lsusb
Bus 001 Device 005: ID 12d1:14db Huawei Technologies Co., Ltd. E353/E3131
……
d). 查看网络,出现eth1,正确获取ip并连接到网络,下面就可以用使用以太网同样方法使用网络了
root@colibri-imx6:/home# ifconfig
……
eth1 Link encap:Ethernet HWaddr 58:2C:80:13:92:63
inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::5a2c:80ff:fe13:9263/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:37 errors:0 dropped:0 overruns:0 frame:0
TX packets:69 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6899 (6.7 KiB) TX bytes:10381 (10.1 KiB)
……
e). 验证网络连接
root@colibri-imx6:/home# ping www.baidu.com
PING www.baidu.com (61.135.169.121): 56 data bytes
64 bytes from 61.135.169.121: seq=0 ttl=51 time=88.261 ms
64 bytes from 61.135.169.121: seq=1 ttl=51 time=77.998 ms
64 bytes from 61.135.169.121: seq=2 ttl=51 time=68.410 ms
64 bytes from 61.135.169.121: seq=3 ttl=51 time=68.242 ms
64 bytes from 61.135.169.121: seq=4 ttl=51 time=68.166 ms
2). Fallback模式
有一些较老型号的2G/3G Modem只支持这个模式,无需再用usb-modeswitch转换,可以直接参考下面转换后的操作部分。
a). 如下修改usb-modeswitch-data-20160112/usb_modeswitch.d目录下的12d1:1f01文件
-----------------------
# Huawei E303s Fallback Mode
DefaultVendor=0x12d1
DefaultProduct=0x1f01
TargetVendor=0x12d1
TargetProduct=0x1001
MessageEndPoint = "0x01"
MessageContent="55534243123456780000000000000011063000000100010000000000000000"
NoDriverLoading=0
------------------------
b). 使用usb-modeswitch工具将modem设置为Fallback mode
root@colibri-imx6:/home# ./usb_modeswitch -c 12d1\:1f01
……
root@colibri-imx6:/home# [ 2090.566190] usb 1-1.1: new high-speed USB device number 8 using ci_hdrc
[ 2090.701092] option 1-1.1:1.0: GSM modem (1-port) converter detected
[ 2090.713488] usb 1-1.1: GSM modem (1-port) converter now attached to ttyUSB0
[ 2090.723517] option 1-1.1:1.1: GSM modem (1-port) converter detected
[ 2090.731817] usb 1-1.1: GSM modem (1-port) converter now attached to ttyUSB1
[ 2090.740318] option 1-1.1:1.2: GSM modem (1-port) converter detected
[ 2090.748184] usb 1-1.1: GSM modem (1-port) converter now attached to ttyUSB
lsusb 查看
root@colibri-imx6:/home# lsusb
Bus 001 Device 008: ID 12d1:1001 Huawei Technologies Co., Ltd. E169/E620/E800 HSDPA Modem
……
c). 测试连接
root@colibri-imx6:/home# ls -l /dev/ttyUSB*
crw-rw---- 1 root dialout 188, 0 Apr 11 09:49 /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 1 Apr 11 09:49 /dev/ttyUSB1
crw-rw---- 1 root dialout 188, 2 Apr 11 09:49 /dev/ttyUSB2
root@colibri-imx6:/home# minicom -D /dev/ttyUSB0
------------------
Welcome to minicom 2.7
OPTIONS: I18n
Compiled on Nov 3 2015, 04:06:26.
Port /dev/ttyUSB0, 09:53:44
Press CTRL-A Z for help on special keys
AT
OK
--------------------
d). 编写ppp连接配置文件wcdma, wcdma-chat-connect和wcdma-chat-disconnect
root@colibri-imx6:/etc/ppp/peers# vi wcdma
-----------------
nodetach
lock
/dev/ttyUSB0
115200
user "card"
password "card"
crtscts
#hide-password
show-password
usepeerdns
noauth
noipdefault
novj
novjccomp
noccp
defaultroute
ipcp-accept-local
ipcp-accept-remote
connect '/usr/sbin/chat -s -v -f /etc/ppp/peers/wcdma-chat-connect'
disconnect '/usr/sbin/chat -s -v -f /etc/ppp/peers/wcdma-chat-disconnect'
----------------------
root@colibri-imx6:/etc/ppp/peers# vi wcdma-chat-connect
--------------------
ABORT "BUSY"
ABORT "NO CARRIER"
ABORT "NO DIALTONE"
ABORT "ERROR"
ABORT "NO ANSWER"
TIMEOUT 120
"" AT
OK \rATZ
OK \rAT+CGDCONT=1,"IP","3gnet",,0,0
OK-AT-OK ATDT*99#
CONNECT \d\c
--------------------------
root@colibri-imx6:/etc/ppp/peers# vi wcdma-chat-disconnect
-----------------------
ABORT "ERROR"
ABORT "NO DIALTONE"
SAY "\NSending break to the modem\n"
""\k"
""+++ATH"
SAY "\nGood bay\n"
---------------------------
e). 连接网络
root@colibri-imx6:/etc/ppp/peers# pppd call wcdma
……
ATDT*99#^M^M
CONNECT
-- got it
send (\d)
Serial connection established.
Using interface ppp0
Connect: ppp0 <--> /dev/ttyUSB0
CHAP authentication succeeded: Welcome!!
CHAP authentication succeeded
Could not determine remote IP address: defaulting to 10.64.64.64
local IP address 10.91.228.253
remote IP address 10.64.64.64
primary DNS address 221.12.1.227
secondary DNS address 221.12.33.227
查看网络状态
root@colibri-imx6:/etc/ppp/peers# ifconfig
……
ppp0 Link encap:Point-to-Point Protocol
inet addr:10.23.107.61 P-t-P:10.64.64.64 Mask:255.255.255.255
UP POINTOPOINT RUNNING NOARP MULTICAST MTU:1500 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:3
RX bytes:84 (84.0 B) TX bytes:102 (102.0 B)
f). 测试网络
root@colibri-imx6:/etc/ppp/peers# echo nameserver 221.12.1.227 > /etc/resolv.conf //设置DNS服务器
root@colibri-imx6:/etc/ppp/peers# ping www.baidu.com
PING www.baidu.com (61.135.169.121): 56 data bytes
64 bytes from 61.135.169.121: seq=0 ttl=52 time=218.477 ms
64 bytes from 61.135.169.121: seq=1 ttl=52 time=68.066 ms
64 bytes from 61.135.169.121: seq=2 ttl=52 time=107.773 ms
64 bytes from 61.135.169.121: seq=3 ttl=52 time=77.604 ms
64 bytes from 61.135.169.121: seq=4 ttl=52 time=77.339 ms
总结
根据上述两种模式的测试结果,第一种Ethernet mode相对速度要更稳定一些,因此对于支持两种模式的modem更建议采用Ethernet mode;另外串口modem使用配置也可以参考Fallback mode。本文虽以E303s为例,但其他3G/4G Modem的配置也是类似,只是需要确定对应模式的Vendor ID,Product ID和Message。
参考文档
https://www.toradex.cn/zh_cn/computer-on-modules/colibri-arm-family/nxp-freescale-imx6
https://www.toradex.cn/zh_cn/products/carrier-board/colibri-evaluation-carrier-board
http://developer.toradex.com/software/linux/linux-software
http://www.draisberghof.de/usb_modeswitch/
http://www.cnblogs.com/ziqiuqiandao/archive/2013/06/06/3120972.html