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

/** @defgroup TMR2_OCActive_Functions Functions
  @{
*/

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

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

    TMR2_Init();

    while(1)
    {
    }
}

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

    /* Set counter = 1000, divider = 2^4 */
    TMR2_ConfigTimerBase(4, 1000);

    ocConfigStruct.channel = TMR2_CHANNEL_1;
    /* TMR2 CH1 delay = 400/1MHz = 400us */
    ocConfigStruct.count = 400;
    ocConfigStruct.mode = TMR2_OC_MODE_ACTIVE;
    ocConfigStruct.OCxOutputState = TMR2_OC_OUTPUT_ENABLE;
    ocConfigStruct.OCxPolarity = TMR2_OC_POLARITY_HIGH;
    TMR2_ConfigOutputCompare(&ocConfigStruct);

    ocConfigStruct.channel = TMR2_CHANNEL_2;
    /* TMR2 CH2 delay = 600/1MHz = 600us */
    ocConfigStruct.count = 600;
    TMR2_ConfigOutputCompare(&ocConfigStruct);

    ocConfigStruct.channel = TMR2_CHANNEL_3;
    /* TMR2 CH3 delay = 800/1MHz = 800us */
    ocConfigStruct.count = 800;
    TMR2_ConfigOutputCompare(&ocConfigStruct);

    TMR2_DisableAutoReloadBuffer();
    /* Enable TMR2  */
    TMR2_Enable();
}

/*!
 * @brief       IO remapping
 *
 * @param       None
 *
 * @retval      None
 */
void FMC_OPT(void)
{
    if(!(OB->AFR & 0x01))
    {
        FMC_Unlock();
        FMC_EraseOptionByte();
        NVIC_SystemReset();
    }
}
/**@} end of group TMR2_OCActive_Functions */
/**@} end of group TMR2_OCActive */
/**@} end of group Examples */
