ch32v30x_gpio.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v30x_gpio.h
  3. * Author : WCH
  4. * Version : V1.0.1
  5. * Date : 2025/04/09
  6. * Description : This file contains all the functions prototypes for the
  7. * GPIO firmware library.
  8. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  9. * Attention: This software (modified or not) and binary are used for
  10. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  11. *******************************************************************************/
  12. #ifndef __CH32V30x_GPIO_H
  13. #define __CH32V30x_GPIO_H
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #include "ch32v30x.h"
  18. /* Output Maximum frequency selection */
  19. typedef enum
  20. {
  21. GPIO_Speed_10MHz = 1,
  22. GPIO_Speed_2MHz,
  23. GPIO_Speed_50MHz
  24. }GPIOSpeed_TypeDef;
  25. /* Configuration Mode enumeration */
  26. typedef enum
  27. { GPIO_Mode_AIN = 0x0,
  28. GPIO_Mode_IN_FLOATING = 0x04,
  29. GPIO_Mode_IPD = 0x28,
  30. GPIO_Mode_IPU = 0x48,
  31. GPIO_Mode_Out_OD = 0x14,
  32. GPIO_Mode_Out_PP = 0x10,
  33. GPIO_Mode_AF_OD = 0x1C,
  34. GPIO_Mode_AF_PP = 0x18
  35. }GPIOMode_TypeDef;
  36. /* GPIO Init structure definition */
  37. typedef struct
  38. {
  39. uint16_t GPIO_Pin; /* Specifies the GPIO pins to be configured.
  40. This parameter can be any value of @ref GPIO_pins_define */
  41. GPIOSpeed_TypeDef GPIO_Speed; /* Specifies the speed for the selected pins.
  42. This parameter can be a value of @ref GPIOSpeed_TypeDef */
  43. GPIOMode_TypeDef GPIO_Mode; /* Specifies the operating mode for the selected pins.
  44. This parameter can be a value of @ref GPIOMode_TypeDef */
  45. }GPIO_InitTypeDef;
  46. /* Bit_SET and Bit_RESET enumeration */
  47. typedef enum
  48. {
  49. Bit_RESET = 0,
  50. Bit_SET
  51. }BitAction;
  52. /* GPIO_pins_define */
  53. #define GPIO_Pin_0 ((uint16_t)0x0001) /* Pin 0 selected */
  54. #define GPIO_Pin_1 ((uint16_t)0x0002) /* Pin 1 selected */
  55. #define GPIO_Pin_2 ((uint16_t)0x0004) /* Pin 2 selected */
  56. #define GPIO_Pin_3 ((uint16_t)0x0008) /* Pin 3 selected */
  57. #define GPIO_Pin_4 ((uint16_t)0x0010) /* Pin 4 selected */
  58. #define GPIO_Pin_5 ((uint16_t)0x0020) /* Pin 5 selected */
  59. #define GPIO_Pin_6 ((uint16_t)0x0040) /* Pin 6 selected */
  60. #define GPIO_Pin_7 ((uint16_t)0x0080) /* Pin 7 selected */
  61. #define GPIO_Pin_8 ((uint16_t)0x0100) /* Pin 8 selected */
  62. #define GPIO_Pin_9 ((uint16_t)0x0200) /* Pin 9 selected */
  63. #define GPIO_Pin_10 ((uint16_t)0x0400) /* Pin 10 selected */
  64. #define GPIO_Pin_11 ((uint16_t)0x0800) /* Pin 11 selected */
  65. #define GPIO_Pin_12 ((uint16_t)0x1000) /* Pin 12 selected */
  66. #define GPIO_Pin_13 ((uint16_t)0x2000) /* Pin 13 selected */
  67. #define GPIO_Pin_14 ((uint16_t)0x4000) /* Pin 14 selected */
  68. #define GPIO_Pin_15 ((uint16_t)0x8000) /* Pin 15 selected */
  69. #define GPIO_Pin_All ((uint16_t)0xFFFF) /* All pins selected */
  70. /* GPIO_Remap_define */
  71. /* PCFR1 */
  72. #define GPIO_Remap_SPI1 ((uint32_t)0x00000001) /* SPI1 Alternate Function mapping */
  73. #define GPIO_Remap_I2C1 ((uint32_t)0x00000002) /* I2C1 Alternate Function mapping */
  74. #define GPIO_Remap_USART1 ((uint32_t)0x00000004) /* USART1 Alternate Function mapping low bit */
  75. #define GPIO_Remap_USART2 ((uint32_t)0x00000008) /* USART2 Alternate Function mapping */
  76. #define GPIO_PartialRemap_USART3 ((uint32_t)0x00140010) /* USART3 Partial Alternate Function mapping */
  77. #define GPIO_PartialRemap1_USART3 ((uint32_t)0x00140020) /* USART3 Partial1 Alternate Function mapping */
  78. #define GPIO_FullRemap_USART3 ((uint32_t)0x00140030) /* USART3 Full Alternate Function mapping */
  79. #define GPIO_PartialRemap_TIM1 ((uint32_t)0x00160040) /* TIM1 Partial Alternate Function mapping */
  80. #define GPIO_FullRemap_TIM1 ((uint32_t)0x001600C0) /* TIM1 Full Alternate Function mapping */
  81. #define GPIO_PartialRemap1_TIM2 ((uint32_t)0x00180100) /* TIM2 Partial1 Alternate Function mapping */
  82. #define GPIO_PartialRemap2_TIM2 ((uint32_t)0x00180200) /* TIM2 Partial2 Alternate Function mapping */
  83. #define GPIO_FullRemap_TIM2 ((uint32_t)0x00180300) /* TIM2 Full Alternate Function mapping */
  84. #define GPIO_PartialRemap_TIM3 ((uint32_t)0x001A0800) /* TIM3 Partial Alternate Function mapping */
  85. #define GPIO_FullRemap_TIM3 ((uint32_t)0x001A0C00) /* TIM3 Full Alternate Function mapping */
  86. #define GPIO_Remap_TIM4 ((uint32_t)0x00001000) /* TIM4 Alternate Function mapping */
  87. #define GPIO_Remap1_CAN1 ((uint32_t)0x001D4000) /* CAN1 Alternate Function mapping */
  88. #define GPIO_Remap2_CAN1 ((uint32_t)0x001D6000) /* CAN1 Alternate Function mapping */
  89. #define GPIO_Remap_PD0PD1 ((uint32_t)0x00008000) /* PD0 and PD1 Alternate Function mapping */
  90. #define GPIO_Remap_TIM5CH4_LSI ((uint32_t)0x00200001) /* LSI connected to TIM5 Channel4 input capture for calibration */
  91. #define GPIO_Remap_ADC1_ETRGINJ ((uint32_t)0x00200002) /* ADC1 External Trigger Injected Conversion remapping */
  92. #define GPIO_Remap_ADC1_ETRGREG ((uint32_t)0x00200004) /* ADC1 External Trigger Regular Conversion remapping */
  93. #define GPIO_Remap_ADC2_ETRGINJ ((uint32_t)0x00200008) /* ADC2 External Trigger Injected Conversion remapping */
  94. #define GPIO_Remap_ADC2_ETRGREG ((uint32_t)0x00200010) /* ADC2 External Trigger Regular Conversion remapping */
  95. #define GPIO_Remap_ETH ((uint32_t)0x00200020) /* Ethernet remapping (only for Connectivity line devices) */
  96. #define GPIO_Remap_CAN2 ((uint32_t)0x00200040) /* CAN2 remapping (only for Connectivity line devices) */
  97. #define GPIO_Remap_MII_RMII_SEL ((uint32_t)0x00200080) /* MII or RMII selection */
  98. #define GPIO_Remap_SWJ_Disable ((uint32_t)0x00300400) /* Full SWJ Disabled */
  99. #define GPIO_Remap_SPI3 ((uint32_t)0x00201000) /* SPI3/I2S3 Alternate Function mapping (only for Connectivity line devices) */
  100. #define GPIO_Remap_TIM2ITR1_PTP_SOF ((uint32_t)0x00202000) /* Ethernet PTP output or USB OTG SOF (Start of Frame) connected
  101. to TIM2 Internal Trigger 1 for calibration
  102. (only for Connectivity line devices) */
  103. #define GPIO_Remap_PTP_PPS ((uint32_t)0x00204000) /* Ethernet MAC PPS_PTS output on PB05 (only for Connectivity line devices) */
  104. #define GPIO_Remap_PD01 GPIO_Remap_PD0PD1
  105. /* PCFR2 */
  106. #define GPIO_Remap_TIM8 ((uint32_t)0x80000004) /* TIM8 Alternate Function mapping */
  107. #define GPIO_PartialRemap_TIM9 ((uint32_t)0x80130008) /* TIM9 Partial Alternate Function mapping */
  108. #define GPIO_FullRemap_TIM9 ((uint32_t)0x80130010) /* TIM9 Full Alternate Function mapping */
  109. #define GPIO_PartialRemap_TIM10 ((uint32_t)0x80150020) /* TIM10 Partial Alternate Function mapping */
  110. #define GPIO_FullRemap_TIM10 ((uint32_t)0x80150040) /* TIM10 Full Alternate Function mapping */
  111. #define GPIO_Remap_FSMC_NADV ((uint32_t)0x80000400) /* FSMC_NADV Alternate Function mapping */
  112. #define GPIO_PartialRemap_USART4 ((uint32_t)0x80300001) /* USART4 Partial Alternate Function mapping */
  113. #define GPIO_FullRemap_USART4 ((uint32_t)0x80300002) /* USART4 Full Alternate Function mapping */
  114. #define GPIO_PartialRemap_USART5 ((uint32_t)0x80320004) /* USART5 Partial Alternate Function mapping */
  115. #define GPIO_FullRemap_USART5 ((uint32_t)0x80320008) /* USART5 Full Alternate Function mapping */
  116. #define GPIO_PartialRemap_USART6 ((uint32_t)0x80340010) /* USART6 Partial Alternate Function mapping */
  117. #define GPIO_FullRemap_USART6 ((uint32_t)0x80340020) /* USART6 Full Alternate Function mapping */
  118. #define GPIO_PartialRemap_USART7 ((uint32_t)0x80360040) /* USART7 Partial Alternate Function mapping */
  119. #define GPIO_FullRemap_USART7 ((uint32_t)0x80360080) /* USART7 Full Alternate Function mapping */
  120. #define GPIO_PartialRemap_USART8 ((uint32_t)0x80380100) /* USART8 Partial Alternate Function mapping */
  121. #define GPIO_FullRemap_USART8 ((uint32_t)0x80380200) /* USART8 Full Alternate Function mapping */
  122. #define GPIO_Remap_USART1_HighBit ((uint32_t)0x80200400) /* USART1 Alternate Function mapping high bit */
  123. /* GPIO_Port_Sources */
  124. #define GPIO_PortSourceGPIOA ((uint8_t)0x00)
  125. #define GPIO_PortSourceGPIOB ((uint8_t)0x01)
  126. #define GPIO_PortSourceGPIOC ((uint8_t)0x02)
  127. #define GPIO_PortSourceGPIOD ((uint8_t)0x03)
  128. #define GPIO_PortSourceGPIOE ((uint8_t)0x04)
  129. /* GPIO_Pin_sources */
  130. #define GPIO_PinSource0 ((uint8_t)0x00)
  131. #define GPIO_PinSource1 ((uint8_t)0x01)
  132. #define GPIO_PinSource2 ((uint8_t)0x02)
  133. #define GPIO_PinSource3 ((uint8_t)0x03)
  134. #define GPIO_PinSource4 ((uint8_t)0x04)
  135. #define GPIO_PinSource5 ((uint8_t)0x05)
  136. #define GPIO_PinSource6 ((uint8_t)0x06)
  137. #define GPIO_PinSource7 ((uint8_t)0x07)
  138. #define GPIO_PinSource8 ((uint8_t)0x08)
  139. #define GPIO_PinSource9 ((uint8_t)0x09)
  140. #define GPIO_PinSource10 ((uint8_t)0x0A)
  141. #define GPIO_PinSource11 ((uint8_t)0x0B)
  142. #define GPIO_PinSource12 ((uint8_t)0x0C)
  143. #define GPIO_PinSource13 ((uint8_t)0x0D)
  144. #define GPIO_PinSource14 ((uint8_t)0x0E)
  145. #define GPIO_PinSource15 ((uint8_t)0x0F)
  146. /* Ethernet_Media_Interface */
  147. #define GPIO_ETH_MediaInterface_MII ((u32)0x00000000)
  148. #define GPIO_ETH_MediaInterface_RMII ((u32)0x00000001)
  149. void GPIO_DeInit(GPIO_TypeDef* GPIOx);
  150. void GPIO_AFIODeInit(void);
  151. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
  152. void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct);
  153. uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  154. uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);
  155. uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  156. uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);
  157. void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  158. void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  159. void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);
  160. void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);
  161. void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
  162. void GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);
  163. void GPIO_EventOutputCmd(FunctionalState NewState);
  164. void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);
  165. void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource);
  166. void GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface);
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif