米尔i.MX6UL开发板的Linux应用开发(二)

本章继i.MX6UL开发板的Linux应用开发(一),介绍MYD-Y6ULX开发板底板外围硬件设备应用例程的使用。
使用前,需要先安装Yocto提供的SDK工具链,再编译所有例程代码,并拷贝至开发板目录下。
在这里插入图片描述
硬件:米尔i.MX6UL开发板MYD-Y6ULX
简介:MYD-Y6ULX开发板(i.MX6UL开发板)由MYC-Y6ULX核心板和底板组成,基于NXP i.MX6UL处理器或i.MX6ULL处理器可选,MYD-Y6ULX开发板(i.MX6ULL开发板)提供丰富外设硬件,板载了Mini PCIE接口(用于4G模块)及SIM卡槽、WIFI芯片及天线接口、双百兆网口、LCD液晶接口、音频接口、带隔离的CAN,RS485,RS232等。
部署开发环境:开发前需要PC安装好Linux操作系统,推荐使用Ubuntu 16.04 64bit发行版,连接网线并配置好网络,后续操作需要连接互联网安装或下载相关软件包。

正文:
RS485 测试
注意:本例程需要在MYS-6ULX-IOT或MYS-6ULX-IND上安装MYB-6ULX扩展
板,同时使用支持MYB-6ULX的dtb文件启动Linux系统
本例程演示如何使用 Linux Serial API编程,使用MYB-6ULX扩展板上的
RS485接口,实现数据发送和接收,详细请参考源码。
硬件连接
MYB-6ULX扩展板上有一个RS485接口(J9),可以将A,B信号线与另外的
RS485设备相连接。或者是USB转RS485的转换器设备。
软件测试
将编译出来的可执行程序拷贝至MYS-6ULX开发板系统内。MYS-6ULX作为发
送端执行以下命令,另外一端的设备可以接数据。

./rs485_write -d /dev/ttymxc2 -b 4800 -e 1
SEND[20]: 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x
0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 
SEND[20]: 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x
0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 
SEND[20]: 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x
0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 
SEND[20]: 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x
0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14

MYS-6ULX作为接收端:

./rs485_read -d /dev/ttymxc2 -b 4800 -e 1
RECV[20]: 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x
0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 
RECV[20]: 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x
0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 
RECV[20]: 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x
0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14 
RECV[20]: 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x
0c 0x0d 0x0e 0x0f 0x10 0x11 0x12 0x13 0x14

CAN Bus 测试
注意:本例程需要在MYS-6ULX-IOT或MYS-6ULX-IND上安装MYB-6ULX扩展
板,同时使用支持MYB-6ULX的dtb文件启动Linux系统
本例程演示使用Linux socket CAN API,使用MYB-6ULX扩展板上的CAN总线
接口发送和接收数据。将can_send和can_receive拷贝至开发板。执行以下步
骤:
硬件连接
MYB-6ULX开发板有一个CAN总线接口(J11),将H,L信号线与另外的CAN通讯
设备或USB CAN转换器相连接。
软件测试
配置开发板上CAN0通信波特率都设置为 50kbps,并使能CAN0设备
Linux上可以使用两种工具来配置CAN设备,canconfig和ip,MYS-6ULX附带的
系统默认使用ip命令。 canconfig命令配置:

canconfig can0 bitrate 50000 ctrlmode triple-sampling on
canconfig can0 start

ip命令配置

ip link set can0 type can bitrate 50000 triple-sampling on
ifconfig can0 up

CAN收发测试可以使用系统附带的cansend、candump命令,也可以使用资源
包中CAN收发例程。
MYB-6ULX扩展板作为发送端:
使用cansend发送数据到CAN总线:

cansend can0 100#01.02.03.04.05.06.07.08

can_send例程运行后会一直发送数据,直到ctrl + c结束。

./can_send -d can0 -i 100 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88

MYB-6ULX扩展板作为接收端:
使用candump接收CAN数据:

candump can0
can0  100   [8]  01 02 03 04 05 06 07 08

can_receive例程接收来自CAN总线的数据:

can0  0x100  [8]  0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 
can0  0x100  [8]  0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 
can0  0x100  [8]  0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 
can0  0x100  [8]  0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88

Audio 测试
注意:本例程需要在MYS-6ULX-IOT或MYS-6ULX-IND上安装MYB-6ULX扩展
板,同时使用支持MYB-6ULX的dtb文件启动Linux系统

硬件连接
本例程演示使用Linux系统中的arecord/aplay命令对音频接口录音和放音。需
要使用两头3.5mm的音频AUX线,从电脑音频输出孔和开发板的LINE IN(J7)
接口连接,HEADERPHONE(J5)连接耳机。

软件操作
在电脑中播放音频文件,执行arecord命令会先将LINE IN中的音频录制并保存
为test.wav文件。运行一分钟后再按ctrl + c来停止。

