/**
  ******************************************************************************
  * @file    NVIC/IRQ_Mask/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 IRQ_Mask
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void TMR_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
       */

    /* Initialize Leds mounted on APM3210X-EVAL board */
#if defined(APM32_MINI)
    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
#elif defined (APM32_DISCOVERY)
    APM_DISCOVERY_LEDInit(LED2);
    APM_DISCOVERY_LEDInit(LED3);
#elif defined(APM32F103V_EVAL)
    APM_EVAL_LEDInit(LED1);
    APM_EVAL_LEDInit(LED2);
    APM_EVAL_LEDInit(LED3);
    APM_EVAL_LEDInit(LED4);
#endif
    /* Initialize the KEY and WAKEUP buttons mounted on APM3210X-EVAL board */
#if defined(APM32_MINI)
    APM_MINI_PBInit(BUTTON_KEY1, BUTTON_MODE_GPIO);
    APM_MINI_PBInit(BUTTON_KEY2, BUTTON_MODE_GPIO);
#elif defined (APM32_DISCOVERY)
    APM_DISCOVERY_PBInit(BUTTON_KEY1, BUTTON_MODE_GPIO);
    APM_DISCOVERY_PBInit(BUTTON_KEY2, BUTTON_MODE_GPIO);
#elif defined(APM32F103V_EVAL)
    APM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_GPIO,GPIO_Mode_IN_FLOATING);
    APM_EVAL_PBInit(BUTTON_WAKEUP, BUTTON_MODE_EINT,GPIO_Mode_IN_FLOATING);
#endif
    /* TMR configuration -------------------------------------------------------*/
    TMR_Config();

    while (1)
    {
        /* Wait until KEY button is pressed. */
#if defined(APM32_MINI)
        while (APM_MINI_PBGetState(BUTTON_KEY1) == RESET)
        {
        }

        while (APM_MINI_PBGetState(BUTTON_KEY1) != RESET)
        {
        }

#elif defined (APM32_DISCOVERY)

        while (APM_DISCOVERY_PBGetState(BUTTON_KEY1) == RESET)
        {
        }

        while (APM_DISCOVERY_PBGetState(BUTTON_KEY1) != RESET)
        {
        }

#elif defined(APM32F103V_EVAL)

        while (APM_EVAL_PBGetState(BUTTON_KEY) == RESET)
        {
        }

        while (APM_EVAL_PBGetState(BUTTON_KEY) != RESET)
        {
        }

#endif
        /* This instruction raises the execution priority to 0. This prevents all
           exceptions with configurable priority from activating, other than through
           the HardFault fault escalation mechanism. */
        __disable_irq();

        /* Turn LED4 ON */
#if defined(APM32_MINI)
        APM_MINI_LEDOn(LED2);
#elif defined (APM32_DISCOVERY)
        APM_DISCOVERY_LEDOn(LED2);
#elif defined(APM32F103V_EVAL)
        APM_EVAL_LEDOn(LED4);
#endif
        /* Wait until KEY button is pressed. */
#if defined(APM32_MINI)

        while (APM_MINI_PBGetState(BUTTON_KEY1) == RESET)
        {
        }

        while (APM_MINI_PBGetState(BUTTON_KEY1) != RESET)
        {
        }

#elif defined (APM32_DISCOVERY)

        while (APM_DISCOVERY_PBGetState(BUTTON_KEY1) == RESET)
        {
        }

        while (APM_DISCOVERY_PBGetState(BUTTON_KEY1) != RESET)
        {
        }

#elif defined(APM32F103V_EVAL)

        while (APM_EVAL_PBGetState(BUTTON_KEY) == RESET)
        {
        }

        while (APM_EVAL_PBGetState(BUTTON_KEY) != RESET)
        {
        }

#endif

        /* This instruction will allow all exceptions with configurable priority to
           be activated. */
        __enable_irq();

        /* Turn LED4 OFF */
#if defined(APM32_MINI)
        APM_MINI_LEDOff(LED2);
#elif defined (APM32_DISCOVERY)
        APM_DISCOVERY_LEDOff(LED2);
#elif defined(APM32F103V_EVAL)
        APM_EVAL_LEDOff(LED4);
#endif
    }
}

