/*!
 * @file        main.c
 *
 * @brief       Main program body
 *
 * @version     V1.0.0
 *
 * @date        2022-09-30
 *
 * @attention
 *
 *  Copyright (C) 2022-2024 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 "bsp_delay.h"
#include "apm32f035_rcm.h"
#include "apm32f035_gpio.h"
#include "apm32f035_rtc.h"
#include "apm32f035_pmu.h"
#include "apm32f035_eint.h"
#include "apm32f035_misc.h"
#include <stdio.h>
#include <string.h>


/** @addtogroup Examples
  * @brief RTC Stamp examples
  @{
  */

/** @addtogroup RTC_Stamp
  @{
  */

/** @defgroup RTC_Stamp_Macros Macros
  @{
*/

/* printf using USART1 */
#define DEBUG_USART             USART1
#define RTC_BKP_VALUE           0xAA55

/**@} end of group RTC_Stamp_Macros*/

/** @defgroup RTC_Stamp_Enumerates Enumerates
  @{
  */

/**@} end of group RTC_Stamp_Enumerates_Enumerates*/

/** @defgroup RTC_Stamp_Variables Variables
  @{
  */

RTC_DATE_T DateStruct;
RTC_TIME_T TimeStruct;

/**@} end of group RTC_Stamp_Variables*/

/** @defgroup RTC_Stamp_Functions Functions
  @{
  */

void APM_MINI_Init(void);
void RTC_ConfigStamp(void);

/*!
 * @brief       Main program
 *
 * @param       None
 *
 * @retval      None
 *
 */
int main(void)
{
    uint8_t prevTime;
    char dateStr[11];
    char timeStr[9];

    APM_MINI_Init();

    RTC_ConfigStamp();

    printf("This is an example of RTC stamp \r\n");

    while (1)
    {
        /* Read last Time*/
        prevTime = TimeStruct.seconds;

        /* Read time*/
        RTC_ReadTime(RTC_FORMAT_BIN, &TimeStruct);

        /** Read Date */
        RTC_ReadDate(RTC_FORMAT_BIN, &DateStruct);

        /** If pass 1 second */
        if (prevTime != TimeStruct.seconds)
        {
            sprintf(dateStr, "20%02d-%02d-%02d", DateStruct.year, DateStruct.month, DateStruct.date);
            sprintf(timeStr, "%02d:%02d:%02d", TimeStruct.hours, TimeStruct.minutes, TimeStruct.seconds);

            printf("%s\r\n", dateStr);
            printf("%s\r\n", timeStr);

            RTC_WriteBackup(RTC_BAKP_DATA0, RTC->TIME);

            if ((TimeStruct.hours != 0) && (TimeStruct.minutes != 0) && (TimeStruct.seconds != 0))
                RTC_WriteBackup(RTC_BAKP_DATA1, RTC->DATE);

        }
    }
}

/*!
 * @brief       Init MINI board
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void APM_MINI_Init(void)
{
    /* Init delay function */
    APM_MINI_DelayInit();
    APM_MINI_LEDInit(LED2);
    APM_MINI_LEDInit(LED3);
    APM_MINI_PBInit(BUTTON_KEY1, BUTTON_MODE_GPIO);
    APM_MINI_COMInit(COM1);

    APM_MINI_LEDOff(LED2);
    APM_MINI_LEDOff(LED3);
}

/*!
 * @brief       RTC Isr
 *
 * @param       None
 *
 * @retval      None
 */

void RTC_Isr(void)
{
    if (RTC_ReadIntFlag(RTC_INT_FLAG_TAMP1) == SET)
    {
        printf("RTC TAMP1 interrupt\r\n");

        APM_MINI_LEDOn(LED2);

        /* Clear RTC TAMP1 flag*/
        RTC_ClearIntFlag(RTC_INT_FLAG_TAMP1);

        /* Clear EINT RTC flag*/
        EINT_ClearIntFlag(EINT_LINE19);
    }

    if (RTC_ReadIntFlag(RTC_INT_FLAG_TS) == SET)
    {
        /* Read time*/
        RTC_ReadTime(RTC_FORMAT_BIN, &TimeStruct);

        /* Read Date */
        RTC_ReadDate(RTC_FORMAT_BIN, &DateStruct);

        printf("RTC Time Stamp : 20%02d-%02d-%02d , %02d:%02d:%02d\r\n",
               DateStruct.year, DateStruct.month, DateStruct.date,
               TimeStruct.hours, TimeStruct.minutes, TimeStruct.seconds);

        /* Clear RTC TS flag*/
        RTC_ClearIntFlag(RTC_INT_FLAG_TS);
        /* Clear EINT RTC flag*/
        EINT_ClearIntFlag(EINT_LINE19);
    }
}

