pin.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * File : pin.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2015-01-20 Bernard the first version
  23. * 2017-10-20 ZYH add mode open drain and input pull down
  24. */
  25. #ifndef PIN_H__
  26. #define PIN_H__
  27. #include <rtthread.h>
  28. #include <rtdevice.h>
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* pin device and operations for RT-Thread */
  33. struct rt_device_pin
  34. {
  35. struct rt_device parent;
  36. const struct rt_pin_ops *ops;
  37. };
  38. #define PIN_LOW 0x00
  39. #define PIN_HIGH 0x01
  40. #define PIN_MODE_OUTPUT 0x00
  41. #define PIN_MODE_INPUT 0x01
  42. #define PIN_MODE_INPUT_PULLUP 0x02
  43. #define PIN_MODE_INPUT_PULLDOWN 0x03
  44. #define PIN_MODE_OUTPUT_OD 0x04
  45. #define PIN_IRQ_MODE_RISING 0x00
  46. #define PIN_IRQ_MODE_FALLING 0x01
  47. #define PIN_IRQ_MODE_RISING_FALLING 0x02
  48. #define PIN_IRQ_DISABLE 0x00
  49. #define PIN_IRQ_ENABLE 0x01
  50. #define PIN_IRQ_PIN_NONE -1
  51. struct rt_device_pin_mode
  52. {
  53. rt_uint16_t pin;
  54. rt_uint16_t mode;
  55. };
  56. struct rt_device_pin_status
  57. {
  58. rt_uint16_t pin;
  59. rt_uint16_t status;
  60. };
  61. struct rt_pin_irq_hdr
  62. {
  63. rt_int16_t pin;
  64. rt_uint16_t mode;
  65. void (*hdr)(void *args);
  66. void *args;
  67. };
  68. struct rt_pin_ops
  69. {
  70. void (*pin_mode)(struct rt_device *device, rt_base_t pin, rt_base_t mode);
  71. void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_base_t value);
  72. int (*pin_read)(struct rt_device *device, rt_base_t pin);
  73. /* TODO: add GPIO interrupt */
  74. rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_int32_t pin,
  75. rt_uint32_t mode, void (*hdr)(void *args), void *args);
  76. rt_err_t (*pin_dettach_irq)(struct rt_device *device, rt_int32_t pin);
  77. rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled);
  78. };
  79. int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data);
  80. void rt_pin_mode(rt_base_t pin, rt_base_t mode);
  81. void rt_pin_write(rt_base_t pin, rt_base_t value);
  82. int rt_pin_read(rt_base_t pin);
  83. rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
  84. void (*hdr)(void *args), void *args);
  85. rt_err_t rt_pin_dettach_irq(rt_int32_t pin);
  86. rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled);
  87. int rt_device_pin_irq_register(const char *name, const struct rt_pin_ops *ops,
  88. void *user_data);
  89. #ifdef __cplusplus
  90. }
  91. #endif
  92. #endif