ch390h_api.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #include "luat_base.h"
  2. #include "luat_netdrv.h"
  3. #include "luat_network_adapter.h"
  4. #include "luat_netdrv_ch390h.h"
  5. #include "luat_malloc.h"
  6. #include "luat_spi.h"
  7. #include "luat_gpio.h"
  8. #include "net_lwip2.h"
  9. #include "luat_ulwip.h"
  10. #include "lwip/tcp.h"
  11. #include "lwip/sys.h"
  12. #include "lwip/tcpip.h"
  13. #include "luat_ch390h.h"
  14. #define LUAT_LOG_TAG "ch390h.api"
  15. #include "luat_log.h"
  16. int luat_ch390h_read(ch390h_t* ch, uint8_t addr, uint16_t count, uint8_t* buff) {
  17. char tmp[1] = {0};
  18. int ret = 0;
  19. luat_spi_lock(ch->spiid);
  20. if (addr == 0x72) {
  21. tmp[0] = addr;
  22. luat_gpio_set(ch->cspin, 0);
  23. ret = luat_spi_send(ch->spiid, tmp, 1);
  24. if (ret != 1) {
  25. LLOGE("luat_spi_send失败 expect=1 ret=%d", ret);
  26. luat_gpio_set(ch->cspin, 1);
  27. luat_spi_unlock(ch->spiid);
  28. return -1;
  29. }
  30. char* ptr = (char*)buff;
  31. while (count > 0) {
  32. if (count > 256) {
  33. ret = luat_spi_recv(ch->spiid, ptr, 256);
  34. count -= 256;
  35. ptr += 256;
  36. }
  37. else {
  38. ret = luat_spi_recv(ch->spiid, ptr, count);
  39. break;
  40. }
  41. }
  42. luat_gpio_set(ch->cspin, 1);
  43. }
  44. else {
  45. for (size_t i = 0; i < count; i++)
  46. {
  47. tmp[0] = addr + i;
  48. luat_gpio_set(ch->cspin, 0);
  49. luat_spi_send(ch->spiid, tmp, 1);
  50. luat_spi_recv(ch->spiid, (char*)&buff[i], 1);
  51. luat_gpio_set(ch->cspin, 1);
  52. }
  53. }
  54. luat_spi_unlock(ch->spiid);
  55. return 0;
  56. }
  57. int luat_ch390h_write(ch390h_t* ch, uint8_t addr, uint16_t count, uint8_t* buff) {
  58. if (ch->txtmp == NULL) {
  59. ch->txtmp = luat_heap_malloc(3 * 1024);
  60. if (ch->txtmp == NULL) {
  61. LLOGE("txtmp内存分配失败");
  62. return -1;
  63. }
  64. }
  65. if (count > 1600) {
  66. LLOGW("数据包过大(%d),丢弃", count);
  67. return -2;
  68. }
  69. luat_spi_lock(ch->spiid);
  70. if (addr == 0x78) {
  71. ch->txtmp[0] = addr | 0x80;
  72. memcpy(ch->txtmp+1, buff, count);
  73. luat_gpio_set(ch->cspin, 0);
  74. luat_spi_send(ch->spiid, (const char* )ch->txtmp, 1 + count);
  75. luat_gpio_set(ch->cspin, 1);
  76. }
  77. else {
  78. for (size_t i = 0; i < count; i++)
  79. {
  80. ch->txtmp[0] = (addr + i) | 0x80;
  81. ch->txtmp[1] = buff[i];
  82. luat_gpio_set(ch->cspin, 0);
  83. luat_spi_send(ch->spiid, (const char* )ch->txtmp, 2);
  84. luat_gpio_set(ch->cspin, 1);
  85. }
  86. }
  87. luat_spi_unlock(ch->spiid);
  88. return 0;
  89. }
  90. int luat_ch390h_write_reg(ch390h_t* ch, uint8_t addr, uint8_t value) {
  91. uint8_t buff[1] = {0};
  92. buff[0] = value;
  93. luat_ch390h_write(ch, addr, 1, buff);
  94. return 0;
  95. }
  96. int luat_ch390h_read_mac(ch390h_t* ch, uint8_t* buff) {
  97. return luat_ch390h_read(ch, 0x10, 6, buff);
  98. }
  99. int luat_ch390h_read_vid_pid(ch390h_t* ch, uint8_t* buff) {
  100. return luat_ch390h_read(ch, 0x28, 4, buff);
  101. }
  102. int luat_ch390h_basic_config(ch390h_t* ch) {
  103. /*
  104. ch390h.write(0, 0x01)
  105. sys.wait(20)
  106. ch390h.write(0x01, (1 << 5) | (1 << 3) | (1 << 2))
  107. ch390h.write(0x7E, 0xFF)
  108. ch390h.write(0x2D, 0x80)
  109. -- write_reg(0x31, 0x1F)
  110. ch390h.write(0x7F, 0xFF)
  111. ch390h.write(0x55, 0x01)
  112. ch390h.write(0x75, 0x0c)
  113. sys.wait(20)
  114. -- ch390h.enable_rx()
  115. */
  116. luat_ch390h_write_reg(ch, 0x7E, 0xFF);
  117. luat_ch390h_write_reg(ch, 0x2D, 0x80);
  118. luat_ch390h_write_reg(ch, 0x7F, 0xFF);
  119. luat_ch390h_write_reg(ch, 0x55, 0x01);
  120. luat_ch390h_write_reg(ch, 0x75, 0x0C);
  121. return 0;
  122. }
  123. int luat_ch390h_software_reset(ch390h_t* ch) {
  124. // ch390h.write(0, 0x01)
  125. luat_ch390h_write_reg(ch, 0x00, 0x01);
  126. // sys.wait(20)
  127. // ch390h.write(0, 0x00)
  128. luat_ch390h_write_reg(ch, 0x00, 0x00);
  129. // ch390h.write(0, 0x01)
  130. luat_ch390h_write_reg(ch, 0x00, 0x01);
  131. // sys.wait(20)
  132. return 0;
  133. }
  134. int luat_ch390h_set_rx(ch390h_t* ch, int enable) {
  135. // ch390h.write(0x05, (1 <<4) | (1 <<0) | (1 << 3))
  136. if (enable) {
  137. luat_ch390h_write_reg(ch, 0x05, (1 <<4) | (1 <<0) | (1 << 3));
  138. }
  139. else {
  140. luat_ch390h_write_reg(ch, 0x05, 0);
  141. }
  142. return 0;
  143. }
  144. int luat_ch390h_set_phy(ch390h_t* ch, int enable) {
  145. uint8_t buff[1] = {enable == 0 ? 1 : 0};
  146. luat_ch390h_write(ch, 0x1F, 1, buff);
  147. return 0;
  148. }
  149. int luat_ch390h_read_pkg(ch390h_t* ch, uint8_t *buff, uint16_t* len) {
  150. uint8_t tmp[4] = {0};
  151. // 先假读一次
  152. luat_ch390h_read(ch, 0x70, 1, tmp);
  153. tmp[0] = 0;
  154. // 真正读取一次
  155. luat_ch390h_read(ch, 0x70, 1, tmp);
  156. uint8_t MRCMDX = tmp[0];
  157. uint8_t MRCMDX2 = tmp[0];
  158. if (MRCMDX & 0xFE) {
  159. // 出错了!!
  160. // 再读一次
  161. luat_ch390h_read(ch, 0x70, 1, tmp);
  162. luat_ch390h_read(ch, 0x70, 1, tmp);
  163. MRCMDX2 = tmp[0];
  164. if (MRCMDX2 & 0xFE) {
  165. LLOGW("MRCMDX %02X/%02X !!! reset self", MRCMDX, MRCMDX2);
  166. return -1;
  167. }
  168. }
  169. if ((MRCMDX & 0x01) == 0) {
  170. // 没有数据
  171. *len = 0;
  172. return 0;
  173. }
  174. luat_ch390h_read(ch, 0x72, 4, tmp);
  175. *len = tmp[2] + (tmp[3] << 8);
  176. if (*len == 0) {
  177. return 1; // 出错了啊!!!
  178. }
  179. if (*len > 1600) {
  180. LLOGE("pkg too large %ld", len);
  181. return 2;
  182. }
  183. luat_ch390h_read(ch, 0x72, *len, buff);
  184. return 0;
  185. }
  186. int luat_ch390h_write_pkg(ch390h_t* ch, uint8_t *buff, uint16_t len) {
  187. uint8_t tmp[4] = {0};
  188. luat_ch390h_read(ch, 0x02, 1, tmp);
  189. uint8_t TCR = tmp[0];
  190. uint8_t NCR = 0;
  191. uint8_t NSR = 0;
  192. if (TCR & 0x01) {
  193. // busy!! 增加重试次数
  194. for (size_t i = 0; i < 100; i++)
  195. {
  196. luat_timer_us_delay(10);
  197. luat_ch390h_read(ch, 0x02, 1, tmp);
  198. TCR = tmp[0];
  199. if (!(TCR & 0x01)) {
  200. ch->tx_busy_count = 0; // 成功后清零计数器
  201. break;
  202. }
  203. }
  204. if (TCR & 0x01) {
  205. ch->tx_busy_count++;
  206. LLOGW("tx busy, drop pkg len %d, busy_count=%d", len, ch->tx_busy_count);
  207. // 只有连续多次TX忙且距离上次复位超过5秒才复位
  208. extern uint64_t luat_mcu_tick64_ms(void);
  209. uint32_t now = (uint32_t)luat_mcu_tick64_ms();
  210. if (ch->tx_busy_count >= 10 && (now - ch->last_reset_time > 5000)) {
  211. // 读出NCR 和 NSR
  212. luat_ch390h_read(ch, 0x00, 1, tmp);
  213. NCR = tmp[0];
  214. luat_ch390h_read(ch, 0x01, 1, tmp);
  215. NSR = tmp[0];
  216. LLOGE("连续TX忙超过阈值,执行复位 NCR=%02X NSR=%02X", NCR, NSR);
  217. LLOGD("NCR->FDR %02X NSR->SPEED %02X NSR->LINKST %02X", NCR & (1<<3), NSR & (1<<7), NSR & (1<<6));
  218. luat_ch390h_software_reset(ch);
  219. ch->tx_busy_count = 0;
  220. ch->last_reset_time = now;
  221. ch->total_reset_count++;
  222. luat_timer_mdelay(2);
  223. }
  224. return -1;
  225. }
  226. }
  227. luat_ch390h_write_reg(ch, 0x55, 2); // 发数据之前重置一下tx的内存指针
  228. // 写入下一个数据
  229. luat_ch390h_write(ch, 0x78, len, buff);
  230. // TCR == 0之后, 才能写入长度
  231. luat_ch390h_write_reg(ch, 0x7C, len & 0xFF);
  232. luat_ch390h_write_reg(ch, 0x7D, (len >> 8) & 0xFF);
  233. // 再读一次TCR
  234. luat_ch390h_read(ch, 0x02, 1, tmp);
  235. // 发送
  236. luat_ch390h_write_reg(ch, 0x02, tmp[0] | 0x01);
  237. return 0;
  238. }