ch390h.lua 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. local ch390h = {
  2. buff = zbuff.create(2048)
  3. }
  4. local sys = require "sys"
  5. local spiId = 0
  6. local cs = 8
  7. local CS = gpio.setup(cs, 1, gpio.PULLUP)
  8. function ch390h.read(addr, len)
  9. if not len then
  10. len = 1
  11. end
  12. CS(0)
  13. local data = ""
  14. if addr == 0x72 then
  15. spi.send(spiId, string.char(addr))
  16. while len > 0 do
  17. if len > 64 then
  18. data = data .. spi.recv(spiId, 64)
  19. len = len - 64
  20. else
  21. data = data .. spi.recv(spiId, len)
  22. len = 0
  23. break
  24. end
  25. end
  26. else
  27. for i = 1, len, 1 do
  28. spi.send(spiId, string.char(addr + i - 1))
  29. data = data .. spi.recv(spiId, 1)
  30. end
  31. end
  32. CS(1)
  33. return data
  34. end
  35. function ch390h.write(addr, data)
  36. -- log.info("ch390h", "写入寄存器", addr, data, (string.char(addr | 0x80, data):toHex()))
  37. if type(data) == "number" then
  38. data = string.char(data)
  39. end
  40. CS(0)
  41. local tmp = string.char(addr | 0x80) .. data
  42. spi.send(spiId, tmp)
  43. -- local len = #tmp
  44. -- local offset = 1
  45. -- while len > 0 do
  46. -- if len > 64 then
  47. -- spi.send(spiId, data:sub(offset, offset + 64))
  48. -- len = len - 64
  49. -- offset = offset + 64
  50. -- else
  51. -- spi.send(spiId, data:sub(offset))
  52. -- break
  53. -- end
  54. -- end
  55. CS(1)
  56. -- log.info("读到的数据", addr, data:toHex())
  57. return data
  58. end
  59. function ch390h.mac()
  60. return ch390h.read(0x10, 6)
  61. end
  62. function ch390h.vid()
  63. return ch390h.read(0x28, 2)
  64. end
  65. function ch390h.pid()
  66. return ch390h.read(0x2A, 2)
  67. end
  68. function ch390h.revision()
  69. return ch390h.read(0x2C, 1)
  70. end
  71. function ch390h.default_config()
  72. ch390h.write(0, 0x01)
  73. sys.wait(20)
  74. ch390h.write(0x01, (1 << 5) | (1 << 3) | (1 << 2))
  75. ch390h.write(0x7E, 0xFF)
  76. ch390h.write(0x2D, 0x80)
  77. -- write_reg(0x31, 0x1F)
  78. ch390h.write(0x7F, 0xFF)
  79. ch390h.write(0x55, 0x01)
  80. ch390h.write(0x75, 0x0c)
  81. sys.wait(20)
  82. -- ch390h.enable_rx()
  83. end
  84. function ch390h.software_reset()
  85. ch390h.write(0, 0x01)
  86. sys.wait(20)
  87. ch390h.write(0, 0x00)
  88. ch390h.write(0, 0x01)
  89. sys.wait(20)
  90. end
  91. function ch390h.enable_rx()
  92. -- ch390h.write(0x05, (1 <<4) | (1 <<0) | (1 << 1) | (1 << 3))
  93. ch390h.write(0x05, (1 <<4) | (1 <<0) | (1 << 3))
  94. -- ch390h.write(0x05, (1 <<4) | (1 <<0))
  95. ch390h.write(0x1F, 0)
  96. end
  97. function ch390h.disable_rx()
  98. ch390h.write(0x05, 0)
  99. end
  100. function ch390h.enable_phy()
  101. ch390h.write(0x1F, 0)
  102. end
  103. function ch390h.link()
  104. local link = (ch390h.read(0x01, 1):byte() & (1 << 6))
  105. return link ~= 0
  106. end
  107. function ch390h.receive_packet()
  108. -- 先假读一次
  109. ch390h.read(0x70, 1)
  110. local rx_ready = ch390h.read(0x70, 1):byte()
  111. if (rx_ready & 0xFE) ~= 0x0 then
  112. log.info("ch390h", "出错了,复位!!!", (string.char(rx_ready):toHex()))
  113. ch390h.write(0x05, 0)
  114. ch390h.write(0x55, 1)
  115. ch390h.write(0x75, 0)
  116. sys.wait(1)
  117. -- ch390h.enable_rx()
  118. -- ch390h.software_reset()
  119. ch390h.default_config()
  120. -- ch390h.write(0x05, (1 <<4) | (1 <<3) | (1 <<1) | (1 <<0))
  121. return
  122. end
  123. if (rx_ready & 0x01) == 0 then
  124. -- log.info("ch390h", "没有数据", (string.char(rx_ready):toHex()))
  125. return -- 没有数据
  126. end
  127. -- 读4个字节
  128. local tmp = ch390h.read(0x72, 4)
  129. local rx_status = tmp:byte(2)
  130. local rx_len = tmp:byte(3) + (tmp:byte(4) << 8)
  131. -- log.info("ch390h", rx_status, rx_len, (tmp:toHex()))
  132. return ch390h.read(0x72, rx_len)
  133. end
  134. function ch390h.send_packet(buff)
  135. ch390h.write(0x78, buff:query())
  136. local tmp = ch390h.read(0x02, 1):byte()
  137. if (tmp & 0x01) == 0 then
  138. local len = buff:used()
  139. ch390h.write(0x7C, len & 0xFF)
  140. ch390h.write(0x7D, (len >> 8) & 0xFF)
  141. tmp = ch390h.read(0x02, 1):byte()
  142. ch390h.write(0x02, tmp | (1<<0))
  143. -- log.info("ch390h", "底层发送", buff:used())
  144. else
  145. log.info("ch390h", "底层忙")
  146. end
  147. end
  148. return ch390h