pin.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2015-01-20 Bernard the first version
  9. * 2017-10-20 ZYH add mode open drain and input pull down
  10. */
  11. #ifndef PIN_H__
  12. #define PIN_H__
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. /* pin device and operations for RT-Thread */
  19. struct rt_device_pin
  20. {
  21. struct rt_device parent;
  22. const struct rt_pin_ops *ops;
  23. };
  24. #define PIN_LOW 0x00
  25. #define PIN_HIGH 0x01
  26. #define PIN_MODE_OUTPUT 0x00
  27. #define PIN_MODE_INPUT 0x01
  28. #define PIN_MODE_INPUT_PULLUP 0x02
  29. #define PIN_MODE_INPUT_PULLDOWN 0x03
  30. #define PIN_MODE_OUTPUT_OD 0x04
  31. #define PIN_IRQ_MODE_RISING 0x00
  32. #define PIN_IRQ_MODE_FALLING 0x01
  33. #define PIN_IRQ_MODE_RISING_FALLING 0x02
  34. #define PIN_IRQ_MODE_HIGH_LEVEL 0x03
  35. #define PIN_IRQ_MODE_LOW_LEVEL 0x04
  36. #define PIN_IRQ_DISABLE 0x00
  37. #define PIN_IRQ_ENABLE 0x01
  38. #define PIN_IRQ_PIN_NONE -1
  39. struct rt_device_pin_mode
  40. {
  41. rt_uint16_t pin;
  42. rt_uint16_t mode;
  43. };
  44. struct rt_device_pin_status
  45. {
  46. rt_uint16_t pin;
  47. rt_uint16_t status;
  48. };
  49. struct rt_pin_irq_hdr
  50. {
  51. rt_int16_t pin;
  52. rt_uint16_t mode;
  53. void (*hdr)(void *args);
  54. void *args;
  55. };
  56. struct rt_pin_ops
  57. {
  58. void (*pin_mode)(struct rt_device *device, rt_base_t pin, rt_base_t mode);
  59. void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_base_t value);
  60. int (*pin_read)(struct rt_device *device, rt_base_t pin);
  61. /* TODO: add GPIO interrupt */
  62. rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_int32_t pin,
  63. rt_uint32_t mode, void (*hdr)(void *args), void *args);
  64. rt_err_t (*pin_detach_irq)(struct rt_device *device, rt_int32_t pin);
  65. rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled);
  66. };
  67. int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data);
  68. void rt_pin_mode(rt_base_t pin, rt_base_t mode);
  69. void rt_pin_write(rt_base_t pin, rt_base_t value);
  70. int rt_pin_read(rt_base_t pin);
  71. rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
  72. void (*hdr)(void *args), void *args);
  73. rt_err_t rt_pin_detach_irq(rt_int32_t pin);
  74. rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled);
  75. #ifdef __cplusplus
  76. }
  77. #endif
  78. #endif