/*!
 * @brief       RTC_Eint Line configuration
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void RTC_EintConfig(void)
{
    EINT_Config_T   eintConfig;
    GPIO_Config_T   gpioConfig;

    RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_GPIOC);

    gpioConfig.pin  = GPIO_PIN_13;
    gpioConfig.mode = GPIO_MODE_IN;
    gpioConfig.pupd = GPIO_PUPD_PU;
    GPIO_Config(GPIOC, &gpioConfig);

    /* Config EINT Line as EINT_LINE17 and EINT_LINE19*/
    eintConfig.line = EINT_LINE19 | EINT_LINE17;
    eintConfig.mode = EINT_MODE_INTERRUPT;
    eintConfig.trigger = EINT_TRIGGER_RISING;
    eintConfig.lineCmd = ENABLE;
    EINT_Config(&eintConfig);

    NVIC_EnableIRQRequest(RTC_IRQn, 0);
}

/*!
 * @brief       RTC Init
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void RTC_Init(void)
{
    RTC_Config_T Struct;

    /* Enable PMU Periph Clock */
    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_PMU);

    /* Backup Access */
    PMU_EnableBackupAccess();

    /* Enable LSI */
    RCM_EnableLSI();

    /* Wait until LSI is ready */
    while (RCM_ReadStatusFlag(RCM_FLAG_LSIRDY) == RESET)
    {
    }
    /* Select LICT as RCM clock */
    RCM_ConfigRTCCLK(RCM_RTCCLK_LSI);

    /* Enable RTC clock */
    RCM_EnableRTCCLK();

    /* Disable RTC write Protection */
    RTC_DisableWriteProtection();

    /* Wait Synchro */
    RTC_WaitForSynchro();

    /* set LSI Prediv */
    RTC_ConfigStructInit(&Struct);
    Struct.AsynchPrediv = 0x63;
    Struct.SynchPrediv  = 0x18F;

    RTC_Config(&Struct);
}

/*!
 * @brief       RTC configure date and time
 *
 * @param       None
 *
 * @retval      None
 *
 * @note
 */
void RTC_ConfigStamp(void)
{
    uint32_t date;
    uint32_t time;

    /* RTC Reset */
    RTC_Init();
    RTC_Reset();
    RTC_Init();

    /* RTC Enable Init*/
    RTC_EnableInit();

    RTC_ConfigDateStructInit(&DateStruct);

    /* First time Init*/
    if (RTC_ReadBackup(RTC_BAKP_DATA4) != RTC_BKP_VALUE)
    {
        /* Init Date 22/8/22/5*/
        DateStruct.year =  22;
        DateStruct.month = 8;
        DateStruct.date =  26;
        DateStruct.weekday = 5;
        RTC_ConfigDate(RTC_FORMAT_BIN, &DateStruct);
        APM_MINI_DelayMs(10);

        TimeStruct.H12 = 12;
        TimeStruct.hours = 23;
        TimeStruct.minutes = 59;
        TimeStruct.seconds = 50;
        RTC_ConfigTime(RTC_FORMAT_BIN, &TimeStruct);

        RTC_WriteBackup(RTC_BAKP_DATA1, RTC->DATE);
        RTC_WriteBackup(RTC_BAKP_DATA0, RTC->TIME);

        RTC_WriteBackup(RTC_BAKP_DATA4, RTC_BKP_VALUE);
    }
    /* Read Date information from Backup*/
    else
    {
        time = RTC_ReadBackup(RTC_BAKP_DATA0);
        TimeStruct.hours = time >> 16;
        TimeStruct.minutes = (time >> 8) & 0xff;
        TimeStruct.seconds = (time) & 0xff;
        RTC_ConfigTime(RTC_FORMAT_BCD, &TimeStruct);
        APM_MINI_DelayMs(10);

        date = RTC_ReadBackup(RTC_BAKP_DATA1);
        DateStruct.year =  date >> 16;
        DateStruct.month = (date >> 8) & 0x0f;
        DateStruct.date =  date & 0xff;
        DateStruct.weekday = (date >> 13) & 0x07;
        RTC_ConfigDate(RTC_FORMAT_BCD, &DateStruct);
    }

    printf("Connect PC13 in the air to require a TAMPER1\r\n");
    /* RTC Eint Line Init*/
    RTC_EintConfig();

    /* Config Tamper*/
    RTC_ConfigTamperTrigger(RTC_TAMPER_TRIGGER_RISINGEDGE);
    RTC_EnableTamper();

    /* Config Time Stamp */
    RTC_EnableTimeStamp(RTC_TIME_STAMPEDGE_RISING);

    /* Enable TimeStamp Interrupt*/
    RTC_EnableInterrupt(RTC_INT_FLAG_TAMP1);

    /* Enable TimeStamp Interrupt*/
    RTC_EnableInterrupt(RTC_INT_FLAG_TS);
    NVIC_EnableIRQRequest(RTC_IRQn, 2);

    /* Exit RTC init mode */
    RTC_DisableInit();
}

#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 RTC_Stamp_Functions */
/**@} end of group RTC_Stamp */
/**@} end of group Examples */
