/*!
 * @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 TMR1_ComplementaryOutput
  @{
*/

/** @defgroup TMR1_ComplementaryOutput_Functions Functions
  @{
*/

/* TMR1 Init */
void TMR1_Init(void);
/* GPIO Alternate function configure */
void AFConfig(void);
/* delay */
void Delay(uint32_t count);

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

    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
    APM_DelayInit(APM_DELAY_MS);

    APM_MINI_LEDOn(LED2);
    APM_MINI_LEDOn(LED3);
    TMR1_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_OCConfig_T ocConfigStruct;
    TMR1_TimeBaseConfig_T timeBaseConfig;

    /* Up-counter */
    timeBaseConfig.cntMode = TMR1_CNT_MODE_UP;
    /* Set counter = 250 */
    timeBaseConfig.count = 250;
    /* Set divider = 47.So TMR1 clock freq ~= 24/(47 + 1) = 500KHZ */
    timeBaseConfig.divider = 47;
    /* Repetition counter = 0x0 */
    timeBaseConfig.repetitionCount = 0;
    TMR1_ConfigTimerBase(TMR1, &timeBaseConfig);

    /* Select channen1 1 */
    ocConfigStruct.channel = TMR1_CHANNEL_1;
    /* Set compare value = 0x7D */
    ocConfigStruct.count = 0x7D;
    /* PWM1 mode */
    ocConfigStruct.mode = TMR1_OC_MODE_PWM1;
    /* Idle State is reset */
    ocConfigStruct.OCxIdleState = TMR1_OC_IDLE_RESET;
    /* Idle State is reset */
    ocConfigStruct.OCxNIdleState = TMR1_OC_IDLE_RESET;
    /* Enable CH1N ouput */
    ocConfigStruct.OCxNOutputState = TMR1_OC_OUTPUT_ENABLE;
    /* Enable CH1 ouput */
    ocConfigStruct.OCxOutputState = TMR1_OC_OUTPUT_ENABLE;
    /* CH1N polarity is high */
    ocConfigStruct.OCxNPolarity = TMR1_OC_POLARITY_HIGH;
    /* CH1 polarity is high */
    ocConfigStruct.OCxPolarity = TMR1_OC_POLARITY_HIGH;
    TMR1_ConfigOutputCompare(TMR1, &ocConfigStruct);

    /* Set dead time */
    TMR1_SetDeadTime(TMR1, 100);
    /* Disable output when CHx output closed */
    TMR1_SelectOffStateInRunMode(TMR1, TMR1_OSRM_DISABLE);
    /* Disable output when CHxN output closed */
    TMR1_SelectOffStateInIdleMode(TMR1, TMR1_OSIM_DISABLE);

    /* Enable PWM output */
    TMR1_EnableOutputPWM(TMR1);

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

/*!
 * @brief       GPIO Alternate function configure.
 *              Set PC6 alternate function = TMR1_CH1.
 *              Set PC3 alternate function = TMR1_CH1N.
 *
 * @param       None
 *
 * @retval      None
 */
void AFConfig(void)
{
    FMC_AFRConfig_T AFRConfig;

    /* Set PC6 alternate function = TMR1_CH1 */
    AFRConfig.AFR0 = FMC_OB_AFR_ENABLE;
    AFRConfig.AFR1 = FMC_OB_AFR_DISABLE;
    AFRConfig.AFR3 = FMC_OB_AFR_DISABLE;
    AFRConfig.AFR4 = FMC_OB_AFR_DISABLE;
    /* Set PC3 alternate function = TMR1_CH1N */
    AFRConfig.AFR7 = FMC_OB_AFR_ENABLE;

    FMC_Unlock();
    FMC_EraseOptionByte();
    FMC_ConfigOptionByteAFR(&AFRConfig);
    FMC_Lock();
}

/**@} end of group TMR1_ComplementaryOutput_Functions */
/**@} end of group TMR1_ComplementaryOutput */
/**@} end of group Examples */
