/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2026-02-25
 *
 * @attention
 *
 *  Copyright (C) 2026 Geehy Semiconductor
 *
 *  You may not use this file except in compliance with the
 *  GEEHY COPYRIGHT NOTICE (Geehy Semiconductor Software License Agreement).
 *
 *  The program is only for reference, which is distributed in the hope
 *  that it will be useful and instructional for customers to develop
 *  their software. Unless required by applicable law or agreed to in
 *  writing, the program is distributed in an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the Geehy Semiconductor Software License Agreement for the governing permissions
 *  and limitations under the license.
 */

/* Includes */
#include "Board.h"
#include "stdio.h"
#include "inttypes.h"
#include "g32a10xx_gpio.h"
#include "g32a10xx_tmr.h"
#include "g32a10xx_misc.h"
#include "user_tmr_config.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_EdgeCounter
  @{
*/

/** @defgroup TMR_EdgeCounter_Macros Macros
  @{
*/

/* printf function configs to USART1*/
#define DEBUG_USART  USART1

/**@} end of group TMR_EdgeCounter_Macros */

/** @defgroup TMR_EdgeCounter_Variables Variables
  @{
*/
uint32_t g_ic3Value = 0;
uint32_t g_ic4Value = 0;

/**@} end of group TMR_EdgeCounter_Variables */

/** @defgroup TMR_EdgeCounter_Functions Functions
  @{
  */

/* TMR1 Input capture Init */
void Tmr1_InputCaptureInit(void);
/* Tmr_Delay */
void Tmr_Delay(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    uint32_t g_ic3Value1 = 0;
    uint32_t g_ic3Value2 = 0;
    uint32_t g_ic4Value1 = 0;
    uint32_t g_ic4Value2 = 0;
    
    Board_LedInit(LED2);
    Board_LedInit(LED3);
    Board_ComInit(COM1);

    Tmr1_InputCaptureInit();

    /* Turn LED2 on */
    Board_LedOn(LED2);
    /* Turn LED3 On */
    Board_LedOn(LED3);
    
    g_ic3Value = 0;
    g_ic4Value = 0;
    
    /* Using the line to connect the GPIOC0(KEY1) with GPIOC15 on the dev board */
    printf("Press the KEY1 3 times(Give PC15 3 falling edges):\r\n");
    Tmr_Delay();
    printf("Expected Channel_3 edge count is 3\r\n");
    g_ic3Value1 = g_ic3Value;
    printf("First delay over, Actural Channel_3 edge count:%d\r\n\r\n", g_ic3Value1);
    
    g_ic3Value = 0;
    Tmr_DisableInterrupt(TMR1, (uint16_t)TMR_INT_CH3);
    printf("Press the KEY1 3 times(Give PA10 3 falling edges) again:\r\n");
    Tmr_Delay();
    printf("Expected Channel_3 edge count is 0\r\n");
    g_ic3Value2 = g_ic3Value;
    printf("Second delay over, Actual Channel_3 edge count:%d\r\n", g_ic3Value2);
    
    if(((uint32_t)3 == g_ic3Value1) && ((uint32_t)0 == g_ic3Value2))
    {
        printf("Step 1 SUCCESSED\r\n\r\n");
    }
    else
    {
        printf("Step 1 FAILED\r\n\r\n");
    }
    
    /* Using the line to connect the GPIOC1(KEY2) with GPIOA11 on the dev board */
    printf("Press the KEY2 3 times(Give PA11 3 rising edges and 3 failing edges):\r\n");
    Tmr_Delay();
    g_ic4Value1 = g_ic4Value;
    printf("Expected Channel_4 edge count is 6\r\n");
    printf("First delay over, Actual Channel_4 edge count:%d\r\n\r\n", g_ic4Value1);

    g_ic4Value = 0;
    Tmr_DisableInterrupt(TMR1, (uint16_t)TMR_INT_CH4);
    printf("Press the KEY2 3 times(Give PA11 3 rising edges and 3 failing edges) again:\r\n");
    Tmr_Delay();
    g_ic4Value2 = g_ic4Value;
    printf("Expected Channel_4 edge count is 0\r\n");
    printf("Second delay over, Atual Channel_4 edge count:%d\r\n", g_ic4Value2);

    if(((uint32_t)6 == g_ic4Value1) && ((uint32_t)0 == g_ic4Value2))
    {
        printf("Step 2 SUCCESSED\r\n\r\n");
    }
    else
    {
        printf("Step 2 FAILED\r\n\r\n");
    }
    
    if (((uint32_t)3 == g_ic3Value1) && ((uint32_t)0 == g_ic3Value2) &&
        ((uint32_t)6 == g_ic4Value1) && ((uint32_t)0 == g_ic4Value2))
    {
        printf("demo is OK!\r\n");
    }
    else
    {
        printf("demo is ERROR!\r\n");
    }
    
    for (;;)
    {

    }
}

/*!
 * @brief       TMR1 Input_Capture Init
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Tmr1_InputCaptureInit(void)
{
    Gpio_ConfigType   gpioconfig;
    /* Enable Clock*/
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOA);
    /* Enable Clock*/
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOC);
    
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_TMR1);

    /*  Connect TMR1 to CH3 */
    Gpio_ConfigPinAF(TMR1_CH3_GPIO_PORT, TMR1_CH3_PIN_SOURCE, TMR1_CH3_GPIO_AF);
    gpioconfig = g_tmrGpioConfig;
    gpioconfig.pin = (uint16_t)TMR1_CH3_PIN;
    Gpio_Config(TMR1_CH3_GPIO_PORT, &gpioconfig);
    
    /*  Connect TMR1 to CH4 */
    Gpio_ConfigPinAF(TMR1_CH4_GPIO_PORT, TMR1_CH4_PIN_SOURCE, TMR1_CH4_GPIO_AF);
    gpioconfig.pin = (uint16_t)TMR1_CH4_PIN;
    Gpio_Config(TMR1_CH4_GPIO_PORT, &gpioconfig);

    /* config TMR1 */
    Tmr_ConfigTimeBase(TMR1, &g_tmrBaseConfig);

    /* config channel 3 */
    Tmr_ICConfig(TMR1, &g_tmrIc3Config);
    
    /* config channel 4 */
    Tmr_ICConfig(TMR1, &g_tmrIc4Config);
    
    /* Enable TMR1 Interrupt */
    Nvic_EnableIrqRequest(TMR1_CC_IRQn, 0);
    Tmr_EnableInterrupt(TMR1, (uint16_t)TMR_INT_CH3);
    Tmr_EnableInterrupt(TMR1, (uint16_t)TMR_INT_CH4);

    /*  Enable TMR1  */
    Tmr_Enable(TMR1);
}


