/**
  ******************************************************************************
  * @file    PMU/PVD/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 PMU_PVD
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void EINT_Configuration(void);
void NVIC_Configuration(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
       */

    /* Initialize LEDs and Key Button mounted on APM32-MINI board */
#if defined (APM32_MINI)
    APM_MINI_LEDInit(LED2);
#elif defined(APM32_DISCOVERY)
    APM_DISCOVERY_LEDInit(LED2);
#endif
    /* Enable PMU and BAKR clock */
    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_PMU | RCM_APB1_PERIPH_BAKR);

    /* Configure EINT Line to generate an interrupt on falling edge */
    EINT_Configuration();

    /* NVIC configuration */
    NVIC_Configuration();

    /* Configure the PVD Level to 2.9V */
    PMU_SetPVMLeve(PMU_PVMLevel_2V9);

    /* Enable the PVD Output */
    PMU_EnablePVM();

    while (1)
    {
    }
}

/**
  * @brief  Configures EINT Lines.
  * @param  None
  * @retval None
  */
void EINT_Configuration(void)
{
    EINT_ConfigStruct_T EINT_configStruct;

    /* Configure EINT Line16(PVD Output) to generate an interrupt on rising and
       falling edges */
    EINT_ClearIntFlag(0x10000);
    EINT_configStruct.EINT_Line = 0x10000;
    EINT_configStruct.EINT_Mode = EINT_MODE_Interrupt;
    EINT_configStruct.EINT_Trigger = EINT_Trigger_Rising_Falling;
    EINT_configStruct.EINT_LineCmd = ENABLE;
    EINT_Config(&EINT_configStruct);
}

/**
  * @brief  Configures NVIC and Vector Table base location.
  * @param  None
  * @retval None
  */
void NVIC_Configuration(void)
{
    /* Configure one bit for preemption priority */
    NVIC_ConfigPriorityGroup(NVIC_PRIORITY_GROUP_1);

    /* Enable the PVD Interrupt */
    NVIC_EnableIRQRequest(PVD_IRQn, 0x00, 0x00);
}

#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

/**
  * @}
  */

/**
  * @}
  */

