/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.1
 *
 * @date        2025-04-31
 *
 * @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 on 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 "Port.h"
#include "CDD_Usart.h"
#include "Mcu.h"
#include "Icu.h"
#include "TestCommon.h"

/** @addtogroup Examples
  @{
  */

/**
* @brief  Icu Tmr Edge counter.
*/
static void Icu_Tmr_EdgeCounter(void)
{
    char *l_p_name_test_case = "Icu_Tmr_EdgeCounter Example";
    uint16_t l_channel0FirstCnt = 0;
    uint16_t l_channel0SecondCnt = 0;
    uint16_t l_channel1FirstCnt = 0;
    uint16_t l_channel1SecondCnt = 0;

    print_test_start((uint8_t*)l_p_name_test_case);
    Icu_Init(&g_IcuConfig);

    printf("Using the line to connect the GPIOC0(KEY1) with GPIOA11 on the dev board\r\n");
    printf("Using the line to connect the GPIOC1(KEY2) with GPIOA3 on the dev board\r\n\r\n");

    print_test_step(TEST_STEP_1);
    Icu_EnableEdgeCount(IcuChannel_0);

    NVIC_SetPriority(TMR1_CC_IRQn, 0x0f);
    NVIC_EnableIRQ(TMR1_CC_IRQn);

    /* Using the line to connect the GPIOC0(KEY1) with GPIOA11 on the dev board */
    printf("Press the KEY1 3 times(Give GPIOA11 3 falling edges):\r\n");
    sys_delay(20000000);
    printf("Expected IcuChannel_0 edge count is 3\r\n");
    l_channel0FirstCnt = Icu_GetEdgeNumbers(IcuChannel_0);
    printf("First delay over, Actural IcuChannel_0 edge count:%d\r\n\r\n", l_channel0FirstCnt);
    
    Icu_DisableEdgeCount(IcuChannel_0);
    Icu_ResetEdgeCount(IcuChannel_0);
    printf("Press the KEY1 3 times(Give GPIOA11 3 falling edges) again:\r\n");
    sys_delay(20000000);
    l_channel0SecondCnt = Icu_GetEdgeNumbers(IcuChannel_0);
    printf("Expected IcuChannel_0 edge count is 0\r\n");
    printf("Second delay over, Actual IcuChannel_ edge count:%d\r\n", l_channel0SecondCnt);

    if((3 == l_channel0FirstCnt) && (0 == l_channel0SecondCnt))
    {
        printf("Step 1 SUCCESSED\r\n");
    }
    else
    {
        printf("Step 1 FAILED\r\n");
    }

    print_test_step(TEST_STEP_2);
    Icu_EnableEdgeCount(IcuChannel_1);

    NVIC_SetPriority(TMR2_IRQn, 0x0f);
    NVIC_EnableIRQ(TMR2_IRQn);

    /* Using the line to connect the GPIOC1(KEY2) with GPIOA3 on the dev board */
    printf("Press the KEY2 3 times(Give GPIOA3 3 rising edges and 3 failing edges):\r\n");
    sys_delay(20000000);
    l_channel1FirstCnt = Icu_GetEdgeNumbers(IcuChannel_1);
    printf("Expected IcuChannel_1 edge count is 6\r\n");
    printf("First delay over, Actual IcuChannel_1 edge count:%d\r\n\r\n", l_channel1FirstCnt);

    Icu_DisableEdgeCount(IcuChannel_1);
    Icu_ResetEdgeCount(IcuChannel_1);
    printf("Press the KEY2 3 times(Give GPIOA3 3 rising edges and 3 failing edges) again:\r\n");
    sys_delay(20000000);
    l_channel1SecondCnt = Icu_GetEdgeNumbers(IcuChannel_1);
    printf("Expected IcuChannel_1 edge count is 0\r\n");
    printf("Second delay over, Atual IcuChannel_1 edge count:%d\r\n", l_channel1SecondCnt);

    if((6 == l_channel1FirstCnt) && (0 == l_channel1SecondCnt))
    {
        printf("Step 2 SUCCESSED\r\n");
    }
    else
    {
        printf("Step 2 FAILED\r\n");
    }

    print_test_end((uint8_t*)l_p_name_test_case);

    Icu_DeInit();
}

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    Mcu_Init(&g_mcuConfig);
    Mcu_InitClock(McuClockSettingConfig_0);

    Port_Init(&g_portConfig);
    Usart_Init(g_usartConfg); //TX PIN:PA9

    Icu_Tmr_EdgeCounter();

    while(1)
    {
     
    }
}

#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)
{
    int reVal = 0;
    reVal = Usart_PutRedefinition(USART1_ID,&ch);
    
    return reVal;
}

/* If configuring the full library using versions after IAR 9.30,
   it is necessary to implement the _write function for printf redirection.*/
size_t __write(int handle,const unsigned char *buf,size_t bufSize)
{
    int32 tempCh = 0;
    for(int i=0; i<bufSize; i++)
    {
        Usart_PutRedefinition(USART1_ID,&tempCh);
    }
    return bufSize;
}

#elif defined (__GNUC__)

/* For Arm Cross GCC: Eclipse */
int _write(int fd,const unsigned char *buf,int len)
{
    int32 tempCh = 0;
    for(int i=0; i<len; i++)
    {
        int32 tempCh = (int32)buf[i];
        Usart_PutRedefinition(USART1_ID,&tempCh);
    }
    return len;
}

#endif
/**@} end of group Template_Functions */
/**@} end of group Template */
/**@} end of group Examples */
