luat_lib_w5500.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. @module w5500
  3. @summary w5500的硬件初始化,和注册进network适配器
  4. @version 1.0
  5. @date 2022.04.11
  6. */
  7. #include "luat_base.h"
  8. #ifdef LUAT_USE_W5500
  9. #include "luat_rtos.h"
  10. #include "luat_zbuff.h"
  11. #include "luat_spi.h"
  12. #define LUAT_LOG_TAG "w5500"
  13. #include "luat_log.h"
  14. #include "w5500_def.h"
  15. #include "luat_network_adapter.h"
  16. /*
  17. 初始化w5500
  18. @api w5500.init(spiid, speed, cs_pin, irq_pin, rst_pin, link_pin)
  19. @int spi通道号
  20. @int spi速度
  21. @int cs pin
  22. @int irq pin
  23. @int reset pin
  24. @usage
  25. w5500.init(spi.SPI_0, 24000000, pin.PB13, pin.PC08, pin.PC09)
  26. */
  27. static int l_w5500_init(lua_State *L){
  28. luat_spi_t spi = {0};
  29. spi.id = luaL_checkinteger(L, 1);
  30. spi.bandrate = luaL_checkinteger(L, 2);
  31. spi.cs = luaL_checkinteger(L, 3);
  32. spi.CPHA = 0;
  33. spi.CPOL = 0;
  34. spi.master = 1;
  35. spi.mode = 1;
  36. spi.dataw = 8;
  37. spi.bit_dict = 1;
  38. uint32_t irq_pin = luaL_checkinteger(L, 4);
  39. uint32_t reset_pin = luaL_checkinteger(L, 5);
  40. uint32_t link_pin = luaL_optinteger(L, 6, 0xff);
  41. w5500_init(&spi, irq_pin, reset_pin, link_pin);
  42. return 0;
  43. }
  44. #ifndef LUAT_USE_LWIP
  45. /*
  46. w5500配置网络信息
  47. @api w5500.config(ip, submask, gateway, mac, RTR, RCR, speed)
  48. @string 静态ip地址,如果需要用DHCP获取,请写nil
  49. @string 子网掩码,如果使用动态ip,则忽略
  50. @string 网关,如果使用动态ip,则忽略
  51. @string MAC,写nil则通过MCU唯一码自动生成,如果要写,长度必须是6byte
  52. @int 重试间隔时间,默认2000,单位100us,不懂的不要改
  53. @int 最大重试次数,默认8,不懂的不要改
  54. @int 速度类型,目前只有0硬件配置,1自适应,默认为0
  55. @usage
  56. w5500.config("192.168.1.2", "255.255.255.0", "192.168.1.1", string.fromHex("102a3b4c5d6e"))
  57. */
  58. static int l_w5500_config(lua_State *L){
  59. if (!w5500_device_ready())
  60. {
  61. lua_pushboolean(L, 0);
  62. LLOGD("device no ready");
  63. return 1;
  64. }
  65. if (lua_isstring(L, 1))
  66. {
  67. size_t ip_len = 0;
  68. const char* ip = luaL_checklstring(L, 1, &ip_len);
  69. size_t mask_len = 0;
  70. const char* mask = luaL_checklstring(L, 2, &mask_len);
  71. size_t gateway_len = 0;
  72. const char* gateway = luaL_checklstring(L, 3, &gateway_len);
  73. 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));
  74. }
  75. else
  76. {
  77. w5500_set_static_ip(0, 0, 0);
  78. }
  79. if (lua_isstring(L, 4))
  80. {
  81. size_t mac_len = 0;
  82. const char *mac = luaL_checklstring(L, 4, &mac_len);
  83. w5500_set_mac(mac);
  84. }
  85. w5500_set_param(luaL_optinteger(L, 5, 2000), luaL_optinteger(L, 6, 8), luaL_optinteger(L, 7, 0), 0);
  86. w5500_reset();
  87. lua_pushboolean(L, 1);
  88. return 1;
  89. }
  90. #endif
  91. /*
  92. 将w5500注册进通用网络接口
  93. @api w5500.bind(network.xxx)
  94. @int 通用网络通道号
  95. @usage
  96. w5500.bind(network.ETH0)
  97. */
  98. static int l_w5500_network_register(lua_State *L){
  99. int index = luaL_checkinteger(L, 1);
  100. w5500_register_adapter(index);
  101. return 0;
  102. }
  103. #include "rotable2.h"
  104. static const rotable_Reg_t reg_w5500[] =
  105. {
  106. { "init", ROREG_FUNC(l_w5500_init)},
  107. #ifndef LUAT_USE_LWIP
  108. { "config", ROREG_FUNC(l_w5500_config)},
  109. #endif
  110. { "bind", ROREG_FUNC(l_w5500_network_register)},
  111. { NULL, ROREG_INT(0)}
  112. };
  113. LUAMOD_API int luaopen_w5500( lua_State *L ) {
  114. luat_newlib2(L, reg_w5500);
  115. return 1;
  116. }
  117. #endif