testUartGpioSwitch.lua 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. --- 模块功能:GPIO和UART切换功能测试
  2. -- @author openLuat
  3. -- @module gpio.testUartGpioSwitch
  4. -- @license MIT
  5. -- @copyright openLuat
  6. -- @release 2018.03.27
  7. module(...,package.seeall)
  8. require"pins"
  9. --uartuse:引脚当前是否做为uart功能使用,true表示是,其余的表示不是
  10. local uartid,uartuse = 2,true
  11. --[[
  12. 函数名:uartopn
  13. 功能 :打开uart
  14. 参数 :无
  15. 返回值:无
  16. ]]
  17. local function uartopn()
  18. uart.setup(uartid,115200,8,uart.PAR_NONE,uart.STOP_1)
  19. end
  20. --[[
  21. 函数名:uartclose
  22. 功能 :关闭uart
  23. 参数 :无
  24. 返回值:无
  25. ]]
  26. local function uartclose()
  27. uart.close(uartid)
  28. end
  29. --[[
  30. 函数名:switchtouart
  31. 功能 :切换到uart功能使用
  32. 参数 :无
  33. 返回值:无
  34. ]]
  35. local function switchtouart()
  36. log.info("switchtouart",uartuse)
  37. if not uartuse then
  38. --关闭gpio功能
  39. pins.close(pio.P0_20)
  40. pins.close(pio.P0_21)
  41. --打开uart功能
  42. uartopn()
  43. uartuse = true
  44. end
  45. end
  46. --[[
  47. 函数名:switchtogpio
  48. 功能 :切换到gpio功能使用
  49. 参数 :无
  50. 返回值:无
  51. ]]
  52. local function switchtogpio()
  53. log.info("switchtogpio",uartuse)
  54. if uartuse then
  55. --关闭uart功能
  56. uartclose()
  57. pins.setup(pio.P0_20,1)
  58. pins.setup(pio.P0_21,0)
  59. uartuse = false
  60. end
  61. end
  62. --[[
  63. 函数名:switch
  64. 功能 :切换uart和gpio功能
  65. 参数 :无
  66. 返回值:无
  67. ]]
  68. local function switch()
  69. if uartuse then
  70. switchtogpio()
  71. else
  72. switchtouart()
  73. end
  74. end
  75. uartopn()
  76. --循环定时器,5秒切换一次功能
  77. sys.timerLoopStart(switch,5000)