1 /********************************** (C) COPYRIGHT ******************************* 2 * File Name : ch32f20x_gpio.h 3 * Author : WCH 4 * Version : V1.0.0 5 * Date : 2021/08/08 6 * Description : This file contains all the functions prototypes for the 7 * GPIO firmware library. 8 *******************************************************************************/ 9 #ifndef __CH32F20x_GPIO_H 10 #define __CH32F20x_GPIO_H 11 12 #ifdef __cplusplus 13 extern "C" { 14 #endif 15 16 #include "ch32f20x.h" 17 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 26 /* Configuration Mode enumeration */ 27 typedef enum 28 { GPIO_Mode_AIN = 0x0, 29 GPIO_Mode_IN_FLOATING = 0x04, 30 GPIO_Mode_IPD = 0x28, 31 GPIO_Mode_IPU = 0x48, 32 GPIO_Mode_Out_OD = 0x14, 33 GPIO_Mode_Out_PP = 0x10, 34 GPIO_Mode_AF_OD = 0x1C, 35 GPIO_Mode_AF_PP = 0x18 36 }GPIOMode_TypeDef; 37 38 /* GPIO Init structure definition */ 39 typedef struct 40 { 41 uint16_t GPIO_Pin; /* Specifies the GPIO pins to be configured. 42 This parameter can be any value of @ref GPIO_pins_define */ 43 44 GPIOSpeed_TypeDef GPIO_Speed; /* Specifies the speed for the selected pins. 45 This parameter can be a value of @ref GPIOSpeed_TypeDef */ 46 47 GPIOMode_TypeDef GPIO_Mode; /* Specifies the operating mode for the selected pins. 48 This parameter can be a value of @ref GPIOMode_TypeDef */ 49 }GPIO_InitTypeDef; 50 51 /* Bit_SET and Bit_RESET enumeration */ 52 typedef enum 53 { 54 Bit_RESET = 0, 55 Bit_SET 56 }BitAction; 57 58 /* GPIO_pins_define */ 59 #define GPIO_Pin_0 ((uint16_t)0x0001) /* Pin 0 selected */ 60 #define GPIO_Pin_1 ((uint16_t)0x0002) /* Pin 1 selected */ 61 #define GPIO_Pin_2 ((uint16_t)0x0004) /* Pin 2 selected */ 62 #define GPIO_Pin_3 ((uint16_t)0x0008) /* Pin 3 selected */ 63 #define GPIO_Pin_4 ((uint16_t)0x0010) /* Pin 4 selected */ 64 #define GPIO_Pin_5 ((uint16_t)0x0020) /* Pin 5 selected */ 65 #define GPIO_Pin_6 ((uint16_t)0x0040) /* Pin 6 selected */ 66 #define GPIO_Pin_7 ((uint16_t)0x0080) /* Pin 7 selected */ 67 #define GPIO_Pin_8 ((uint16_t)0x0100) /* Pin 8 selected */ 68 #define GPIO_Pin_9 ((uint16_t)0x0200) /* Pin 9 selected */ 69 #define GPIO_Pin_10 ((uint16_t)0x0400) /* Pin 10 selected */ 70 #define GPIO_Pin_11 ((uint16_t)0x0800) /* Pin 11 selected */ 71 #define GPIO_Pin_12 ((uint16_t)0x1000) /* Pin 12 selected */ 72 #define GPIO_Pin_13 ((uint16_t)0x2000) /* Pin 13 selected */ 73 #define GPIO_Pin_14 ((uint16_t)0x4000) /* Pin 14 selected */ 74 #define GPIO_Pin_15 ((uint16_t)0x8000) /* Pin 15 selected */ 75 #define GPIO_Pin_All ((uint16_t)0xFFFF) /* All pins selected */ 76 77 /* GPIO_Remap_define */ 78 #define GPIO_Remap_SPI1 ((uint32_t)0x00000001) /* SPI1 Alternate Function mapping */ 79 #define GPIO_Remap_I2C1 ((uint32_t)0x00000002) /* I2C1 Alternate Function mapping */ 80 #define GPIO_Remap_USART1 ((uint32_t)0x00000004) /* USART1 Alternate Function mapping */ 81 #define GPIO_Remap_USART2 ((uint32_t)0x00000008) /* USART2 Alternate Function mapping */ 82 #define GPIO_PartialRemap_USART3 ((uint32_t)0x00140010) /* USART3 Partial Alternate Function mapping */ 83 #define GPIO_FullRemap_USART3 ((uint32_t)0x00140030) /* USART3 Full Alternate Function mapping */ 84 #define GPIO_PartialRemap_TIM1 ((uint32_t)0x00160040) /* TIM1 Partial Alternate Function mapping */ 85 #define GPIO_FullRemap_TIM1 ((uint32_t)0x001600C0) /* TIM1 Full Alternate Function mapping */ 86 #define GPIO_PartialRemap1_TIM2 ((uint32_t)0x00180100) /* TIM2 Partial1 Alternate Function mapping */ 87 #define GPIO_PartialRemap2_TIM2 ((uint32_t)0x00180200) /* TIM2 Partial2 Alternate Function mapping */ 88 #define GPIO_FullRemap_TIM2 ((uint32_t)0x00180300) /* TIM2 Full Alternate Function mapping */ 89 #define GPIO_PartialRemap_TIM3 ((uint32_t)0x001A0800) /* TIM3 Partial Alternate Function mapping */ 90 #define GPIO_FullRemap_TIM3 ((uint32_t)0x001A0C00) /* TIM3 Full Alternate Function mapping */ 91 #define GPIO_Remap_TIM4 ((uint32_t)0x00001000) /* TIM4 Alternate Function mapping */ 92 #define GPIO_Remap1_CAN1 ((uint32_t)0x001D4000) /* CAN1 Alternate Function mapping */ 93 #define GPIO_Remap2_CAN1 ((uint32_t)0x001D6000) /* CAN1 Alternate Function mapping */ 94 #define GPIO_Remap_PD01 ((uint32_t)0x00008000) /* PD01 Alternate Function mapping */ 95 #define GPIO_Remap_TIM5CH4_LSI ((uint32_t)0x00200001) /* LSI connected to TIM5 Channel4 input capture for calibration */ 96 #define GPIO_Remap_ADC1_ETRGINJ ((uint32_t)0x00200002) /* ADC1 External Trigger Injected Conversion remapping */ 97 #define GPIO_Remap_ADC1_ETRGREG ((uint32_t)0x00200004) /* ADC1 External Trigger Regular Conversion remapping */ 98 #define GPIO_Remap_ADC2_ETRGINJ ((uint32_t)0x00200008) /* ADC2 External Trigger Injected Conversion remapping */ 99 #define GPIO_Remap_ADC2_ETRGREG ((uint32_t)0x00200010) /* ADC2 External Trigger Regular Conversion remapping */ 100 #define GPIO_Remap_ETH ((uint32_t)0x00200020) /* Ethernet remapping (only for Connectivity line devices) */ 101 #define GPIO_Remap_CAN2 ((uint32_t)0x00200040) /* CAN2 remapping (only for Connectivity line devices) */ 102 #define GPIO_Remap_SWJ_NoJTRST ((uint32_t)0x00300100) /* Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST */ 103 #define GPIO_Remap_SWJ_JTAGDisable ((uint32_t)0x00300200) /* JTAG-DP Disabled and SW-DP Enabled */ 104 #define GPIO_Remap_SWJ_Disable ((uint32_t)0x00300400) /* Full SWJ Disabled (JTAG-DP + SW-DP) */ 105 #define GPIO_Remap_SPI3 ((uint32_t)0x00201100) /* SPI3/I2S3 Alternate Function mapping (only for Connectivity line devices) */ 106 #define GPIO_Remap_TIM2ITR1_PTP_SOF ((uint32_t)0x00202000) /* Ethernet PTP output or USB OTG SOF (Start of Frame) connected 107 to TIM2 Internal Trigger 1 for calibration 108 (only for Connectivity line devices) */ 109 #define GPIO_Remap_PTP_PPS ((uint32_t)0x00204000) /* Ethernet MAC PPS_PTS output on PB05 (only for Connectivity line devices) */ 110 #define GPIO_Remap_TIM9 ((uint32_t)0x80000020) /* TIM9 Alternate Function mapping (only for XL-density devices) */ 111 #define GPIO_Remap_TIM10 ((uint32_t)0x80000040) /* TIM10 Alternate Function mapping (only for XL-density devices) */ 112 #define GPIO_Remap_FSMC_NADV ((uint32_t)0x80000400) /* FSMC_NADV Alternate Function mapping (only for High density Value line and XL-density devices) */ 113 #define GPIO_Remap_TIM1_DMA ((uint32_t)0x80000010) /* TIM1 DMA requests mapping (only for Value line devices) */ 114 #define GPIO_Remap_TIM67_DAC_DMA ((uint32_t)0x80000800) /* TIM6/TIM7 and DAC DMA requests remapping (only for High density Value line devices) */ 115 #define GPIO_Remap_MISC ((uint32_t)0x80002000) /* Miscellaneous Remap (DMA2 Channel5 Position and DAC Trigger remapping, 116 only for High density Value line devices) */ 117 118 /* GPIO_Port_Sources */ 119 #define GPIO_PortSourceGPIOA ((uint8_t)0x00) 120 #define GPIO_PortSourceGPIOB ((uint8_t)0x01) 121 #define GPIO_PortSourceGPIOC ((uint8_t)0x02) 122 #define GPIO_PortSourceGPIOD ((uint8_t)0x03) 123 #define GPIO_PortSourceGPIOE ((uint8_t)0x04) 124 #define GPIO_PortSourceGPIOF ((uint8_t)0x05) 125 #define GPIO_PortSourceGPIOG ((uint8_t)0x06) 126 127 /* GPIO_Pin_sources */ 128 #define GPIO_PinSource0 ((uint8_t)0x00) 129 #define GPIO_PinSource1 ((uint8_t)0x01) 130 #define GPIO_PinSource2 ((uint8_t)0x02) 131 #define GPIO_PinSource3 ((uint8_t)0x03) 132 #define GPIO_PinSource4 ((uint8_t)0x04) 133 #define GPIO_PinSource5 ((uint8_t)0x05) 134 #define GPIO_PinSource6 ((uint8_t)0x06) 135 #define GPIO_PinSource7 ((uint8_t)0x07) 136 #define GPIO_PinSource8 ((uint8_t)0x08) 137 #define GPIO_PinSource9 ((uint8_t)0x09) 138 #define GPIO_PinSource10 ((uint8_t)0x0A) 139 #define GPIO_PinSource11 ((uint8_t)0x0B) 140 #define GPIO_PinSource12 ((uint8_t)0x0C) 141 #define GPIO_PinSource13 ((uint8_t)0x0D) 142 #define GPIO_PinSource14 ((uint8_t)0x0E) 143 #define GPIO_PinSource15 ((uint8_t)0x0F) 144 145 /* Ethernet_Media_Interface */ 146 #define GPIO_ETH_MediaInterface_MII ((u32)0x00000000) 147 #define GPIO_ETH_MediaInterface_RMII ((u32)0x00000001) 148 149 150 void GPIO_DeInit(GPIO_TypeDef* GPIOx); 151 void GPIO_AFIODeInit(void); 152 void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct); 153 void GPIO_StructInit(GPIO_InitTypeDef* GPIO_InitStruct); 154 uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); 155 uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx); 156 uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); 157 uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx); 158 void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); 159 void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); 160 void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal); 161 void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal); 162 void GPIO_PinLockConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); 163 void GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource); 164 void GPIO_EventOutputCmd(FunctionalState NewState); 165 void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState); 166 void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource); 167 void GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface); 168 169 #ifdef __cplusplus 170 } 171 #endif 172 173 #endif 174 175 176 177 178 179 180 181