/**
  ******************************************************************************
  * @file    GPIO/IOToggle/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 GPIO_IOToggle
  * @{
  */

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

/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

static void DELAY_TIME (uint32_t delay)
{
    volatile int32_t count;

    count = delay;

    while (--count)
    { }
}

/**
  * @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
       */

    /* GPIOD Periph clock enable */
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOE);

    /* Configure PE5 and PE6 in output pushpull mode */
    GPIO_configStruct.pin = GPIO_PIN_6 | GPIO_PIN_5;
    GPIO_configStruct.speed = GPIO_SPEED_50MHz;
    GPIO_configStruct.mode = GPIO_MODE_OUT_PP;
    GPIO_Config(GPIOE, &GPIO_configStruct);


    /* To achieve GPIO toggling maximum frequency, the following  sequence is mandatory.
       You can monitor PE5 or PE6 on the scope to measure the output signal.
       If you need to fine tune this frequency, you can add more GPIO set/reset
       cycles to minimize more the infinite loop timing.
       This code needs to be compiled with high speed optimization option.  */
    while (1)
    {
#if 0
        /*  generate a square wave  */
        /* Set PE5 and PE6 */
        GPIOE->BSC = 0x00000060;
        DELAY_TIME(10);
        /* Reset PE5 and PE6 */
        GPIOE->BC  = 0x00000060;
        DELAY_TIME(10);
#else
        /* Set PE5 and PE6 */
        GPIOE->BSC = 0x00000060;
        DELAY_TIME(5000000);
        /* Reset PE5 and PE6 */
        GPIOE->BC  = 0x00000060;
        DELAY_TIME(5000000);
        /* Set PE5 and PE6 */
        GPIOE->BSC = 0x00000060;
        DELAY_TIME(5000000);
        /* Reset PE5 and PE6 */
        GPIOE->BC  = 0x00000060;
        DELAY_TIME(5000000);
        /* Set PE5 and PE6 */
        GPIOE->BSC = 0x00000060;
        DELAY_TIME(5000000);
        /* Reset PE5 and PE6 */
        GPIOE->BC  = 0x00000060;
        DELAY_TIME(5000000);
        /* Set PE5 and PE6 */
        GPIOE->BSC = 0x00000060;
        DELAY_TIME(5000000);
        /* Reset PE5 and PE6 */
        GPIOE->BC  = 0x00000060;
        DELAY_TIME(5000000);
        /* Set PE5 and PE6 */
        GPIOE->BSC = 0x00000060;
        DELAY_TIME(5000000);
        /* Reset PE5 and PE6 */
        GPIOE->BC  = 0x00000060;
        DELAY_TIME(5000000);
        /* Set PE5 and PE6 */
        GPIOE->BSC = 0x00000060;
        DELAY_TIME(5000000);
        /* Reset PE5 and PE6 */
        GPIOE->BC  = 0x00000060;
        DELAY_TIME(5000000);
        /* Set PE5 and PE6 */
        GPIOE->BSC = 0x00000060;
        DELAY_TIME(5000000);
        /* Reset PE5 and PE6 */
        GPIOE->BC  = 0x00000060;
        DELAY_TIME(5000000);
        /* Set PE5 and PE6 */
        GPIOE->BSC = 0x00000060;
        DELAY_TIME(5000000);
        /* Reset PE5 and PE6 */
        GPIOE->BC  = 0x00000060;
        DELAY_TIME(5000000);
        /* Set PE5 and PE6 */
        GPIOE->BSC = 0x00000060;
        DELAY_TIME(5000000);
        /* Reset PE5 and PE6 */
        GPIOE->BC  = 0x00000060;
        DELAY_TIME(5000000);
#endif
    }
}

#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

/**
  * @}
  */

/**
  * @}
  */

