Browse Source

add: 完成ch390h非路由模式的验证

Wendal Chen 1 năm trước cách đây
mục cha
commit
996ec59ce9
2 tập tin đã thay đổi với 275 bổ sung0 xóa
  1. 166 0
      demo/ch390h/ch390h.lua
  2. 109 0
      demo/ch390h/main.lua

+ 166 - 0
demo/ch390h/ch390h.lua

@@ -0,0 +1,166 @@
+local ch390h = {
+    buff = zbuff.create(2048)
+}
+
+local sys = require "sys"
+
+local spiId = 0
+local cs = 8
+local CS = gpio.setup(cs, 1, gpio.PULLUP)
+
+function ch390h.read(addr, len)
+    if not len then
+        len = 1
+    end
+    CS(0)
+    local data = ""
+    if addr == 0x72 then
+        spi.send(spiId, string.char(addr))
+        while len > 0 do
+            if len > 64 then
+                data = data .. spi.recv(spiId, 64)
+                len = len - 64
+            else
+                data = data .. spi.recv(spiId, len)
+                len = 0
+                break
+            end
+        end
+    else
+        for i = 1, len, 1 do
+            spi.send(spiId, string.char(addr + i - 1))
+            data = data .. spi.recv(spiId, 1)
+        end
+    end
+    CS(1)
+    return data
+end
+
+function ch390h.write(addr, data)
+    -- log.info("ch390h", "写入寄存器", addr, data, (string.char(addr | 0x80, data):toHex()))
+    if type(data) == "number" then
+        data = string.char(data)
+    end
+    CS(0)
+    local tmp = string.char(addr | 0x80) .. data
+    spi.send(spiId, tmp)
+    -- local len = #tmp
+    -- local offset = 1
+    -- while len > 0 do
+    --     if len > 64 then
+    --         spi.send(spiId, data:sub(offset, offset + 64))
+    --         len = len - 64
+    --         offset = offset + 64
+    --     else
+    --         spi.send(spiId, data:sub(offset))
+    --         break
+    --     end
+    -- end
+    CS(1)
+    -- log.info("读到的数据", addr, data:toHex())
+    return data
+end
+
+function ch390h.mac()
+    return ch390h.read(0x10, 6)
+end
+
+function ch390h.vid()
+    return ch390h.read(0x28, 2)
+end
+
+function ch390h.pid()
+    return ch390h.read(0x2A, 2)
+end
+
+function ch390h.revision()
+    return ch390h.read(0x2C, 1)
+end
+
+function ch390h.default_config()
+    ch390h.write(0, 0x01)
+    sys.wait(20)
+    ch390h.write(0x01, (1 << 5) | (1 << 3) | (1 << 2))
+    ch390h.write(0x7E, 0xFF)
+    ch390h.write(0x2D, 0x80)
+    -- write_reg(0x31, 0x1F)
+    ch390h.write(0x7F, 0xFF)
+
+    ch390h.write(0x55, 0x01)
+    ch390h.write(0x75, 0x0c)
+    sys.wait(20)
+    -- ch390h.enable_rx()
+end
+
+function ch390h.software_reset()
+    ch390h.write(0, 0x01)
+    sys.wait(20)
+    ch390h.write(0, 0x00)
+    ch390h.write(0, 0x01)
+    sys.wait(20)
+end
+
+function ch390h.enable_rx()
+    -- ch390h.write(0x05, (1 <<4) | (1 <<0) | (1 << 1) | (1 << 3))
+    ch390h.write(0x05, (1 <<4) | (1 <<0) | (1 << 3))
+    -- ch390h.write(0x05, (1 <<4) | (1 <<0))
+    ch390h.write(0x1F, 0)
+end
+
+function ch390h.disable_rx()
+    ch390h.write(0x05, 0)
+end
+
+function ch390h.enable_phy()
+    ch390h.write(0x1F, 0)
+end
+
+function ch390h.link()
+    local link = (ch390h.read(0x01, 1):byte() & (1 << 6))
+    return link ~= 0
+end
+
+function ch390h.receive_packet()
+    -- 先假读一次
+    ch390h.read(0x70, 1)
+    local rx_ready = ch390h.read(0x70, 1):byte()
+    if (rx_ready & 0xFE) ~= 0x0 then
+        log.info("ch390h", "出错了,复位!!!", (string.char(rx_ready):toHex()))
+        ch390h.write(0x05, 0)
+        ch390h.write(0x55, 1)
+        ch390h.write(0x75, 0)
+        sys.wait(1)
+        -- ch390h.enable_rx()
+        -- ch390h.software_reset()
+        ch390h.default_config()
+        -- ch390h.write(0x05, (1 <<4) | (1 <<3) | (1 <<1) | (1 <<0))
+        return
+    end
+    if (rx_ready & 0x01) == 0 then
+        -- log.info("ch390h", "没有数据", (string.char(rx_ready):toHex()))
+        return -- 没有数据
+    end
+    -- 读4个字节
+    local tmp = ch390h.read(0x72, 4)
+    local rx_status = tmp:byte(2)
+    local rx_len = tmp:byte(3) + (tmp:byte(4) << 8)
+    log.info("ch390h", rx_status, rx_len, (tmp:toHex()))
+    return ch390h.read(0x72, rx_len)
+end
+
+function ch390h.send_packet(buff)
+    ch390h.write(0x78, buff:query())
+    local tmp = ch390h.read(0x02, 1):byte()
+    if (tmp & 0x01) == 0 then
+        local len = buff:used()
+        ch390h.write(0x7C, len & 0xFF)
+        ch390h.write(0x7D, (len >> 8) & 0xFF)
+        tmp = ch390h.read(0x02, 1):byte()
+        ch390h.write(0x02, tmp | (1<<0))
+        log.info("ch390h", "底层发送", buff:used())
+    else
+        log.info("ch390h", "底层忙")
+    end
+end
+
+return ch390h

