/*!
 * @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 on an "AS IS" BASIS, WITHOUT
 *  ANY WARRANTIES 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 "user_tmr_config.h"
#include "inttypes.h"
#include "stdio.h"
#include "Board.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_32BitCount
  @{
*/

/** @defgroup TMR_32BitCount_Macros Macros
  @{
*/

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

/**@} end of group TMR_32BitCount_Macros */

/** @defgroup TMR_32BitCount_Variables Variables
  @{
  */

extern volatile uint32_t tick;

/**@} end of group TMR_32BitCount_Variables */

/** @defgroup TMR_32BitCount_Functions Functions
  @{
  */

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note        TMR1 as time base for delay,
 *              TMR3 as High 16 bit count value,
 *              TMR4 as Low  16 bit count value.
 *
 */
int main(void)
{
    uint32_t temp;

    Board_LedInit(LED2);
    Board_LedOff(LED2);

    Board_ComInit(COM1);

    /* Enable TIMER peripheral clock */
    Rcm_EnableApb1PeriphClock(RCM_APB1_PERIPH_TMR3); 
    Rcm_EnableApb1PeriphClock(RCM_APB1_PERIPH_TMR2);
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_TMR1);

    /* Configure TMR2 as low 16-bit counter */
    Tmr_ConfigTimeBase(TMR2, &g_tmr2BaseConfig);
    Tmr_ConfigDIV(TMR2, 0, TMR_PRESCALER_RELOAD_UPDATA);
    Tmr_DisableNGUpdate(TMR2);

    /* Configure TMR2 input trigger and slave mode */
    Tmr_SelectInputTrigger(TMR2, TMR_TS_ITR1);
    Tmr_SelectSlaveMode(TMR2, TMR_SLAVEMODE_EXTERNALL);
    Tmr_EnableMasterSlaveMode(TMR2);
    Tmr_EnableAUTOReload(TMR2);

    /* Configure TMR3 as high 16-bit counter */
    Tmr_ConfigTimeBase(TMR3, &g_tmr3BaseConfig);
    Tmr_EnableAUTOReload(TMR3);

    /* Configure TMR3 output trigger and slave mode */
    Tmr_SelectSlaveMode(TMR3, TMR_SLAVEMODE_RESET);
    Tmr_ConfigUPdateRequest(TMR3, TMR_UPDATE_SOURCE_REGULAR);
    Tmr_SelectOutputTrigger(TMR3, TMR_TRGOSOURCE_UPDATE);
    Tmr_EnableMasterSlaveMode(TMR3);

    /* Configure TMR1 for delay interrupt */
    Tmr_ConfigTimeBase(TMR1, &g_tmr1BaseConfig);

    /* Enable TMR1 update interrupt */
    Tmr_EnableInterrupt(TMR1, (uint16_t)TMR_INT_UPDATE);
    Nvic_EnableIrqRequest(TMR1_BRK_UP_TRG_COM_IRQn, 0);

    /* Enable all timers */
    Tmr_Enable(TMR1);
    Tmr_Enable(TMR3);
    Tmr_Enable(TMR2);

    while (1)
    {
        /* Combine 32-bit counter value: TMR2 (high 16-bit) + TMR3 (low 16-bit) */
        temp = (uint32_t)TMR2->CNT_R.CNT << 16u;
        temp |= (uint32_t)TMR3->CNT_R.CNT;

        /* Print count value and toggle LED */
        printf(" Count Value: 0x%08" PRIx32 "\r\n", temp);
        Board_LedToggle(LED2);
        Tmr_Delay();
    }
}

/*!
 * @brief       Tmr_Delay function
 *
 * @param       None
 *
 * @retval      None
 *
 */
void Tmr_Delay(void)
{
    tick = 0;

    /* Wait until tick reaches target value */
    while (tick < (uint32_t)500u)
    {
        /* nothing */
    }
}

#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)
    {
        /* 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_32BitCount_Functions */
/**@} end of group TMR_32BitCount */
/**@} end of group TMR_Examples */
/**@} end of group Examples */
