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<EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param pin - PA0-PA15
|
|||
|
* @param mode - <EFBFBD><EFBFBD><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<EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param pin - PB0-PB23
|
|||
|
* @param mode - <EFBFBD><EFBFBD><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<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param pin - PA0-PA15
|
|||
|
* @param mode - <EFBFBD><EFBFBD><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<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>ģʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param pin - PB0-PB23
|
|||
|
* @param mode - <EFBFBD><EFBFBD><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 <EFBFBD><EFBFBD><EFBFBD>蹦<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param s - <EFBFBD>Ƿ<EFBFBD>ʹ<EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD>
|
|||
|
* @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 ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>GPIO<EFBFBD><EFBFBD><EFBFBD>Ź<EFBFBD><EFBFBD>ܿ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*
|
|||
|
* @param s - ENABLE - <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>蹦<EFBFBD>ܣ<EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* DISABLE - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֹ<EFBFBD><EFBFBD>ܣ<EFBFBD><EFBFBD>ر<EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>蹦<EFBFBD><EFBFBD>
|
|||
|
* @param perph - RB_PIN_ADC0_1_IE - ADC0-1ͨ<EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_ADC2_3_IE - ADC2-3ͨ<EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_ADC4_5_IE - ADC4-5ͨ<EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_ADC6_7_IE - ADC6-7ͨ<EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_ADC8_9_IE - ADC8-9ͨ<EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_ADC10_11_IE - ADC10-11ͨ<EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_ADC12_13_IE - ADC12-13ͨ<EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_XT32K_IE - <EFBFBD>ⲿ32K<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_USB_IE - USB<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_ETH_IE - <EFBFBD><EFBFBD>̫<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_SEG0_3_IE - LCD<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SEG0-3<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_SEG4_7_IE - LCD<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SEG4-7<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_SEG8_11_IE - LCD<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SEG8-11<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_SEG12_15_IE - LCD<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SEG12-15<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_SEG16_19_IE - LCD<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SEG16-19<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* RB_PIN_SEG20_23_IE - LCD<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SEG20-23<EFBFBD><EFBFBD><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;
|
|||
|
}
|
|||
|
}
|