luat_lib_pins.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. @module pins
  3. @summary 管脚外设复用
  4. @version 1.0
  5. @date 2025.4.10
  6. @tag LUAT_USE_PINS
  7. @usage
  8. */
  9. #include "luat_base.h"
  10. #include "luat_pin.h"
  11. #include "luat_mcu.h"
  12. #include <stdlib.h>
  13. #define LUAT_LOG_TAG "pins"
  14. #include "luat_log.h"
  15. static luat_pin_peripheral_function_description_t luat_pins_function_analyze(char *string)
  16. {
  17. const char *peripheral_names[LUAT_MCU_PERIPHERAL_QTY] = {
  18. "UART","I2C","SPI","PWM","CAN","GPIO","I2S","SDIO","LCD","CAMERA","ONEWIRE","KEYBORAD"
  19. };
  20. const char *function0_names[3] = {
  21. "RX","SCL","MOSI",
  22. };
  23. const char *function1_names[3] = {
  24. "TX","SDA","MISO",
  25. };
  26. const char *function2_names[4] = {
  27. "RTS","CLK","STB","BCLK"
  28. };
  29. const char *function3_names[3] = {
  30. "CTS","CS","LRCLK"
  31. };
  32. const char *function4_names[2] = {
  33. "MCLK","CMD"
  34. };
  35. }
  36. /**
  37. 当某种外设允许复用在不同引脚上时,指定某个管脚允许复用成某种外设功能,需要在外设启用前配置好,外设启用时起作用。
  38. @api pins.setup(pin, func)
  39. @int 管脚物理编号, 对应模组俯视图下的顺序编号, 例如 67, 68
  40. @string 功能说明, 例如 "GPIO18", "UART1_TX", "UART1_RX", "SPI1_CLK", "I2C1_CLK", 目前支持的外设有"UART","I2C","SPI","PWM","CAN","GPIO","ONEWIRE"
  41. @return boolean 配置成功,返回true, 其他情况均返回nil, 并在日志中提示失败原因
  42. @usage
  43. -- 把air780epm的PIN67脚,做GPIO 18用
  44. --pins.setup(67, "GPIO18")
  45. -- 把air780epm的PIN55脚,做uart2 rx用
  46. --pins.setup(55, "UART2_RX")
  47. -- 把air780epm的PIN56脚,做uart2 tx用
  48. --pins.setup(56, "UART2_TX")
  49. */
  50. static int luat_pins_setup(lua_State *L){
  51. return 1;
  52. }
  53. /**
  54. 加载硬件配置,如果存在/luadb/pins.json,开机后自动加载/luadb/pins.json,无需调用
  55. @api pins.load(path)
  56. @string path, 配置文件路径, 可选, 默认值是 /luadb/pins.json
  57. @return boolean 成功返回true, 失败返回false
  58. @usage
  59. pins.load()
  60. pins.load("/my.json")
  61. */
  62. static int luat_pins_load(lua_State *L){
  63. return 1;
  64. }
  65. #include "rotable2.h"
  66. static const rotable_Reg_t reg_pins[] =
  67. {
  68. {"setup", ROREG_FUNC(luat_pins_setup)},
  69. {"load", ROREG_FUNC(luat_pins_load)},
  70. { NULL, ROREG_INT(0) }
  71. };
  72. LUAMOD_API int luaopen_pins( lua_State *L ) {
  73. luat_newlib2(L, reg_pins);
  74. return 1;
  75. }