can_sleep.lua 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. --[[
  2. @module can_sleep
  3. @summary CAN总线休眠模式使用示例
  4. @version 1.0
  5. @date 2025.11.25
  6. @author 魏健强
  7. @usage
  8. 本文件为CAN总线休眠模式使用示例,核心业务逻辑为:
  9. 1. 初始化CAN总线
  10. 2. 空闲时间进入休眠模式,定时唤醒发送数据
  11. 本文件没有对外接口,直接在main.lua模块中require "can_self_test"就可以加载运行;
  12. ]]
  13. local can_id = 0
  14. local stb_pin = 28 -- 780EPM V1.3开发板上STB引脚为GPIO28
  15. local rx_id = 0x12345678
  16. local tx_id = 0x12345677
  17. local test_cnt = 0
  18. local tx_buf = zbuff.create(8) --创建zbuff
  19. local function can_cb(id, cb_type, param)
  20. if cb_type == can.CB_MSG then
  21. log.info("有新的消息")
  22. local succ, id, id_type, rtr, data = can.rx(id)
  23. while succ do
  24. log.info(mcu.x32(id), #data, data:toHex())
  25. succ, id, id_type, rtr, data = can.rx(id)
  26. end
  27. end
  28. if cb_type == can.CB_TX then
  29. if param then
  30. log.info("发送成功")
  31. else
  32. log.info("发送失败")
  33. end
  34. end
  35. if cb_type == can.CB_ERR then
  36. -- param参数就是4字节错误码
  37. log.error("CAN错误", "错误码:", string.format("0x%08X", param))
  38. -- 解析错误码
  39. local direction = (param >> 16) & 0xFF -- byte2: 方向
  40. local error_type = (param >> 8) & 0xFF -- byte1: 错误类型
  41. local position = param & 0xFF -- byte0: 错误位置
  42. -- 判断错误方向
  43. if direction == 0 then
  44. log.info("错误方向", "发送错误")
  45. else
  46. log.info("错误方向", "接收错误")
  47. end
  48. -- 判断错误类型
  49. if error_type == 0 then
  50. log.info("错误类型", "位错误")
  51. elseif error_type == 1 then
  52. log.info("错误类型", "格式错误")
  53. elseif error_type == 2 then
  54. log.info("错误类型", "填充错误")
  55. end
  56. -- 输出错误位置
  57. log.info("错误位置", string.format("0x%02X", position))
  58. end
  59. if cb_type == can.CB_STATE then
  60. -- 获取总线状态
  61. local state = can.state(can_id)
  62. log.info("can.state", "当前状态", state)
  63. -- 根据状态处理
  64. if state == can.STATE_ACTIVE then
  65. log.info("can.state", "总线正常")
  66. elseif state == can.STATE_PASSIVE then
  67. log.warn("can.state", "被动错误状态")
  68. elseif state == can.STATE_BUSOFF then
  69. log.error("can.state", "总线离线")
  70. -- 需要手动恢复
  71. can.reset(can_id)
  72. end
  73. end
  74. end
  75. local function can_tx_test()
  76. log.info("can tx")
  77. test_cnt = test_cnt + 1
  78. if test_cnt > 8 then
  79. test_cnt = 1
  80. end
  81. tx_buf:set(0,test_cnt) --zbuff的类似于memset操作,类似于memset(&buff[start], num, len)
  82. tx_buf:seek(test_cnt) --zbuff设置光标位置(可能与当前指针位置有关;执行后指针会被设置到指定位置)
  83. can.tx(can_id, tx_id, can.EXT, false, true, tx_buf)
  84. end
  85. -- can.debug(true)
  86. -- gpio.setup(stb_pin,0) -- 配置STB引脚为输出低电平
  87. gpio.setup(stb_pin, 1) -- 780EPM V1.3开发板STB信号有逻辑取反,要配置成输出高电
  88. can.init(can_id, 128) -- 初始化CAN,参数为CAN ID,接收缓存消息数的最大值
  89. can.on(can_id, can_cb) -- 注册CAN的回调函数
  90. can.timing(can_id, 1000000, 6, 6, 4, 2) --CAN总线配置时序
  91. can.node(can_id, rx_id, can.EXT) -- 设置过滤,只接收消息id为rx_id的扩展帧数据
  92. can.mode(can_id, can.MODE_SLEEP) -- 设置sleep模式
  93. local function CAN_MODE_SLEEP()
  94. while true do
  95. sys.wait(1000)
  96. log.info("can_state", can.state(can_id))
  97. if can.state(can_id) == can.STATE_ACTIVE then
  98. can.mode(can_id, can.MODE_SLEEP)
  99. end
  100. end
  101. end
  102. sys.taskInit(CAN_MODE_SLEEP)
  103. sys.timerLoopStart(can_tx_test, 10000)