pin.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. /* pin device and operations for RT-Thread */
  18. struct rt_device_pin
  19. {
  20. struct rt_device parent;
  21. const struct rt_pin_ops *ops;
  22. };
  23. #define PIN_LOW 0x00
  24. #define PIN_HIGH 0x01
  25. #define PIN_MODE_OUTPUT 0x00
  26. #define PIN_MODE_INPUT 0x01
  27. #define PIN_MODE_INPUT_PULLUP 0x02
  28. #define PIN_MODE_INPUT_PULLDOWN 0x03
  29. #define PIN_MODE_OUTPUT_OD 0x04
  30. #define PIN_IRQ_MODE_RISING 0x00
  31. #define PIN_IRQ_MODE_FALLING 0x01
  32. #define PIN_IRQ_MODE_RISING_FALLING 0x02
  33. #define PIN_IRQ_MODE_HIGH_LEVEL 0x03
  34. #define PIN_IRQ_MODE_LOW_LEVEL 0x04
  35. #define PIN_IRQ_DISABLE 0x00
  36. #define PIN_IRQ_ENABLE 0x01
  37. #define PIN_IRQ_PIN_NONE -1
  38. struct rt_device_pin_mode
  39. {
  40. rt_uint16_t pin;
  41. rt_uint16_t mode;
  42. };
  43. struct rt_device_pin_status
  44. {
  45. rt_uint16_t pin;
  46. rt_uint16_t status;
  47. };
  48. struct rt_pin_irq_hdr
  49. {
  50. rt_int16_t pin;
  51. rt_uint16_t mode;
  52. void (*hdr)(void *args);
  53. void *args;
  54. };
  55. struct rt_pin_ops
  56. {
  57. void (*pin_mode)(struct rt_device *device, rt_base_t pin, rt_base_t mode);
  58. void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_base_t value);
  59. int (*pin_read)(struct rt_device *device, rt_base_t pin);
  60. /* TODO: add GPIO interrupt */
  61. rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_int32_t pin,
  62. rt_uint32_t mode, void (*hdr)(void *args), void *args);
  63. rt_err_t (*pin_detach_irq)(struct rt_device *device, rt_int32_t pin);
  64. rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled);
  65. rt_base_t (*pin_get)(const char *name);
  66. };
  67. int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data);
  68. /* Get pin number by name,such as PA.0,P0.12 */
  69. rt_base_t rt_pin_get(const char *name);
  70. void rt_pin_mode(rt_base_t pin, rt_base_t mode);
  71. void rt_pin_write(rt_base_t pin, rt_base_t value);
  72. int rt_pin_read(rt_base_t pin);
  73. rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
  74. void (*hdr)(void *args), void *args);
  75. rt_err_t rt_pin_detach_irq(rt_int32_t pin);
  76. rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled);
  77. #ifdef __cplusplus
  78. }
  79. #endif
  80. #endif