rt-thread/bsp/synwit/swm320/libraries/SWM320_StdPeriph_Driver/SWM320_exti.c

132 lines
7.5 KiB
C
Raw Normal View History

2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : SWM320_exti.c
* : SWM320单片机的外部中断功能驱动库
2021-05-06 10:42:41 +08:00
* : http://www.synwit.com.cn/e/tool/gbook/?bid=1
* :
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_exti.h"
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
2021-05-06 10:42:41 +08:00
* : EXTI_Init()
2021-09-13 21:04:42 +08:00
* :
* : 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
2021-05-06 10:42:41 +08:00
* :
* : GPIOAGPIOBGPIOCGPIOM的PIN0--7NVIC中的引脚中断GPIOA0_IRQnNVIC的组中断GPIOA_IRQn
2021-09-13 21:04:42 +08:00
* NVIC_EnableIRQ()使NVIC中断NVIC_EnableIRQ(GPIOA0_IRQn)NVIC_EnableIRQ(GPIOA_IRQn)
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
void EXTI_Init(GPIO_TypeDef * GPIOx, uint32_t n, uint32_t mode)
2021-02-18 13:29:12 +08:00
{
EXTI_Close(GPIOx, n); //配置关键寄存器前先关闭
2021-05-06 10:42:41 +08:00
if(mode & 0x10)
2021-05-06 10:42:41 +08:00
{
GPIOx->INTLVLTRG |= (0x01 << n); //电平触发
2021-05-06 10:42:41 +08:00
if(mode & 0x01)
GPIOx->INTRISEEN |= (0x01 << n); //高电平触发
2021-05-06 10:42:41 +08:00
else
GPIOx->INTRISEEN &= ~(0x01 << n); //低电平触发
2021-05-06 10:42:41 +08:00
}
else
{
GPIOx->INTLVLTRG &= ~(0x01 << n); //边沿触发
2021-05-06 10:42:41 +08:00
if(mode & 0x02)
2021-05-06 10:42:41 +08:00
{
GPIOx->INTBE |= (0x01 << n); //双边沿触发
2021-05-06 10:42:41 +08:00
}
else
{
GPIOx->INTBE &= ~(0x01 << n); //单边沿触发
2021-05-06 10:42:41 +08:00
if(mode & 0x01)
GPIOx->INTRISEEN |= (0x01 << n); //上升沿触发
2021-05-06 10:42:41 +08:00
else
GPIOx->INTRISEEN &= ~(0x01 << n); //下降沿触发
2021-05-06 10:42:41 +08:00
}
}
GPIOx->INTCLR = (1 << n); //清除掉因为模式配置可能产生的中断
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : EXTI_Open()
* : 使
* : 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
******************************************************************************************************************************************/
void EXTI_Open(GPIO_TypeDef * GPIOx, uint32_t n)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
GPIOx->INTEN |= (0x01 << n);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : EXTI_Close()
* :
* : 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
******************************************************************************************************************************************/
void EXTI_Close(GPIO_TypeDef * GPIOx, uint32_t n)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
GPIOx->INTEN &= ~(0x01 << n);
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : EXTI_State()
* :
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* : uint32_t 1 0
2021-05-06 10:42:41 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t EXTI_State(GPIO_TypeDef * GPIOx, uint32_t n)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (GPIOx->INTSTAT >> n) & 0x01;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : EXTI_RawState()
* : //
* : GPIO_TypeDef * GPIOx GPIO端口GPIOAGPIOBGPIOCGPIOMGPIONGPIOP
* uint32_t n GPIO引脚PIN0PIN1PIN2... ... PIN22PIN23
* : uint32_t 1 / 0 /
2021-05-06 10:42:41 +08:00
* :
2021-02-18 13:29:12 +08:00
******************************************************************************************************************************************/
uint32_t EXTI_RawState(GPIO_TypeDef * GPIOx, uint32_t n)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
return (GPIOx->INTRAWSTAT >> n) & 0x01;
2021-02-18 13:29:12 +08:00
}
2021-09-13 21:04:42 +08:00
/******************************************************************************************************************************************
* : EXTI_Clear()
* :
* : 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
******************************************************************************************************************************************/
void EXTI_Clear(GPIO_TypeDef * GPIOx, uint32_t n)
2021-02-18 13:29:12 +08:00
{
2021-05-06 10:42:41 +08:00
GPIOx->INTCLR = (0x01 << n);
2021-02-18 13:29:12 +08:00
}