/**
  ******************************************************************************
  * @file    GPIO/JTAG_Remap/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_JTAG_Remap
  * @{
  */

/**
  * @brief  Bit_SET and Bit_RESET enumeration
  */

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

/* Private function prototypes -----------------------------------------------*/
void RCM_Configuration(void);
void Delay(__IO uint32_t nCount);

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

    /* Configure the system clocks */
    RCM_Configuration();

    /* Initialize LEDs and Key Button mounted on APM3210X-MINI board */
    APM_MINI_LEDInit(LED3);
    APM_MINI_LEDInit(LED2);

    APM_MINI_PBInit(BUTTON_KEY1, BUTTON_MODE_GPIO);

    /* Test if Key Button GPIO Pin level is low (Key push-button on MINI Board pressed) */
    if (APM_MINI_PBGetState(BUTTON_KEY1) == 0x00)
    {
        /* Key is pressed */

        /* Turn on LED3 */
        APM_MINI_LEDOn(LED3);

        /* Disable the Serial Wire Jtag Debug Port SWJ-DP */
        GPIO_ConfigPinRemap(GPIO_REMAP_SWJ_Disable);

        /* Configure PA.13 (JTMS/SWDAT), PA.14 (JTCK/SWCLK) and PA.15 (JTDI) as
           output push-pull */
        GPIO_configStruct.pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15;
        GPIO_configStruct.speed = GPIO_SPEED_50MHz;
        GPIO_configStruct.mode = GPIO_MODE_OUT_PP;
        GPIO_Config(GPIOA, &GPIO_configStruct);

        /* Configure PB.03 (JTDO) and PB.04 (JTRST) as output push-pull */
        GPIO_configStruct.pin = GPIO_PIN_3 | GPIO_PIN_4;
        GPIO_Config(GPIOB, &GPIO_configStruct);

        while (1)
        {
            /* Toggle JTMS/SWDAT pin */
            if (GPIO_ReadOutputBit(GPIOA, GPIO_PIN_13))
            {
                GPIO_ResetBits(GPIOA, GPIO_PIN_13);
            }
            else
            {
                GPIO_SetBits(GPIOA, GPIO_PIN_13);
            }

            /* Insert delay */
            Delay(0x5FFFF);

            /* Toggle JTCK/SWCLK pin */
            if (GPIO_ReadOutputBit(GPIOA, GPIO_PIN_14))
            {
                GPIO_ResetBits(GPIOA, GPIO_PIN_14);
            }
            else
            {
                GPIO_SetBits(GPIOA, GPIO_PIN_14);
            }

            /* Insert delay */
            Delay(0x5FFFF);

            /* Toggle JTDI pin */
            if (GPIO_ReadOutputBit(GPIOA, GPIO_PIN_15))
            {
                GPIO_ResetBits(GPIOA, GPIO_PIN_15);
            }
            else
            {
                GPIO_SetBits(GPIOA, GPIO_PIN_15);
            }

            /* Insert delay */
            Delay(0x5FFFF);

            /* Toggle JTDO pin */
            if (GPIO_ReadOutputBit(GPIOB, GPIO_PIN_3))
            {
                GPIO_ResetBits(GPIOB, GPIO_PIN_3);
            }
            else
            {
                GPIO_SetBits(GPIOB, GPIO_PIN_3);
            }

            /* Insert delay */
            Delay(0x5FFFF);

            /* Toggle JTRST pin */
            if (GPIO_ReadOutputBit(GPIOB, GPIO_PIN_4))
            {
                GPIO_ResetBits(GPIOB, GPIO_PIN_4);
            }
            else
            {
                GPIO_SetBits(GPIOB, GPIO_PIN_4);
            }

            /* Insert delay */
            Delay(0x5FFFF);
        }
    }
    else
    {
        /* Turn on LED2 */
        APM_MINI_LEDOn(LED2);

        while (1)
        {
        }
    }
}

/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
void RCM_Configuration(void)
{
    /* Enable GPIOA, GPIOB, RCM_APB2Periph_GPIO_KEY_BUTTON and AFIO clocks */
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA | RCM_APB2_PERIPH_GPIOB |
                              RCM_APB2_PERIPH_AFIO);
}

/**
  * @brief  Inserts a delay time.
  * @param  nCount: specifies the delay time length.
  * @retval None
  */
void Delay(__IO uint32_t nCount)
{
    for (; nCount != 0; nCount--);
}

#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

/**
  * @}
  */

/**
  * @}
  */
