bsp_gpio.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #ifndef __BSP_GPIO_H_
  2. #define __BSP_GPIO_H_
  3. #include "xinc_m0.h"
  4. #define DETECT_INTER_NUM(num,val) ((val>>num)&0x01)
  5. #define GPIO_OUTPUT_HIGH(num) gpio_output_high(num)
  6. #define GPIO_OUTPUT_LOW(num) gpio_output_low(num)
  7. #define GPIO_INPUT_VAL(num) gpio_input_val(num)
  8. typedef enum{
  9. GPIO_Dx=0,//普通GPIO口功能
  10. UART0_TX,
  11. UART0_RX,
  12. UART0_CTS,
  13. UART0_RTS,
  14. I2C_SCL,
  15. I2C_SDA,
  16. UART1_RX,
  17. UART1_TX,
  18. SIM_IO,
  19. SIM_RST,
  20. SIM_CLK_OUT,
  21. PWM0,
  22. PWM1,
  23. SSI1_CLK,
  24. SSI1_SSN,
  25. SSI1_RX,
  26. SSI1_TX,
  27. PWM0_INV,
  28. PWM1_INV,
  29. }GPIO_FUN_SEL_TypeDef;
  30. typedef enum{
  31. NOT_INT=0,
  32. NA1_INT,
  33. NA2_INT,
  34. NA3_INT,
  35. NA4_INT,
  36. RIS_EDGE_INT,//上升沿中断
  37. NA6_INT,
  38. FAIL_EDGE_INT,//下降沿中断
  39. NA8_INT,
  40. RIS_FAIL_EDGE_INT,//上升沿下降沿中断
  41. }GPIO_INT_TypeDef;
  42. typedef enum{
  43. GPIO_PIN_0=0,
  44. GPIO_PIN_1,
  45. GPIO_PIN_2,
  46. GPIO_PIN_3,
  47. GPIO_PIN_4,
  48. GPIO_PIN_5,
  49. GPIO_PIN_6,
  50. GPIO_PIN_7,
  51. GPIO_PIN_8,
  52. GPIO_PIN_9,
  53. GPIO_PIN_10,
  54. GPIO_PIN_11,
  55. GPIO_PIN_12,
  56. GPIO_PIN_13,
  57. GPIO_PIN_14,
  58. GPIO_PIN_15,
  59. GPIO_PIN_16,
  60. GPIO_PIN_17,
  61. GPIO_PIN_18,
  62. GPIO_PIN_19,
  63. GPIO_PIN_20,
  64. GPIO_PIN_21,
  65. GPIO_PIN_22,
  66. GPIO_PIN_23,
  67. GPIO_PIN_24,
  68. GPIO_PIN_25,
  69. GPIO_PIN_26,
  70. GPIO_PIN_27,
  71. GPIO_PIN_28,
  72. GPIO_PIN_29,
  73. GPIO_PIN_30,
  74. GPIO_PIN_31,
  75. }GPIO_Pin_TypeDef;
  76. typedef enum{
  77. GPIO_Mode_Input_Up=0, /*上拉输入*/
  78. GPIO_Mode_Input_Down, /*下拉输入*/
  79. GPIO_Mode_Input_Float,/*浮空输入*/
  80. }GPIO_InputMode_TypeDef;
  81. /* ---------------------------------------------------------------------------------------------------
  82. - IO复用控制 -
  83. ----------------------------------------------------------------------------------------------------*/
  84. typedef union gpio_fun_config {
  85. uint32_t config;
  86. struct bit {
  87. uint8_t b0004 :5;
  88. uint8_t b0812 :5;
  89. uint8_t b1620 :5;
  90. uint8_t b2428 :5;
  91. }bits;
  92. }gpio_fun_config_t;
  93. typedef struct gpio_fun_sel_config{
  94. gpio_fun_config_t fun_sel0;
  95. gpio_fun_config_t fun_sel1;
  96. gpio_fun_config_t fun_sel2;
  97. gpio_fun_config_t fun_sel3;
  98. gpio_fun_config_t fun_sel4;
  99. gpio_fun_config_t fun_sel5;
  100. gpio_fun_config_t fun_sel6;
  101. gpio_fun_config_t fun_sel7;
  102. }gpio_fun_sel_config_t;
  103. /* ---------------------------------------------------------------------------------------------------
  104. - IO功能
  105. ----------------------------------------------------------------------------------------------------*/
  106. typedef union mux1_config {
  107. uint32_t muxctl1;
  108. struct bit1 {
  109. uint8_t gpio0 : 2; uint8_t gpio1 : 2; uint8_t gpio2 : 2; uint8_t gpio3 : 2;
  110. uint8_t gpio4 : 2; uint8_t gpio5 : 2; uint8_t gpio6 : 2; uint8_t gpio7 : 2;
  111. uint8_t gpio8 : 2; uint8_t gpio9 : 2; uint8_t gpio10: 2; uint8_t gpio11 : 2;
  112. uint8_t gpio12: 2; uint8_t gpio13 : 2; uint8_t gpio14: 2; uint8_t gpio15 : 2;
  113. }pad1;
  114. }mux1_config_t;
  115. typedef union mux2_config {
  116. uint32_t muxctl2;
  117. struct bit2 {
  118. uint8_t gpio16 : 2; uint8_t gpio17 : 2; uint8_t gpio18 : 2; uint8_t gpio19 : 2;
  119. uint8_t gpio20 : 2; uint8_t gpio21 : 2; uint8_t gpio22 : 2; uint8_t gpio23 : 2;
  120. uint8_t gpio24 : 2; uint8_t gpio25 : 2; uint8_t gpio26 : 2; uint8_t gpio27 : 2;
  121. uint8_t gpio28 : 2; uint8_t bootctl : 2; uint16_t swk : 2; uint8_t swd : 2;
  122. }pad2;
  123. }mux2_config_t;
  124. typedef struct gpio_config {
  125. mux1_config_t mux1_ctl;
  126. mux2_config_t mux2_ctl;
  127. }gpio_config_t;
  128. /* ---------------------------------------------------------------------------------------------------
  129. - IO方向
  130. ----------------------------------------------------------------------------------------------------*/
  131. typedef union dir_config {
  132. uint32_t dir;
  133. struct bitd {
  134. uint8_t gpio0 : 1; uint8_t gpio1 : 1; uint8_t gpio2 : 1; uint8_t gpio3 : 1;
  135. uint8_t gpio4 : 1; uint8_t gpio5 : 1; uint8_t gpio6 : 1; uint8_t gpio7 : 1;
  136. uint8_t gpio8 : 1; uint8_t gpio9 : 1; uint8_t gpio10 : 1; uint8_t gpio11 : 1;
  137. uint8_t gpio12: 1; uint8_t gpio13 : 1; uint8_t gpio14 : 1; uint8_t gpio15 : 1;
  138. uint8_t gpio16: 1; uint8_t gpio17 : 1; uint8_t gpio18 : 1; uint8_t gpio19 : 1;
  139. uint8_t gpio20: 1; uint8_t gpio21 : 1; uint8_t gpio22 : 1; uint8_t gpio23 : 1;
  140. uint8_t gpio24: 1; uint8_t gpio25 : 1; uint8_t gpio26 : 1; uint8_t gpio27 : 1;
  141. uint8_t gpio28: 1; uint8_t gpio29 : 1; uint8_t gpio30 : 1; uint8_t gpio31 : 1;
  142. }pad;
  143. }dir_config_t;
  144. /* ---------------------------------------------------------------------------------------------------
  145. - IO上下拉
  146. ----------------------------------------------------------------------------------------------------*/
  147. typedef union pupd1_config {
  148. uint32_t pupd;
  149. struct bita{
  150. uint8_t GPIO0 : 2; uint8_t GPIO1 : 2; uint8_t GPIO2 : 2; uint8_t GPIO3 : 2;
  151. uint8_t GPIO4 : 2; uint8_t GPIO5 : 2; uint8_t GPIO6 : 2; uint8_t GPIO7 : 2;
  152. uint8_t GPIO8 : 2; uint8_t GPIO9 : 2; uint8_t GPIO10 : 2; uint8_t GPIO11 : 2;
  153. uint8_t GPIO12 : 2; uint8_t GPIO13 : 2; uint8_t GPIO14 : 2; uint8_t GPIO15 : 2;
  154. }pad;
  155. }pupd1_config_t;
  156. typedef union pupd2_config {
  157. uint32_t pupd;
  158. struct bitb{
  159. uint8_t GPIO16 : 2; uint8_t GPIO17 : 2; uint8_t GPIO18 : 2; uint8_t GPIO19 : 2;
  160. uint8_t GPIO20 : 2; uint8_t GPIO21 : 2; uint8_t GPIO22 : 2; uint8_t GPIO23 : 2;
  161. uint8_t GPIO24 : 2; uint8_t GPIO25 : 2; uint8_t GPIO26 : 2; uint8_t GPIO27 : 2;
  162. uint8_t GPIO28 : 2; uint8_t BOOTCTL : 2; uint8_t SWCK : 2; uint8_t SWD : 2;
  163. }pad;
  164. }pupd2_config_t;
  165. typedef struct {
  166. pupd1_config_t CTL_PECTL1;
  167. pupd2_config_t CTL_PECTL2;
  168. }pupd_config_t;
  169. /* ---------------------------------------------------------------------------------------------------
  170. - IO中断模式
  171. ----------------------------------------------------------------------------------------------------*/
  172. typedef union int_config {
  173. uint32_t interrupt;
  174. struct bitt {
  175. uint8_t mode0 : 4; uint8_t mode1 : 4;
  176. uint8_t mode2 : 4; uint8_t mode3 : 4;
  177. uint16_t reserved;
  178. }pad;
  179. }int_config_t;
  180. typedef struct int_config_i {
  181. int_config_t intr_ctl0;
  182. int_config_t intr_ctl1;
  183. int_config_t intr_ctl2;
  184. int_config_t intr_ctl3;
  185. int_config_t intr_ctl4;
  186. int_config_t intr_ctl5;
  187. int_config_t intr_ctl6;
  188. int_config_t intr_ctl7;
  189. }interrupt_config_t;
  190. typedef void (*IoHandler_callback)(uint32_t val);
  191. extern void Init_gpio(void);
  192. extern void gpio_Register_Callback(IoHandler_callback callback);
  193. extern void gpio_direction_input(uint8_t num, uint8_t pull_up_type);
  194. extern uint8_t gpio_input_val(uint8_t num);
  195. extern void gpio_output_low(uint8_t num);
  196. extern void gpio_output_high(uint8_t num);
  197. extern void gpio_mux_ctl(uint8_t num,uint8_t mux);
  198. extern void gpio_fun_inter(uint8_t num,uint8_t inter);
  199. extern void gpio_fun_sel(uint8_t num,uint8_t sel);
  200. extern void gpio_direction_output(uint8_t num);
  201. #endif