luat_lib_w5500.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. @module w5500
  3. @summary w5500以太网驱动
  4. @version 1.0
  5. @date 2022.04.11
  6. @tag LUAT_USE_W5500
  7. */
  8. #include "luat_base.h"
  9. #ifdef LUAT_USE_W5500
  10. #include "luat_rtos.h"
  11. #include "luat_zbuff.h"
  12. #include "luat_spi.h"
  13. #define LUAT_LOG_TAG "w5500"
  14. #include "luat_log.h"
  15. #include "luat_msgbus.h"
  16. #include "w5500_def.h"
  17. #include "luat_network_adapter.h"
  18. /*
  19. 初始化w5500
  20. @api w5500.init(spiid, speed, cs_pin, irq_pin, rst_pin, link_pin)
  21. @int spi通道号
  22. @int spi速度
  23. @int cs pin
  24. @int irq pin
  25. @int reset pin
  26. @int link 状态 pin,可以留空不使用,默认不使用
  27. @usage
  28. w5500.init(spi.SPI_0, 24000000, pin.PB13, pin.PC08, pin.PC09)
  29. */
  30. static int l_w5500_init(lua_State *L){
  31. luat_spi_t spi = {0};
  32. spi.id = luaL_checkinteger(L, 1);
  33. spi.bandrate = luaL_checkinteger(L, 2);
  34. spi.cs = luaL_checkinteger(L, 3);
  35. spi.CPHA = 0;
  36. spi.CPOL = 0;
  37. spi.master = 1;
  38. spi.mode = 1;
  39. spi.dataw = 8;
  40. spi.bit_dict = 1;
  41. uint32_t irq_pin = luaL_checkinteger(L, 4);
  42. uint32_t reset_pin = luaL_checkinteger(L, 5);
  43. uint32_t link_pin = luaL_optinteger(L, 6, 0xff);
  44. w5500_init(&spi, irq_pin, reset_pin, link_pin);
  45. return 0;
  46. }
  47. /*
  48. w5500配置网络信息
  49. @api w5500.config(ip, submask, gateway, mac, RTR, RCR, speed)
  50. @string 静态ip地址,如果需要用DHCP获取,请写nil
  51. @string 子网掩码,如果使用动态ip,则忽略
  52. @string 网关,如果使用动态ip,则忽略
  53. @string MAC,写nil则通过MCU唯一码自动生成,如果要写,长度必须是6byte
  54. @int 重试间隔时间,默认2000,单位100us,不懂的不要改
  55. @int 最大重试次数,默认8,不懂的不要改
  56. @int 速度类型,目前只有0硬件配置,1自适应,默认为0
  57. @usage
  58. w5500.config("192.168.1.2", "255.255.255.0", "192.168.1.1", string.fromHex("102a3b4c5d6e"))
  59. */
  60. static int l_w5500_config(lua_State *L){
  61. if (!w5500_device_ready())
  62. {
  63. lua_pushboolean(L, 0);
  64. LLOGD("device no ready");
  65. return 1;
  66. }
  67. if (lua_isstring(L, 1))
  68. {
  69. size_t ip_len = 0;
  70. const char* ip = luaL_checklstring(L, 1, &ip_len);
  71. size_t mask_len = 0;
  72. const char* mask = luaL_checklstring(L, 2, &mask_len);
  73. size_t gateway_len = 0;
  74. const char* gateway = luaL_checklstring(L, 3, &gateway_len);
  75. #ifdef LUAT_USE_LWIP
  76. ip_addr_t lwip_ip,lwip_mask,lwip_gateway;
  77. ipaddr_aton(ip, &lwip_ip);
  78. ipaddr_aton(mask, &lwip_mask);
  79. ipaddr_aton(gateway, &lwip_gateway);
  80. w5500_set_static_ip(lwip_ip.u_addr.ip4.addr, lwip_mask.u_addr.ip4.addr, lwip_gateway.u_addr.ip4.addr);
  81. #else
  82. w5500_set_static_ip(network_string_to_ipv4(ip, ip_len), network_string_to_ipv4(mask, mask_len), network_string_to_ipv4(gateway, gateway_len));
  83. #endif
  84. }
  85. else
  86. {
  87. w5500_set_static_ip(0, 0, 0);
  88. }
  89. if (lua_isstring(L, 4))
  90. {
  91. size_t mac_len = 0;
  92. const char *mac = luaL_checklstring(L, 4, &mac_len);
  93. w5500_set_mac((uint8_t*)mac);
  94. }
  95. w5500_set_param(luaL_optinteger(L, 5, 2000), luaL_optinteger(L, 6, 8), luaL_optinteger(L, 7, 0), 0);
  96. w5500_reset();
  97. lua_pushboolean(L, 1);
  98. return 1;
  99. }
  100. /*
  101. 将w5500注册进通用网络接口
  102. @api w5500.bind(id)
  103. @int 通用网络通道号
  104. @usage
  105. -- 若使用的版本不带socket库, 改成 network.ETH0
  106. w5500.bind(socket.ETH0)
  107. */
  108. static int l_w5500_network_register(lua_State *L){
  109. int index = luaL_checkinteger(L, 1);
  110. w5500_register_adapter(index);
  111. return 0;
  112. }
  113. /*
  114. 获取w5500当前的MAC,必须在init之后用,如果config中设置了自己的MAC,需要延迟一点时间再读
  115. @api w5500.getMAC()
  116. @return string 当前的MAC
  117. @usage
  118. local mac = w5500.getMAC()
  119. log.info("w5500 mac", mac:toHex())
  120. */
  121. static int l_w5500_get_mac(lua_State *L){
  122. uint8_t mac[6] = {0};
  123. w5500_get_mac(mac);
  124. lua_pushlstring(L, (const char*)mac, 6);
  125. return 1;
  126. }
  127. #include "rotable2.h"
  128. static const rotable_Reg_t reg_w5500[] =
  129. {
  130. { "init", ROREG_FUNC(l_w5500_init)},
  131. { "config", ROREG_FUNC(l_w5500_config)},
  132. { "bind", ROREG_FUNC(l_w5500_network_register)},
  133. { "getMac", ROREG_FUNC(l_w5500_get_mac)},
  134. { NULL, ROREG_INT(0)}
  135. };
  136. LUAMOD_API int luaopen_w5500( lua_State *L ) {
  137. luat_newlib2(L, reg_w5500);
  138. return 1;
  139. }
  140. static int l_nw_state_handler(lua_State *L, void* ptr) {
  141. (void)ptr;
  142. rtos_msg_t* msg = (rtos_msg_t*)lua_topointer(L, -1);
  143. lua_getglobal(L, "sys_pub");
  144. if (msg->arg1) {
  145. /*
  146. @sys_pub w5500
  147. 已联网
  148. IP_READY
  149. @usage
  150. -- 联网后会发一次这个消息
  151. sys.subscribe("IP_READY", function(ip, adapter)
  152. log.info("w5500", "IP_READY", ip, (adapter or -1) == socket.LWIP_GP)
  153. end)
  154. */
  155. lua_pushliteral(L, "IP_READY");
  156. uint32_t ip = msg->arg2;
  157. lua_pushfstring(L, "%d.%d.%d.%d", (ip) & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, (ip >> 24) & 0xFF);
  158. lua_pushinteger(L, NW_ADAPTER_INDEX_ETH0);
  159. lua_call(L, 3, 0);
  160. }
  161. else {
  162. /*
  163. @sys_pub w5500
  164. 已断网
  165. IP_LOSE
  166. @usage
  167. -- 断网后会发一次这个消息
  168. sys.subscribe("IP_LOSE", function(adapter)
  169. log.info("w5500", "IP_LOSE", (adapter or -1) == socket.ETH0)
  170. end)
  171. */
  172. lua_pushliteral(L, "IP_LOSE");
  173. lua_pushinteger(L, NW_ADAPTER_INDEX_ETH0);
  174. lua_call(L, 2, 0);
  175. }
  176. return 0;
  177. }
  178. // W5500的状态回调函数
  179. void w5500_nw_state_cb(int state, uint32_t ip) {
  180. rtos_msg_t msg = {0};
  181. msg.handler = l_nw_state_handler;
  182. msg.arg1 = state; // READY
  183. msg.arg2 = ip;
  184. luat_msgbus_put(&msg, 0);
  185. }
  186. #endif