pin.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2006-2023, 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_NONE (-1)
  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 PIN_NONE
  39. struct rt_device_pin_mode
  40. {
  41. rt_base_t pin;
  42. rt_uint8_t mode; /* e.g. PIN_MODE_OUTPUT */
  43. };
  44. struct rt_device_pin_value
  45. {
  46. rt_base_t pin;
  47. rt_uint8_t value; /* PIN_LOW or PIN_HIGH */
  48. };
  49. struct rt_pin_irq_hdr
  50. {
  51. rt_base_t pin;
  52. rt_uint8_t mode; /* e.g. PIN_IRQ_MODE_RISING */
  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_uint8_t mode);
  59. void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_uint8_t value);
  60. rt_int8_t (*pin_read)(struct rt_device *device, rt_base_t pin);
  61. rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_base_t pin,
  62. rt_uint8_t mode, void (*hdr)(void *args), void *args);
  63. rt_err_t (*pin_detach_irq)(struct rt_device *device, rt_base_t pin);
  64. rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint8_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. void rt_pin_mode(rt_base_t pin, rt_uint8_t mode);
  69. void rt_pin_write(rt_base_t pin, rt_uint8_t value);
  70. rt_int8_t rt_pin_read(rt_base_t pin);
  71. rt_base_t rt_pin_get(const char *name);
  72. rt_err_t rt_pin_attach_irq(rt_base_t pin, rt_uint8_t mode,
  73. void (*hdr)(void *args), void *args);
  74. rt_err_t rt_pin_detach_irq(rt_base_t pin);
  75. rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint8_t enabled);
  76. #ifdef __cplusplus
  77. }
  78. #endif
  79. #endif