arecord -f cd test.wav

执行aplay命令来播放上面录制好的音频文件。

aplay test.wav

Camera 测试
注意:本次测试需要在MYS-6ULX-IOT或MYS-6ULX-IND上安装MYB-6ULX扩展
板和MY-CAM011B,同时使用支持MYB-6ULX的dtb文件启动Linux系统
MYB-6ULX扩展板上提供一个并行Camera接口(J10),可以连接MY-CAM011B
型号的Camera模块,模块之间使用FPC线连接。由于信号序列影响,请勿直
接将其它型号的Camera的模块插入,否则会引起模块或开发板的损坏。

本例程演示使用一款开源的视频流软件uvc_stream,可以将Camera设备捕捉
的数据显示在web页面。

硬件连接
使用FPC数据线将MYB-CAM011B模块和MYB-6ULX板上的J10接口相连接。

软件操作
uvc_stream是通过的网络传输数据,需要先设置好MYS-6ULX板的以太网IP地
址,对应系统中的eth1设备。Linux系统中的MY-CAM011B模块的设备,可通
通过v4l2-ctl命令来查询到,输出信息的i.MX6S_CSI表示Camera控制器,对
应设备是/dev/video1。uvc_stream参数中’-y’是使用yuyv方式,’-P’后面是设
置web界面的登录密码,用户名默认为uvc_user。’-r’是指定分辨率,当前仅
支持800x600。可以用ctrl + c来停止。

ifconfig eth1 192.168.1.42
v4l2-ctl --list-devices
i.MX6S_CSI (platform:21c4000.csi):
    /dev/video1
    pxp (pxp_v4l2):
        /dev/video0
./uvc_stream -d /dev/video1 -y -P 123456 -r 800x600

uvc_stream提供两种web功能,snapshot和streaming。snapshot的请求URL
是snapshot.jpeg,streaming的请求URL是stream.mjpeg。 PC和开发板在同
一网络内时,打开流览器,输入地

http://192.168.1.42:8080/stream.mjpeg
,可以看到有登录框,输入用户
名为uvc_user,密码为123456,就可以看到从MY-CAM011B实时采集到的图
像了。

WiFi 测试
注意:本例程适用于MYS-6ULX-IOT开发板
MYS-6ULX-IOT开发板的背面提供一个WiFi模块(U13),支持Client和AP模式,
本节分别测试两种场景下的使用方法。

硬件连接
将附带I-PEX接口的天线安装在开发板的J10位置。

Client模式
Client模式是用于将WiFi模块作为客户访问设备,主动连接于路由器或其它提
供无线热点的设备。

系统中已经加入WiFi模块的驱动,启动后会自动加载相应驱动,可以使用
lsmod命令来确认。驱动加载成功后会出现对应的wlan0网络设备,使用
ifconfig命令来确认。

lsmod
Module                  Size  Used by
8188eu                758318  0
ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr a0:2c:36:60:ee:e0  
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:3388 errors:0 dropped:10 overruns:0 frame:0
          TX packets:37 errors:0 dropped:3 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:395459 (386.1 KiB)  TX bytes:6074 (5.9 KiB)

下面使用wpa_passphrase生成对应WiFi热点SSID的密码,然后由
wpa_supplicant命令实现WiFi模块与WiFi热点的连接。

wpa_passphrase "MYiRTech" >> wifi.conf
12345678
cat wifi.conf
# reading passphrase from stdin
network={
    ssid="MYiRTech"
    #psk="12345678"
    psk=b96d9a5de2d9480ad5f987857e20216b47a0c4bf43397825ba909438bc5

WiFi测试

2aaff
}
wpa_supplicant -D wext -B -i wlan0 -c wifi.conf
Successfully initialized wpa_supplicant
rfkill: Cannot open RFKILL control device
R8188EU: Firmware Version 11, SubVersion 1, Signature 0x88e1
MAC Address = a0:2c:36:60:ee:e0
IPv6: ADDRCONF(NETDEV_UP): wlan0: link is not ready
ioctl[SIOCSIWAP]: Operation not permitted
R8188EU: INFO indicate disassoc

连接成功后,使用udhcpc获取IP地址后,就可以使用了。

udhcpc -b -i wlan0 -R
ifconfig wlan0
wlan0     Link encap:Ethernet  HWaddr a0:2c:36:60:ee:e0  
          inet addr:192.168.1.211  Bcast:192.168.1.255  Mask:255.25
5.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:5577 errors:0 dropped:15 overruns:0 frame:0
          TX packets:46 errors:0 dropped:3 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:651690 (636.4 KiB)  TX bytes:7472 (7.2 KiB)

AP模式
AP模式是由软件和硬件同时支持才可以实现,MYS-6ULX-IOT板上的WiFi模块
可以支持AP模式。软件上使用Hostapd来实现热点配置功能。

