gpiohs.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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_GPIOHS_H
  16. #define _DRIVER_GPIOHS_H
  17. #include <stddef.h>
  18. #include <stdint.h>
  19. #include "gpio_common.h"
  20. #include "platform.h"
  21. #include "plic.h"
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /* clang-format off */
  26. /* Register address offsets */
  27. #define GPIOHS_INPUT_VAL (0x00)
  28. #define GPIOHS_INPUT_EN (0x04)
  29. #define GPIOHS_OUTPUT_EN (0x08)
  30. #define GPIOHS_OUTPUT_VAL (0x0C)
  31. #define GPIOHS_PULLUP_EN (0x10)
  32. #define GPIOHS_DRIVE (0x14)
  33. #define GPIOHS_RISE_IE (0x18)
  34. #define GPIOHS_RISE_IP (0x1C)
  35. #define GPIOHS_FALL_IE (0x20)
  36. #define GPIOHS_FALL_IP (0x24)
  37. #define GPIOHS_HIGH_IE (0x28)
  38. #define GPIOHS_HIGH_IP (0x2C)
  39. #define GPIOHS_LOW_IE (0x30)
  40. #define GPIOHS_LOW_IP (0x34)
  41. #define GPIOHS_IOF_EN (0x38)
  42. #define GPIOHS_IOF_SEL (0x3C)
  43. #define GPIOHS_OUTPUT_XOR (0x40)
  44. /* clang-format on */
  45. /**
  46. * @brief GPIO bits raw object
  47. */
  48. typedef struct _gpiohs_raw
  49. {
  50. /* Address offset 0x00 */
  51. uint32_t input_val;
  52. /* Address offset 0x04 */
  53. uint32_t input_en;
  54. /* Address offset 0x08 */
  55. uint32_t output_en;
  56. /* Address offset 0x0c */
  57. uint32_t output_val;
  58. /* Address offset 0x10 */
  59. uint32_t pullup_en;
  60. /* Address offset 0x14 */
  61. uint32_t drive;
  62. /* Address offset 0x18 */
  63. uint32_t rise_ie;
  64. /* Address offset 0x1c */
  65. uint32_t rise_ip;
  66. /* Address offset 0x20 */
  67. uint32_t fall_ie;
  68. /* Address offset 0x24 */
  69. uint32_t fall_ip;
  70. /* Address offset 0x28 */
  71. uint32_t high_ie;
  72. /* Address offset 0x2c */
  73. uint32_t high_ip;
  74. /* Address offset 0x30 */
  75. uint32_t low_ie;
  76. /* Address offset 0x34 */
  77. uint32_t low_ip;
  78. /* Address offset 0x38 */
  79. uint32_t iof_en;
  80. /* Address offset 0x3c */
  81. uint32_t iof_sel;
  82. /* Address offset 0x40 */
  83. uint32_t output_xor;
  84. } __attribute__((packed, aligned(4))) gpiohs_raw_t;
  85. /**
  86. * @brief GPIO bits object
  87. */
  88. typedef struct _gpiohs_bits
  89. {
  90. uint32_t b0 : 1;
  91. uint32_t b1 : 1;
  92. uint32_t b2 : 1;
  93. uint32_t b3 : 1;
  94. uint32_t b4 : 1;
  95. uint32_t b5 : 1;
  96. uint32_t b6 : 1;
  97. uint32_t b7 : 1;
  98. uint32_t b8 : 1;
  99. uint32_t b9 : 1;
  100. uint32_t b10 : 1;
  101. uint32_t b11 : 1;
  102. uint32_t b12 : 1;
  103. uint32_t b13 : 1;
  104. uint32_t b14 : 1;
  105. uint32_t b15 : 1;
  106. uint32_t b16 : 1;
  107. uint32_t b17 : 1;
  108. uint32_t b18 : 1;
  109. uint32_t b19 : 1;
  110. uint32_t b20 : 1;
  111. uint32_t b21 : 1;
  112. uint32_t b22 : 1;
  113. uint32_t b23 : 1;
  114. uint32_t b24 : 1;
  115. uint32_t b25 : 1;
  116. uint32_t b26 : 1;
  117. uint32_t b27 : 1;
  118. uint32_t b28 : 1;
  119. uint32_t b29 : 1;
  120. uint32_t b30 : 1;
  121. uint32_t b31 : 1;
  122. } __attribute__((packed, aligned(4))) gpiohs_bits_t;
  123. /**
  124. * @brief GPIO bits multi access union
  125. */
  126. typedef union _gpiohs_u32
  127. {
  128. /* 32x1 bit mode */
  129. uint32_t u32[1];
  130. /* 16x2 bit mode */
  131. uint16_t u16[2];
  132. /* 8x4 bit mode */
  133. uint8_t u8[4];
  134. /* 1 bit mode */
  135. gpiohs_bits_t bits;
  136. } __attribute__((packed, aligned(4))) gpiohs_u32_t;
  137. /**
  138. * @brief GPIO object
  139. *
  140. * The GPIO controller is a peripheral device mapped in the
  141. * internal memory map, discoverable in the Configuration String.
  142. * It is responsible for low-level configuration of the actual
  143. * GPIO pads on the device (direction, pull up-enable, and drive
  144. * value), as well as selecting between various sources of the
  145. * controls for these signals. The GPIO controller allows seperate
  146. * configuration of each of N GPIO bits.
  147. *
  148. * Once the interrupt is pending, it will remain set until a 1 is
  149. * written to the *_ip register at that bit.
  150. */
  151. typedef struct _gpiohs
  152. {
  153. /* Address offset 0x00, Input Values */
  154. gpiohs_u32_t input_val;
  155. /* Address offset 0x04, Input enable */
  156. gpiohs_u32_t input_en;
  157. /* Address offset 0x08, Output enable */
  158. gpiohs_u32_t output_en;
  159. /* Address offset 0x0c, Onput Values */
  160. gpiohs_u32_t output_val;
  161. /* Address offset 0x10, Internal Pull-Ups enable */
  162. gpiohs_u32_t pullup_en;
  163. /* Address offset 0x14, Drive Strength */
  164. gpiohs_u32_t drive;
  165. /* Address offset 0x18, Rise interrupt enable */
  166. gpiohs_u32_t rise_ie;
  167. /* Address offset 0x1c, Rise interrupt pending */
  168. gpiohs_u32_t rise_ip;
  169. /* Address offset 0x20, Fall interrupt enable */
  170. gpiohs_u32_t fall_ie;
  171. /* Address offset 0x24, Fall interrupt pending */
  172. gpiohs_u32_t fall_ip;
  173. /* Address offset 0x28, High interrupt enable */
  174. gpiohs_u32_t high_ie;
  175. /* Address offset 0x2c, High interrupt pending */
  176. gpiohs_u32_t high_ip;
  177. /* Address offset 0x30, Low interrupt enable */
  178. gpiohs_u32_t low_ie;
  179. /* Address offset 0x34, Low interrupt pending */
  180. gpiohs_u32_t low_ip;
  181. /* Address offset 0x38, HW I/O Function enable */
  182. gpiohs_u32_t iof_en;
  183. /* Address offset 0x3c, HW I/O Function select */
  184. gpiohs_u32_t iof_sel;
  185. /* Address offset 0x40, Output XOR (invert) */
  186. gpiohs_u32_t output_xor;
  187. } __attribute__((packed, aligned(4))) gpiohs_t;
  188. /**
  189. * @brief GPIO High-speed object instanse
  190. */
  191. extern volatile gpiohs_t *const gpiohs;
  192. /**
  193. * @brief Set Gpiohs drive mode
  194. *
  195. * @param[in] pin Gpiohs pin
  196. * @param[in] mode Gpiohs pin drive mode
  197. */
  198. void gpiohs_set_drive_mode(uint8_t pin, gpio_drive_mode_t mode);
  199. /**
  200. * @brief Get Gpiohs pin value
  201. *
  202. * @param[in] pin Gpiohs pin
  203. * @return Pin value
  204. *
  205. * - GPIO_PV_Low Gpiohs pin low
  206. * - GPIO_PV_High Gpiohs pin high
  207. */
  208. gpio_pin_value_t gpiohs_get_pin(uint8_t pin);
  209. /**
  210. * @brief Set Gpiohs pin value
  211. *
  212. * @param[in] pin Gpiohs pin
  213. * @param[in] value Gpiohs pin value
  214. */
  215. void gpiohs_set_pin(uint8_t pin, gpio_pin_value_t value);
  216. /**
  217. * @brief Set Gpiohs pin edge for interrupt
  218. *
  219. * @param[in] pin Gpiohs pin
  220. * @param[in] edge Gpiohs pin edge type
  221. */
  222. void gpiohs_set_pin_edge(uint8_t pin, gpio_pin_edge_t edge);
  223. /**
  224. * @brief Set Gpiohs pin interrupt
  225. *
  226. * @param[in] pin Gpiohs pin
  227. * @param[in] priority Gpiohs pin interrupt priority
  228. * @param[in] func Gpiohs pin interrupt service routine
  229. */
  230. void gpiohs_set_irq(uint8_t pin, uint32_t priority, void (*func)());
  231. /**
  232. * @brief Set Gpiohs pin interrupt
  233. *
  234. * @param[in] pin Gpiohs pin
  235. * @param[in] priority Gpiohs pin interrupt priority
  236. * @param[in] callback Gpiohs pin interrupt service routine
  237. * @param[in] ctx Gpiohs interrupt param
  238. */
  239. void gpiohs_irq_register(uint8_t pin, uint32_t priority, plic_irq_callback_t callback, void *ctx);
  240. /**
  241. * @brief Unregister Gpiohs pin interrupt
  242. *
  243. * @param[in] pin Gpiohs pin
  244. */
  245. void gpiohs_irq_unregister(uint8_t pin);
  246. #ifdef __cplusplus
  247. }
  248. #endif
  249. #endif /* _DRIVER_GPIOHS_H */