/*!
 * @brief       Tmr_Delay
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Tmr_Delay(void)
{
    volatile uint32_t delay = 0xffffff;

    while ((uint32_t)0u != delay)
    {
        delay = delay - (uint32_t)1u;
    };
}

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
    /* send a byte of data to the serial port */
    Usart_TxData(DEBUG_USART, (uint8_t)ch);

    /* wait for the data to be send  */
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == (uint8_t)RESET)
    {
        /* nothing */
    }

    return (ch);
}

#elif defined (__GNUC__)

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @retval      The characters that need to be send.
*
* @note
*/
int __io_putchar(int ch)
{
    /* send a byte of data to the serial port */
    Usart_TxData(DEBUG_USART, ch);

    /* wait for the data to be send  */
    while (Usart_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == (uint8_t)RESET)
    {
        /* nothing */
    }

    return ch;
}

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       file:  Meaningless in this function.
*
* @param       *ptr:  Buffer pointer for data to be sent.
*
* @param       len:  Length of data to be sent.
*
* @retval      The characters that need to be send.
*
* @note
*/
int _write(int file, char* ptr, int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        __io_putchar(*ptr++);
    }

    return len;
}

#else
#pragma message("Not supported compiler type")
#endif

/**@} end of group TMR_EdgeCounter_Functions */
/**@} end of group TMR_EdgeCounter */
/**@} end of group TMR_Examples */
/**@} end of group Examples */
