/*******************************************************************************
* @file             main.c
*
* @version          1.0.0
*
* @brief            Integration Framework -  main function
*
* Copyright (C) 2026 Geehy Semiconductor
*
* You may not use this file except in compliance with the GEEHY COPYRIGHT NOTICE
* (Geehy Semiconductor Software License Agreement).
*
* 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 Semiconductor Software License Agreement for
* the governing permissions and limitations under the License.
*
*******************************************************************************/

#ifdef __cplusplus
extern "C"{
#endif

#include "Port.h"
#include "CDD_Usart.h"
#include "Mcu.h"
#include "CDD_Rtc.h"
#include "Dio.h"

/*******************************************************************************
**                     GLOBAL FUNCTION                                        **
*******************************************************************************/

 /*!
 * @brief       Delay function
 *
 * @param       DelayTime：The delay time
 *
 * @retval      None
 *
 * @note        None
 */
/*  */
void sys_delay(uint32_t DelayTime)
{
  while(DelayTime--);
}

int main()
{
   /* Init the mcu,port,uart and port modules */
    Mcu_Init(&g_mcuConfig);
    Mcu_InitClock(McuClockSettingConfig_0);
    Port_Init(&g_portConfig);
    Usart_Init(g_usartConfg);
        
    Rtc_Init(&g_rtcConfig);

    printf("\r\n");
    printf("G32A1085 RTC demo start!\r\n");
    Dio_WriteChannel(DIO_PORT_B_PIN_14,DIO_STD_LOW);
    
    printf("\r\n---------------- RTC_ITA Start ----------------\r\n\r\n");
    
    /* Config Alarm */
    Rtc_AlarmsConfig(&g_rtcConfig);
    /* 8 Second Calibration Cycle Period Configure */
    Rtc_ConfigSmoothCalib(RTC_SCP_8SEC,RTC_SCPP_SET,0);
    
    printf("Please wait 5 second\r\n");
    /* Alarm Enable */
    Rtc_EnableAlarm();
    Dio_WriteChannel(DIO_PORT_B_PIN_14,DIO_STD_HIGH);
    /* Wait until time out */
    while (Rtc_ReadStatusFlag(RTC_FLAG_ALRF) == FALSE);
    Dio_WriteChannel(DIO_PORT_B_PIN_14,DIO_STD_LOW);
    printf("Time out\r\n");
    Rtc_DisableAlarm();
    
    Rtc_DeInit();
    
    while(1)
    {
        sys_delay(10000);
    }
}

#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)
{
    int reVal = 0;
    reVal = Usart_PutRedefinition(USART1_ID,&ch);
    
    return reVal;
}

/* If configuring the full library using versions after IAR 9.30,
   it is necessary to implement the _write function for printf redirection.*/
size_t __write(int handle,const unsigned char *buf,size_t bufSize)
{
    int32 tempCh = 0;
    for(int i=0; i<bufSize; i++)
    {
        Usart_PutRedefinition(USART1_ID,&tempCh);
    }
    return bufSize;
}

#elif defined (__GNUC__)

/* For Arm Cross GCC: Eclipse */
int _write(int fd,const unsigned char *buf,int len)
{
    int32 tempCh = 0;
    for(int i=0; i<len; i++)
    {
        int32 tempCh = (int32)buf[i];
        Usart_PutRedefinition(USART1_ID,&tempCh);
    }
    return len;
}

#endif

#ifdef __cplusplus
}
#endif
