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

/** @defgroup GPIO_AF_Functions Functions
  @{
*/

/* GPIOA Init */
void GPIOA_Init(void);
/* System clock Source change */
void RCM_ChangeCLKSource(void);

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

    APM_MINI_LEDOn(LED2);
    APM_MINI_LEDOn(LED3);

    GPIOA_Init();

    APM_MINI_LEDOff(LED3);

    RCM_ChangeCLKSource();

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

/*!
 * @brief       Configure OSCIN and OSCOUT as normal GPIO
 *
 * @param       None
 *
 * @retval      None
 */
void GPIOA_Init(void)
{
    GPIO_Config_T gpioConfig;

    gpioConfig.mode = GPIO_MODE_OUT_OD;
    gpioConfig.speed = GPIO_SPEED_10MHz;
    gpioConfig.intEn = GPIO_EINT_DISABLE;
    gpioConfig.pin = GPIO_PIN_1 | GPIO_PIN_2;
    GPIO_Config(GPIOA, &gpioConfig);
}

/*!
 * @brief       Change system clock source
 *
 * @param       None
 *
 * @retval      None
 */
void RCM_ChangeCLKSource(void)
{
    RCM_SwitchClockStruct_T switchClockStruct;
    /* switch HIRC as master clock  */
    switchClockStruct.clockSource = RCM_MCLKSRC_HIRC;
    /* 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;
    /* Automatic switching */
    switchClockStruct.mode = RCM_SWITCH_MODE_AUTO;
    RCM_ConfigClockSwitch(&switchClockStruct);
}

/**@} end of group GPIO_AF_Functions */
/**@} end of group GPIO_AF */
/**@} end of group Examples */