+ 109 - 0
demo/ch390h/main.lua

@@ -0,0 +1,109 @@
+-- LuaTools需要PROJECT和VERSION这两个信息
+PROJECT = "ch390h"
+VERSION = "1.0.0"
+
+-- 使用合宙iot平台时需要这个参数
+PRODUCT_KEY = "xxx" -- 到 iot.openluat.com 创建项目,获取正确的项目id
+
+-- 引入必要的库文件(lua编写), 内部库不需要require
+sys = require("sys")
+sysplus = require("sysplus")
+
+
+ch390h = require "ch390h"
+
+function netif_write_out(id, buff)
+    -- log.info("ch390h", "out", buff:query():toHex())
+    ch390h.send_packet(buff)
+end
+
+sys.taskInit(function ()
+    sys.wait(100)
+    local result = spi.setup(
+        0,--串口id
+        nil,
+        0,--CPHA
+        0,--CPOL
+        8,--数据宽度
+        25600000--,--频率
+        -- spi.MSB,--高低位顺序    可选,默认高位在前
+        -- spi.master,--主模式     可选,默认主
+        -- spi.full--全双工       可选,默认全双工
+    )
+    print("open",result)
+    if result ~= 0 then--返回值为0,表示打开成功
+        print("spi open error",result)
+        return
+    end
+
+
+    ch390h.software_reset()
+    ch390h.default_config()
+
+    local mac = ch390h.mac()
+    log.info("MAC地址", (mac:toHex()))
+    log.info("vid", (ch390h.vid():toHex()), "pid", (ch390h.pid():toHex()))
+
+    
+    -- log.info("通用寄存器", (ch390h.read(0x1F):toHex()))
+
+    log.info("ulwip可用", ulwip)
+    local neti = socket.LWIP_ETH
+    ulwip.setup(neti, mac, netif_write_out, {zbuff_out=true})
+    ulwip.reg(neti)
+    -- ulwip.ip(neti, "192.168.1.219", "255.255.255.0", "192.168.1.1")
+
+    local prev_stat = false
+    ulwip.link(neti, prev_stat)
+    ch390h.disable_rx()
+    ch390h.enable_phy()
+    ulwip.dhcp(neti, true)
+    ulwip.updown(neti, true)
+    while 1 do
+        local link = ch390h.link()
+        -- log.info("网线状态", link, "通用寄存器", (ch390h.read(0x01):toHex()))
+        ch390h.write(0x05, (1 <<4) | (1 <<0) | (1 << 3))
+        if link ~= prev_stat then
+            log.info("ch390h", "网线状态变化", prev_stat, link)
+            if link then
+                ch390h.enable_rx()
+            end
+            ulwip.link(neti, link)
+            prev_stat = link
+        end
+        if link then
+            -- 读取rx寄存器长度
+            local tmp = ch390h.receive_packet()
+            if not tmp then
+                sys.wait(5)
+            else
+                -- https://packetor.com/
+                -- https://hpd.gasmi.net/
+                -- log.info("ch390h", (tmp:toHex()))
+                log.info("ch390h", "收到数据长度", #tmp)
+                ulwip.input(neti, tmp)
+            end
+        else
+            ch390h.enable_phy()
+            sys.wait(100)
+        end
+        
+    end
+end)
+
+
+sys.taskInit(function()
+    sys.waitUntil("IP_READY")
+    while 1 do
+        sys.wait(1000)
+        log.info("http", http.request("GET", "https://httpbin.air32.cn/get", nil, nil, {adapter=socket.LWIP_ETH}).wait())
+        log.info("lua", rtos.meminfo())
+        log.info("sys", rtos.meminfo("sys"))
+    end
+end)
+
+-- 用户代码已结束---------------------------------------------
+-- 结尾总是这一句
+sys.run()
+-- sys.run()之后后面不要加任何语句!!!!!
+