/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2023-03-30
 *
 * @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"
#include "Board.h"
#include "stdio.h"
#include "apm32a0xx_pmu.h"
#include "apm32a0xx_eint.h"
#include "stdint.h"
#include "apm32a0xx_misc.h"

/** @addtogroup Examples
  @{
  */

/** @addtogroup PMU_Stop
  @{
  */
  
/** @defgroup PMU_Stop_Macros Macros
  @{
*/

/* printf using USART1  */
#define DEBUG_USART  USART1

/**@} end of group PMU_Stop_Macros */

/** @defgroup PMU_Stop_Enumerations Enumerations
  @{
*/

/**@} end of group PMU_Stop_Enumerations */

/** @defgroup PMU_Stop_Structures Structures
  @{
*/

/**@} end of group PMU_Stop_Structures */

/** @defgroup PMU_Stop_Variables Variables
  @{
*/

/* Judgement flag bit */
static uint8_t s_enterStopModeFlag = 0;
static uint8_t s_stopModeStatus = 0;

/**@} end of group PMU_Stop_Variables */

/** @defgroup PMU_Stop_Functions Functions
  @{
  */

/* Delay */
void Delay(uint32_t count);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
int main(void)
{
    /* Configures LED GPIO */
    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);

    /* KEY1 \ KEY2 Set */
    APM_MINI_PBInit(BUTTON_KEY1, BUTTON_MODE_EINT);
    APM_MINI_PBInit(BUTTON_KEY2, BUTTON_MODE_EINT);
    
    /* Configures COM port */
    APM_MINI_COMInit(COM1);

    /* Enable APB peripheral clock */
    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_PMU);
    
    /* Resets the PMU peripheral registers */
    PMU_Reset();

    printf("System is running  \r\n");
    printf("Press KEY2 to Enter STOP Mode\r\n");
    while (1)
    {
        /* Delay */
        Delay(0xFFFFF);
        
        /* Check whether key2 is pressed for entering STOP Mode*/ 
        if(s_enterStopModeFlag == 1)
        {
            /* Delay */
            Delay(0xfffff);
            s_enterStopModeFlag = 0;

            printf("System has entered Stop Mode \r\n");
            printf("Please press KEY1/KEY2 or reset System to exit STOP Mode \r\n");
            printf("\r\n");
            if(s_stopModeStatus == 0)
            {
                s_stopModeStatus = 1;
                /* Enter STOP Mode in WFI*/
                PMU_EnterSTOPMode(PMU_REGULATOR_ON, PMU_STOPENTRY_WFI);
            }
            printf("\r\n");
            printf("System continues to run, press KEY2 to enter STOP Mode \r\n");
        }
        else
        {
            /* Turns selected LED On */
            APM_MINI_LEDOn(LED2);
        }
    }
}

/*!
 * @brief       EINT WakeUp From Isr
 *
 * @param       None
 *
 * @retval      None
 *
 * @note        This function need to put into EINT0_1_IRQHandler in apm32a0xx_int.h
 */
void EINT_WakeUp_From_Isr(void)
{
    /* Read the specified EINT line interrupt flag */
    if (EINT_ReadIntFlag(EINT_LINE1) != RESET)
    {
        Delay(0xfffff);
        if(s_stopModeStatus == 1)
        {
            s_stopModeStatus = 0;

            /* Wait for system init */
            SystemInit();
            
            APM_MINI_LEDOff(LED3);
            printf("Wake up from STOP Mode by KEY1 \r\n");
        }
        /* Clears the EINT line pending bits */
        EINT_ClearIntFlag(EINT_LINE1);
    }
}

/*!
 * @brief       EINT Enter From Isr
 *
 * @param       None
 *
 * @retval      None
 *
 * @note        This function need to put into EINT0_1_IRQHandler in apm32a0xx_int.h
 */
void EINT_Enter_In_Isr(void)
{
    /* Read the specified EINT line interrupt flag */
    if (EINT_ReadIntFlag(EINT_LINE0) != RESET)
    {
        Delay(0xfffff);
        if(s_stopModeStatus == 1)
        {
            s_stopModeStatus = 0;

            /* Wait for system init */
            SystemInit();
            
            APM_MINI_LEDOff(LED3);
            printf("Wake up from STOP Mode by KEY2 \r\n");
        }
        else
        {
            APM_MINI_LEDOn(LED3);
            APM_MINI_LEDOff(LED2);
            
            /* Mark Key 2 Press to enter STOP Mode*/
            s_enterStopModeFlag = 1;
        }
        
        /* Clears the EINT line pending bits */
        EINT_ClearIntFlag(EINT_LINE0);
    }
}

/*!
 * @brief       Delay
 *
 * @param       count:  delay count
 *
 * @retval      None
 *
 * @note
 */
void Delay(uint32_t count)
{
    volatile uint32_t delay = count;
    while (delay--);
}

#if defined (__CC_ARM) || defined (__ICCARM__) || (defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050))

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @param       *f:  pointer to a FILE that can recording all information
*              needed to control a stream
*
* @retval      The characters that need to be send.
*
* @note
*/
int fputc(int ch, FILE* f)
{
    /* send a byte of data to the serial port */
    USART_TxData(DEBUG_USART, (uint8_t)ch);

    /* wait for the data to be send  */
    while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

    return (ch);
}

#elif defined (__GNUC__)

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       ch:  The characters that need to be send.
*
* @retval      The characters that need to be send.
*
* @note
*/
int __io_putchar(int ch)
{
    /* send a byte of data to the serial port */
    USART_TxData(DEBUG_USART, ch);

    /* wait for the data to be send  */
    while (USART_ReadStatusFlag(DEBUG_USART, USART_FLAG_TXBE) == RESET);

    return ch;
}

/*!
* @brief       Redirect C Library function printf to serial port.
*              After Redirection, you can use printf function.
*
* @param       file:  Meaningless in this function.
*
* @param       *ptr:  Buffer pointer for data to be sent.
*
* @param       len:  Length of data to be sent.
*
* @retval      The characters that need to be send.
*
* @note
*/
int _write(int file, char* ptr, int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        __io_putchar(*ptr++);
    }

    return len;
}

#else
#warning Not supported compiler type
#endif

/**@} end of group PMU_Stop_Functions */
/**@} end of group PMU_Stop */
/**@} end of group Examples */
