/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.2
 *
 * @date        2023-03-31
 *
 * @attention
 *
 *  Copyright (C) 2023 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 "main.h"

/** @addtogroup Examples
  @{
*/

/** @addtogroup TMR2_Synchronization
  @{
*/

/** @defgroup TMR2_Synchronization_Functions Functions
  @{
*/

/* TMR1 Init */
void TMR1_Init(void);
/* TMR2 Init */
void TMR2_Init(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
    APM_DelayInit(APM_DELAY_MS);

    TMR1_Init();
    TMR2_Init();

    while(1)
    {
        APM_Delay(500);
        APM_MINI_LEDToggle(LED2);
        APM_MINI_LEDToggle(LED3);
    }
}

/*!
 * @brief       TMR1 Init
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1_Init(void)
{
    TMR1_TimeBaseConfig_T timeBaseConfig;

    /* Up-counter */
    timeBaseConfig.cntMode = TMR1_CNT_MODE_UP;
    /* Set counter = 47 */
    timeBaseConfig.count = 47;
    /* Set divider = 0 */
    timeBaseConfig.divider = 0;
    /* Repetition counter = 0x0 */
    timeBaseConfig.repetitionCount = 0;
    TMR1_ConfigTimerBase(TMR1, &timeBaseConfig);

    /* The update event is selected as trigger output */
    TMR1_ConfigMasterMode(TMR1, TMR1_MASTER_MODE_UPDATE);

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

/*!
 * @brief       TMR2 Init
 *
 * @param       None
 *
 * @retval      None
 */
void TMR2_Init(void)
{
    TMR2_OCConfig_T ocConfigStruct;
    GPIO_Config_T gpio_struct;

    gpio_struct.mode = GPIO_MODE_OUT_PP;
    gpio_struct.pin = GPIO_PIN_3;
    gpio_struct.speed = GPIO_SPEED_10MHz;
    GPIO_Config(GPIOD, &gpio_struct);

    /* Set counter = 250, divider = 0 */
    TMR2_ConfigTimerBase(0, 250);
    /* IT1 connected to TMR1 TRGOUT */
    TMR2_ConfigInputTrigger(TMR2_ITC_TMR1);
    /* Slave mode is External clock mode 1  */
    TMR2_ConfigSlaveMode(TMR2_SLAVE_MODE_EXTERNAL1);

    /* CH2 output wave that period = 250us, duty = 1 */
    ocConfigStruct.channel = TMR2_CHANNEL_2;
    ocConfigStruct.count = 0X7D;
    ocConfigStruct.mode = TMR2_OC_MODE_PWM1;
    ocConfigStruct.OCxOutputState = TMR2_OC_OUTPUT_ENABLE;
    ocConfigStruct.OCxPolarity = TMR2_OC_POLARITY_HIGH;
    TMR2_ConfigOutputCompare(&ocConfigStruct);

    /* Enable TMR2  */
    TMR2_Enable();
}

/**@} end of group TMR2_Synchronization_Functions */
/**@} end of group TMR2_Synchronization */
/**@} end of group Examples */
