pin.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file pin.h
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef PIN_H__
  35. #define PIN_H__
  36. #include <rtthread.h>
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /* pin device and operations for RT-Thread */
  41. struct rt_device_pin
  42. {
  43. struct rt_device parent;
  44. const struct rt_pin_ops *ops;
  45. };
  46. #define PIN_LOW 0x00
  47. #define PIN_HIGH 0x01
  48. #define PIN_MODE_OUTPUT 0x00
  49. #define PIN_MODE_INPUT 0x01
  50. #define PIN_MODE_INPUT_PULLUP 0x02
  51. #define PIN_MODE_INPUT_PULLDOWN 0x03
  52. #define PIN_MODE_OUTPUT_OD 0x04
  53. #define PIN_IRQ_MODE_RISING 0x00
  54. #define PIN_IRQ_MODE_FALLING 0x01
  55. #define PIN_IRQ_MODE_RISING_FALLING 0x02
  56. #define PIN_IRQ_MODE_HIGH_LEVEL 0x03
  57. #define PIN_IRQ_MODE_LOW_LEVEL 0x04
  58. #define PIN_IRQ_DISABLE 0x00
  59. #define PIN_IRQ_ENABLE 0x01
  60. #define PIN_IRQ_PIN_NONE -1
  61. struct rt_device_pin_mode
  62. {
  63. rt_uint16_t pin;
  64. rt_uint16_t mode;
  65. };
  66. struct rt_device_pin_status
  67. {
  68. rt_uint16_t pin;
  69. rt_uint16_t status;
  70. };
  71. struct rt_pin_irq_hdr
  72. {
  73. rt_int16_t pin;
  74. rt_uint16_t mode;
  75. void (*hdr)(void *args);
  76. void *args;
  77. };
  78. struct rt_pin_ops
  79. {
  80. void (*pin_mode)(struct rt_device *device, rt_base_t pin, rt_base_t mode);
  81. void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_base_t value);
  82. int (*pin_read)(struct rt_device *device, rt_base_t pin);
  83. /* TODO: add GPIO interrupt */
  84. rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_int32_t pin,
  85. rt_uint32_t mode, void (*hdr)(void *args), void *args);
  86. rt_err_t (*pin_detach_irq)(struct rt_device *device, rt_int32_t pin);
  87. rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled);
  88. };
  89. int rt_device_pin_register(const char *name, const struct rt_pin_ops *ops, void *user_data);
  90. void rt_pin_mode(rt_base_t pin, rt_base_t mode);
  91. void rt_pin_write(rt_base_t pin, rt_base_t value);
  92. int rt_pin_read(rt_base_t pin);
  93. rt_err_t rt_pin_attach_irq(rt_int32_t pin, rt_uint32_t mode,
  94. void (*hdr)(void *args), void *args);
  95. rt_err_t rt_pin_detach_irq(rt_int32_t pin);
  96. rt_err_t rt_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled);
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. #endif