pin.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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_MODE_HIGH_LEVEL 0x03
  49. #define PIN_IRQ_MODE_LOW_LEVEL 0x04
  50. #define PIN_IRQ_DISABLE 0x00
  51. #define PIN_IRQ_ENABLE 0x01
  52. #define PIN_IRQ_PIN_NONE -1
  53. struct rt_device_pin_mode
  54. {
  55. rt_uint16_t pin;
  56. rt_uint16_t mode;
  57. };
  58. struct rt_device_pin_status
  59. {
  60. rt_uint16_t pin;
  61. rt_uint16_t status;
  62. };
  63. struct rt_pin_irq_hdr
  64. {
  65. rt_int16_t pin;
  66. rt_uint16_t mode;
  67. void (*hdr)(void *args);
  68. void *args;
  69. };
  70. struct rt_pin_ops
  71. {
  72. void (*pin_mode)(struct rt_device *device, rt_base_t pin, rt_base_t mode);
  73. void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_base_t value);
  74. int (*pin_read)(struct rt_device *device, rt_base_t pin);
  75. /* TODO: add GPIO interrupt */
  76. rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_int32_t pin,
  77. rt_uint32_t mode, void (*hdr)(void *args), void *args);
  78. rt_err_t (*pin_dettach_irq)(struct rt_device *device, rt_int32_t pin);
  79. rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled);
  80. };
  81. int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data);
  82. void rt_pin_mode(rt_base_t pin, rt_base_t mode);
  83. void rt_pin_write(rt_base_t pin, rt_base_t value);
  84. int rt_pin_read(rt_base_t pin);
  85. rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
  86. void (*hdr)(void *args), void *args);
  87. rt_err_t rt_pin_dettach_irq(rt_int32_t pin);
  88. rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled);
  89. int rt_device_pin_irq_register(const char *name, const struct rt_pin_ops *ops,
  90. void *user_data);
  91. #ifdef __cplusplus
  92. }
  93. #endif
  94. #endif