/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.3
 *
 * @date        2025-05-31
 *
 * @attention
 *
 *  Copyright (C) 2023-2025 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 FMC_AFR
  @{
*/

/** @defgroup FMC_AFR_Functions Functions
  @{
*/

/* TMR1 Init */
void TMR1Init(void);
/* GPIO Alternate function config */
void AFConfig(void);
OB_T GetOptionByte(void);

/*!
 * @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);
    TMR1Init();

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

/*!
 * @brief       TMR1 PWM output Initialization
 *
 * @param       None
 *
 * @retval      None
 */
void TMR1Init(void)
{
    TMR1_OCConfig_T ocConfigStruct;
    TMR1_TimeBaseConfig_T timeBaseConfig;

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

    /* Select channen1 1 */
    ocConfigStruct.channel = TMR1_CHANNEL_1;
    /* Set compare value = 500 */
    ocConfigStruct.count = 500;
    /* 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 config.
 *              Set PC6 alternate function = TMR1_CH1.
 *              Set PC3 alternate function = TMR1_CH1N.
 *
 * @param       None
 *
 * @retval      None
 */
void AFConfig(void)
{
    FMC_AFRConfig_T AFRConfig;
    OB_T ob;

    /* 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;
    AFRConfig.AFR5 = FMC_OB_AFR_DISABLE;
    /* Set PC3 alternate function = TMR1_CH1N */
    AFRConfig.AFR7 = FMC_OB_AFR_ENABLE;

    FMC_Unlock();
    if(OB->AFR != 0xff)
    {
        /* Back up option byte */
        ob = GetOptionByte();
        FMC_EraseOptionByte();

        /* Write back to READPROT */
        FMC_ProgramOptionByte(0x20400, ob.READPROT);
        /* Write back to USER */
        FMC_ProgramOptionByte(0x20402, ob.USER);
        /* Write back to DATA0 */
        FMC_ProgramOptionByte(0x20404, ob.DATA0);
        /* Write back to DATA1 */
        FMC_ProgramOptionByte(0x20406, ob.DATA1);
        /* Write back to WRTPROT0 */
        FMC_ProgramOptionByte(0x20408, ob.WRTPROT0);
        /* Write back to WRTPROT1 */
        FMC_ProgramOptionByte(0x2040A, ob.WRTPROT1);
        /* Write back to WRTPROT2 */
        FMC_ProgramOptionByte(0x2040C, ob.WRTPROT2);
        /* Write back to WRTPROT3 */
        FMC_ProgramOptionByte(0x2040E, ob.WRTPROT3);
        /* Write back to HXTCNT */
        FMC_ProgramOptionByte(0x20414, ob.HXTCNT);
        /* Write back to CLKOPT */
        FMC_ProgramOptionByte(0x20416, ob.CLKOPT);
    }
    FMC_ConfigOptionByteAFR(&AFRConfig);
    FMC_Lock();
}

/*!
 * @brief       Get all byte of option byte
 *
 * @param       None
 *
 * @retval      OB structure
 */
OB_T GetOptionByte(void)
{
    OB_T ob;
    ob.READPROT = OB->READPROT;
    ob.USER = OB->USER;
    ob.DATA0 = OB->DATA0;
    ob.DATA1 = OB->DATA1;
    ob.WRTPROT0 = OB->WRTPROT0;
    ob.WRTPROT1 = OB->WRTPROT1;
    ob.WRTPROT2 = OB->WRTPROT2;
    ob.WRTPROT3 = OB->WRTPROT3;
    ob.AFR = OB->AFR;
    ob.HXTCNT = OB->HXTCNT;
    ob.CLKOPT = OB->CLKOPT;
    return ob;
}

/**@} end of group FMC_AFR_Functions */
/**@} end of group FMC_AFR */
/**@} end of group Examples */
