rt-thread/bsp/swm320/libraries/SWM320_StdPeriph_Driver/SWM320_gpio.c

378 lines
18 KiB
C
Raw Normal View History

2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : SWM320_gpio.c
2021-09-13 21:04:42 +08:00
* : SWM320单片机的通用输入输出功能驱动库
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
2021-05-06 10:42:41 +08:00
* :
2021-09-13 21:04:42 +08:00
* : V1.1.0 20171025
* :
2021-02-18 13:29:12 +08:00
*
*
*******************************************************************************************************************************************
* @attention
*
2021-09-13 21:04:42 +08:00
* 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
2021-02-18 13:29:12 +08:00
* OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN IN CONN-
* -ECTION WITH THEIR PRODUCTS.
*
2021-09-13 21:04:42 +08:00
* COPYRIGHT 2012 Synwit Technology
2021-02-18 13:29:12 +08:00
*******************************************************************************************************************************************/
#include "SWM320.h"
#include "SWM320_gpio.h"
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : GPIO_Init()
2021-09-13 21:04:42 +08:00
* :
* : 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
2021-05-06 10:42:41 +08:00
* :
* : GPIOAGPIOCGPIOMGPIOP只有上拉GPIOBGPION只有下拉PN0PN1PN2三个引脚有上拉没下拉
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_Init(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t dir, uint32_t pull_up, uint32_t pull_down)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
switch ((uint32_t)GPIOx)
{
case ((uint32_t)GPIOA):
SYS->CLKEN |= (0x01 << SYS_CLKEN_GPIOA_Pos);
PORT_Init(PORTA, n, 0, 1); //PORTA.PINn引脚配置为GPIO功能数字输入开启
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);
PORT_Init(PORTB, n, 0, 1); //PORTB.PINn引脚配置为GPIO功能数字输入开启
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);
PORT_Init(PORTC, n, 0, 1); //PORTC.PINn引脚配置为GPIO功能数字输入开启
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);
PORT_Init(PORTM, n, 0, 1); //PORTM.PINn引脚配置为GPIO功能数字输入开启
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);
PORT_Init(PORTN, n, 0, 1); //PORTN.PINn引脚配置为GPIO功能数字输入开启
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);
PORT_Init(PORTP, n, 0, 1); //PORTP.PINn引脚配置为GPIO功能数字输入开启
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-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : GPIO_SetBit()
2021-09-13 21:04:42 +08:00
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_SetBit(GPIO_TypeDef *GPIOx, uint32_t n)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
GPIOx->DATA |= (0x01 << n);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : GPIO_ClrBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_ClrBit(GPIO_TypeDef *GPIOx, uint32_t n)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
GPIOx->DATA &= ~(0x01 << n);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : GPIO_InvBit()
2021-09-13 21:04:42 +08:00
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_InvBit(GPIO_TypeDef *GPIOx, uint32_t n)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
GPIOx->DATA ^= (0x01 << n);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : GPIO_GetBit()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* : 0 1
2021-05-06 10:42:41 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
uint32_t GPIO_GetBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
return ((GPIOx->DATA >> n) & 0x01);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : GPIO_SetBits()
* : n开始的w位连续引脚的电平置高
2021-09-13 21:04:42 +08:00
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_SetBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
GPIOx->DATA |= (bits << n);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : GPIO_ClrBits()
2021-05-06 10:42:41 +08:00
* : n开始的w位连续引脚的电平置低
2021-09-13 21:04:42 +08:00
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_ClrBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
GPIOx->DATA &= ~(bits << n);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : GPIO_InvBits()
* : n开始的w位连续引脚的电平反转
2021-09-13 21:04:42 +08:00
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
2021-05-06 10:42:41 +08:00
* :
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_InvBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
GPIOx->DATA ^= (bits << n);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : GPIO_GetBits()
2021-05-06 10:42:41 +08:00
* : n开始的w位连续引脚的电平状态
2021-09-13 21:04:42 +08:00
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t w
* : n开始的w位连续引脚的电平状态 0 1
* 0n的电平状态1n+1... ...w位表示引脚n+w的电平状态
2021-05-06 10:42:41 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
uint32_t GPIO_GetBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
return ((GPIOx->DATA >> n) & bits);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : GPIO_AtomicSetBit()
2021-09-13 21:04:42 +08:00
* : --ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
2021-05-06 10:42:41 +08:00
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_AtomicSetBit(GPIO_TypeDef *GPIOx, uint32_t n)
{
*((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000) * 32 + n * 4)) = 1;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : GPIO_AtomicClrBit()
* : --ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
2021-05-06 10:42:41 +08:00
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_AtomicClrBit(GPIO_TypeDef *GPIOx, uint32_t n)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
*((volatile uint32_t *)(0x42000000 + ((uint32_t)&GPIOx->DATA - 0x40000000) * 32 + n * 4)) = 0;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : GPIO_AtomicInvBit()
2021-09-13 21:04:42 +08:00
* : --ISR打断
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
2021-05-06 10:42:41 +08:00
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_AtomicInvBit(GPIO_TypeDef *GPIOx, uint32_t n)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
*((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));
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : GPIO_AtomicSetBits()
* : n开始的w位连续引脚的电平置高--ISR打断
2021-09-13 21:04:42 +08:00
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* uint32_t w
2021-05-06 10:42:41 +08:00
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_AtomicSetBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
__disable_irq();
GPIOx->DATA |= (bits << n);
__enable_irq();
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : GPIO_AtomicClrBits()
2021-05-06 10:42:41 +08:00
* : n开始的w位连续引脚的电平置低--ISR打断
2021-09-13 21:04:42 +08:00
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* uint32_t w
2021-05-06 10:42:41 +08:00
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_AtomicClrBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
__disable_irq();
GPIOx->DATA &= ~(bits << n);
__enable_irq();
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : GPIO_AtomicInvBits()
* : n开始的w位连续引脚的电平反转--ISR打断
2021-09-13 21:04:42 +08:00
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOD
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN14PIN15
* uint32_t w
2021-05-06 10:42:41 +08:00
* :
* : GPIOx的16个引脚中ISR中操作时GPIOx的引脚必须都用GPIO_Atomic类型函数操作
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
2021-05-06 10:42:41 +08:00
void GPIO_AtomicInvBits(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t w)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
uint32_t bits;
bits = 0xFFFFFF >> (24 - w);
__disable_irq();
GPIOx->DATA ^= (bits << n);
__enable_irq();
2021-02-18 13:29:12 +08:00
}