| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999 |
- /********************************** (C) COPYRIGHT *******************************
- * File Name : ch32v20x_gpio.c
- * Author : WCH
- * Version : V1.0.0
- * Date : 2024/05/06
- * Description : This file provides all the GPIO firmware functions.
- *********************************************************************************
- * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
- * Attention: This software (modified or not) and binary are used for
- * microcontroller manufactured by Nanjing Qinheng Microelectronics.
- *******************************************************************************/
- #include "ch32v20x_gpio.h"
- #include "ch32v20x_rcc.h"
- /* MASK */
- #define ECR_PORTPINCONFIG_MASK ((uint16_t)0xFF80)
- #define LSB_MASK ((uint16_t)0xFFFF)
- #define DBGAFR_POSITION_MASK ((uint32_t)0x000F0000)
- #define DBGAFR_SWJCFG_MASK ((uint32_t)0xF0FFFFFF)
- #define DBGAFR_LOCATION_MASK ((uint32_t)0x00200000)
- #define DBGAFR_NUMBITS_MASK ((uint32_t)0x00100000)
- #if defined (CH32V20x_D6)
- uint8_t MCU_Version = 0;
- #endif
- /*********************************************************************
- * @fn GPIO_DeInit
- *
- * @brief Deinitializes the GPIOx peripheral registers to their default
- * reset values.
- *
- * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
- *
- * @return none
- */
- void GPIO_DeInit(GPIO_TypeDef *GPIOx)
- {
- if(GPIOx == GPIOA)
- {
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, ENABLE);
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOA, DISABLE);
- }
- else if(GPIOx == GPIOB)
- {
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, ENABLE);
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOB, DISABLE);
- }
- else if(GPIOx == GPIOC)
- {
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, ENABLE);
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOC, DISABLE);
- }
- else if(GPIOx == GPIOD)
- {
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, ENABLE);
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD, DISABLE);
- }
- else if(GPIOx == GPIOE)
- {
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, ENABLE);
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOE, DISABLE);
- }
- }
- /*********************************************************************
- * @fn GPIO_AFIODeInit
- *
- * @brief Deinitializes the Alternate Functions (remap, event control
- * and EXTI configuration) registers to their default reset values.
- *
- * @return none
- */
- void GPIO_AFIODeInit(void)
- {
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, ENABLE);
- RCC_APB2PeriphResetCmd(RCC_APB2Periph_AFIO, DISABLE);
- }
- /*********************************************************************
- * @fn GPIO_Init
- *
- * @brief GPIOx - where x can be (A..G) to select the GPIO peripheral.
- *
- * @param GPIO_InitStruct - pointer to a GPIO_InitTypeDef structure that
- * contains the configuration information for the specified GPIO peripheral.
- *
- * @return none
- */
- void GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_InitStruct)
- {
- uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
- uint32_t tmpreg = 0x00, pinmask = 0x00;
- currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
- if((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
- {
- currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
- }
- #if defined (CH32V20x_D6)
- if(((*(uint32_t *) 0x40022030) & 0x0F000000) == 0)
- {
- MCU_Version = 1;
- }
- if((GPIOx == GPIOC) && MCU_Version){
- GPIO_InitStruct->GPIO_Pin = GPIO_InitStruct->GPIO_Pin >> 13;
- }
- #endif
- if(((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)
- {
- tmpreg = GPIOx->CFGLR;
- for(pinpos = 0x00; pinpos < 0x08; pinpos++)
- {
- pos = ((uint32_t)0x01) << pinpos;
- currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
- if(currentpin == pos)
- {
- pos = pinpos << 2;
- pinmask = ((uint32_t)0x0F) << pos;
- tmpreg &= ~pinmask;
- tmpreg |= (currentmode << pos);
- if(GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
- {
- GPIOx->BCR = (((uint32_t)0x01) << pinpos);
- }
- else
- {
- if(GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
- {
- GPIOx->BSHR = (((uint32_t)0x01) << pinpos);
- }
- }
- }
- }
- GPIOx->CFGLR = tmpreg;
- }
- if(GPIO_InitStruct->GPIO_Pin > 0x00FF)
- {
- tmpreg = GPIOx->CFGHR;
- for(pinpos = 0x00; pinpos < 0x08; pinpos++)
- {
- pos = (((uint32_t)0x01) << (pinpos + 0x08));
- currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
- if(currentpin == pos)
- {
- pos = pinpos << 2;
- pinmask = ((uint32_t)0x0F) << pos;
- tmpreg &= ~pinmask;
- tmpreg |= (currentmode << pos);
- if(GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
- {
- GPIOx->BCR = (((uint32_t)0x01) << (pinpos + 0x08));
- }
- if(GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
- {
- GPIOx->BSHR = (((uint32_t)0x01) << (pinpos + 0x08));
- }
- }
- }
- GPIOx->CFGHR = tmpreg;
- }
- }
- /*********************************************************************
- * @fn GPIO_StructInit
- *
- * @brief Fills each GPIO_InitStruct member with its default
- *
- * @param GPIO_InitStruct - pointer to a GPIO_InitTypeDef structure
- * which will be initialized.
- *
- * @return none
- */
- void GPIO_StructInit(GPIO_InitTypeDef *GPIO_InitStruct)
- {
- GPIO_InitStruct->GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN_FLOATING;
- }
- /*********************************************************************
- * @fn GPIO_ReadInputDataBit
- *
- * @brief GPIOx - where x can be (A..G) to select the GPIO peripheral.
- *
- * @param GPIO_Pin - specifies the port bit to read.
- * This parameter can be GPIO_Pin_x where x can be (0..15).
- *
- * @return The input port pin value.
- */
- uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
- {
- uint8_t bitstatus = 0x00;
- #if defined (CH32V20x_D6)
- if((GPIOx == GPIOC) && MCU_Version){
- GPIO_Pin = GPIO_Pin >> 13;
- }
- #endif
- if((GPIOx->INDR & GPIO_Pin) != (uint32_t)Bit_RESET)
- {
- bitstatus = (uint8_t)Bit_SET;
- }
- else
- {
- bitstatus = (uint8_t)Bit_RESET;
- }
- return bitstatus;
- }
- /*********************************************************************
- * @fn GPIO_ReadInputData
- *
- * @brief Reads the specified GPIO input data port.
- *
- * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
- *
- * @return The output port pin value.
- */
- uint16_t GPIO_ReadInputData(GPIO_TypeDef *GPIOx)
- {
- uint16_t val;
- #if defined (CH32V20x_D6)
- if((GPIOx == GPIOC) && MCU_Version){
- val = ( uint16_t )(GPIOx->INDR << 13);
- }
- else{
- val = ( uint16_t )GPIOx->INDR;
- }
- #else
- val = ( uint16_t )GPIOx->INDR;
- #endif
- return ( val );
- }
- /*********************************************************************
- * @fn GPIO_ReadOutputDataBit
- *
- * @brief Reads the specified output data port bit.
- *
- * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
- * GPIO_Pin - specifies the port bit to read.
- * This parameter can be GPIO_Pin_x where x can be (0..15).
- *
- * @return none
- */
- uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
- {
- uint8_t bitstatus = 0x00;
- #if defined (CH32V20x_D6)
- if((GPIOx == GPIOC) && MCU_Version){
- GPIO_Pin = GPIO_Pin >> 13;
- }
- #endif
- if((GPIOx->OUTDR & GPIO_Pin) != (uint32_t)Bit_RESET)
- {
- bitstatus = (uint8_t)Bit_SET;
- }
- else
- {
- bitstatus = (uint8_t)Bit_RESET;
- }
- return bitstatus;
- }
- /*********************************************************************
- * @fn GPIO_ReadOutputData
- *
- * @brief Reads the specified GPIO output data port.
- *
- * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
- *
- * @return GPIO output port pin value.
- */
- uint16_t GPIO_ReadOutputData(GPIO_TypeDef *GPIOx)
- {
- uint16_t val;
- #if defined (CH32V20x_D6)
- if((GPIOx == GPIOC) && MCU_Version){
- val = ( uint16_t )(GPIOx->OUTDR << 13);
- }
- else{
- val = ( uint16_t )GPIOx->OUTDR;
- }
- #else
- val = ( uint16_t )GPIOx->OUTDR;
- #endif
- return ( val );
- }
- /*********************************************************************
- * @fn GPIO_SetBits
- *
- * @brief Sets the selected data port bits.
- *
- * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
- * GPIO_Pin - specifies the port bits to be written.
- * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
- *
- * @return none
- */
- void GPIO_SetBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
- {
- #if defined (CH32V20x_D6)
- if((GPIOx == GPIOC) && MCU_Version){
- GPIO_Pin = GPIO_Pin >> 13;
- }
- #endif
- GPIOx->BSHR = GPIO_Pin;
- }
- /*********************************************************************
- * @fn GPIO_ResetBits
- *
- * @brief Clears the selected data port bits.
- *
- * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
- * GPIO_Pin - specifies the port bits to be written.
- * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
- *
- * @return none
- */
- void GPIO_ResetBits(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
- {
- #if defined (CH32V20x_D6)
- if((GPIOx == GPIOC) && MCU_Version){
- GPIO_Pin = GPIO_Pin >> 13;
- }
- #endif
- GPIOx->BCR = GPIO_Pin;
- }
- /*********************************************************************
- * @fn GPIO_WriteBit
- *
- * @brief Sets or clears the selected data port bit.
- *
- * @param GPIO_Pin - specifies the port bit to be written.
- * This parameter can be one of GPIO_Pin_x where x can be (0..15).
- * BitVal - specifies the value to be written to the selected bit.
- * Bit_RESET - to clear the port pin.
- * Bit_SET - to set the port pin.
- *
- * @return none
- */
- void GPIO_WriteBit(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
- {
- #if defined (CH32V20x_D6)
- if((GPIOx == GPIOC) && MCU_Version){
- GPIO_Pin = GPIO_Pin >> 13;
- }
- #endif
- if(BitVal != Bit_RESET)
- {
- GPIOx->BSHR = GPIO_Pin;
- }
- else
- {
- GPIOx->BCR = GPIO_Pin;
- }
- }
- /*********************************************************************
- * @fn GPIO_Write
- *
- * @brief Writes data to the specified GPIO data port.
- *
- * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
- * PortVal - specifies the value to be written to the port output data register.
- *
- * @return none
- */
- void GPIO_Write(GPIO_TypeDef *GPIOx, uint16_t PortVal)
- {
- #if defined (CH32V20x_D6)
- if((GPIOx == GPIOC) && MCU_Version){
- PortVal = PortVal >> 13;
- }
- #endif
- GPIOx->OUTDR = PortVal;
- }
- /*********************************************************************
- * @fn GPIO_PinLockConfig
- *
- * @brief Locks GPIO Pins configuration registers.
- *
- * @param GPIOx - where x can be (A..G) to select the GPIO peripheral.
- * GPIO_Pin - specifies the port bit to be written.
- * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
- *
- * @return none
- */
- void GPIO_PinLockConfig(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
- {
- uint32_t tmp = 0x00010000;
- #if defined (CH32V20x_D6)
- if((GPIOx == GPIOC) && MCU_Version){
- GPIO_Pin = GPIO_Pin >> 13;
- }
- #endif
- tmp |= GPIO_Pin;
- GPIOx->LCKR = tmp;
- GPIOx->LCKR = GPIO_Pin;
- GPIOx->LCKR = tmp;
- tmp = GPIOx->LCKR;
- tmp = GPIOx->LCKR;
- }
- /*********************************************************************
- * @fn GPIO_EventOutputConfig
- *
- * @brief Selects the GPIO pin used as Event output.
- *
- * @param GPIO_PortSource - selects the GPIO port to be used as source
- * for Event output.
- * This parameter can be GPIO_PortSourceGPIOx where x can be (A..E).
- * GPIO_PinSource - specifies the pin for the Event output.
- * This parameter can be GPIO_PinSourcex where x can be (0..15).
- *
- * @return none
- */
- void GPIO_EventOutputConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)
- {
- uint32_t tmpreg = 0x00;
- tmpreg = AFIO->ECR;
- tmpreg &= ECR_PORTPINCONFIG_MASK;
- tmpreg |= (uint32_t)GPIO_PortSource << 0x04;
- tmpreg |= GPIO_PinSource;
- AFIO->ECR = tmpreg;
- }
- /*********************************************************************
- * @fn GPIO_EventOutputCmd
- *
- * @brief Enables or disables the Event Output.
- *
- * @param NewState - ENABLE or DISABLE.
- *
- * @return none
- */
- void GPIO_EventOutputCmd(FunctionalState NewState)
- {
- if(NewState)
- {
- AFIO->ECR |= (1 << 7);
- }
- else
- {
- AFIO->ECR &= ~(1 << 7);
- }
- }
- /*********************************************************************
- * @fn GPIO_PinRemapConfig
- *
- * @brief Changes the mapping of the specified pin.
- *
- * @param GPIO_Remap - selects the pin to remap.
- * GPIO_Remap_SPI1 - SPI1 Alternate Function mapping
- * GPIO_Remap_I2C1 - I2C1 Alternate Function mapping
- * GPIO_Remap_USART1 - USART1 Alternate Function mapping
- * GPIO_Remap_USART2 - USART2 Alternate Function mapping
- * GPIO_PartialRemap_USART3 - USART3 Partial Alternate Function mapping
- * GPIO_FullRemap_USART3 - USART3 Full Alternate Function mapping
- * GPIO_PartialRemap_TIM1 - TIM1 Partial Alternate Function mapping
- * GPIO_FullRemap_TIM1 - TIM1 Full Alternate Function mapping
- * GPIO_PartialRemap1_TIM2 - TIM2 Partial1 Alternate Function mapping
- * GPIO_PartialRemap2_TIM2 - TIM2 Partial2 Alternate Function mapping
- * GPIO_FullRemap_TIM2 - TIM2 Full Alternate Function mapping
- * GPIO_PartialRemap_TIM3 - TIM3 Partial Alternate Function mapping
- * GPIO_FullRemap_TIM3 - TIM3 Full Alternate Function mapping
- * GPIO_Remap_TIM4 - TIM4 Alternate Function mapping
- * GPIO_Remap1_CAN1 - CAN1 Alternate Function mapping
- * GPIO_Remap2_CAN1 - CAN1 Alternate Function mapping
- * GPIO_Remap_PD0PD1 - PD0 and PD1 Alternate Function mapping
- * GPIO_Remap_ADC1_ETRGINJ - ADC1 External Trigger Injected Conversion remapping
- * GPIO_Remap_ADC1_ETRGREG - ADC1 External Trigger Regular Conversion remapping
- * GPIO_Remap_ADC2_ETRGINJ - ADC2 External Trigger Injected Conversion remapping
- * GPIO_Remap_ADC2_ETRGREG - ADC2 External Trigger Regular Conversion remapping
- * GPIO_Remap_ETH - Ethernet remapping
- * GPIO_Remap_CAN2 - CAN2 remapping
- * GPIO_Remap_MII_RMII_SEL - MII or RMII selection
- * GPIO_Remap_SWJ_Disable - Full SWJ Disabled
- * GPIO_Remap_TIM2ITR1_PTP_SOF - Ethernet PTP output or USB OTG SOF (Start of Frame) connected
- * to TIM2 Internal Trigger 1 for calibration
- * GPIO_Remap_TIM2ITR1_PTP_SOF - Ethernet PTP output or USB OTG SOF (Start of Frame)
- * GPIO_Remap_TIM8 - TIM8 Alternate Function mapping
- * GPIO_PartialRemap_TIM9 - TIM9 Partial Alternate Function mapping
- * GPIO_FullRemap_TIM9 - TIM9 Full Alternate Function mapping
- * GPIO_PartialRemap_TIM10 - TIM10 Partial Alternate Function mapping
- * GPIO_FullRemap_TIM10 - TIM10 Full Alternate Function mapping
- * GPIO_Remap_FSMC_NADV - FSMC_NADV Alternate Function mapping
- * GPIO_PartialRemap_USART4 - USART4 Partial Alternate Function mapping
- * GPIO_FullRemap_USART4 - USART4 Full Alternate Function mapping
- * GPIO_PartialRemap_USART5 - USART5 Partial Alternate Function mapping
- * GPIO_FullRemap_USART5 - USART5 Full Alternate Function mapping
- * GPIO_PartialRemap_USART6 - USART6 Partial Alternate Function mapping
- * GPIO_FullRemap_USART6 - USART6 Full Alternate Function mapping
- * GPIO_PartialRemap_USART7 - USART7 Partial Alternate Function mapping
- * GPIO_FullRemap_USART7 - USART7 Full Alternate Function mapping
- * GPIO_PartialRemap_USART8 - USART8 Partial Alternate Function mapping
- * GPIO_FullRemap_USART8 - USART8 Full Alternate Function mapping
- * GPIO_Remap_USART1_HighBit - USART1 Alternate Function mapping high bit
- * NewState - ENABLE or DISABLE.
- *
- * @return none
- */
- void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState)
- {
- uint32_t tmp = 0x00, tmp1 = 0x00, tmpreg = 0x00, tmpmask = 0x00;
- if((GPIO_Remap & 0x80000000) == 0x80000000)
- {
- tmpreg = AFIO->PCFR2;
- }
- else
- {
- tmpreg = AFIO->PCFR1;
- if(((*(uint32_t *) 0x40022030) & 0x0F000000) == 0){
- tmpreg = ((tmpreg>>1)&0xFFFFE000)|(tmpreg&0x00001FFF);
- }
- }
- tmpmask = (GPIO_Remap & DBGAFR_POSITION_MASK) >> 0x10;
- tmp = GPIO_Remap & LSB_MASK;
- /* Clear bit */
- if((GPIO_Remap & 0x80000000) == 0x80000000)
- { /* PCFR2 */
- if((GPIO_Remap & (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK)) == (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK)) /* [31:16] 2bit */
- {
- tmp1 = ((uint32_t)0x03) << (tmpmask + 0x10);
- tmpreg &= ~tmp1;
- }
- else if((GPIO_Remap & DBGAFR_NUMBITS_MASK) == DBGAFR_NUMBITS_MASK) /* [15:0] 2bit */
- {
- tmp1 = ((uint32_t)0x03) << tmpmask;
- tmpreg &= ~tmp1;
- }
- else /* [31:0] 1bit */
- {
- tmpreg &= ~(tmp << (((GPIO_Remap & 0x7FFFFFFF ) >> 0x15) * 0x10));
- }
- }
- else
- { /* PCFR1 */
- if((GPIO_Remap & (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK)) == (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK)) /* [26:24] 3bit SWD_JTAG */
- {
- tmpreg &= DBGAFR_SWJCFG_MASK;
- AFIO->PCFR1 &= DBGAFR_SWJCFG_MASK;
- }
- else if((GPIO_Remap & DBGAFR_NUMBITS_MASK) == DBGAFR_NUMBITS_MASK) /* [15:0] 2bit */
- {
- tmp1 = ((uint32_t)0x03) << tmpmask;
- tmpreg &= ~tmp1;
- tmpreg |= ~DBGAFR_SWJCFG_MASK;
- }
- else /* [31:0] 1bit */
- {
- tmpreg &= ~(tmp << ((GPIO_Remap >> 0x15) * 0x10));
- tmpreg |= ~DBGAFR_SWJCFG_MASK;
- }
- }
- /* Set bit */
- if(NewState != DISABLE)
- {
- tmpreg |= (tmp << (((GPIO_Remap & 0x7FFFFFFF )>> 0x15) * 0x10));
- }
- if((GPIO_Remap & 0x80000000) == 0x80000000)
- {
- AFIO->PCFR2 = tmpreg;
- }
- else
- {
- AFIO->PCFR1 = tmpreg;
- }
- }
- /*********************************************************************
- * @fn GPIO_EXTILineConfig
- *
- * @brief Selects the GPIO pin used as EXTI Line.
- *
- * @param GPIO_PortSource - selects the GPIO port to be used as source for EXTI lines.
- * This parameter can be GPIO_PortSourceGPIOx where x can be (A..G).
- * GPIO_PinSource - specifies the EXTI line to be configured.
- * This parameter can be GPIO_PinSourcex where x can be (0..15).
- *
- * @return none
- */
- void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource)
- {
- uint32_t tmp = 0x00;
- tmp = ((uint32_t)0x0F) << (0x04 * (GPIO_PinSource & (uint8_t)0x03));
- AFIO->EXTICR[GPIO_PinSource >> 0x02] &= ~tmp;
- AFIO->EXTICR[GPIO_PinSource >> 0x02] |= (((uint32_t)GPIO_PortSource) << (0x04 * (GPIO_PinSource & (uint8_t)0x03)));
- }
- /*********************************************************************
- * @fn GPIO_ETH_MediaInterfaceConfig
- *
- * @brief Selects the Ethernet media interface.
- *
- * @param GPIO_ETH_MediaInterface - specifies the Media Interface mode.
- * GPIO_ETH_MediaInterface_MII - MII mode
- * GPIO_ETH_MediaInterface_RMII - RMII mode
- *
- * @return none
- */
- void GPIO_ETH_MediaInterfaceConfig(uint32_t GPIO_ETH_MediaInterface)
- {
- if(GPIO_ETH_MediaInterface)
- {
- AFIO->PCFR1 |= (1 << 23);
- }
- else
- {
- AFIO->PCFR1 &= ~(1 << 23);
- }
- }
- /*********************************************************************
- * @fn GPIO_IPD_Unused
- *
- * @brief Configure unused GPIO as input pull-up.
- *
- * @param none
- *
- * @return none
- */
- void GPIO_IPD_Unused(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure = {0};
- uint32_t chip = 0;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB \
- | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
- chip = *( uint32_t * )0x1FFFF704 & (~0x000000F0);
- switch(chip)
- {
- #ifdef CH32V20x_D6
- case 0x20370500: //CH32V203F6P6
- {
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0\
- |GPIO_Pin_3|GPIO_Pin_4\
- |GPIO_Pin_5|GPIO_Pin_6\
- |GPIO_Pin_7|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12|GPIO_Pin_13\
- |GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1\
- |GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6|GPIO_Pin_7\
- |GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12|GPIO_Pin_13\
- |GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- case 0x203A0500: //CH32V203F8P6
- {
- GPIO_PinRemapConfig(GPIO_Remap_PD01, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12\
- |GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1\
- |GPIO_Pin_3|GPIO_Pin_4\
- |GPIO_Pin_5|GPIO_Pin_8\
- |GPIO_Pin_9|GPIO_Pin_10\
- |GPIO_Pin_11|GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1\
- |GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6|GPIO_Pin_7\
- |GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12|GPIO_Pin_13\
- |GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- case 0x203E0500: //CH32V203F8U6
- {
- GPIO_PinRemapConfig(GPIO_Remap_PD01, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6|GPIO_Pin_7\
- |GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_12|GPIO_Pin_13;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1\
- |GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6|GPIO_Pin_7\
- |GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12|GPIO_Pin_13\
- |GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- case 0x20360500: //CH32V203G6U6
- {
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12|GPIO_Pin_13\
- |GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1\
- |GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6|GPIO_Pin_7\
- |GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12|GPIO_Pin_13\
- |GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- case 0x203B0500: //CH32V203G8R6
- {
- GPIO_PinRemapConfig(GPIO_Remap_PD01, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1\
- |GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6|GPIO_Pin_7\
- |GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12|GPIO_Pin_13\
- |GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- case 0x20320500: //CH32V203K8T6
- {
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12|GPIO_Pin_13\
- |GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1\
- |GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6|GPIO_Pin_7\
- |GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12|GPIO_Pin_13\
- |GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- case 0x20330500: //CH32V203C6T6
- {
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1\
- |GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6|GPIO_Pin_7\
- |GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- case 0x20310500: //CH32V203C8T6
- {
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1\
- |GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6|GPIO_Pin_7\
- |GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- case 0x20300500: //CH32V203C8U6
- {
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1\
- |GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6|GPIO_Pin_7\
- |GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- #elif defined(CH32V20x_D8)
- case 0x2034050C: //CH32V203RBT6
- {
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4\
- |GPIO_Pin_5|GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- #elif defined(CH32V20x_D8W)
- case 0x2083050C: //CH32V208GBU6
- {
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1\
- |GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_9|GPIO_Pin_10\
- |GPIO_Pin_11|GPIO_Pin_12\
- |GPIO_Pin_13|GPIO_Pin_14\
- |GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1\
- |GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12|GPIO_Pin_13;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- case 0x2082050C: //CH32V208CBU6
- {
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1\
- |GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6|GPIO_Pin_7\
- |GPIO_Pin_8|GPIO_Pin_9\
- |GPIO_Pin_10|GPIO_Pin_11\
- |GPIO_Pin_12;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3\
- |GPIO_Pin_4|GPIO_Pin_5\
- |GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- case 0x2081050C: //CH32V208RBT6
- {
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4\
- |GPIO_Pin_5|GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- break;
- }
- #endif
- default:
- {
- break;
- }
- }
- }
|