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

/** @defgroup RCM_ClockSwitch_Functions Functions
  @{
*/

/* Key pressed */
BOOL KeyPressed(void);
/* Clock output init */
void ClockOutputInit(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 */
int main(void)
{
    uint8_t count = 0;
    RCM_SwitchClockStruct_T switchClockStruct;

    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
    APM_DelayInit(APM_DELAY_MS);
    APM_MINI_PBInit(BUTTON_KEY1, BUTTON_MODE_GPIO);

    APM_MINI_LEDOff(LED2);
    APM_MINI_LEDOff(LED3);

    ClockOutputInit();

    /* Close the current clock if switching succeed.In this case,Close the HXT clock */
    switchClockStruct.currentClkState = RCM_CLOCK_STATE_OFF;
    /* Disable interrupt when switching is completed */
    switchClockStruct.intEnable = DISABLE;

    while(1)
    {
        if(KeyPressed())
        {
            switch(count)
            {
                case 0:
                    /* switch HXT as master clock  */
                    switchClockStruct.clockSource = RCM_MCLKSRC_HXT;
                    /* Manual switching */
                    switchClockStruct.mode = RCM_SWITCH_MODE_MANUAL;
                    RCM_ConfigClockSwitch(&switchClockStruct);

                    APM_MINI_LEDOn(LED2);
                    APM_MINI_LEDOff(LED3);
                    break;

                case 1:
                    /* switch HIRC as master clock  */
                    switchClockStruct.clockSource = RCM_MCLKSRC_HIRC;
                    /* Automatic switching */
                    switchClockStruct.mode = RCM_SWITCH_MODE_AUTO;
                    RCM_ConfigClockSwitch(&switchClockStruct);

                    APM_MINI_LEDOff(LED2);
                    APM_MINI_LEDOn(LED3);
                    break;

                default:
                    break;
            }
            count++;
            count &= 0X01;
        }
    }
}

/*!
 * @brief       Clock output init
 *
 * @param       None
 *
 * @retval      None
 */
void ClockOutputInit(void)
{
    GPIO_Config_T gpioConfig;

    /* GPIO configuration  */
    gpioConfig.mode = GPIO_MODE_OUT_PP;
    gpioConfig.pin = GPIO_PIN_4;
    gpioConfig.speed = GPIO_SPEED_10MHz;
    GPIO_Config(GPIOC, &gpioConfig);

    /* master clock freq = 48MHZ / 2 = 24MHZ  */
    RCM_ConfigHIRCDiv(RCM_HIRC_DIV_2);

    /* master clock as clock output  */
    RCM_ConfigClockOutput(RCM_COCCLK_MASTER);
    RCM_EnableClockOutput();
}

/*!
 * @brief       Check Key is pressed
 *
 * @param       None
 *
 * @retval      Return TRUE if key has been pressed else return FALSE
 */
BOOL KeyPressed(void)
{
    BOOL ret = FALSE;

    if(APM_MINI_ReadPBState(BUTTON_KEY1) == BIT_RESET)
    {
        APM_Delay(50);
        if(APM_MINI_ReadPBState(BUTTON_KEY1) == BIT_RESET)
        {
            while(APM_MINI_ReadPBState(BUTTON_KEY1) == BIT_RESET);

            ret = TRUE;
        }
    }

    return ret;
}

/**@} end of group RCM_ClockSwitch_Functions */
/**@} end of group RCM_ClockSwitch */
/**@} end of group Examples */
