/*!
 * @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 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 "user_tmr_config.h"
#include "Board.h"
#include "stdio.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR_Examples
@{
*/

/** @addtogroup TMR_SynchronizationWithTMR1
  @{
  */

/** @defgroup TMR_SynchronizationWithTMR1_Macros Macros
  @{
*/

/**@} end of group TMR_SynchronizationWithTMR1_Macros */

/** @defgroup TMR_SynchronizationWithTMR1_Enumerations Enumerations
  @{
*/

/**@} end of group TMR_SynchronizationWithTMR1_Enumerations */

/** @defgroup TMR_SynchronizationWithTMR1_Structures Structures
  @{
*/

/**@} end of group TMR_SynchronizationWithTMR1_Structures */

/** @defgroup TMR_SynchronizationWithTMR1_Variables Variables
  @{
*/

/**@} end of group TMR_SynchronizationWithTMR1_Variables */

/** @defgroup TMR_SynchronizationWithTMR1_Functions Functions
  @{
  */

/* TMR Synchronization Init*/
void Tmr_SynchronizationInit(void);
void Tmr_Delay(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    Board_LedInit(LED2);

    Tmr_SynchronizationInit();

    /* Turn LED2 on */
    Gpio_ClearBit(LED2_GPIO_PORT, (uint16_t)LED2_PIN);
    /* Turn LED3 off */
    Gpio_SetBit(LED3_GPIO_PORT, (uint16_t)LED3_PIN);

    for (;;)
    {
        Tmr_Delay();
        Board_LedToggle(LED2);
    }
}

/*!
 * @brief       TMR synchronization Init
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void Tmr_SynchronizationInit(void)
{
    /* Enable Clock */
    Rcm_EnableAhbPeriphClock(RCM_AHB_PERIPH_GPIOA);
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_SYSCFG);
    Rcm_EnableApb2PeriphClock(RCM_APB2_PERIPH_TMR1);
    Rcm_EnableApb1PeriphClock(RCM_APB1_PERIPH_TMR3);

    /* Connect TMR3 to CH1 */
    Gpio_Config(TMR3_CH1_GPIO_PORT, &g_tmr3_Ch1GpioConfig);
    Gpio_ConfigPinAF(TMR3_CH1_GPIO_PORT, TMR3_CH1_PIN_SOURCE, TMR3_CH1_GPIO_AF);

    /* Config TimeBase */
    Tmr_ConfigTimeBase(TMR1, &g_tmr1_BaseConfig);
    Tmr_ConfigTimeBase(TMR3, &g_tmr3_BaseConfig);

    Tmr_EnableMasterSlaveMode(TMR1);
    Tmr_SelectOutputTrigger(TMR1, TMR_TRGOSOURCE_UPDATE);

    /* Config PWM */
    Tmr_OC1Config(TMR3, &g_tmr3_OC1PWMConfig);

    /* Slave Mode Configuration */
    Tmr_SelectSlaveMode(TMR3, TMR_SLAVEMODE_GATED);
    Tmr_SelectInputTrigger(TMR3, TMR_TS_ITR0);

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

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

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

/**@} end of group TMR_SynchronizationWithTMR1_Functions */
/**@} end of group TMR_SynchronizationWithTMR1 */
/**@} end of group TMR_Examples */
/**@} end of group Examples */