编译Hostapd
资源包中有提供hostapd的源码,同时提供的镜像中也有安装hostapd软件。

tar xvf RTL8188-hostapd-2.0.tar.gz
cd RTL8188-hostapd-2.0/hostapd
make
mkdir ~/hostapd-armhf
make install DESTDIR=~/hostapd-armhf

这里把hostapd安装在了"~/hostapd-armhf"位置,可以使用NFS或sdcard将其
复制到开发板上。可以使用"-v"检查版本信息。

hostapd -v
hostapd v0.8.x_rtw_r7475.20130812_beta
User space daemon for IEEE 802.11 AP management,
IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator
Copyright (c) 2002-2011, Jouni Malinen <[email protected]> and contributors

配置Hostapd
hostapd的配置文件是"/etc/hostapd/hostapd.conf",用于配置SSID和热点
的密码。"ssid"参数是WiFi热点的名称,"pass_passphrase"是WiFi热点的密
码。

cat /etc/hostapd/hostapd.conf 
# Basic configuration
interface=wlan0
ssid=myir-test
channel=1
#bridge=br0
# WPA and WPA2 configuration
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=3
wpa_passphrase=12345678
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
# Hardware configuration
driver=rtl871xdrv
ieee80211n=1
hw_mode=g
device_name=RTL8192CU
manufacturer=Realtek

Hostapd以daemon方式运行,命令如下:

  ifconfig wlan0 up
    hostapd /etc/hostapd/hostapd.conf -B

Hostapd只是提供AP的功能,还需要有DHCP服务才可以给连接到WiFi网络的
设备分配IP地址。udhcpd命令实现了简单的DHCP服务器功能。首先将下面的
配置写入/etc/udhcpd.conf文件,若没有请先创建该文件。

# The start and end of the IP lease block
start         192.168.2.20    #default: 192.168.0.20
end        192.168.2.254    #default: 192.168.0.254
# The interface that udhcpd will use
interface    wlan0        #default: eth0
# The maximim number of leases (includes addressesd reserved
# by OFFER's, DECLINE's, and ARP conficts
#max_leases    254        #default: 254
# If remaining is true (default), udhcpd will store the time
# remaining for each lease in the udhcpd leases file. This is
# for embedded systems that cannot keep time between reboots.
# If you set remaining to no, the absolute time that the lease
# expires at will be stored in the dhcpd.leases file.
#remaining    yes        #default: yes
# The time period at which udhcpd will write out a dhcpd.leases
# file. If this is 0, udhcpd will never automatically write a
# lease file. (specified in seconds)
#auto_time    7200        #default: 7200 (2 hours)
# The amount of time that an IP will be reserved (leased) for if a 
# DHCP decline message is received (seconds).
#decline_time    3600        #default: 3600 (1 hour)
# The amount of time that an IP will be reserved (leased) for if an
# ARP conflct occurs. (seconds
#conflict_time    3600        #default: 3600 (1 hour)
# How long an offered address is reserved (leased) in seconds
#offer_time    60        #default: 60 (1 minute)
# If a lease to be given is below this value, the full lease time i
s
# instead used (seconds).
#min_lease    60        #defult: 60
4.12 WiFi测试
54
# The location of the leases file
#lease_file    /var/lib/misc/udhcpd.leases    #defualt: /var/lib/mi
sc/udhcpd.leases
# The location of the pid file
#pidfile    /var/run/udhcpd.pid    #default: /var/run/udhcpd.pid
# Everytime udhcpd writes a leases file, the below script will be c
alled.
# Useful for writing the lease file to flash every few hours.
#notify_file                #default: (no script)
#notify_file    dumpleases     # <--- usefull for debugging
# The following are bootp specific options, setable by udhcpd.
#siaddr        192.168.0.22        #default: 0.0.0.0
#sname        zorak            #default: (none)
#boot_file    /var/nfs_root        #default: (none)
# The remainer of options are DHCP options and can be specifed with
 the
# keyword 'opt' or 'option'. If an option can take multiple items, 
such
# as the dns option, they can be listed on the same line, or multip
le
# lines. The only option with a default is 'lease'.
#Examles
opt    dns    192.168.2.1
option    subnet    255.255.255.0
opt    router    192.168.2.1
#opt    wins    192.168.10.10
#option    dns    129.219.13.81    # appened to above DNS servers f
or a total of 3
option    domain    local
option    lease    864000        # 10 days of seconds

然后将wlan0作为路由,运行udhcpd服务:

ifconfig wlan0 192.168.2.1
udhcpd -fS /etc/udhcpd.conf

使用手机或其他WiFi设备连接"myir-test"热点后,即可分配到正确的IP地址。

此章结束,下一章将继续介绍i.MX6UL开发板的Linux应用开发(三)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章