/**
  ******************************************************************************
  * @file    EINT/EINT_Config/main.c
  * @author  IOT Development Team
  * @version V1.0.0
  * @date    10-January-2020
  * @brief   Main program body
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "apm32f10x.h"
#include "platform_config.h"

/** @addtogroup APM32F10x_StdPeriph_Examples
  * @{
  */

/** @addtogroup EINT_Config
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
EINT_ConfigStruct_T   EINT_configStruct;
GPIO_ConfigStruct_T   GPIO_configStruct;

/* Private function prototypes -----------------------------------------------*/
void EINT0_Config(void);
void EINT1_Config(void);

/* Private functions ---------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
    /*!< At this stage the microcontroller clock setting is already configured,
         this is done through SystemInit() function which is called from startup
         file (startup_apm32f10x_xx.s) before to branch to application main.
         To reconfigure the default setting of SystemInit() function, refer to
         system_apm32f10x.c file
       */
#if defined (APM32_MINI)
    /* Initialize LED3 and Key Button mounted on APM32-MINI board */
    APM_MINI_LEDInit(LED3);
    APM_MINI_LEDInit(LED2);
#elif defined(APM32_DISCOVERY)
    /* Initialize LED3 and Key Button mounted on APM32-DISCOVERY board */
    APM_DISCOVERY_LEDInit(LED3);
    APM_DISCOVERY_LEDInit(LED2);
#endif
    /* Configure PA.00 in interrupt mode */
    EINT0_Config();

    /* Configure PA.01 in interrupt mode */
    EINT1_Config();

    /* Generate software interrupt: simulate a falling edge applied on EINT0 line */
//  EINT_GenerateSWInterrupt(EINT_Line0);

    while (1)
    {
    }
}

/**
  * @brief  Configure PA.00 in interrupt mode
  * @param  None
  * @retval None
  */
void EINT0_Config(void)
{
    /* Enable GPIOA clock */
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);

    /* Configure PA.00 pin as input floating */
    GPIO_configStruct.pin = GPIO_PIN_0;
    GPIO_configStruct.mode = GPIO_MODE_IN_PU;
    GPIO_Config(GPIOA, &GPIO_configStruct);

    /* Enable AFIO clock */
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);

    /* Connect EINT0 Line to PA.00 pin */
    GPIO_ConfigEXTILine(GPIO_PORT_SOURCE_A, GPIO_PIN_SOURCE_0);

    /* Configure EINT0 line */
    EINT_configStruct.EINT_Line = EINT_LINE0;
    EINT_configStruct.EINT_Mode = EINT_MODE_Interrupt;
    EINT_configStruct.EINT_Trigger = EINT_Trigger_Falling;
    EINT_configStruct.EINT_LineCmd = ENABLE;
    EINT_Config(&EINT_configStruct);

    /* Enable and set EINT0 Interrupt to the lowest priority */
    NVIC_EnableIRQRequest(EINT0_IRQn, 0x0f, 0x0f);
}

/**
  * @brief  Configure PA.01 in interrupt mode
  * @param  None
  * @retval None
  */
void EINT1_Config(void)
{
    /* Enable GPIOA clock */
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);

    /* Configure PB.09 pin as input floating */
    GPIO_configStruct.pin = GPIO_PIN_1;
    GPIO_configStruct.mode = GPIO_MODE_IN_PU;
    GPIO_Config(GPIOA, &GPIO_configStruct);

    /* Enable AFIO clock */
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
    /* Connect EINT1 Line to PA.01 pin */
    GPIO_ConfigEXTILine(GPIO_PORT_SOURCE_A, GPIO_PIN_SOURCE_1);

    /* Configure EINT1 line */
    EINT_configStruct.EINT_Line = EINT_LINE1;
    EINT_configStruct.EINT_Mode = EINT_MODE_Interrupt;
    EINT_configStruct.EINT_Trigger = EINT_Trigger_Falling;
    EINT_configStruct.EINT_LineCmd = ENABLE;
    EINT_Config(&EINT_configStruct);

    /* Enable and set EINT1 Interrupt to the lowest priority */
    NVIC_EnableIRQRequest(EINT1_IRQn, 0x0f, 0x0f);
}

#ifdef  USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
       ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    /* Infinite loop */
    while (1)
    {
    }
}

#endif

/**
  * @}
  */

/**
  * @}
  */

