/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @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 TMR2_PWMOutput
  @{
*/

/** @defgroup TMR2_PWMOutput_Functions Functions
  @{
*/

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

void AFR_Init(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
    APM_MINI_LEDOff(LED2);
    APM_MINI_LEDOff(LED3);
    APM_DelayInit(APM_DELAY_MS);
    uint32_t afrr = OB->AFR;
    if(afrr != 0x817E)
    {
        AFR_Init();
        APM_MINI_LEDOn(LED2);
    }
    TMR2_Init();

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

void AFR_Init(void)
{
    FMC_AFRConfig_T afr;
    afr.AFR0 = FMC_OB_AFR_ENABLE;
    afr.AFR1 = FMC_OB_AFR_DISABLE;
    afr.AFR3 = FMC_OB_AFR_DISABLE;
    afr.AFR4 = FMC_OB_AFR_DISABLE;
    afr.AFR5 = FMC_OB_AFR_DISABLE;
    afr.AFR7 = FMC_OB_AFR_ENABLE;
    FMC_Unlock();
    FMC_ConfigOptionByteAFR(&afr);
    FMC_Lock();
}

/*!
 * @brief       TMR2 Init
 *
 * @param       None
 *
 * @retval      None
 */
void TMR2_Init(void)
{
    GPIO_Config_T gpio;
    GPIO_ConfigStructInit(&gpio);


    TMR2_OCConfig_T ocConfigStruct;
    TMR2_ConfigTimerBase(47, 0);

    /* Select channel 3 */
    ocConfigStruct.channel = TMR2_CHANNEL_1;
    /* Set compare value = 250 */
    ocConfigStruct.count = 250;
    /* PWM1 mode */
    ocConfigStruct.mode = TMR2_OC_MODE_PWM1;
    /* Enable CH1 ouput */
    ocConfigStruct.OCxOutputState = TMR2_OC_OUTPUT_ENABLE;
    /* CH1 polarity is high */
    ocConfigStruct.OCxPolarity = TMR2_OC_POLARITY_HIGH;
    TMR2_ConfigOutputCompare(&ocConfigStruct);

    /* Enable TMR2  */
    TMR2_Enable();
}

/**@} end of group TMR2_PWMOutput_Functions */
/**@} end of group TMR2_PWMOutput */
/**@} end of group Examples */
