/*!
 * @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 SOFTWARE PACKAGE LICENSE).
 *
 *  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 SOFTWARE PACKAGE LICENSE for the governing permissions
 *  and limitations under the License.
 */

/* Includes */
#include "user_config.h"
#include "Board.h"
#include "stdio.h"
#include "inttypes.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup RCM_Examples
  @{
  */

/** @addtogroup RCM_ClockDetection
  @{
  */

/** @defgroup RCM_ClockDetection_Macros Macros
  @{
*/

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

/**@} end of group RCM_ClockDetection_Macros */

/** @defgroup RCM_ClockDetection_Functions Functions
  @{
  */
static void Rcm_delay(uint32_t count)
{
    volatile uint32_t delay = count;

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

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 */
int main(void)
{
    uint32_t timeout = 0xFFFF;
    uint16_t tempVal1 = 0u;
    uint16_t tempVal2 = 0u;
    uint16_t measVal = 0u;
    
    Board_LedInit(LED2);
    Rcm_Delay();
    Board_LedOff(LED2);
    /* Open HSE */
    Rcm_ConfigHSE(RCM_HSE_OPEN);
    
    while ((Rcm_WaitHseReady() == (uint8_t)ERROR) && (timeout > 0U))
    {
        timeout = timeout - 1U;
    }
    if(Rcm_WaitHseReady() == (uint8_t)ERROR)
    {
        printf("\r\n HSE config timeout\r\n");
    }
    else
    {
        /* select HSE as SYSCLK */
        Rcm_ConfigSysClk(RCM_SYSCLK_SEL_HSE);
    }
    Board_ComInit(COM1);
    
    Rcm_CmuEnable(DISABLE);
    Rcm_CmuConfigReferenceClock(CMU_CLK_DIV_16);
    Rcm_CmuMeasureClockSelect(CMU_MEASURE_PLL);
    Rcm_LOCResetEnable(DISABLE);
    Rcm_ReferenceClockCounts(0x13);
    Rcm_LocInterruptEnable(ENABLE);
    Rcm_CmuEnable(ENABLE);
    
    Rcm_delay(1000);
    tempVal1 = Rcm_CmuGetMeasValue();
    if ((tempVal1 > 0x510u) || (tempVal1 < 0x4E0u))
    {
        printf("LOC error\r\n");
    }
    else
    {
        tempVal2 = Rcm_CmuGetMeasValue();
        if ((tempVal2 > 0x510u) || (tempVal2 < 0x4E0u))
        {
            printf("LOC error\r\n");;
        }
        else
        {
            if (tempVal1 == tempVal2)
            {
                measVal = tempVal2;
            }
            else
            {
                measVal = 0u;
            }
            
            if (0x0u == measVal)
            {
                Board_LedOff(LED2);
                printf("LOC error\r\n");
            }
            else
            {
                Board_LedOn(LED2);
                printf("The total number of complete clock pulses is:%d\r\n",measVal);
            }
        }
    }
    
    for (;;)
    {}
}


#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) == RESET);

    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 RCM_ClockDetection_Functions */
/**@} end of group RCM_ClockDetection */
/**@} end of group RCM_Examples */
/**@} end of group Examples */
