rt-thread-official/bsp/swm320-lq100/Libraries/SWM320_StdPeriph_Driver/SWM320_exti.c

132 lines
7.4 KiB
C
Raw Normal View History

2018-12-24 17:17:27 +08:00
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : SWM320_exti.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_exti.h"
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : EXTI_Init()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* uint32_t mode EXTI_FALL_EDGEEXTI_RISE_EDGEEXTI_BOTH_EDGEEXTI_LOW_LEVELEXTI_HIGH_LEVEL
* :
* : GPIOAGPIOBGPIOCGPIOM的PIN0--7NVIC中的引脚中断GPIOA0_IRQnNVIC的组中断GPIOA_IRQn
* NVIC_EnableIRQ()使NVIC中断NVIC_EnableIRQ(GPIOA0_IRQn)NVIC_EnableIRQ(GPIOA_IRQn)
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
void EXTI_Init(GPIO_TypeDef *GPIOx, uint32_t n, uint32_t mode)
{
2021-09-30 17:55:16 +08:00
EXTI_Close(GPIOx, n); //配置关键寄存器前先关闭
2018-12-24 17:17:27 +08:00
if (mode & 0x10)
{
2021-09-30 17:55:16 +08:00
GPIOx->INTLVLTRG |= (0x01 << n); //电平触发
2018-12-24 17:17:27 +08:00
if (mode & 0x01)
2021-09-30 17:55:16 +08:00
GPIOx->INTRISEEN |= (0x01 << n); //高电平触发
2018-12-24 17:17:27 +08:00
else
2021-09-30 17:55:16 +08:00
GPIOx->INTRISEEN &= ~(0x01 << n); //低电平触发
2018-12-24 17:17:27 +08:00
}
else
{
2021-09-30 17:55:16 +08:00
GPIOx->INTLVLTRG &= ~(0x01 << n); //边沿触发
2018-12-24 17:17:27 +08:00
if (mode & 0x02)
{
2021-09-30 17:55:16 +08:00
GPIOx->INTBE |= (0x01 << n); //双边沿触发
2018-12-24 17:17:27 +08:00
}
else
{
2021-09-30 17:55:16 +08:00
GPIOx->INTBE &= ~(0x01 << n); //单边沿触发
2018-12-24 17:17:27 +08:00
if (mode & 0x01)
2021-09-30 17:55:16 +08:00
GPIOx->INTRISEEN |= (0x01 << n); //上升沿触发
2018-12-24 17:17:27 +08:00
else
2021-09-30 17:55:16 +08:00
GPIOx->INTRISEEN &= ~(0x01 << n); //下降沿触发
2018-12-24 17:17:27 +08:00
}
}
2021-09-30 17:55:16 +08:00
GPIOx->INTCLR = (1 << n); //清除掉因为模式配置可能产生的中断
2018-12-24 17:17:27 +08:00
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : EXTI_Open()
* : 使
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
void EXTI_Open(GPIO_TypeDef *GPIOx, uint32_t n)
{
GPIOx->INTEN |= (0x01 << n);
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : EXTI_Close()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
void EXTI_Close(GPIO_TypeDef *GPIOx, uint32_t n)
{
GPIOx->INTEN &= ~(0x01 << n);
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : EXTI_State()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* : uint32_t 1 0
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
uint32_t EXTI_State(GPIO_TypeDef *GPIOx, uint32_t n)
{
return (GPIOx->INTSTAT >> n) & 0x01;
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : EXTI_RawState()
* : //
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* : uint32_t 1 / 0 /
* :
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
uint32_t EXTI_RawState(GPIO_TypeDef *GPIOx, uint32_t n)
{
2021-09-30 17:55:16 +08:00
return (GPIOx->INTRAWSTAT >> n) & 0x01;
2018-12-24 17:17:27 +08:00
}
/******************************************************************************************************************************************
2021-09-30 17:55:16 +08:00
* : EXTI_Clear()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* :
* : 沿
2018-12-24 17:17:27 +08:00
******************************************************************************************************************************************/
void EXTI_Clear(GPIO_TypeDef *GPIOx, uint32_t n)
{
GPIOx->INTCLR = (0x01 << n);
}