米爾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應用開發(三)

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