drv_gpio.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions are met:
  5. * 1. Redistributions of source code must retain the above copyright
  6. * notice, this list of conditions and the following disclaimer.
  7. * 2. Redistributions in binary form must reproduce the above copyright
  8. * notice, this list of conditions and the following disclaimer in the
  9. * documentation and/or other materials provided with the distribution.
  10. *
  11. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  12. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  13. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  16. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  18. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  22. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. /*
  26. * Copyright (c) 2006-2025, RT-Thread Development Team
  27. *
  28. * SPDX-License-Identifier: Apache-2.0
  29. */
  30. #ifndef DRV_GPIO_H__
  31. #define DRV_GPIO_H__
  32. #define GPIO_IRQ_MAX_NUM (64)
  33. #define GPIO_MAX_NUM (64+8)
  34. #define IRQN_GPIO0_INTERRUPT 32
  35. /* k230 gpio register table */
  36. #define DATA_OUTPUT 0x0
  37. #define DIRECTION 0x04
  38. #define DATA_SOURCE 0x08
  39. #define INT_ENABLE 0x30
  40. #define INT_MASK 0x34
  41. #define INT_TYPE_LEVEL 0x38
  42. #define INT_POLARITY 0x3c
  43. #define INT_STATUS 0x40
  44. #define INT_STATUS_RAW 0x44
  45. #define INT_DEBOUNCE 0x48
  46. #define INT_CLEAR 0x4c
  47. #define DATA_INPUT 0x50
  48. #define VER_ID_CODE 0x64
  49. #define INT_BOTHEDGE 0x68
  50. #define DATA_INPUT_STRIDE 0x04 /* register stride 32 bits */
  51. #define DATA_OUTPUT_STRIDE 0x0c /* register stride 3*32 bits */
  52. #define DIRECTION_STRIDE 0x0c /* register stride 3*32 bits, */
  53. #define KD_GPIO_HIGH 1
  54. #define KD_GPIO_LOW 0
  55. #define KD_GPIO_IRQ_DISABLE 0x00
  56. #define KD_GPIO_IRQ_ENABLE 0x01
  57. /* ioctl */
  58. #define KD_GPIO_DM_OUTPUT _IOW('G', 0, int)
  59. #define KD_GPIO_DM_INPUT _IOW('G', 1, int)
  60. #define KD_GPIO_DM_INPUT_PULL_UP _IOW('G', 2, int)
  61. #define KD_GPIO_DM_INPUT_PULL_DOWN _IOW('G', 3, int)
  62. #define KD_GPIO_WRITE_LOW _IOW('G', 4, int)
  63. #define KD_GPIO_WRITE_HIGH _IOW('G', 5, int)
  64. #define KD_GPIO_PE_RISING _IOW('G', 7, int)
  65. #define KD_GPIO_PE_FALLING _IOW('G', 8, int)
  66. #define KD_GPIO_PE_BOTH _IOW('G', 9, int)
  67. #define KD_GPIO_PE_HIGH _IOW('G', 10, int)
  68. #define KD_GPIO_PE_LOW _IOW('G', 11, int)
  69. #define KD_GPIO_READ_VALUE _IOW('G', 12, int)
  70. #define KD_GPIO_SET_MODE _IOW('G', 20, int)
  71. #define KD_GPIO_GET_MODE _IOWR('G', 21, int)
  72. #define KD_GPIO_SET_VALUE _IOW('G', 22, int)
  73. #define KD_GPIO_GET_VALUE _IOWR('G', 23, int)
  74. #define KD_GPIO_SET_IRQ _IOW('G', 24, int)
  75. #define KD_GPIO_GET_IRQ _IOWR('G', 25, int)
  76. typedef enum _gpio_pin_edge
  77. {
  78. GPIO_PE_RISING,
  79. GPIO_PE_FALLING,
  80. GPIO_PE_BOTH,
  81. GPIO_PE_HIGH,
  82. GPIO_PE_LOW,
  83. } gpio_pin_edge_t;
  84. typedef enum _gpio_drive_mode
  85. {
  86. GPIO_DM_OUTPUT,
  87. GPIO_DM_INPUT,
  88. GPIO_DM_INPUT_PULL_UP,
  89. GPIO_DM_INPUT_PULL_DOWN,
  90. } gpio_drive_mode_t;
  91. typedef enum _gpio_pin_value
  92. {
  93. GPIO_PV_LOW,
  94. GPIO_PV_HIGH
  95. } gpio_pin_value_t;
  96. typedef struct {
  97. rt_uint16_t pin;
  98. rt_uint16_t value;
  99. } gpio_cfg_t;
  100. typedef struct {
  101. rt_uint16_t pin;
  102. rt_uint8_t enable;
  103. rt_uint8_t mode;
  104. rt_uint16_t debounce;
  105. rt_uint8_t signo;
  106. void *sigval;
  107. } gpio_irqcfg_t;
  108. rt_err_t kd_pin_irq_enable(rt_base_t pin, rt_uint32_t enabled);
  109. rt_err_t kd_pin_detach_irq(rt_int32_t pin);
  110. rt_err_t kd_pin_attach_irq(rt_int32_t pin,rt_uint32_t mode, void (*hdr)(void *args), void *args);
  111. rt_err_t kd_pin_write(rt_base_t pin, rt_base_t value);
  112. rt_err_t kd_pin_mode(rt_base_t pin, rt_base_t mode);
  113. int kd_pin_read(rt_base_t pin);
  114. #endif