xuxinyi 4 months ago
parent
commit
4d7c47ffbf
1 changed files with 52 additions and 18 deletions
  1. 52 18
      drv_periph/sx126x_driver.lua

+ 52 - 18
drv_periph/sx126x_driver.lua

@@ -62,29 +62,54 @@ function SX126xWriteCommand(command,buffer)
     log.info("SX126xWriteCommand",ret)
     sys.wait(20)
 end
+-- -----------------------------------------------SPI基础读写---------------------------------------------------
+-- 读取寄存器 完成
+-- function SX126xReadRegisters(address,size)
+--     local command = sx126x_reg.RadioCommands.RADIO_READ_REGISTER
+--     local sendData = command .. address .. "00"
+--     local receiverDataCount= size
 
-function SX126xReadRegisters(address,size)
-    local command = sx126x_reg.RadioCommands.RADIO_READ_REGISTER
-    local sendData = command .. address .. "00"
-    local receiverDataCount= size
+--     while size > 0 do
+--         sendData = sendData .. "00"
+--         size = size - 1
+--     end
 
-    while size > 0 do
-        sendData = sendData .. "00"
-        size = size - 1
-    end
+--     local result = Drv_spi.drv_spi_read_write_byte(sendData,receiverDataCount)  --这里乘2的意思是,16进制是两位,但是转成字符串的形式就是4位了。
 
-    local result = Drv_spi.drv_spi_read_write_byte(sendData,receiverDataCount*2)  --这里乘2的意思是,16进制是两位,但是转成字符串的形式就是4位了。
+--     log.info("SX126xReadRegisters",result)
+--     return result
+-- end
 
-    log.info("SX126xReadRegisters",result)
-    return result
+function SX126xReadRegisters(address,size)
+    local receiverData = {}
+    Drv_spi.drv_spi_read_write_byte(sx126x_reg.RadioCommands.RADIO_READ_REGISTER,0)
+    -- Drv_spi.drv_spi_read_write_byte(string.sub(address, 1, 2),0);
+    -- Drv_spi.drv_spi_read_write_byte(string.sub(address, 3, 4),0);
+    -- receiverData[0] = Drv_spi.drv_spi_read_write_byte("00",1)
+    -- log.info("SX126xReadRegisters:",receiverData[0])
+    Drv_spi.drv_spi_read_write_byte(string.sub(address, 1, 2),0);
+    Drv_spi.drv_spi_read_write_byte(string.sub(address, 3, 4),0);
+    Drv_spi.drv_spi_read_write_byte("0");
+
+    log.info("SX126xReadRegisters:",string.fromHex("1d"):toHex())
+    log.info("SX126xReadRegisters:",string.fromHex("07"):toHex())
+    log.info("SX126xReadRegisters:",string.fromHex("40"):toHex())
+    log.info("SX126xReadRegisters:",string.fromHex("00"):toHex())
+    log.info("SX126xReadRegisters:",string.fromHex("00"):toHex())
+    log.info("SX126xReadRegisters:",string.fromHex("1d07400000"):toHex())
+    for i = 1, size do
+        receiverData[i] = Drv_spi.drv_spi_read_write_byte("00",1)
+        log.info("SX126xReadRegisters:",i,":",receiverData[i])
+    end
+end
+--封装后的读寄存器
+function SX126xReadRegister(address)
+    local ret = SX126xReadRegisters(address,1)
+    return ret
 end
 
-
+-- 写入寄存器
 function SX126xWriteRegisters(address,buffer,size)
-    if address == nil or buffer == nil or size == nil then
-        log.error("SX126xWriteRegisters","参数错误")
-        return
-    end
     local command = sx126x_reg.RadioCommands.RADIO_WRITE_REGISTER
     local sendData = command .. address .. "00"
     local receiverDataCount= size
@@ -94,7 +119,9 @@ function SX126xWriteRegisters(address,buffer,size)
         size = size - 1
     end
 
-    local result = Drv_spi.drv_spi_read_write_byte(sendData,receiverDataCount*2)  --这里乘2的意思是,16进制是两位,但是转成字符串的形式就是4位了。
+    local result = Drv_spi.drv_spi_read_write_byte(sendData,receiverDataCount)  --这里乘2的意思是,16进制是两位,但是转成字符串的形式就是4位了。
+
+    
 
     log.info("SX126xReadRegisters",result)
     return result
@@ -391,7 +418,14 @@ sys.taskInit(function ()
         -- local ret = Drv_spi.drv_spi_read_write_byte("07400000",1)
         -- log.info("testSpiFlash.readFlashID",ret)
 
-        SX126xWriteCommand("1d",{"0740"})
+        -- SX126xWriteCommand("1d",{"0740"})
+        
+        -- log.info("计算结果", SX126xReadRegisters("0740",1))
+        log.info("testSpiFlash.sendCommand",spi.send(spi.SPI_1,string.char(0x1d)))--发数据
+        log.info("testSpiFlash.sendCommand",spi.send(spi.SPI_1,string.char(0x07)))--发数据
+        log.info("testSpiFlash.sendCommand",spi.send(spi.SPI_1,string.char(0x40)))--发数据
+        log.info("testSpiFlash.readFlashID",spi.recv(spi.SPI_1,6):toHex())--收数据
+        log.info("testSpiFlash.readFlashID",spi.recv(spi.SPI_1,6):toHex())--收数据
         sys.wait(2000)
     end
     spi.close(spi.SPI_1)