luat_lib_pins.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. @module pins
  3. @summary 管脚外设复用
  4. @version 1.0
  5. @date 2025.4.10
  6. @tag @tag LUAT_USE_PINS
  7. @demo pins
  8. @usage
  9. -- 请使用LuaTools的可视化工具进行配置,你通常不需要使用这个demo
  10. -- 请使用LuaTools的可视化工具进行配置,你通常不需要使用这个demo
  11. -- 请使用LuaTools的可视化工具进行配置,你通常不需要使用这个demo
  12. -- 请使用LuaTools的可视化工具进行配置,你通常不需要使用这个demo
  13. -- 请使用LuaTools的可视化工具进行配置,你通常不需要使用这个demo
  14. -- 请使用LuaTools的可视化工具进行配置,你通常不需要使用这个demo
  15. -- 请使用LuaTools的可视化工具进行配置,你通常不需要使用这个demo
  16. -- 请使用LuaTools的可视化工具进行配置,你通常不需要使用这个demo
  17. -- 请使用LuaTools的可视化工具进行配置,你通常不需要使用这个demo
  18. -- 请使用LuaTools的可视化工具进行配置,你通常不需要使用这个demo
  19. -- 请使用LuaTools的可视化工具进行配置,你通常不需要使用这个demo
  20. -- 本库的API属于高级用法, 仅动态配置管脚时使用
  21. -- 本库的API属于高级用法, 仅动态配置管脚时使用
  22. -- 本库的API属于高级用法, 仅动态配置管脚时使用
  23. */
  24. #include "luat_base.h"
  25. #include "luat_pins.h"
  26. #include "luat_mcu.h"
  27. #include <stdlib.h>
  28. #include "luat_fs.h"
  29. #include "luat_mem.h"
  30. #include "luat_hmeta.h"
  31. #define LUAT_LOG_TAG "pins"
  32. #include "luat_log.h"
  33. /**
  34. 当某种外设允许复用在不同引脚上时,指定某个管脚允许复用成某种外设功能,需要在外设启用前配置好,外设启用时起作用。
  35. @api pins.setup(pin, func)
  36. @int 管脚物理编号, 对应模组俯视图下的顺序编号, 例如 67, 68
  37. @string 功能说明, 例如 "GPIO18", "UART1_TX", "UART1_RX", "SPI1_CLK", "I2C1_CLK", 目前支持的外设有"UART","I2C","SPI","PWM","CAN","GPIO","ONEWIRE"
  38. @return boolean 配置成功,返回true, 其他情况均返回false, 并在日志中提示失败原因
  39. @usage
  40. --把air780epm的PIN67脚,做GPIO 18用
  41. --pins.setup(67, "GPIO18")
  42. --把air780epm的PIN55脚,做uart2 rx用
  43. --pins.setup(55, "UART2_RXD")
  44. --把air780epm的PIN56脚,做uart2 tx用
  45. --pins.setup(56, "UART2_TXD")
  46. */
  47. static int l_pins_setup(lua_State *L){
  48. size_t len = 0;
  49. int pin = 0;
  50. int result = 0;
  51. int altfun_id = 0;
  52. const char* func_name = NULL;
  53. pin = luaL_optinteger(L, 1, -1);
  54. if (pin < 0)
  55. {
  56. LLOGE("pin序号参数错误");
  57. goto LUAT_PIN_SETUP_DONE;
  58. }
  59. else
  60. {
  61. if (lua_type(L, 2) == LUA_TSTRING) {
  62. func_name = luaL_checklstring(L, 2, &len);
  63. }
  64. else if (lua_isinteger(L, 2)) {
  65. altfun_id = luaL_checkinteger(L, 2);
  66. }
  67. else {
  68. LLOGE("参数错误");
  69. goto LUAT_PIN_SETUP_DONE;
  70. }
  71. result = luat_pins_setup(pin, func_name, len, altfun_id);
  72. }
  73. LUAT_PIN_SETUP_DONE:
  74. lua_pushboolean(L, result);
  75. return 1;
  76. }
  77. /**
  78. 将对应管脚变成高阻或者输入,不对外输出
  79. @api pins.close(pin)
  80. @int 管脚物理编号, 对应模组俯视图下的顺序编号, 例如 67, 68
  81. @return boolean 配置成功,返回true, 其他情况均返回false, 并在日志中提示失败原因
  82. @usage
  83. --把air780epm的PIN67脚关闭掉
  84. --pins.close(67)
  85. */
  86. static int l_pins_close(lua_State *L){
  87. int pin = 0;
  88. int result = 0;
  89. pin = luaL_optinteger(L, 1, -1);
  90. if (pin < 0)
  91. {
  92. LLOGE("pin序号参数错误");
  93. goto LUAT_PIN_SETUP_DONE;
  94. }
  95. else
  96. {
  97. luat_pin_function_description_t pin_function;
  98. if (luat_pin_get_description_from_num(pin, &pin_function))
  99. {
  100. LLOGE("pin%d不支持修改", pin);
  101. goto LUAT_PIN_SETUP_DONE;
  102. }
  103. luat_pin_iomux_info pin_info;
  104. pin_info.uid = pin_function.uid;
  105. luat_pin_close(pin_info);
  106. result = 1;
  107. }
  108. LUAT_PIN_SETUP_DONE:
  109. lua_pushboolean(L, result);
  110. return 1;
  111. }
  112. /**
  113. 加载硬件配置
  114. @api pins.loadjson(path)
  115. @string path, 配置文件路径, 可选, 默认值是 /luadb/pins.json
  116. @return boolean 成功返回true, 失败返回nil, 并在日志中提示失败原因
  117. @return int 失败返回错误码, 成功返回0
  118. @usage
  119. -- ,如果存在/luadb/pins_$model.json 就自动加载
  120. -- 其中的 $model是型号, 例如 Air780EPM, 默认加载的是 luadb/pins_Air780EPM.json
  121. -- 以下是自行加载配置的例子, 一般用不到
  122. pins.loadjson("/my.json")
  123. */
  124. static int l_pins_load(lua_State *L) {
  125. const char* path = luaL_checkstring(L, 1);
  126. int ret = luat_pins_load_from_file(path);
  127. lua_pushboolean(L, ret == 0 ? 1 : 0);
  128. lua_pushinteger(L, ret);
  129. return 2;
  130. }
  131. /**
  132. 调试模式
  133. @api pins.debug(mode)
  134. @boolean 是否开启调试模式, 默认是关闭的, 打开之后日志多很多
  135. @return nil 无返回值
  136. @usage
  137. pins.debug(true)
  138. */
  139. static int l_pins_debug(lua_State *L) {
  140. extern uint8_t g_pins_debug;
  141. if (lua_isinteger(L, 1)) {
  142. g_pins_debug = luaL_checkinteger(L, 1);
  143. }
  144. else if (lua_isboolean(L, 1)) {
  145. g_pins_debug = (uint8_t)lua_toboolean(L, 1);
  146. }
  147. lua_pushboolean(L, g_pins_debug);
  148. return 1;
  149. }
  150. #include "rotable2.h"
  151. static const rotable_Reg_t reg_pins[] =
  152. {
  153. {"setup", ROREG_FUNC(l_pins_setup)},
  154. {"close", ROREG_FUNC(l_pins_close)},
  155. {"loadjson", ROREG_FUNC(l_pins_load)},
  156. {"debug", ROREG_FUNC(l_pins_debug)},
  157. { NULL, ROREG_INT(0) }
  158. };
  159. LUAMOD_API int luaopen_pins( lua_State *L ) {
  160. luat_newlib2(L, reg_pins);
  161. char buff[64] = {0};
  162. char name[40] = {0};
  163. luat_hmeta_model_name(name);
  164. snprintf(buff, sizeof(buff), "/luadb/pins_%s.json", name);
  165. int ret = luat_pins_load_from_file(buff);
  166. if (ret == 0) {
  167. LLOGD("%s 加载和配置完成", buff);
  168. }
  169. return 1;
  170. }