上面的DIO maping 看懂就比较好理解了,DIO0,DIO1,DIO3,
发送和接收都可以触发中断,cad模式需要使用CADDetected和CADDone。
CADDetected这个中断就是有数据接收了,触发进入接收模式,前导码个数的计算还是比较简单的。
local lora = {}
local sys = require "sys"
local spiId = 2
local _nss=13
local _dio0=8
local _dio1=4
local _dio3=5
local MC1={
BW125=0x70,
BW250=0x80,
BW500=0x90,
BW150=0x00
}
local MC2={
FSK=0x00,
SF6=0x60,
SF7=0x70,
SF8=0x80,
SF9=0x90,
SF10=0xA0,
SF11=0xB0,
SF12=0xC0
}
-- MC1["4/5"]=0x02
-- MC1["4/6"]=0x04
-- MC1["4/7"]=0x06
-- MC1["4/8"]=0x08
local _freq=470000000
local _bw=MC1.BW125
local _sf=MC2.SF9
local cspin = gpio.setup(_nss, 1)
local rst = gpio.setup(9, 1)
--收发数据
local function sendRecv(data,len)
local r = ""
cspin(0)
if data then spi.send(spiId,data) end
if len then r = spi.recv(spiId,len) end
cspin(1)
return r
end
local function lora_read(addr)
return string.byte(sendRecv(string.char(addr),1))
end
local function lora_write(addr,value)
cspin(0)
spi.send(spiId,string.char(addr+0x80,value))
cspin(1)
end
local function setFreq(freqHz)
-- local FRF_MSB=0x06
-- local FRF_MID=0x07
-- local FRF_LSB=0x08
local frfMsb,frfMid,frfLsb=0x75,0x80,0x00
local frf=(freqHz/1000*2^11)/125
frfMsb = (frf>>16)%256
frfMid = (frf>>8)%256
frfLsb = (frf>>0)%256
lora_write(0x06,frfMsb)
lora_write(0x07,frfMid)
lora_write(0x08,frfLsb)
print(string.format("%0d hz %02X %02X %02X ",freqHz,frfMsb,frfMid,frfLsb))
end
local function setRate(sf,bw,cr,crc,iiq,powe)
-- local SF10=0xA0
-- local SF11=0xB0
-- local SF12=0xC0
-- local PA_CONFIG=0x09
-- local MODEM_CONFIG1=0x1D
-- local MODEM_CONFIG2=0x1E
-- local MODEM_CONFIG3=0x26
-- local SYMB_TIMEOUT_LSB=0x1F
-- local INVERT_IQ=0x33
local mc1=bit.bor(bw,cr)
local mc2=bit.bor(sf,crc)
--local mc3=0x04
local mc3=0x00 -- no AGC
if (sf == 0xB0 or sf == 0xC0) then mc3=0x08 end -- MC2.SF11=0xB0, MC2.SF12=0xC0
local stl=0x08
if (sf == 0xA0 or sf == 0xB0 or sf == 0xC0) then stl=0x05 end
local pac
if powe > 17 then pac = 0x8F -- 17dbm
elseif powe < -3 then pac = 0x20 -- -3dbm
elseif powe <= 12 then pac = 0x20+powe+3 -- -3dbm .. 12dbm
else pac = 0x80+powe-2 -- 13dbm .. 16dbm
end
lora_write(0x09,pac)--发射功率
lora_write(0x1D,mc1)
lora_write(0x1E,mc2)
lora_write(0x26,mc3)
lora_write(0x1F,stl)
lora_write(0x33,iiq)
end
local function setChannel(freq,sf)
-- local HOP_PERIOD=0x24
-- local FIFO_ADDR_PTR=0x0D
-- local FIFO_RX_BASE_AD=0x0F
-- local CR4_5=0x02
-- local CRC_ON=0x04
setFreq(freq)
setRate(sf,_bw,0x02,0x04,0x27,14) -- CR4/5=0x02, CRC_ON=0x04
lora_write(0x24,0x00)
lora_write(0x0D,lora_read(0x0F))
end
--射频接收数据
local function RcvData()
local count = lora_read(0x13)--获取数据长度
lora_write(0x0D,lora_read(0x10))--指针需要手动清零
return sendRecv(string.char(0x00),count)--读数据
end
--DIO0--RxDone------------------------------------------------------
local getCount=0
local function dio0handler()
lora_write(0x12,0xff)--清除全部的中断标志位
if lora_read(0x40) == 0x40 then
print("TxDone")
if station_Module=="main" then --主站
Rx_mode("lora")
end
else
print("RxDone")
local data_buff=RcvData()
local pkt_rssi=lora_read(0x1A)
local rssi=lora_read(0x1B)
pkt_rssi=-137+16/15*pkt_rssi
getCount=getCount+1
local buff=string.format("rssi:%d,pkt_rssi:%f,getCount:%d.",rssi,pkt_rssi,getCount)
print(data_buff,#data_buff,buff)
if station_Module=="main" then --主站
--RFSend(buff,"wor")
else
RFSend(buff,"lora")
end
end
end
--DIO1--CADDetected------------------------------------------------------
local function dio1handler()
Rx_mode("wor")
print("CADDetected")
lora_write(0x12,0xff)--清除全部的中断标志位
sys.timerStart(RF_Sleepmode,4000)
end
--DIO3---CADDone------------------------------------------------------
local function dio3handler()
print("CADDone")
lora_write(0x12,0xff)--清除全部的中断标志位
RF_Sleepmode()
end
--接收模式
function Rx_mode(flag)
--local REG_LR_OPMODE = 0x01
--local REG_LR_PREAMBLEMSB = 0x20
--local REG_LR_PREAMBLELSB = 0x21
--local REG_LR_PAYLOADLENGTH = 0x22
--local RFLR_OPMODE_STANDBY = 0x01
--local REG_LR_DIOMAPPING1 = 0x40
--local RFLR_DIOMAPPING1_DIO0_00= 0x00
--local REG_LR_FIFOADDRPTR = 0x0D
--local RFLR_OPMODE_RECEIVER = 0x05
--local REG_LR_IRQFLAGS = 0x12
--gpio.close(_dio0)
gpio.close(_dio1)
gpio.close(_dio3)
setConfig(cid,"rxd")
lora_write(0x01,0x81) -- set mode LoRa standby
if flag=="wor" then
lora_write(0x20,0x03)--前导码长度
lora_write(0x21,0xec)--前导码长度
elseif flag=="lora" then
lora_write(0x20,0)--前导码长度
lora_write(0x21,10)--前导码长度
end
lora_write(0x40,0x00)-- 把DIO0-RXDONE标志位写入中断映射寄存器
lora_write(0x12,0xff)-- clear interrupt flags
gpio.setup(_dio0,dio0handler,gpio.PULLDOWN,gpio.RISING)
lora_write(0x0D,lora_read(0x0E))
lora_write(0x01, 0x85)--set mode LoRa rxContinuous
end
--发送数据
function RFSend(SendBuf,flag)
--local REG_LR_OPMODE = 0x01
--local REG_LR_PREAMBLEMSB = 0x20
--local REG_LR_PREAMBLELSB = 0x21
--local REG_LR_PAYLOADLENGTH = 0x22
--local REG_LR_FIFOADDRPTR = 0x0D
--local REG_LR_IRQFLAGS = 0x12
--local REG_LR_IRQFLAGSMASK = 0x11
--local REG_LR_DIOMAPPING1 = 0x40
--local RFLR_IRQFLAGS_TXDONE = 0x08
setConfig(cid,"txd")
gpio.close(_dio0)
gpio.close(_dio1)
gpio.close(_dio3)
lora_write(0x01,0x81)
if flag=="wor" then
lora_write(0x20,0x03)--前导码长度
lora_write(0x21,0xec)--前导码长度
elseif flag=="lora" then
lora_write(0x20,0)--前导码长度
lora_write(0x21,10)--前导码长度
end
lora_write(0x40,0x40)--RFLR_DIOMAPPING1_DIO0_01
lora_write(0x12,0xff) --clear interrupt flags
gpio.setup(_dio0,dio0handler,gpio.PULLDOWN,gpio.RISING)
lora_write(0x0D,lora_read(0x0E))
lora_write(0x22,#SendBuf) --发送数据的长度
sendRecv(string.char(0x00+0x80)..SendBuf)
lora_write(0x01,0x83)--send
print("RFSend",SendBuf)
end
function RF_Sleepmode()
lora_write(0x01,0x81)
lora_write(0x12,0xff)
lora_write(0x01,0x80)--sleep
gpio.close(_dio0)
gpio.close(_dio1)
gpio.close(_dio3)
print("RF_Sleepmode")
--esp32.enterLightSleep(esp32.RTC, 4 * 1000 * 1000)
--RF_CAD_Sample()
sys.timerStart(RF_CAD_Sample ,4000)
end
-- 说明: DIO1--CADDetected DIO3---CADDone
-- 说明 : 采样时间约为(2^SF+32)/BW
function RF_CAD_Sample()
--local RFLR_DIOMAPPING1_DIO3_00 = 0x00
--local RFLR_DIOMAPPING1_DIO1_10 = 0x20
gpio.close(_dio0)
gpio.setup(_dio1,dio1handler,gpio.PULLDOWN,gpio.RISING)
gpio.setup(_dio3,dio3handler,gpio.PULLDOWN,gpio.RISING)
setConfig(cid,"rxd")
lora_write(0x01,0x81)
lora_write(0x20,0x1f)--设置前导码长度为最长
lora_write(0x21,0x48)
lora_write(0x40,0x20)--dio3:CadDone.dio1:CadDetected RFLR_DIOMAPPING1_DIO3_00 | RFLR_DIOMAPPING1_DIO1_10
lora_write(0x01,0x87)--cad
lora_write(0x12,0xff)--clear interrupt flags
print("RF_CAD_Sample")
end
station_Module="main1"
cid = 99991234
function setConfig(cid,rxtx)
local RFoffset=(cid % 250)* 200000
local RF=420000000
if rxtx=="rxd" then--接收
if station_Module=="main" then --主站
RF=420000000 + RFoffset
else
RF=470000000 + RFoffset
end
else --发送
if station_Module=="main" then
RF=470000000 + RFoffset
else
RF=420000000 + RFoffset
end
end
setChannel(RF,MC2.SF9)
end
function lora.sxInit()
rst(0)
local result = spi.setup(spiId, _nss,0,0,8,10000)
print("open",result)
if result ~= 0 then--返回值为0,表示打开成功
print("spi open error",result)
end
rst(1)
if lora_read(0x42) == 0x12 and lora_read(0x44)==0x2D then
print("start init")
else
print("Unknown radio")
--return
end
--local REG_LR_MODEMCONFIG3 = 0x26
--local REG_LR_MODEMCONFIG1 = 0x1D
--local REG_LR_MODEMCONFIG2 = 0x1E
--local REG_LR_PREAMBLELSB = 0x21
--local REG_LR_PARAMP = 0x0A
lora_write(0x01,0x00)--设置为SLEEP模式
lora_write(0x01,0x80)--并且设置为LORA模式
lora_write(0x01,0x81)--设置为STANDBY模式
lora_write(0x4D,0x07)--最大发射功率20dmb开启
lora_write(0x0A,0x02)-- 上升,下降的斜率设置为1000US
lora_write(0x0B,bit.bor(0x20,0x1B))--过流保护开启,最大承载电流为240MA
if station_Module == "main" then --主节点
sys.timerLoopStart(RFSend,8000,"HELLO WORLD 0000000000000 12355555555558844442222111","wor")
print("lora init 主节点 ok")
else --低功耗从机
--setChannel(_freq,MC2.SF9)
sys.timerStart(RF_Sleepmode,500)
print("lora init 从机 ok")
end
end
return lora
以上代码可以直接使用,
下面是接线图