gpio_dev.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * RT-Thread RuiChing
  3. *
  4. * COPYRIGHT (C) 2024-2025 Shanghai Real-Thread Electronic Technology Co., Ltd.
  5. * All rights reserved.
  6. *
  7. * The license and distribution terms for this file may be
  8. * found in the file LICENSE in this distribution.
  9. */
  10. #include <rtdevice.h>
  11. #include <gpio_dev.h>
  12. static struct rt_device *gs_device = RT_NULL;
  13. void rt_pin_mode(rt_base_t pin, rt_uint8_t mode)
  14. {
  15. struct rt_device_pin_mode pin_mode;
  16. if (!gs_device)
  17. {
  18. gs_device = rt_device_find("gpio");
  19. rt_device_open(gs_device, RT_DEVICE_FLAG_RDWR);
  20. }
  21. pin_mode.pin = pin;
  22. pin_mode.mode = mode;
  23. rt_device_control(gs_device, RT_PIN_DEV_CTRL_MODE, &pin_mode);
  24. }
  25. void rt_pin_write(rt_base_t pin, rt_ssize_t value)
  26. {
  27. struct rt_device_pin_value pin_value;
  28. if (!gs_device)
  29. {
  30. gs_device = rt_device_find("gpio");
  31. rt_device_open(gs_device, RT_DEVICE_FLAG_RDWR);
  32. }
  33. pin_value.pin = pin;
  34. pin_value.value = value;
  35. rt_device_write(
  36. gs_device, 0, &pin_value, sizeof(struct rt_device_pin_value));
  37. }
  38. rt_ssize_t rt_pin_read(rt_base_t pin)
  39. {
  40. struct rt_device_pin_value pin_value;
  41. if (!gs_device)
  42. {
  43. gs_device = rt_device_find("gpio");
  44. rt_device_open(gs_device, RT_DEVICE_FLAG_RDWR);
  45. }
  46. pin_value.pin = pin;
  47. rt_device_read(
  48. gs_device, 0, &pin_value, sizeof(struct rt_device_pin_value));
  49. return pin_value.value;
  50. }
  51. rt_base_t rt_pin_get(const char *name)
  52. {
  53. struct rt_pin_name pin_name;
  54. if (!gs_device)
  55. {
  56. gs_device = rt_device_find("gpio");
  57. rt_device_open(gs_device, RT_DEVICE_FLAG_RDWR);
  58. }
  59. pin_name.name = name;
  60. rt_device_control(gs_device, RT_PIN_DEV_CTRL_GET_NAME, &pin_name);
  61. return pin_name.pin;
  62. }
  63. rt_err_t rt_pin_attach_irq(
  64. rt_base_t pin, rt_uint8_t mode, void (*hdr)(void *args), void *args)
  65. {
  66. struct rt_pin_irq_hdr pin_irq_hdr;
  67. if (!gs_device)
  68. {
  69. gs_device = rt_device_find("gpio");
  70. rt_device_open(gs_device, RT_DEVICE_FLAG_RDWR);
  71. }
  72. pin_irq_hdr.pin = pin;
  73. pin_irq_hdr.mode = mode;
  74. pin_irq_hdr.hdr = hdr;
  75. pin_irq_hdr.args = args;
  76. return rt_device_control(gs_device, RT_PIN_DEV_CTRL_SET_HDR, &pin_irq_hdr);
  77. }
  78. rt_err_t rt_pin_detach_irq(rt_base_t pin)
  79. {
  80. if (!gs_device)
  81. {
  82. gs_device = rt_device_find("gpio");
  83. rt_device_open(gs_device, RT_DEVICE_FLAG_RDWR);
  84. }
  85. return rt_device_control(gs_device, RT_PIN_DEV_CTRL_GET_NAME, &pin);
  86. }
  87. rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint8_t enabled)
  88. {
  89. if (!gs_device)
  90. {
  91. gs_device = rt_device_find("gpio");
  92. rt_device_open(gs_device, RT_DEVICE_FLAG_RDWR);
  93. }
  94. if (enabled)
  95. {
  96. return rt_device_control(gs_device, RT_PIN_DEV_CTRL_ENABLE_IRQ, &pin);
  97. }
  98. return rt_device_control(gs_device, RT_PIN_DEV_CTRL_DISABLE_IRQ, &pin);
  99. }
  100. rt_err_t rt_pin_debounce(rt_base_t pin, rt_uint32_t debounce)
  101. {
  102. struct rt_pin_debounce pin_debounce;
  103. if (!gs_device)
  104. {
  105. gs_device = rt_device_find("gpio");
  106. rt_device_open(gs_device, RT_DEVICE_FLAG_RDWR);
  107. }
  108. pin_debounce.pin = pin;
  109. pin_debounce.debounce = debounce;
  110. return rt_device_control(
  111. gs_device, RT_PIN_DEV_CTRL_DEBOUNCE, &pin_debounce);
  112. }