Files
ArduinoCore-AT32F4/cores/arduino/libcore/exti.c

236 lines
5.3 KiB
C
Raw Normal View History

2022-07-10 21:37:33 +08:00
/*
* MIT License
* Copyright (c) 2017 - 2022 _VIFEXTech
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "exti.h"
#include "gpio.h"
#define EXTI_GetPortSourceGPIOx(Pin) GPIO_GetPortNum(Pin)
#define EXTI_GetPinSourcex(Pin) GPIO_GetPinNum(Pin)
static EXTI_CallbackFunction_t EXTI_Function[16] = {0};
/**
* @brief
* @param Pin:
* @retval
*/
static IRQn_Type EXTI_GetIRQn(uint8_t Pin)
{
IRQn_Type EXINTx_IRQn = EXINT0_IRQn;
uint8_t Pinx = GPIO_GetPinNum(Pin);
if(Pinx <= 4)
{
switch(Pinx)
{
case 0:
EXINTx_IRQn = EXINT0_IRQn;
break;
case 1:
EXINTx_IRQn = EXINT1_IRQn;
break;
case 2:
EXINTx_IRQn = EXINT2_IRQn;
break;
case 3:
EXINTx_IRQn = EXINT3_IRQn;
break;
case 4:
EXINTx_IRQn = EXINT4_IRQn;
break;
}
}
else if(Pinx >= 5 && Pinx <= 9)
{
EXINTx_IRQn = EXINT9_5_IRQn;
}
else if(Pinx >= 10 && Pinx <= 15)
{
EXINTx_IRQn = EXINT15_10_IRQn;
}
return EXINTx_IRQn;
}
/**
* @brief
* @param Pin:
* @param Function:
* @param Trigger_Mode:
* @param PreemptionPriority:
* @param SubPriority:
* @retval
*/
void EXTIx_Init(
uint8_t Pin,
EXTI_CallbackFunction_t Function,
exint_polarity_config_type line_polarity,
uint8_t PreemptionPriority,
uint8_t SubPriority
)
{
exint_init_type exint_init_struct;
uint8_t Pinx;
if(!IS_PIN(Pin))
return;
Pinx = GPIO_GetPinNum(Pin);
if(Pinx > 15)
return;
EXTI_Function[Pinx] = Function;
crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
gpio_exint_line_config(GPIO_GetPortNum(Pin), (gpio_pins_source_type)Pinx);
exint_default_para_init(&exint_init_struct);
exint_init_struct.line_select = 1 << Pinx;
exint_init_struct.line_mode = EXINT_LINE_INTERRUPUT;
exint_init_struct.line_polarity = line_polarity;
exint_init_struct.line_enable = TRUE;
exint_init(&exint_init_struct);
nvic_irq_enable(EXTI_GetIRQn(Pin), PreemptionPriority, SubPriority);
}
/**
* @brief (Arduino)
* @param Pin:
* @param function:
* @param Trigger_Mode:
* @retval
*/
void attachInterrupt(uint8_t Pin, EXTI_CallbackFunction_t Function, exint_polarity_config_type line_polarity)
{
EXTIx_Init(
Pin,
Function,
line_polarity,
EXTI_PREEMPTIONPRIORITY_DEFAULT,
EXTI_SUBPRIORITY_DEFAULT
);
}
/**
* @brief (Arduino)
* @param Pin:
* @retval
*/
void detachInterrupt(uint8_t Pin)
{
if(!IS_PIN(Pin))
return;
nvic_irq_disable(EXTI_GetIRQn(Pin));
}
#define EXTIx_IRQHANDLER(n) \
do{\
if(exint_flag_get(EXINT_LINE_##n) != RESET)\
{\
if(EXTI_Function[n]) EXTI_Function[n]();\
exint_flag_clear(EXINT_LINE_##n);\
}\
}while(0)
/**
* @brief 0
* @param
* @retval
*/
void EXTI0_IRQHandler(void)
{
EXTIx_IRQHANDLER(0);
}
/**
* @brief 1
* @param
* @retval
*/
void EXTI1_IRQHandler(void)
{
EXTIx_IRQHANDLER(1);
}
/**
* @brief 2
* @param
* @retval
*/
void EXTI2_IRQHandler(void)
{
EXTIx_IRQHANDLER(2);
}
/**
* @brief 3
* @param
* @retval
*/
void EXTI3_IRQHandler(void)
{
EXTIx_IRQHANDLER(3);
}
/**
* @brief 4
* @param
* @retval
*/
void EXTI4_IRQHandler(void)
{
EXTIx_IRQHANDLER(4);
}
/**
* @brief 9~5
* @param
* @retval
*/
void EXTI9_5_IRQHandler(void)
{
EXTIx_IRQHANDLER(5);
EXTIx_IRQHANDLER(6);
EXTIx_IRQHANDLER(7);
EXTIx_IRQHANDLER(8);
EXTIx_IRQHANDLER(9);
}
/**
* @brief 15~10
* @param
* @retval
*/
void EXTI15_10_IRQHandler(void)
{
EXTIx_IRQHANDLER(10);
EXTIx_IRQHANDLER(11);
EXTIx_IRQHANDLER(12);
EXTIx_IRQHANDLER(13);
EXTIx_IRQHANDLER(14);
EXTIx_IRQHANDLER(15);
}