域名解析到本地服務(wù)器伊春seo
前言
一些老的、Mini 的 ARM 開發(fā)板上沒有預(yù)留網(wǎng)口,這樣在調(diào)試升級內(nèi)核或應(yīng)用程序時很不方便??v使有串口下載工具,但其速度也是慢地捉急。這種情況下,使用其它接口來擴展出一個網(wǎng)口無疑是一個比較好的方法。
ENC28J60 就是一個使用 SPI 接口來擴展網(wǎng)口的模塊,今天我們就來演示下在樹莓派上如何使用 ENC28J60。
環(huán)境
硬件:樹莓派3b+、ENC28J60
軟件:buildroot、kernel-5.10.92
摸索
驅(qū)動
既然想要使用 ENC28J60,那么肯定得有一份它的驅(qū)動程序。
作為一款廣泛使用的模塊,內(nèi)核理應(yīng)有它的驅(qū)動程序,搜一下
果然,內(nèi)核有它的驅(qū)動程序,且其已經(jīng)被編譯成了內(nèi)核模塊:
liyongjun@Box:~/project/board/buildroot$ find RPi3/build/linux-custom/drivers/ -name "enc28j60.ko"
RPi3/build/linux-custom/drivers/net/ethernet/microchip/enc28j60.ko
硬件
有了驅(qū)動,接下來就要看硬件如何連接了。
知道是連 SPI 接口,但是 SPI 接口有 SPI0、SPI1、SPI2 等,要連哪個呢?另外還有 CS、INT 引腳要連接樹莓派的哪個引腳?
那就去看設(shè)備樹。
驅(qū)動的匹配屬性為 “microchip,enc28j60”,那就在設(shè)備樹中搜下該參數(shù)
搜到了兩個設(shè)備樹文件,(Device Tree Overlays 相關(guān)知識請自行搜索,或查看這篇文章)
arch/arm/boot/dts/overlays/enc28j60-overlay.dts
arch/arm/boot/dts/overlays/enc28j60-spi2-overlay.dts
說明 enc28j60 可以接在不同的 SPI 接口上工作,這里我們研究 enc28j60-overlay.dts,即 SPI0。
target = <&spi0>;:指定"插件"設(shè)備樹的父節(jié)點是 spi0
所以,查看 SPI0 的引腳分配
9、10、11 分別是 MISO、MOSI、SCK
8、7 是 CS 引腳,為什么配置兩個引腳作為 CS 引腳這里表示不理解,本實驗中選取 8 引腳作為 CS 引腳。
而 INT 引腳是在 enc28j60-overlay.dts 中配置的,使用 25 引腳,所以最終的硬件連接為
RPi3b+ ENC28J60
----------------------------
+3V3 VCC
GPIO10/MOSI SI
GPIO9/MISO SO
GPIO11/SCLK SCK
GND GND GPIO25 INT
GPIO8/CE0# CS
實物連接
配置
樹莓派硬件比較特殊,開啟 SPI 需要修改板子中的配置文件,如下
# mount /dev/mmcblk0p1 /boot
# cat /boot/config.txt
# Please note that this is only a sample, we recommend you to change it to fit
# your needs.
# You should override this file using BR2_PACKAGE_RPI_FIRMWARE_CONFIG_FILE.
# See http://buildroot.org/manual.html#rootfs-custom
# and http://elinux.org/RPiconfig for a description of config.txt syntaxstart_file=start.elf
fixup_file=fixup.datkernel=zImage# To use an external initramfs file
#initramfs rootfs.cpio.gz# Disable overscan assuming the display supports displaying the full resolution
# If the text shown on the screen disappears off the edge, comment this out
disable_overscan=1# How much memory in MB to assign to the GPU on Pi models having
# 256, 512 or 1024 MB total memory
gpu_mem_256=100
gpu_mem_512=100
gpu_mem_1024=100# fixes rpi (3B, 3B+, 3A+, 4B and Zero W) ttyAMA0 serial console
dtoverlay=miniuart-bt# enable autoprobing of Bluetooth driver without need of hciattach/btattach
dtoverlay=krnbt=ondtparam=spi=on
dtoverlay=enc28j60
需要手動在 config.txt 文件末尾處添加
dtparam=spi=on
dtoverlay=enc28j60
這兩個參數(shù)猜測是給 start.elf 使用的,因為在其文件中搜出了相應(yīng)的字符串
liyongjun@Box:~/project/board/buildroot$ strings RPi3/images/rpi-firmware/start.elf | grep "dtparam"
dtparam: %s=%s
Unknown dtparam '%s' - ignored
dtparam
dtparams
liyongjun@Box:~/project/board/buildroot$ strings RPi3/images/rpi-firmware/start.elf | grep "dtoverlay"
dtoverlay
安裝驅(qū)動
# insmod /lib/modules/5.10.92-v7/kernel/drivers/spi/spi-bcm2835.ko
# insmod /lib/modules/5.10.92-v7/kernel/drivers/net/ethernet/microchip/enc28j60.ko
[ 6214.374882] enc28j60 spi0.0: Ethernet driver 1.02 loaded
查看網(wǎng)口
# ifconfig -a
eth0 Link encap:Ethernet HWaddr B8:27:EB:8A:BC:F4 UP BROADCAST MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)eth1 Link encap:Ethernet HWaddr 76:30:92:53:5E:E4 BROADCAST MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)Interrupt:200 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0UP LOOPBACK RUNNING MTU:65536 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
多出了一個 eth1,說明 ENC28J60 已經(jīng)被成功驅(qū)動起來了
使能網(wǎng)卡
# ifconfig eth1 up
[ 6239.461085] enc28j60 spi0.0 eth1: link down
[ 6239.469427] enc28j60 spi0.0 eth1: normal mode
[ 6239.477886] enc28j60 spi0.0 eth1: multicast mode
# [ 6240.462780] enc28j60 spi0.0 eth1: link up - Half duplex# ifconfig
eth0 Link encap:Ethernet HWaddr B8:27:EB:8A:BC:F4 UP BROADCAST MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)eth1 Link encap:Ethernet HWaddr 76:30:92:53:5E:E4 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:4 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:769 (769.0 B) TX bytes:0 (0.0 B)Interrupt:200 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0UP LOOPBACK RUNNING MTU:65536 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
使能網(wǎng)卡后,并且插上網(wǎng)線,看到 eth1 已經(jīng)處于 RUNNING 狀態(tài),但是還沒有 IP,那就手動觸發(fā)下 dhcpc
# udhcpc -i eth1
udhcpc: started, v1.36.0
[ 6256.013810] enc28j60 spi0.0 eth1: multicast mode
[ 6256.027504] enc28j60 spi0.0 eth1: multicast mode
udhcpc: broadcasting discover
udhcpc: broadcasting select for 192.168.31.239, server 192.168.31.1
udhcpc: lease of 192.168.31.239 obtained from 192.168.31.1, lease time 43200
[ 6256.824266] enc28j60 spi0.0 eth1: multicast mode
[ 6256.832197] enc28j60 spi0.0 eth1: multicast mode
deleting routers
adding dns 192.168.31.1# ifconfig
eth0 Link encap:Ethernet HWaddr B8:27:EB:8A:BC:F4 UP BROADCAST MULTICAST MTU:1500 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)eth1 Link encap:Ethernet HWaddr 76:30:92:53:5E:E4 inet addr:192.168.31.239 Bcast:192.168.31.255 Mask:255.255.255.0UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:27 errors:0 dropped:1 overruns:0 frame:0TX packets:2 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:4775 (4.6 KiB) TX bytes:684 (684.0 B)Interrupt:200 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0UP LOOPBACK RUNNING MTU:65536 Metric:1RX packets:0 errors:0 dropped:0 overruns:0 frame:0TX packets:0 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
這樣,eth1 獲得了 IP 地址 192.168.31.239
ping 下外網(wǎng)試試
# ping www.baidu.com
PING www.baidu.com (180.101.50.242): 56 data bytes
64 bytes from 180.101.50.242: seq=0 ttl=53 time=9.520 ms
64 bytes from 180.101.50.242: seq=1 ttl=53 time=10.309 ms
64 bytes from 180.101.50.242: seq=2 ttl=53 time=9.635 ms
可以 ping 通,同時,插拔網(wǎng)線也有相應(yīng)的 up/down 事件。
# [ 106.197318] enc28j60 spi0.0 eth1: link up - Half duplex
#
# [ 113.533593] enc28j60 spi0.0 eth1: link down
附錄
# ethtool eth1
Settings for eth1:Supported ports: [ TP ]Supported link modes: 10baseT/Half 10baseT/Full Supported pause frame use: NoSupports auto-negotiation: NoSupported FEC modes: Not reportedAdvertised link modes: Not reportedAdvertised pause frame use: NoAdvertised auto-negotiation: NoAdvertised FEC modes: Not reportedSpeed: 10Mb/sDuplex: HalfPort: Twisted PairPHYAD: 0Transceiver: internalAuto-negotiation: offMDI-X: UnknownCurrent message level: 0x00000036 (54)probe link ifdown ifup
ENC28J60 是一款 10Mbps 速率的以太網(wǎng) MAC+PHY 芯片,和單片機的通信接口為 SPI,SPI 最高時鐘頻率為 20MHz。
ENC28J60 支持半雙工和全雙工模式,但是不支持自動協(xié)商。在支持自動協(xié)商的網(wǎng)絡(luò)環(huán)境中,ENC28J60 默認的工作模式是半雙工模式。
黃色燈表示 Activity,即閃爍一次,代表有數(shù)據(jù)傳輸一次;綠色燈表示 Link up,即綠色燈常亮,代表網(wǎng)口正常工作。
性能測試(phy rate:10Mbps 半雙工)
TCP
PS D:\Tools\iperf-3.0.11-win64> .\iperf3.exe -c 192.168.31.239
Connecting to host 192.168.31.239, port 5201
[ 4] local 192.168.31.211 port 1648 connected to 192.168.31.239 port 5201
[ ID] Interval Transfer Bandwidth
[ 4] 0.00-1.00 sec 896 KBytes 7.34 Mbits/sec
[ 4] 1.00-2.00 sec 640 KBytes 5.24 Mbits/sec
[ 4] 2.00-3.00 sec 768 KBytes 6.29 Mbits/sec
[ 4] 3.00-4.00 sec 768 KBytes 6.29 Mbits/sec
[ 4] 4.00-5.00 sec 768 KBytes 6.29 Mbits/sec
[ 4] 5.00-6.00 sec 768 KBytes 6.29 Mbits/sec
[ 4] 6.00-7.00 sec 640 KBytes 5.24 Mbits/sec
[ 4] 7.00-8.00 sec 768 KBytes 6.29 Mbits/sec
[ 4] 8.00-9.00 sec 768 KBytes 6.30 Mbits/sec
[ 4] 9.00-10.00 sec 768 KBytes 6.29 Mbits/sec
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bandwidth
[ 4] 0.00-10.00 sec 7.38 MBytes 6.19 Mbits/sec sender
[ 4] 0.00-10.00 sec 7.19 MBytes 6.03 Mbits/sec receiveriperf Done.
UDP
PS D:\Tools\iperf-3.0.11-win64> .\iperf3.exe -c 192.168.31.239 -b10M -u
Connecting to host 192.168.31.239, port 5201
[ 4] local 192.168.31.211 port 63892 connected to 192.168.31.239 port 5201
[ ID] Interval Transfer Bandwidth Total Datagrams
[ 4] 0.00-1.00 sec 1.08 MBytes 9.03 Mbits/sec 138
[ 4] 1.00-2.00 sec 1.20 MBytes 10.0 Mbits/sec 153
[ 4] 2.00-3.00 sec 1.19 MBytes 9.95 Mbits/sec 152
[ 4] 3.00-4.00 sec 1.20 MBytes 10.0 Mbits/sec 153
[ 4] 4.00-5.00 sec 1.24 MBytes 10.4 Mbits/sec 159
[ 4] 5.00-6.01 sec 1.15 MBytes 9.62 Mbits/sec 147
[ 4] 6.01-7.01 sec 1.14 MBytes 9.57 Mbits/sec 146
[ 4] 7.01-8.01 sec 1.15 MBytes 9.62 Mbits/sec 147
[ 4] 8.01-9.00 sec 1.13 MBytes 9.57 Mbits/sec 145
[ 4] 9.00-10.00 sec 1.14 MBytes 9.56 Mbits/sec 146
- - - - - - - - - - - - - - - - - - - - - - - - -
[ ID] Interval Transfer Bandwidth Jitter Lost/Total Datagrams
[ 4] 0.00-10.00 sec 11.6 MBytes 9.74 Mbits/sec 0.548 ms 0/3 (0%)
[ 4] Sent 3 datagramsiperf Done.
驅(qū)動信息
# ethtool -i eth1
driver: enc28j60
version: 1.02
firmware-version:
expansion-rom-version:
bus-info: spi0.0
supports-statistics: no
supports-test: no
supports-eeprom-access: no
supports-register-dump: no
supports-priv-flags: no