/* Copyright Statement:
 *
 * This software/firmware and related documentation ("AutoChips Software") are
 * protected under relevant copyright laws. The information contained herein is
 * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
 * the prior written permission of AutoChips inc. and/or its licensors, any
 * reproduction, modification, use or disclosure of AutoChips Software, and
 * information contained herein, in whole or in part, shall be strictly
 * prohibited.
 *
 * AutoChips Inc. (C) 2023. All rights reserved.
 *
 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
 * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
 * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
 * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
 * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
 * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
 * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
 * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
 * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
 * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
 * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
 * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
 * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
 * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
 * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
 */
/******************************************************************************
 * @version: V1.0.0 demo
 ******************************************************************************/

#include "rtc.h"
#include "gpio.h"
#include "stdbool.h"
#include "ac780x_rtc_reg.h"

/* ============================================  Define  ============================================ */


/* ===========================================  Typedef  ============================================ */


/* ==========================================  Variables  =========================================== */
RTC_ConfigType rtc_Config;  /* This structure holds the configuration settings for the RTC */

/* ====================================  Functions declaration  ===================================== */


/* ======================================  Functions define  ======================================== */

/*!
 * @brief RTC  CallBack function
 *
 * @param[in] void *device, uint32_t wpara, uint32_t lpara
 *
 * @return none
 */
void RTC_ISR_CallBack(void *device, uint32_t wpara, uint32_t lpara)
{
    if (wpara & RTC_SR_POF_Msk)
    {
        printf("RTC Prescaler ISR\r\n");

        LED1_TOGGLE;
    }

    if (wpara & RTC_SR_TOF_Msk)
    {
        RTC_Enable(DISABLE);
        RTC_SetTimeCntRegister(rtc_Config.timeCnt);
        RTC_Enable(ENABLE);
    }

    if (wpara & RTC_SR_TAF_Msk)
    {
        printf("RTC Alarm ISR\r\n");

        LED2_TOGGLE;
    }

}

/*!
 * @brief RTC Init
 *
 * @param[in] none
 *
 * @return none
 */
void RTC_Basic_Init(void)
{
    memset(&rtc_Config, 0U, sizeof(rtc_Config));

    rtc_Config.clockSource      = RTC_CLOCK_LSI;        /* select RTC clock source: LSI_CLK */
    rtc_Config.psrValue         = 31999U;               /* set RTC prescaler value, time = (psrValue + 1) / clockSource */
    rtc_Config.timeCnt          = 2U;                   /* set RTC real time counter value */
    rtc_Config.alarmValue       = 5U;                   /* set RTC Alarm time value */
    rtc_Config.psrInterruptEn   = ENABLE;               /* Enable prescaler interrupt */
    rtc_Config.alarmInterruptEn = ENABLE;               /* Enable rtc alarm interrupt */
    rtc_Config.clockOutFlag     = DISABLE;              /* Disable RTC clock out source */
    rtc_Config.callBack         = RTC_ISR_CallBack;     /* set RTC CallBack function */

    RTC_Init(&rtc_Config);
}