/**
  * @brief  Configures the used Timers.
  * @param  None
  * @retval None
  */
void TMR_Config(void)
{
    TMR_BASE_CONFIG_T  TMR_baseConfigStruct;
    TMR_OC_CONFIG_T    TMR_OCcongigStruct;

    /* Enable TMR2, TMR3 and TMR4 clocks */
    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR2 | RCM_APB1_PERIPH_TMR3 |
                              RCM_APB1_PERIPH_TMR4);

    /* TMR2 configuration */
    TMR_baseConfigStruct.period = 0x4AF;
    TMR_baseConfigStruct.division = ((SystemCoreClock/1200) - 1);
    TMR_baseConfigStruct.clockDivision = TMR_CKDR1;
    TMR_baseConfigStruct.countMode = TMR_COUNT_MODE_UP;
    TMR_TimeBaseConfig(TMR2, &TMR_baseConfigStruct);
    TMR_OCStructInit(&TMR_OCcongigStruct);

    /* Output Compare Timing Mode configuration: Channel1 */
    TMR_OCcongigStruct.OC_Mode = TMR_OC_MODE_TMRING;
    TMR_OCcongigStruct.Pulse = 0x0;
    TMR_OC1Config(TMR2, &TMR_OCcongigStruct);

    /* TMR3 configuration */
    TMR_baseConfigStruct.period = 0x95F;
    TMR_TimeBaseConfig(TMR3, &TMR_baseConfigStruct);

    /* Output Compare Timing Mode configuration: Channel1 */
    TMR_OC1Config(TMR3, &TMR_OCcongigStruct);

    /* TMR4 configuration */
    TMR_baseConfigStruct.period = 0xE0F;
    TMR_TimeBaseConfig(TMR4, &TMR_baseConfigStruct);

    /* Output Compare Timing Mode configuration: Channel1 */
    TMR_OC1Config(TMR4, &TMR_OCcongigStruct);

    /* Immediate load of TMR2,TMR3 and TMR4 Precaler values */
    TMR_PrescalerConfig(TMR2, ((SystemCoreClock/1200) - 1), TMR_PSCRELOAD_MODE_IMMEDIATE);
    TMR_PrescalerConfig(TMR3, ((SystemCoreClock/1200) - 1), TMR_PSCRELOAD_MODE_IMMEDIATE);
    TMR_PrescalerConfig(TMR4, ((SystemCoreClock/1200) - 1), TMR_PSCRELOAD_MODE_IMMEDIATE);

    /* Clear TMR2, TMR3 and TMR4 update pending flags */
    TMR_ClearFlag(TMR2, TMR_FLAG_UDIF);
    TMR_ClearFlag(TMR3, TMR_FLAG_UDIF);
    TMR_ClearFlag(TMR4, TMR_FLAG_UDIF);

    /* Configure two bits for preemption priority */
    NVIC_ConfigPriorityGroup(NVIC_PRIORITY_GROUP_2);

    /* Enable the TMR2 Interrupt */
    NVIC_EnableIRQRequest(TMR2_IRQn, 0, 0);

    /* Enable the TMR3 Interrupt */
    NVIC_EnableIRQRequest(TMR3_IRQn, 1, 0);

    /* Enable the TMR4 Interrupt */
    NVIC_EnableIRQRequest(TMR4_IRQn, 2, 0);

    /* Enable TMR2, TMR3 and TMR4 Update interrupts */
    TMR_EnableINT(TMR2, TMR_TI_UPDATE);
    TMR_EnableINT(TMR3, TMR_TI_UPDATE);
    TMR_EnableINT(TMR4, TMR_TI_UPDATE);

    /* TMR2, TMR3 and TMR4 enable counters */
    TMR_Enable(TMR2);
    TMR_Enable(TMR3);
    TMR_Enable(TMR4);
}


#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

/**
  * @}
  */

/**
  * @}
  */

