mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
254 lines
6.6 KiB
C
254 lines
6.6 KiB
C
/********************************** (C) COPYRIGHT *******************************
|
||
* File Name : CH58x_gpio.c
|
||
* Author : WCH
|
||
* Version : V1.2
|
||
* Date : 2021/11/17
|
||
* Description
|
||
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
|
||
* SPDX-License-Identifier: Apache-2.0
|
||
*******************************************************************************/
|
||
|
||
#include "CH58x_common.h"
|
||
|
||
/*********************************************************************
|
||
* @fn GPIOA_ModeCfg
|
||
*
|
||
* @brief GPIOA<4F>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
|
||
*
|
||
* @param pin - PA0-PA15
|
||
* @param mode - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*
|
||
* @return none
|
||
*/
|
||
void GPIOA_ModeCfg(uint32_t pin, GPIOModeTypeDef mode)
|
||
{
|
||
switch(mode)
|
||
{
|
||
case GPIO_ModeIN_Floating:
|
||
R32_PA_PD_DRV &= ~pin;
|
||
R32_PA_PU &= ~pin;
|
||
R32_PA_DIR &= ~pin;
|
||
break;
|
||
|
||
case GPIO_ModeIN_PU:
|
||
R32_PA_PD_DRV &= ~pin;
|
||
R32_PA_PU |= pin;
|
||
R32_PA_DIR &= ~pin;
|
||
break;
|
||
|
||
case GPIO_ModeIN_PD:
|
||
R32_PA_PD_DRV |= pin;
|
||
R32_PA_PU &= ~pin;
|
||
R32_PA_DIR &= ~pin;
|
||
break;
|
||
|
||
case GPIO_ModeOut_PP_5mA:
|
||
R32_PA_PD_DRV &= ~pin;
|
||
R32_PA_DIR |= pin;
|
||
break;
|
||
|
||
case GPIO_ModeOut_PP_20mA:
|
||
R32_PA_PD_DRV |= pin;
|
||
R32_PA_DIR |= pin;
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn GPIOB_ModeCfg
|
||
*
|
||
* @brief GPIOB<4F>˿<EFBFBD><CBBF><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
|
||
*
|
||
* @param pin - PB0-PB23
|
||
* @param mode - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*
|
||
* @return none
|
||
*/
|
||
void GPIOB_ModeCfg(uint32_t pin, GPIOModeTypeDef mode)
|
||
{
|
||
switch(mode)
|
||
{
|
||
case GPIO_ModeIN_Floating:
|
||
R32_PB_PD_DRV &= ~pin;
|
||
R32_PB_PU &= ~pin;
|
||
R32_PB_DIR &= ~pin;
|
||
break;
|
||
|
||
case GPIO_ModeIN_PU:
|
||
R32_PB_PD_DRV &= ~pin;
|
||
R32_PB_PU |= pin;
|
||
R32_PB_DIR &= ~pin;
|
||
break;
|
||
|
||
case GPIO_ModeIN_PD:
|
||
R32_PB_PD_DRV |= pin;
|
||
R32_PB_PU &= ~pin;
|
||
R32_PB_DIR &= ~pin;
|
||
break;
|
||
|
||
case GPIO_ModeOut_PP_5mA:
|
||
R32_PB_PD_DRV &= ~pin;
|
||
R32_PB_DIR |= pin;
|
||
break;
|
||
|
||
case GPIO_ModeOut_PP_20mA:
|
||
R32_PB_PD_DRV |= pin;
|
||
R32_PB_DIR |= pin;
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn GPIOA_ITModeCfg
|
||
*
|
||
* @brief GPIOA<4F><41><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
|
||
*
|
||
* @param pin - PA0-PA15
|
||
* @param mode - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*
|
||
* @return none
|
||
*/
|
||
void GPIOA_ITModeCfg(uint32_t pin, GPIOITModeTpDef mode)
|
||
{
|
||
switch(mode)
|
||
{
|
||
case GPIO_ITMode_LowLevel: // <20>͵<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>
|
||
R16_PA_INT_MODE &= ~pin;
|
||
R32_PA_CLR |= pin;
|
||
break;
|
||
|
||
case GPIO_ITMode_HighLevel: // <20>ߵ<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>
|
||
R16_PA_INT_MODE &= ~pin;
|
||
R32_PA_OUT |= pin;
|
||
break;
|
||
|
||
case GPIO_ITMode_FallEdge: // <20>½<EFBFBD><C2BD>ش<EFBFBD><D8B4><EFBFBD>
|
||
R16_PA_INT_MODE |= pin;
|
||
R32_PA_CLR |= pin;
|
||
break;
|
||
|
||
case GPIO_ITMode_RiseEdge: // <20><><EFBFBD><EFBFBD><EFBFBD>ش<EFBFBD><D8B4><EFBFBD>
|
||
R16_PA_INT_MODE |= pin;
|
||
R32_PA_OUT |= pin;
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
R16_PA_INT_IF = pin;
|
||
R16_PA_INT_EN |= pin;
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn GPIOB_ITModeCfg
|
||
*
|
||
* @brief GPIOB<4F><42><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
|
||
*
|
||
* @param pin - PB0-PB23
|
||
* @param mode - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*
|
||
* @return none
|
||
*/
|
||
void GPIOB_ITModeCfg(uint32_t pin, GPIOITModeTpDef mode)
|
||
{
|
||
uint32_t Pin = pin | ((pin & (GPIO_Pin_22 | GPIO_Pin_23)) >> 14);
|
||
switch(mode)
|
||
{
|
||
case GPIO_ITMode_LowLevel: // <20>͵<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>
|
||
R16_PB_INT_MODE &= ~Pin;
|
||
R32_PB_CLR |= pin;
|
||
break;
|
||
|
||
case GPIO_ITMode_HighLevel: // <20>ߵ<EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD>
|
||
R16_PB_INT_MODE &= ~Pin;
|
||
R32_PB_OUT |= pin;
|
||
break;
|
||
|
||
case GPIO_ITMode_FallEdge: // <20>½<EFBFBD><C2BD>ش<EFBFBD><D8B4><EFBFBD>
|
||
R16_PB_INT_MODE |= Pin;
|
||
R32_PB_CLR |= pin;
|
||
break;
|
||
|
||
case GPIO_ITMode_RiseEdge: // <20><><EFBFBD><EFBFBD><EFBFBD>ش<EFBFBD><D8B4><EFBFBD>
|
||
R16_PB_INT_MODE |= Pin;
|
||
R32_PB_OUT |= pin;
|
||
break;
|
||
|
||
default:
|
||
break;
|
||
}
|
||
R16_PB_INT_IF = Pin;
|
||
R16_PB_INT_EN |= Pin;
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn GPIOPinRemap
|
||
*
|
||
* @brief <20><><EFBFBD>蹦<EFBFBD><E8B9A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><D3B3>
|
||
*
|
||
* @param s - <20>Ƿ<EFBFBD>ʹ<EFBFBD><CAB9>ӳ<EFBFBD><D3B3>
|
||
* @param perph - RB_PIN_SPI0 - SPI0: PA12/PA13/PA14/PA15 -> PB12/PB13/PB14/PB15
|
||
* RB_PIN_UART1 - UART1: PA8/PA9 -> PB12/PB13
|
||
* RB_PIN_UART0 - UART0: PB4/PB7 -> PA15/PA14
|
||
* RB_PIN_TMR2 - TMR2: PA11 -> PB11
|
||
* RB_PIN_TMR1 - TMR1: PA10 -> PB10
|
||
* RB_PIN_TMR0 - TMR0: PA9 -> PB23
|
||
*
|
||
* @return none
|
||
*/
|
||
void GPIOPinRemap(FunctionalState s, uint16_t perph)
|
||
{
|
||
if(s)
|
||
{
|
||
R16_PIN_ALTERNATE |= perph;
|
||
}
|
||
else
|
||
{
|
||
R16_PIN_ALTERNATE &= ~perph;
|
||
}
|
||
}
|
||
|
||
/*********************************************************************
|
||
* @fn GPIOAGPPCfg
|
||
*
|
||
* @brief ģ<><C4A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>GPIO<49><4F><EFBFBD>Ź<EFBFBD><C5B9>ܿ<EFBFBD><DCBF><EFBFBD>
|
||
*
|
||
* @param s - ENABLE - <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD>蹦<EFBFBD>ܣ<EFBFBD><DCA3>ر<EFBFBD><D8B1><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9><EFBFBD>
|
||
* DISABLE - <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><D6B9>ܣ<EFBFBD><DCA3>ر<EFBFBD>ģ<EFBFBD><C4A3><EFBFBD><EFBFBD><EFBFBD>蹦<EFBFBD><E8B9A6>
|
||
* @param perph - RB_PIN_ADC0_1_IE - ADC0-1ͨ<31><CDA8>
|
||
* RB_PIN_ADC2_3_IE - ADC2-3ͨ<33><CDA8>
|
||
* RB_PIN_ADC4_5_IE - ADC4-5ͨ<35><CDA8>
|
||
* RB_PIN_ADC6_7_IE - ADC6-7ͨ<37><CDA8>
|
||
* RB_PIN_ADC8_9_IE - ADC8-9ͨ<39><CDA8>
|
||
* RB_PIN_ADC10_11_IE - ADC10-11ͨ<31><CDA8>
|
||
* RB_PIN_ADC12_13_IE - ADC12-13ͨ<33><CDA8>
|
||
* RB_PIN_XT32K_IE - <20>ⲿ32K<32><4B><EFBFBD><EFBFBD>
|
||
* RB_PIN_USB_IE - USB<53><42><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD><C5BA><EFBFBD><EFBFBD><EFBFBD>
|
||
* RB_PIN_ETH_IE - <20><>̫<EFBFBD><CCAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD><C5BA><EFBFBD><EFBFBD><EFBFBD>
|
||
* RB_PIN_SEG0_3_IE - LCD<43><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SEG0-3<><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
* RB_PIN_SEG4_7_IE - LCD<43><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SEG4-7<><37><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
* RB_PIN_SEG8_11_IE - LCD<43><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SEG8-11<31><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
* RB_PIN_SEG12_15_IE - LCD<43><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SEG12-15<31><35><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
* RB_PIN_SEG16_19_IE - LCD<43><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SEG16-19<31><39><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
* RB_PIN_SEG20_23_IE - LCD<43><44><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SEG20-23<32><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
*
|
||
* @return none
|
||
*/
|
||
void GPIOAGPPCfg(FunctionalState s, uint16_t perph)
|
||
{
|
||
if(s)
|
||
{
|
||
R16_PIN_ANALOG_IE |= perph;
|
||
}
|
||
else
|
||
{
|
||
R16_PIN_ANALOG_IE &= ~perph;
|
||
}
|
||
}
|