gpio.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Copyright 2018 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #ifndef _DRIVER_GPIO_H
  16. #define _DRIVER_GPIO_H
  17. #include <inttypes.h>
  18. #include <stddef.h>
  19. #include "gpio_common.h"
  20. #include "platform.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. /**
  25. * @brief Structure for accessing GPIO registers by individual bit
  26. */
  27. typedef struct _gpio_bits
  28. {
  29. uint32_t b0 : 1;
  30. uint32_t b1 : 1;
  31. uint32_t b2 : 1;
  32. uint32_t b3 : 1;
  33. uint32_t b4 : 1;
  34. uint32_t b5 : 1;
  35. uint32_t b6 : 1;
  36. uint32_t b7 : 1;
  37. uint32_t b8 : 1;
  38. uint32_t b9 : 1;
  39. uint32_t b10 : 1;
  40. uint32_t b11 : 1;
  41. uint32_t b12 : 1;
  42. uint32_t b13 : 1;
  43. uint32_t b14 : 1;
  44. uint32_t b15 : 1;
  45. uint32_t b16 : 1;
  46. uint32_t b17 : 1;
  47. uint32_t b18 : 1;
  48. uint32_t b19 : 1;
  49. uint32_t b20 : 1;
  50. uint32_t b21 : 1;
  51. uint32_t b22 : 1;
  52. uint32_t b23 : 1;
  53. uint32_t b24 : 1;
  54. uint32_t b25 : 1;
  55. uint32_t b26 : 1;
  56. uint32_t b27 : 1;
  57. uint32_t b28 : 1;
  58. uint32_t b29 : 1;
  59. uint32_t b30 : 1;
  60. uint32_t b31 : 1;
  61. } __attribute__((packed, aligned(4))) gpio_bits_t;
  62. /**
  63. * @brief Structure of templates for accessing GPIO registers
  64. */
  65. typedef union _gpio_access_tp
  66. {
  67. /* 32x1 bit mode */
  68. uint32_t u32[1];
  69. /* 16x2 bit mode */
  70. uint16_t u16[2];
  71. /* 8x4 bit mode */
  72. uint8_t u8[4];
  73. /* 1 bit mode */
  74. gpio_bits_t bits;
  75. } __attribute__((packed, aligned(4))) gpio_access_tp_t;
  76. /**
  77. * @brief The GPIO address map
  78. */
  79. typedef struct _gpio
  80. {
  81. /* Offset 0x00: Data (output) registers */
  82. gpio_access_tp_t data_output;
  83. /* Offset 0x04: Data direction registers */
  84. gpio_access_tp_t direction;
  85. /* Offset 0x08: Data source registers */
  86. gpio_access_tp_t source;
  87. /* Offset 0x10 - 0x2f: Unused registers, 9x4 bytes */
  88. uint32_t unused_0[9];
  89. /* Offset 0x30: Interrupt enable/disable registers */
  90. gpio_access_tp_t interrupt_enable;
  91. /* Offset 0x34: Interrupt mask registers */
  92. gpio_access_tp_t interrupt_mask;
  93. /* Offset 0x38: Interrupt level registers */
  94. gpio_access_tp_t interrupt_level;
  95. /* Offset 0x3c: Interrupt polarity registers */
  96. gpio_access_tp_t interrupt_polarity;
  97. /* Offset 0x40: Interrupt status registers */
  98. gpio_access_tp_t interrupt_status;
  99. /* Offset 0x44: Raw interrupt status registers */
  100. gpio_access_tp_t interrupt_status_raw;
  101. /* Offset 0x48: Interrupt debounce registers */
  102. gpio_access_tp_t interrupt_debounce;
  103. /* Offset 0x4c: Registers for clearing interrupts */
  104. gpio_access_tp_t interrupt_clear;
  105. /* Offset 0x50: External port (data input) registers */
  106. gpio_access_tp_t data_input;
  107. /* Offset 0x54 - 0x5f: Unused registers, 3x4 bytes */
  108. uint32_t unused_1[3];
  109. /* Offset 0x60: Sync level registers */
  110. gpio_access_tp_t sync_level;
  111. /* Offset 0x64: ID code */
  112. gpio_access_tp_t id_code;
  113. /* Offset 0x68: Interrupt both edge type */
  114. gpio_access_tp_t interrupt_bothedge;
  115. } __attribute__((packed, aligned(4))) gpio_t;
  116. /**
  117. * @brief Bus GPIO object instance
  118. */
  119. extern volatile gpio_t *const gpio;
  120. /**
  121. * @brief Gpio initialize
  122. *
  123. * @return Result
  124. * - 0 Success
  125. * - Other Fail
  126. */
  127. int gpio_init(void);
  128. /**
  129. * @brief Set Gpio drive mode
  130. *
  131. * @param[in] pin Gpio pin
  132. * @param[in] mode Gpio pin drive mode
  133. */
  134. void gpio_set_drive_mode(uint8_t pin, gpio_drive_mode_t mode);
  135. /**
  136. * @brief Get Gpio pin value
  137. *
  138. * @param[in] pin Gpio pin
  139. * @return Pin value
  140. *
  141. * - GPIO_PV_Low Gpio pin low
  142. * - GPIO_PV_High Gpio pin high
  143. */
  144. gpio_pin_value_t gpio_get_pin(uint8_t pin);
  145. /**
  146. * @brief Set Gpio pin value
  147. *
  148. * @param[in] pin Gpio pin
  149. * @param[in] value Gpio pin value
  150. */
  151. void gpio_set_pin(uint8_t pin, gpio_pin_value_t value);
  152. #ifdef __cplusplus
  153. }
  154. #endif
  155. #endif /* _DRIVER_GPIO_H */