rt-thread/bsp/swm320-lq100/Libraries/SWM320_StdPeriph_Driver/SWM320_gpio.c

378 lines
18 KiB
C
Raw Normal View History

2018-12-24 17:17:27 +08:00
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : SWM320_gpio.c
* : SWM320单片机的通用输入输出功能驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
* : V1.1.0 20171025
* :
2018-12-24 17:17:27 +08:00
*
*
*******************************************************************************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH CODING INFORMATION
* REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS A RESULT, SYNWIT SHALL NOT BE HELD LIABLE
* FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
* -ECTION WITH THEIR PRODUCTS.
*
* COPYRIGHT 2012 Synwit Technology
*******************************************************************************************************************************************/
#include "SWM320.h"
#include "SWM320_gpio.h"
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : GPIO_Init()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t dir 0 1
* uint32_t pull_up 0 1
* uint32_t pull_down 0 1
* :
* : GPIOAGPIOCGPIOMGPIOP只有上拉GPIOBGPION只有下拉PN0PN1PN2三个引脚有上拉没下拉
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
void GPIO_Init(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t dir, uint32_t pull_up, uint32_t pull_down)
{
switch ((uint32_t)GPIOx)
{
case ((uint32_t)GPIOA):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOA_Pos);
2021-09-30 17:55:16 +08:00
PORT_Init(PORTA, n, 0, 1); //PORTA.PINn引脚配置为GPIO功能数字输入开启
2018-12-24 17:17:27 +08:00
if (dir == 1)
{
GPIOA->DIR |= (0x01 << n);
}
else
{
GPIOA->DIR &= ~(0x01 << n);
}
if (pull_up == 1)
PORT->PORTA_PULLU |= (0x01 << n);
else
PORT->PORTA_PULLU &= ~(0x01 << n);
break;
case ((uint32_t)GPIOB):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOB_Pos);
2021-09-30 17:55:16 +08:00
PORT_Init(PORTB, n, 0, 1); //PORTB.PINn引脚配置为GPIO功能数字输入开启
2018-12-24 17:17:27 +08:00
if (dir == 1)
{
GPIOB->DIR |= (0x01 << n);
}
else
{
GPIOB->DIR &= ~(0x01 << n);
}
if (pull_down == 1)
PORT->PORTB_PULLD |= (0x01 << n);
else
PORT->PORTB_PULLD &= ~(0x01 << n);
break;
case ((uint32_t)GPIOC):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOC_Pos);
2021-09-30 17:55:16 +08:00
PORT_Init(PORTC, n, 0, 1); //PORTC.PINn引脚配置为GPIO功能数字输入开启
2018-12-24 17:17:27 +08:00
if (dir == 1)
{
GPIOC->DIR |= (0x01 << n);
}
else
{
GPIOC->DIR &= ~(0x01 << n);
}
if (pull_up == 1)
PORT->PORTC_PULLU |= (0x01 << n);
else
PORT->PORTC_PULLU &= ~(0x01 << n);
break;
case ((uint32_t)GPIOM):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOM_Pos);
2021-09-30 17:55:16 +08:00
PORT_Init(PORTM, n, 0, 1); //PORTM.PINn引脚配置为GPIO功能数字输入开启
2018-12-24 17:17:27 +08:00
if (dir == 1)
{
GPIOM->DIR |= (0x01 << n);
}
else
{
GPIOM->DIR &= ~(0x01 << n);
}
if (pull_up == 1)
PORT->PORTM_PULLU |= (0x01 << n);
else
PORT->PORTM_PULLU &= ~(0x01 << n);
break;
case ((uint32_t)GPION):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPION_Pos);
2021-09-30 17:55:16 +08:00
PORT_Init(PORTN, n, 0, 1); //PORTN.PINn引脚配置为GPIO功能数字输入开启
2018-12-24 17:17:27 +08:00
if (dir == 1)
{
GPION->DIR |= (0x01 << n);
}
else
{
GPION->DIR &= ~(0x01 << n);
}
if (pull_down == 1)
PORT->PORTN_PULLD |= (0x01 << n);
else
PORT->PORTN_PULLD &= ~(0x01 << n);
break;
case ((uint32_t)GPIOP):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOP_Pos);
2021-09-30 17:55:16 +08:00
PORT_Init(PORTP, n, 0, 1); //PORTP.PINn引脚配置为GPIO功能数字输入开启
2018-12-24 17:17:27 +08:00
if (dir == 1)
{
GPIOP->DIR |= (0x01 << n);
}
else
{
GPIOP->DIR &= ~(0x01 << n);
}
if (pull_up == 1)
PORT->PORTP_PULLU |= (0x01 << n);
else
PORT->PORTP_PULLU &= ~(0x01 << n);
break;
}
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : GPIO_SetBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
void GPIO_SetBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
GPIOx->DATA |= (0x01 << n);
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : GPIO_ClrBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
void GPIO_ClrBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
GPIOx->DATA &= ~(0x01 << n);
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : GPIO_InvBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
void GPIO_InvBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
GPIOx->DATA ^= (0x01 << n);
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : GPIO_GetBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* : 0 1
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
uint32_t GPIO_GetBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
return ((GPIOx->DATA >> n) & 0x01);
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : GPIO_SetBits()
* : n开始的w位连续引脚的电平置高
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* :
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
void GPIO_SetBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
GPIOx->DATA |= (bits << n);
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : GPIO_ClrBits()
* : n开始的w位连续引脚的电平置低
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* :
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
void GPIO_ClrBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
GPIOx->DATA &= ~(bits << n);
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : GPIO_InvBits()
* : n开始的w位连续引脚的电平反转
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* :
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
void GPIO_InvBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
GPIOx->DATA ^= (bits << n);
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : GPIO_GetBits()
* : n开始的w位连续引脚的电平状态
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* : n开始的w位连续引脚的电平状态 0 1
* 0n的电平状态1n+1... ...w位表示引脚n+w的电平状态
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
uint32_t GPIO_GetBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
return ((GPIOx->DATA >> n) & bits);
}
2021-09-30 17:55:16 +08:00
/******************************************************************************************************************************************
* : GPIO_AtomicSetBit()
* : --ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicSetBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
*((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000) * 32 + n * 4)) = 1;
}
/******************************************************************************************************************************************
* : GPIO_AtomicClrBit()
* : --ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicClrBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
*((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000) * 32 + n * 4)) = 0;
}
/******************************************************************************************************************************************
* : GPIO_AtomicInvBit()
* : --ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicInvBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
*((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000) * 32 + n * 4)) = 1 - *((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000) * 32 + n * 4));
}
/******************************************************************************************************************************************
* : GPIO_AtomicSetBits()
* : n开始的w位连续引脚的电平置高--ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* uint32_t w
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicSetBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
__disable_irq();
GPIOx->DATA |= (bits << n);
__enable_irq();
}
/******************************************************************************************************************************************
* : GPIO_AtomicClrBits()
* : n开始的w位连续引脚的电平置低--ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* uint32_t w
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicClrBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
__disable_irq();
GPIOx->DATA &= ~(bits << n);
__enable_irq();
}
/******************************************************************************************************************************************
* : GPIO_AtomicInvBits()
* : n开始的w位连续引脚的电平反转--ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* uint32_t w
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
******************************************************************************************************************************************/
void GPIO_AtomicInvBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
{
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
__disable_irq();
GPIOx->DATA ^= (bits << n);
__enable_irq();
}