main.lua 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. --- 模块功能:lorademo
  2. -- @module lora
  3. -- @author Dozingfiretruck
  4. -- @release 2021.06.17
  5. -- LuaTools需要PROJECT和VERSION这两个信息
  6. PROJECT = "lorademo"
  7. VERSION = "1.0.0"
  8. log.info("main", PROJECT, VERSION)
  9. -- sys库是标配
  10. _G.sys = require("sys")
  11. --添加硬狗防止程序卡死
  12. if wdt then
  13. wdt.init(9000)--初始化watchdog设置为9s
  14. sys.timerLoopStart(wdt.feed, 3000)--3s喂一次狗
  15. end
  16. local rtos_bsp = rtos.bsp()
  17. -- spi_id,pin_cs,pin_reset,pin_busy,pin_dio1
  18. local function lora_pin()
  19. if rtos_bsp == "AIR101" then
  20. return 0,pin.PB04,pin.PB00,pin.PB01,pin.PB06
  21. elseif rtos_bsp == "AIR103" then
  22. return 0,pin.PB04,pin.PB00,pin.PB01,pin.PB06
  23. elseif rtos_bsp == "AIR105" then
  24. return 5,pin.PC14,pin.PE08,pin.PE09,pin.PE06
  25. elseif rtos_bsp == "ESP32C3" then
  26. return 2,7,6,11,5
  27. elseif rtos_bsp == "ESP32S3" then
  28. return 2,14,15,13,12
  29. elseif rtos_bsp == "EC618" then
  30. return 0,8,1,18,19
  31. else
  32. log.info("main", "bsp not support")
  33. return
  34. end
  35. end
  36. local spi_id,pin_cs,pin_reset,pin_busy,pin_dio1 = lora_pin()
  37. sys.taskInit(function()
  38. spi_lora = spi.deviceSetup(spi_id,pin_cs,0,0,8,10*1000*1000,spi.MSB,1,0)
  39. lora_device = lora2.init("llcc68",{res = pin_reset,busy = pin_busy,dio1 = pin_dio1},spi_lora)
  40. print("lora_device",lora_device)
  41. lora_device:set_channel(433000000)
  42. lora_device:set_txconfig({mode=1,power=22,fdev=0,bandwidth=0,datarate=9,coderate=4,preambleLen=8,
  43. fixLen=false,crcOn=true,freqHopOn=0,hopPeriod=0,iqInverted=false,timeout=3000}
  44. )
  45. lora_device:set_rxconfig({mode=1,bandwidth=0,datarate=9,coderate=4,bandwidthAfc=0,preambleLen=8,symbTimeout=0,fixLen=false,
  46. payloadLen=0,crcOn=true,freqHopOn=0,hopPeriod=0,iqInverted=false,rxContinuous=false}
  47. )
  48. lora_device:on(function(lora_device, event, data, size)
  49. log.info("lora", "event", event, lora_device, data, size)
  50. if event == "tx_done" then
  51. lora_device:recv(1000)
  52. elseif event == "rx_done" then
  53. lora_device:send("PING")
  54. elseif event == "tx_timeout" then
  55. elseif event == "rx_timeout" then
  56. lora_device:recv(1000)
  57. elseif event == "rx_error" then
  58. end
  59. end)
  60. lora_device:send("PING")
  61. while 1 do
  62. print("lora")
  63. sys.wait(1000)
  64. end
  65. end)
  66. -- 用户代码已结束---------------------------------------------
  67. -- 结尾总是这一句
  68. sys.run()
  69. -- sys.run()之后后面不要加任何语句!!!!!