Arrow / Mbed OS DAPLink Reset
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers m480_rtc.c Source File

m480_rtc.c

00001 /**************************************************************************//**
00002  * @file     rtc.c
00003  * @version  V3.00
00004  * @brief    M480 series RTC driver source file
00005  *
00006  * @copyright (C) 2016 Nuvoton Technology Corp. All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without modification,
00009  * are permitted provided that the following conditions are met:
00010  *   1. Redistributions of source code must retain the above copyright notice,
00011  *      this list of conditions and the following disclaimer.
00012  *   2. Redistributions in binary form must reproduce the above copyright notice,
00013  *      this list of conditions and the following disclaimer in the documentation
00014  *      and/or other materials provided with the distribution.
00015  *   3. Neither the name of Nuvoton Technology Corp. nor the names of its contributors
00016  *      may be used to endorse or promote products derived from this software
00017  *      without specific prior written permission.
00018  * 
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00023  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00025  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00027  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 *****************************************************************************/
00030 #include "NuMicro.h"
00031 
00032 
00033 /** @cond HIDDEN_SYMBOLS */
00034 
00035 /*---------------------------------------------------------------------------------------------------------*/
00036 /* Macro, type and constant definitions                                                                    */
00037 /*---------------------------------------------------------------------------------------------------------*/
00038 #define RTC_GLOBALS
00039 
00040 /*---------------------------------------------------------------------------------------------------------*/
00041 /* Global file scope (static) variables                                                                    */
00042 /*---------------------------------------------------------------------------------------------------------*/
00043 static volatile uint32_t g_u32hiYear, g_u32loYear, g_u32hiMonth, g_u32loMonth, g_u32hiDay, g_u32loDay;
00044 static volatile uint32_t g_u32hiHour, g_u32loHour, g_u32hiMin, g_u32loMin, g_u32hiSec, g_u32loSec;
00045 
00046 /** @endcond HIDDEN_SYMBOLS */
00047 
00048 
00049 
00050 
00051 /** @addtogroup Standard_Driver Standard Driver
00052   @{
00053 */
00054 
00055 /** @addtogroup RTC_Driver RTC Driver
00056   @{
00057 */
00058 
00059 /** @addtogroup RTC_EXPORTED_FUNCTIONS RTC Exported Functions
00060   @{
00061 */
00062 
00063 /**
00064   * @brief      Initialize RTC module and start counting
00065   *
00066   * @param[in]  sPt     Specify the time property and current date and time. It includes:           \n
00067   *                     u32Year: Year value, range between 2000 ~ 2099.                             \n
00068   *                     u32Month: Month value, range between 1 ~ 12.                                \n
00069   *                     u32Day: Day value, range between 1 ~ 31.                                    \n
00070   *                     u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
00071   *                                                     RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
00072   *                                                     RTC_SATURDAY]                               \n
00073   *                     u32Hour: Hour value, range between 0 ~ 23.                                  \n
00074   *                     u32Minute: Minute value, range between 0 ~ 59.                              \n
00075   *                     u32Second: Second value, range between 0 ~ 59.                              \n
00076   *                     u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24]                                 \n
00077   *                     u8AmPm: [RTC_AM / RTC_PM]                                                   \n
00078   *
00079   * @return     None
00080   *
00081   * @details    This function is used to: \n
00082   *                 1. Write initial key to let RTC start count.  \n
00083   *                 2. Input parameter indicates start date/time. \n
00084   *                 3. User has to make sure that parameters of RTC date/time are reasonable. \n
00085   * @note       Null pointer for using default starting date/time.
00086   */
00087 void RTC_Open(S_RTC_TIME_DATA_T  *sPt)
00088 {
00089     RTC->INIT = RTC_INIT_KEY;
00090 
00091     if (RTC->INIT != RTC_INIT_ACTIVE_Msk) {
00092         RTC->INIT = RTC_INIT_KEY;
00093 
00094         while (RTC->INIT != RTC_INIT_ACTIVE_Msk) {
00095         }
00096     }
00097 
00098     if (sPt == 0) {
00099     } else {
00100         /* Set RTC date and time */
00101         RTC_SetDateAndTime(sPt);
00102     }
00103 }
00104 
00105 /**
00106   * @brief      Disable RTC Clock
00107   *
00108   * @param      None
00109   *
00110   * @return     None
00111   *
00112   * @details    This API will disable RTC peripheral clock and stops RTC counting.
00113   */
00114 void RTC_Close(void)
00115 {
00116     CLK->APBCLK0 &= ~CLK_APBCLK0_RTCCKEN_Msk;
00117 }
00118 
00119 /**
00120  *  @brief    Set Frequency Compensation Data
00121   *
00122  *  @param[in]    i32FrequencyX10000    Specify the RTC clock X10000, ex: 327736512 means 32773.6512.
00123   *
00124   * @return     None
00125   *
00126   */
00127 void RTC_32KCalibration(int32_t i32FrequencyX10000)
00128 {
00129     uint64_t u64Compensate;
00130     u64Compensate = (uint64_t)(0x2710000000000);
00131     u64Compensate = (uint64_t)(u64Compensate / (uint64_t)i32FrequencyX10000);
00132 
00133     if (u64Compensate >= (uint64_t)0x400000) {
00134         u64Compensate = (uint64_t)0x3FFFFF;
00135     }
00136 
00137     RTC_WaitAccessEnable();
00138     RTC->FREQADJ = (uint32_t)u64Compensate;
00139 }
00140 
00141 /**
00142   * @brief      Get Current RTC Date and Time
00143   *
00144   * @param[out] sPt     The returned pointer is specified the current RTC value. It includes: \n
00145   *                     u32Year: Year value                                                   \n
00146   *                     u32Month: Month value                                                 \n
00147   *                     u32Day: Day value                                                     \n
00148   *                     u32DayOfWeek: Day of week                                             \n
00149   *                     u32Hour: Hour value                                                   \n
00150   *                     u32Minute: Minute value                                               \n
00151   *                     u32Second: Second value                                               \n
00152   *                     u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24]                           \n
00153   *                     u8AmPm: [RTC_AM / RTC_PM]                                             \n
00154   *
00155   * @return     None
00156   *
00157   * @details    This API is used to get the current RTC date and time value.
00158   */
00159 void RTC_GetDateAndTime(S_RTC_TIME_DATA_T  *sPt)
00160 {
00161     uint32_t u32Tmp;
00162     sPt->u32TimeScale  = RTC->CLKFMT & RTC_CLKFMT_24HEN_Msk;     /* 12/24-hour */
00163     sPt->u32DayOfWeek  = RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk; /* Day of the week */
00164     /* Get [Date digit] data */
00165     g_u32hiYear  = (RTC->CAL & RTC_CAL_TENYEAR_Msk) >> RTC_CAL_TENYEAR_Pos;
00166     g_u32loYear  = (RTC->CAL & RTC_CAL_YEAR_Msk) >> RTC_CAL_YEAR_Pos;
00167     g_u32hiMonth = (RTC->CAL & RTC_CAL_TENMON_Msk) >> RTC_CAL_TENMON_Pos;
00168     g_u32loMonth = (RTC->CAL & RTC_CAL_MON_Msk) >> RTC_CAL_MON_Pos;
00169     g_u32hiDay   = (RTC->CAL & RTC_CAL_TENDAY_Msk) >> RTC_CAL_TENDAY_Pos;
00170     g_u32loDay   = (RTC->CAL & RTC_CAL_DAY_Msk) >> RTC_CAL_DAY_Pos;
00171     /* Get [Time digit] data */
00172     g_u32hiHour = (RTC->TIME & RTC_TIME_TENHR_Msk) >> RTC_TIME_TENHR_Pos;
00173     g_u32loHour = (RTC->TIME & RTC_TIME_HR_Msk) >> RTC_TIME_HR_Pos;
00174     g_u32hiMin  = (RTC->TIME & RTC_TIME_TENMIN_Msk) >> RTC_TIME_TENMIN_Pos;
00175     g_u32loMin  = (RTC->TIME & RTC_TIME_MIN_Msk) >> RTC_TIME_MIN_Pos;
00176     g_u32hiSec  = (RTC->TIME & RTC_TIME_TENSEC_Msk) >> RTC_TIME_TENSEC_Pos;
00177     g_u32loSec  = (RTC->TIME & RTC_TIME_SEC_Msk) >> RTC_TIME_SEC_Pos;
00178     /* Compute to 20XX year */
00179     u32Tmp  = (g_u32hiYear * 10ul);
00180     u32Tmp += g_u32loYear;
00181     sPt->u32Year  = u32Tmp + RTC_YEAR2000;
00182     /* Compute 0~12 month */
00183     u32Tmp = (g_u32hiMonth * 10ul);
00184     sPt->u32Month  = u32Tmp + g_u32loMonth;
00185     /* Compute 0~31 day */
00186     u32Tmp = (g_u32hiDay * 10ul);
00187     sPt->u32Day  =  u32Tmp  + g_u32loDay;
00188 
00189     /* Compute 12/24 hour */
00190     if (sPt->u32TimeScale  == RTC_CLOCK_12) {
00191         u32Tmp = (g_u32hiHour * 10ul);
00192         u32Tmp += g_u32loHour;
00193         sPt->u32Hour  = u32Tmp;          /* AM: 1~12. PM: 21~32. */
00194 
00195         if (sPt->u32Hour  >= 21ul) {
00196             sPt->u32AmPm   = RTC_PM;
00197             sPt->u32Hour  -= 20ul;
00198         } else {
00199             sPt->u32AmPm  = RTC_AM;
00200         }
00201 
00202         u32Tmp  = (g_u32hiMin  * 10ul);
00203         u32Tmp += g_u32loMin;
00204         sPt->u32Minute  = u32Tmp;
00205         u32Tmp  = (g_u32hiSec  * 10ul);
00206         u32Tmp += g_u32loSec;
00207         sPt->u32Second  = u32Tmp;
00208     } else {
00209         u32Tmp  = (g_u32hiHour * 10ul);
00210         u32Tmp += g_u32loHour;
00211         sPt->u32Hour  = u32Tmp;
00212         u32Tmp  = (g_u32hiMin * 10ul);
00213         u32Tmp +=  g_u32loMin;
00214         sPt->u32Minute  = u32Tmp;
00215         u32Tmp  = (g_u32hiSec * 10ul);
00216         u32Tmp += g_u32loSec;
00217         sPt->u32Second  = u32Tmp;
00218     }
00219 }
00220 
00221 /**
00222   * @brief      Get RTC Alarm Date and Time
00223   *
00224   * @param[out] sPt     The returned pointer is specified the RTC alarm value. It includes: \n
00225   *                     u32Year: Year value                                                 \n
00226   *                     u32Month: Month value                                               \n
00227   *                     u32Day: Day value                                                   \n
00228   *                     u32DayOfWeek: Day of week                                           \n
00229   *                     u32Hour: Hour value                                                 \n
00230   *                     u32Minute: Minute value                                             \n
00231   *                     u32Second: Second value                                             \n
00232   *                     u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24]                         \n
00233   *                     u8AmPm: [RTC_AM / RTC_PM]                                           \n
00234   *
00235   * @return     None
00236   *
00237   * @details    This API is used to get the RTC alarm date and time setting.
00238   */
00239 void RTC_GetAlarmDateAndTime(S_RTC_TIME_DATA_T  *sPt)
00240 {
00241     uint32_t u32Tmp;
00242     sPt->u32TimeScale  = RTC->CLKFMT & RTC_CLKFMT_24HEN_Msk;     /* 12/24-hour */
00243     sPt->u32DayOfWeek  = RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk; /* Day of the week */
00244     /* Get alarm [Date digit] data */
00245     RTC_WaitAccessEnable();
00246     g_u32hiYear  = (RTC->CALM & RTC_CALM_TENYEAR_Msk) >> RTC_CALM_TENYEAR_Pos;
00247     g_u32loYear  = (RTC->CALM & RTC_CALM_YEAR_Msk) >> RTC_CALM_YEAR_Pos;
00248     g_u32hiMonth = (RTC->CALM & RTC_CALM_TENMON_Msk) >> RTC_CALM_TENMON_Pos;
00249     g_u32loMonth = (RTC->CALM & RTC_CALM_MON_Msk) >> RTC_CALM_MON_Pos;
00250     g_u32hiDay   = (RTC->CALM & RTC_CALM_TENDAY_Msk) >> RTC_CALM_TENDAY_Pos;
00251     g_u32loDay   = (RTC->CALM & RTC_CALM_DAY_Msk) >> RTC_CALM_DAY_Pos;
00252     /* Get alarm [Time digit] data */
00253     RTC_WaitAccessEnable();
00254     g_u32hiHour = (RTC->TALM & RTC_TALM_TENHR_Msk) >> RTC_TALM_TENHR_Pos;
00255     g_u32loHour = (RTC->TALM & RTC_TALM_HR_Msk) >> RTC_TALM_HR_Pos;
00256     g_u32hiMin  = (RTC->TALM & RTC_TALM_TENMIN_Msk) >> RTC_TALM_TENMIN_Pos;
00257     g_u32loMin  = (RTC->TALM & RTC_TALM_MIN_Msk) >> RTC_TALM_MIN_Pos;
00258     g_u32hiSec  = (RTC->TALM & RTC_TALM_TENSEC_Msk) >> RTC_TALM_TENSEC_Pos;
00259     g_u32loSec  = (RTC->TALM & RTC_TALM_SEC_Msk) >> RTC_TALM_SEC_Pos;
00260     /* Compute to 20XX year */
00261     u32Tmp  = (g_u32hiYear * 10ul);
00262     u32Tmp += g_u32loYear;
00263     sPt->u32Year  = u32Tmp + RTC_YEAR2000;
00264     /* Compute 0~12 month */
00265     u32Tmp = (g_u32hiMonth * 10ul);
00266     sPt->u32Month  = u32Tmp + g_u32loMonth;
00267     /* Compute 0~31 day */
00268     u32Tmp = (g_u32hiDay * 10ul);
00269     sPt->u32Day  = u32Tmp + g_u32loDay;
00270 
00271     /* Compute 12/24 hour */
00272     if (sPt->u32TimeScale  == RTC_CLOCK_12) {
00273         u32Tmp  = (g_u32hiHour * 10ul);
00274         u32Tmp += g_u32loHour;
00275         sPt->u32Hour  = u32Tmp;          /* AM: 1~12. PM: 21~32. */
00276 
00277         if (sPt->u32Hour  >= 21ul) {
00278             sPt->u32AmPm   = RTC_PM;
00279             sPt->u32Hour  -= 20ul;
00280         } else {
00281             sPt->u32AmPm  = RTC_AM;
00282         }
00283 
00284         u32Tmp  = (g_u32hiMin * 10ul);
00285         u32Tmp += g_u32loMin;
00286         sPt->u32Minute  = u32Tmp;
00287         u32Tmp  = (g_u32hiSec * 10ul);
00288         u32Tmp += g_u32loSec;
00289         sPt->u32Second  = u32Tmp;
00290     } else {
00291         u32Tmp  = (g_u32hiHour * 10ul);
00292         u32Tmp +=  g_u32loHour;
00293         sPt->u32Hour  = u32Tmp;
00294         u32Tmp  = (g_u32hiMin * 10ul);
00295         u32Tmp += g_u32loMin;
00296         sPt->u32Minute  = u32Tmp;
00297         u32Tmp  = (g_u32hiSec * 10ul);
00298         u32Tmp += g_u32loSec;
00299         sPt->u32Second  = u32Tmp;
00300     }
00301 }
00302 
00303 /**
00304   * @brief      Update Current RTC Date and Time
00305   *
00306   * @param[in]  sPt     Specify the time property and current date and time. It includes:           \n
00307   *                     u32Year: Year value, range between 2000 ~ 2099.                             \n
00308   *                     u32Month: Month value, range between 1 ~ 12.                                \n
00309   *                     u32Day: Day value, range between 1 ~ 31.                                    \n
00310   *                     u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
00311   *                                                     RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
00312   *                                                     RTC_SATURDAY]                               \n
00313   *                     u32Hour: Hour value, range between 0 ~ 23.                                  \n
00314   *                     u32Minute: Minute value, range between 0 ~ 59.                              \n
00315   *                     u32Second: Second value, range between 0 ~ 59.                              \n
00316   *                     u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24]                                 \n
00317   *                     u8AmPm: [RTC_AM / RTC_PM]                                                   \n
00318   *
00319   * @return     None
00320   *
00321   * @details    This API is used to update current date and time to RTC.
00322   */
00323 void RTC_SetDateAndTime(S_RTC_TIME_DATA_T  *sPt)
00324 {
00325     uint32_t u32RegCAL, u32RegTIME;
00326 
00327     if (sPt == 0ul) {
00328     } else {
00329         /*-----------------------------------------------------------------------------------------------------*/
00330         /* Set RTC 24/12 hour setting and Day of the Week                                                      */
00331         /*-----------------------------------------------------------------------------------------------------*/
00332         RTC_WaitAccessEnable();
00333 
00334         if (sPt->u32TimeScale  == RTC_CLOCK_12) {
00335             RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
00336 
00337             /*-------------------------------------------------------------------------------------------------*/
00338             /* Important, range of 12-hour PM mode is 21 up to 32                                               */
00339             /*-------------------------------------------------------------------------------------------------*/
00340             if (sPt->u32AmPm  == RTC_PM) {
00341                 sPt->u32Hour  += 20ul;
00342             }
00343         } else {
00344             RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
00345         }
00346 
00347         /* Set Day of the Week */
00348         RTC_WaitAccessEnable();
00349         RTC->WEEKDAY = sPt->u32DayOfWeek ;
00350         /*-----------------------------------------------------------------------------------------------------*/
00351         /* Set RTC Current Date and Time                                                                       */
00352         /*-----------------------------------------------------------------------------------------------------*/
00353         u32RegCAL  = ((sPt->u32Year  - RTC_YEAR2000) / 10ul) << 20;
00354         u32RegCAL |= (((sPt->u32Year  - RTC_YEAR2000) % 10ul) << 16);
00355         u32RegCAL |= ((sPt->u32Month   / 10ul) << 12);
00356         u32RegCAL |= ((sPt->u32Month   % 10ul) << 8);
00357         u32RegCAL |= ((sPt->u32Day     / 10ul) << 4);
00358         u32RegCAL |= (sPt->u32Day      % 10ul);
00359         u32RegTIME  = ((sPt->u32Hour    / 10ul) << 20);
00360         u32RegTIME |= ((sPt->u32Hour    % 10ul) << 16);
00361         u32RegTIME |= ((sPt->u32Minute  / 10ul) << 12);
00362         u32RegTIME |= ((sPt->u32Minute  % 10ul) << 8);
00363         u32RegTIME |= ((sPt->u32Second  / 10ul) << 4);
00364         u32RegTIME |= (sPt->u32Second  % 10ul);
00365         /*-----------------------------------------------------------------------------------------------------*/
00366         /* Set RTC Calender and Time Loading                                                                   */
00367         /*-----------------------------------------------------------------------------------------------------*/
00368         RTC_WaitAccessEnable();
00369         RTC->CAL  = (uint32_t)u32RegCAL;
00370         RTC_WaitAccessEnable();
00371         RTC->TIME = (uint32_t)u32RegTIME;
00372     }
00373 }
00374 
00375 /**
00376   * @brief      Update RTC Alarm Date and Time
00377   *
00378   * @param[in]  sPt     Specify the time property and alarm date and time. It includes:             \n
00379   *                     u32Year: Year value, range between 2000 ~ 2099.                             \n
00380   *                     u32Month: Month value, range between 1 ~ 12.                                \n
00381   *                     u32Day: Day value, range between 1 ~ 31.                                    \n
00382   *                     u32DayOfWeek: Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
00383   *                                                     RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
00384   *                                                     RTC_SATURDAY]                               \n
00385   *                     u32Hour: Hour value, range between 0 ~ 23.                                  \n
00386   *                     u32Minute: Minute value, range between 0 ~ 59.                              \n
00387   *                     u32Second: Second value, range between 0 ~ 59.                              \n
00388   *                     u32TimeScale: [RTC_CLOCK_12 / RTC_CLOCK_24]                                 \n
00389   *                     u8AmPm: [RTC_AM / RTC_PM]                                                   \n
00390   *
00391   * @return     None
00392   *
00393   * @details    This API is used to update alarm date and time setting to RTC.
00394   */
00395 void RTC_SetAlarmDateAndTime(S_RTC_TIME_DATA_T  *sPt)
00396 {
00397     uint32_t u32RegCALM, u32RegTALM;
00398 
00399     if (sPt == 0) {
00400     } else {
00401         /*-----------------------------------------------------------------------------------------------------*/
00402         /* Set RTC 24/12 hour setting and Day of the Week                                                      */
00403         /*-----------------------------------------------------------------------------------------------------*/
00404         RTC_WaitAccessEnable();
00405 
00406         if (sPt->u32TimeScale  == RTC_CLOCK_12) {
00407             RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
00408 
00409             /*-------------------------------------------------------------------------------------------------*/
00410             /* Important, range of 12-hour PM mode is 21 up to 32                                               */
00411             /*-------------------------------------------------------------------------------------------------*/
00412             if (sPt->u32AmPm  == RTC_PM) {
00413                 sPt->u32Hour  += 20ul;
00414             }
00415         } else {
00416             RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
00417         }
00418 
00419         /*-----------------------------------------------------------------------------------------------------*/
00420         /* Set RTC Alarm Date and Time                                                                         */
00421         /*-----------------------------------------------------------------------------------------------------*/
00422         u32RegCALM  = ((sPt->u32Year  - RTC_YEAR2000) / 10ul) << 20;
00423         u32RegCALM |= (((sPt->u32Year  - RTC_YEAR2000) % 10ul) << 16);
00424         u32RegCALM |= ((sPt->u32Month   / 10ul) << 12);
00425         u32RegCALM |= ((sPt->u32Month   % 10ul) << 8);
00426         u32RegCALM |= ((sPt->u32Day     / 10ul) << 4);
00427         u32RegCALM |= (sPt->u32Day     % 10ul);
00428         u32RegTALM  = ((sPt->u32Hour    / 10ul) << 20);
00429         u32RegTALM |= ((sPt->u32Hour    % 10ul) << 16);
00430         u32RegTALM |= ((sPt->u32Minute  / 10ul) << 12);
00431         u32RegTALM |= ((sPt->u32Minute  % 10ul) << 8);
00432         u32RegTALM |= ((sPt->u32Second  / 10ul) << 4);
00433         u32RegTALM |= (sPt->u32Second  % 10ul);
00434         RTC_WaitAccessEnable();
00435         RTC->CALM = (uint32_t)u32RegCALM;
00436         RTC_WaitAccessEnable();
00437         RTC->TALM = (uint32_t)u32RegTALM;
00438     }
00439 }
00440 
00441 /**
00442   * @brief      Update RTC Current Date
00443   *
00444   * @param[in]  u32Year         The year calendar digit of current RTC setting.
00445   * @param[in]  u32Month        The month calendar digit of current RTC setting.
00446   * @param[in]  u32Day          The day calendar digit of current RTC setting.
00447   * @param[in]  u32DayOfWeek    The Day of the week. [RTC_SUNDAY / RTC_MONDAY / RTC_TUESDAY /
00448   *                                                   RTC_WEDNESDAY / RTC_THURSDAY / RTC_FRIDAY /
00449   *                                                   RTC_SATURDAY]
00450   *
00451   * @return     None
00452   *
00453   * @details    This API is used to update current date to RTC.
00454   */
00455 void RTC_SetDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day, uint32_t u32DayOfWeek)
00456 {
00457     uint32_t u32RegCAL;
00458     u32RegCAL  = ((u32Year - RTC_YEAR2000) / 10ul) << 20;
00459     u32RegCAL |= (((u32Year - RTC_YEAR2000) % 10ul) << 16);
00460     u32RegCAL |= ((u32Month / 10ul) << 12);
00461     u32RegCAL |= ((u32Month % 10ul) << 8);
00462     u32RegCAL |= ((u32Day   / 10ul) << 4);
00463     u32RegCAL |= (u32Day   % 10ul);
00464     /* Set Day of the Week */
00465     RTC_WaitAccessEnable();
00466     RTC->WEEKDAY = u32DayOfWeek & RTC_WEEKDAY_WEEKDAY_Msk;
00467     /* Set RTC Calender Loading */
00468     RTC_WaitAccessEnable();
00469     RTC->CAL = (uint32_t)u32RegCAL;
00470 }
00471 
00472 /**
00473   * @brief      Update RTC Current Time
00474   *
00475   * @param[in]  u32Hour         The hour time digit of current RTC setting.
00476   * @param[in]  u32Minute       The minute time digit of current RTC setting.
00477   * @param[in]  u32Second       The second time digit of current RTC setting.
00478   * @param[in]  u32TimeMode     The 24-Hour / 12-Hour Time Scale Selection. [RTC_CLOCK_12 / RTC_CLOCK_24]
00479   * @param[in]  u32AmPm         12-hour time scale with AM and PM indication. Only Time Scale select 12-hour used. [RTC_AM / RTC_PM]
00480   *
00481   * @return     None
00482   *
00483   * @details    This API is used to update current time to RTC.
00484   */
00485 void RTC_SetTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
00486 {
00487     uint32_t u32RegTIME;
00488 
00489     /* Important, range of 12-hour PM mode is 21 up to 32 */
00490     if ((u32TimeMode == RTC_CLOCK_12) && (u32AmPm == RTC_PM)) {
00491         u32Hour += 20ul;
00492     }
00493 
00494     u32RegTIME  = ((u32Hour   / 10ul) << 20);
00495     u32RegTIME |= ((u32Hour   % 10ul) << 16);
00496     u32RegTIME |= ((u32Minute / 10ul) << 12);
00497     u32RegTIME |= ((u32Minute % 10ul) << 8);
00498     u32RegTIME |= ((u32Second / 10ul) << 4);
00499     u32RegTIME |= (u32Second % 10ul);
00500     /*-----------------------------------------------------------------------------------------------------*/
00501     /* Set RTC 24/12 hour setting and Day of the Week                                                      */
00502     /*-----------------------------------------------------------------------------------------------------*/
00503     RTC_WaitAccessEnable();
00504 
00505     if (u32TimeMode == RTC_CLOCK_12) {
00506         RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
00507     } else {
00508         RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
00509     }
00510 
00511     RTC_WaitAccessEnable();
00512     RTC->TIME = (uint32_t)u32RegTIME;
00513 }
00514 
00515 /**
00516   * @brief      Update RTC Alarm Date
00517   *
00518   * @param[in]  u32Year         The year calendar digit of RTC alarm setting.
00519   * @param[in]  u32Month        The month calendar digit of RTC alarm setting.
00520   * @param[in]  u32Day          The day calendar digit of RTC alarm setting.
00521   *
00522   * @return     None
00523   *
00524   * @details    This API is used to update alarm date setting to RTC.
00525   */
00526 void RTC_SetAlarmDate(uint32_t u32Year, uint32_t u32Month, uint32_t u32Day)
00527 {
00528     uint32_t u32RegCALM;
00529     u32RegCALM  = ((u32Year - RTC_YEAR2000) / 10ul) << 20;
00530     u32RegCALM |= (((u32Year - RTC_YEAR2000) % 10ul) << 16);
00531     u32RegCALM |= ((u32Month / 10ul) << 12);
00532     u32RegCALM |= ((u32Month % 10ul) << 8);
00533     u32RegCALM |= ((u32Day   / 10ul) << 4);
00534     u32RegCALM |= (u32Day   % 10ul);
00535     RTC_WaitAccessEnable();
00536     /* Set RTC Alarm Date */
00537     RTC->CALM = (uint32_t)u32RegCALM;
00538 }
00539 
00540 /**
00541   * @brief      Update RTC Alarm Time
00542   *
00543   * @param[in]  u32Hour         The hour time digit of RTC alarm setting.
00544   * @param[in]  u32Minute       The minute time digit of RTC alarm setting.
00545   * @param[in]  u32Second       The second time digit of RTC alarm setting.
00546   * @param[in]  u32TimeMode     The 24-Hour / 12-Hour Time Scale Selection. [RTC_CLOCK_12 / RTC_CLOCK_24]
00547   * @param[in]  u32AmPm         12-hour time scale with AM and PM indication. Only Time Scale select 12-hour used. [RTC_AM / RTC_PM]
00548   *
00549   * @return     None
00550   *
00551   * @details    This API is used to update alarm time setting to RTC.
00552   */
00553 void RTC_SetAlarmTime(uint32_t u32Hour, uint32_t u32Minute, uint32_t u32Second, uint32_t u32TimeMode, uint32_t u32AmPm)
00554 {
00555     uint32_t u32RegTALM;
00556 
00557     /* Important, range of 12-hour PM mode is 21 up to 32 */
00558     if ((u32TimeMode == RTC_CLOCK_12) && (u32AmPm == RTC_PM)) {
00559         u32Hour += 20ul;
00560     }
00561 
00562     u32RegTALM  = ((u32Hour   / 10ul) << 20);
00563     u32RegTALM |= ((u32Hour   % 10ul) << 16);
00564     u32RegTALM |= ((u32Minute / 10ul) << 12);
00565     u32RegTALM |= ((u32Minute % 10ul) << 8);
00566     u32RegTALM |= ((u32Second / 10ul) << 4);
00567     u32RegTALM |= (u32Second % 10ul);
00568     /*-----------------------------------------------------------------------------------------------------*/
00569     /* Set RTC 24/12 hour setting and Day of the Week                                                      */
00570     /*-----------------------------------------------------------------------------------------------------*/
00571     RTC_WaitAccessEnable();
00572 
00573     if (u32TimeMode == RTC_CLOCK_12) {
00574         RTC->CLKFMT &= ~RTC_CLKFMT_24HEN_Msk;
00575     } else {
00576         RTC->CLKFMT |= RTC_CLKFMT_24HEN_Msk;
00577     }
00578 
00579     /* Set RTC Alarm Time */
00580     RTC_WaitAccessEnable();
00581     RTC->TALM = (uint32_t)u32RegTALM;
00582 }
00583 
00584 /**
00585   * @brief      Set RTC Alarm Date Mask Function
00586   *
00587   * @param[in]  u8IsTenYMsk     1: enable 10-Year digit alarm mask; 0: disabled.
00588   * @param[in]  u8IsYMsk        1: enable 1-Year digit alarm mask; 0: disabled.
00589   * @param[in]  u8IsTenMMsk     1: enable 10-Mon digit alarm mask; 0: disabled.
00590   * @param[in]  u8IsMMsk        1: enable 1-Mon digit alarm mask; 0: disabled.
00591   * @param[in]  u8IsTenDMsk     1: enable 10-Day digit alarm mask; 0: disabled.
00592   * @param[in]  u8IsDMsk        1: enable 1-Day digit alarm mask; 0: disabled.
00593   *
00594   * @return     None
00595   *
00596   * @details    This API is used to enable or disable RTC alarm date mask function.
00597   */
00598 void RTC_SetAlarmDateMask(uint8_t u8IsTenYMsk, uint8_t u8IsYMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenDMsk, uint8_t u8IsDMsk)
00599 {
00600     RTC_WaitAccessEnable();
00601     RTC->CAMSK = ((uint32_t)u8IsTenYMsk << RTC_CAMSK_MTENYEAR_Pos) |
00602                  ((uint32_t)u8IsYMsk    << RTC_CAMSK_MYEAR_Pos) |
00603                  ((uint32_t)u8IsTenMMsk << RTC_CAMSK_MTENMON_Pos) |
00604                  ((uint32_t)u8IsMMsk    << RTC_CAMSK_MMON_Pos) |
00605                  ((uint32_t)u8IsTenDMsk << RTC_CAMSK_MTENDAY_Pos) |
00606                  ((uint32_t)u8IsDMsk    << RTC_CAMSK_MDAY_Pos);
00607 }
00608 
00609 /**
00610   * @brief      Set RTC Alarm Time Mask Function
00611   *
00612   * @param[in]  u8IsTenHMsk     1: enable 10-Hour digit alarm mask; 0: disabled.
00613   * @param[in]  u8IsHMsk        1: enable 1-Hour digit alarm mask; 0: disabled.
00614   * @param[in]  u8IsTenMMsk     1: enable 10-Min digit alarm mask; 0: disabled.
00615   * @param[in]  u8IsMMsk        1: enable 1-Min digit alarm mask; 0: disabled.
00616   * @param[in]  u8IsTenSMsk     1: enable 10-Sec digit alarm mask; 0: disabled.
00617   * @param[in]  u8IsSMsk        1: enable 1-Sec digit alarm mask; 0: disabled.
00618   *
00619   * @return     None
00620   *
00621   * @details    This API is used to enable or disable RTC alarm time mask function.
00622   */
00623 void RTC_SetAlarmTimeMask(uint8_t u8IsTenHMsk, uint8_t u8IsHMsk, uint8_t u8IsTenMMsk, uint8_t u8IsMMsk, uint8_t u8IsTenSMsk, uint8_t u8IsSMsk)
00624 {
00625     RTC_WaitAccessEnable();
00626     RTC->TAMSK = ((uint32_t)u8IsTenHMsk << RTC_TAMSK_MTENHR_Pos) |
00627                  ((uint32_t)u8IsHMsk    << RTC_TAMSK_MHR_Pos) |
00628                  ((uint32_t)u8IsTenMMsk << RTC_TAMSK_MTENMIN_Pos) |
00629                  ((uint32_t)u8IsMMsk    << RTC_TAMSK_MMIN_Pos) |
00630                  ((uint32_t)u8IsTenSMsk << RTC_TAMSK_MTENSEC_Pos) |
00631                  ((uint32_t)u8IsSMsk    << RTC_TAMSK_MSEC_Pos);
00632 }
00633 
00634 /**
00635   * @brief      Get Day of the Week
00636   *
00637   * @param      None
00638   *
00639   * @retval     0   Sunday
00640   * @retval     1   Monday
00641   * @retval     2   Tuesday
00642   * @retval     3   Wednesday
00643   * @retval     4   Thursday
00644   * @retval     5   Friday
00645   * @retval     6   Saturday
00646   *
00647   * @details    This API is used to get day of the week of current RTC date.
00648   */
00649 uint32_t RTC_GetDayOfWeek(void)
00650 {
00651     return (RTC->WEEKDAY & RTC_WEEKDAY_WEEKDAY_Msk);
00652 }
00653 
00654 /**
00655   * @brief      Set RTC Tick Period Time
00656   *
00657   * @param[in]  u32TickSelection    It is used to set the RTC tick period time for Periodic Time Tick request. \n
00658   *                                 It consists of:
00659   *                                     - \ref RTC_TICK_1_SEC     : Time tick is 1 second
00660   *                                     - \ref RTC_TICK_1_2_SEC   : Time tick is 1/2 second
00661   *                                     - \ref RTC_TICK_1_4_SEC   : Time tick is 1/4 second
00662   *                                     - \ref RTC_TICK_1_8_SEC   : Time tick is 1/8 second
00663   *                                     - \ref RTC_TICK_1_16_SEC  : Time tick is 1/16 second
00664   *                                     - \ref RTC_TICK_1_32_SEC  : Time tick is 1/32 second
00665   *                                     - \ref RTC_TICK_1_64_SEC  : Time tick is 1/64 second
00666   *                                     - \ref RTC_TICK_1_128_SEC : Time tick is 1/128 second
00667   *
00668   * @return     None
00669   *
00670   * @details    This API is used to set RTC tick period time for each tick interrupt.
00671   */
00672 void RTC_SetTickPeriod(uint32_t u32TickSelection)
00673 {
00674     RTC_WaitAccessEnable();
00675     RTC->TICK = (RTC->TICK & ~RTC_TICK_TICK_Msk) | u32TickSelection;
00676 }
00677 
00678 /**
00679   * @brief      Enable RTC Interrupt
00680   *
00681   * @param[in]  u32IntFlagMask      Specify the interrupt source. It consists of:
00682   *                                     - \ref RTC_INTEN_ALMIEN_Msk   : Alarm interrupt
00683   *                                     - \ref RTC_INTEN_TICKIEN_Msk  : Tick interrupt
00684   *                                     - \ref RTC_INTEN_TAMP0IEN_Msk : Tamper 0 Pin Event Detection interrupt
00685   *                                     - \ref RTC_INTEN_TAMP1IEN_Msk : Tamper 1 Pin Event Detection interrupt
00686   *                                     - \ref RTC_INTEN_TAMP2IEN_Msk : Tamper 2 Pin Event Detection interrupt
00687   *                                     - \ref RTC_INTEN_TAMP3IEN_Msk : Tamper 3 Pin Event Detection interrupt
00688   *                                     - \ref RTC_INTEN_TAMP4IEN_Msk : Tamper 4 Pin Event Detection interrupt
00689   *                                     - \ref RTC_INTEN_TAMP5IEN_Msk : Tamper 5 Pin Event Detection interrupt
00690   *
00691   * @return     None
00692   *
00693   * @details    This API is used to enable the specify RTC interrupt function.
00694   */
00695 void RTC_EnableInt(uint32_t u32IntFlagMask)
00696 {
00697     RTC_WaitAccessEnable();
00698     RTC->INTEN |= u32IntFlagMask;
00699 }
00700 
00701 /**
00702   * @brief      Disable RTC Interrupt
00703   *
00704   * @param[in]  u32IntFlagMask      Specify the interrupt source. It consists of:
00705   *                                     - \ref RTC_INTEN_ALMIEN_Msk   : Alarm interrupt
00706   *                                     - \ref RTC_INTEN_TICKIEN_Msk  : Tick interrupt
00707   *                                     - \ref RTC_INTEN_TAMP0IEN_Msk : Tamper 0 Pin Event Detection interrupt
00708   *                                     - \ref RTC_INTEN_TAMP1IEN_Msk : Tamper 1 Pin Event Detection interrupt
00709   *                                     - \ref RTC_INTEN_TAMP2IEN_Msk : Tamper 2 Pin Event Detection interrupt
00710   *                                     - \ref RTC_INTEN_TAMP3IEN_Msk : Tamper 3 Pin Event Detection interrupt
00711   *                                     - \ref RTC_INTEN_TAMP4IEN_Msk : Tamper 4 Pin Event Detection interrupt
00712   *                                     - \ref RTC_INTEN_TAMP5IEN_Msk : Tamper 5 Pin Event Detection interrupt
00713   *
00714   * @return     None
00715   *
00716   * @details    This API is used to disable the specify RTC interrupt function.
00717   */
00718 void RTC_DisableInt(uint32_t u32IntFlagMask)
00719 {
00720     RTC_WaitAccessEnable();
00721     RTC->INTEN  &= ~u32IntFlagMask;
00722     RTC_WaitAccessEnable();
00723     RTC->INTSTS = u32IntFlagMask;
00724 }
00725 
00726 /**
00727   * @brief      Enable Spare Registers Access
00728   *
00729   * @param      None
00730   *
00731   * @return     None
00732   *
00733   * @details    This API is used to enable the spare registers 0~19 can be accessed.
00734   */
00735 void RTC_EnableSpareAccess(void)
00736 {
00737     RTC_WaitAccessEnable();
00738     RTC->SPRCTL |= RTC_SPRCTL_SPRRWEN_Msk;
00739 }
00740 
00741 /**
00742   * @brief      Disable Spare Register
00743   *
00744   * @param      None
00745   *
00746   * @return     None
00747   *
00748   * @details    This API is used to disable the spare register 0~19 cannot be accessed.
00749   */
00750 void RTC_DisableSpareRegister(void)
00751 {
00752     RTC_WaitAccessEnable();
00753     RTC->SPRCTL &= ~RTC_SPRCTL_SPRRWEN_Msk;
00754 }
00755 
00756 /**
00757   * @brief      Static Tamper Detect
00758   *
00759   * @param[in]  u32TamperSelect     Tamper pin select. Possible options are
00760   *                                 - \ref RTC_TAMPER5_SELECT
00761   *                                 - \ref RTC_TAMPER4_SELECT
00762   *                                 - \ref RTC_TAMPER3_SELECT
00763   *                                 - \ref RTC_TAMPER2_SELECT
00764   *                                 - \ref RTC_TAMPER1_SELECT
00765   *                                 - \ref RTC_TAMPER0_SELECT
00766   *
00767   * @param[in]  u32DetecLevel       Tamper pin detection level select. Possible options are
00768   *                                 - \ref RTC_TAMPER_HIGH_LEVEL_DETECT
00769   *                                 - \ref RTC_TAMPER_LOW_LEVEL_DETECT
00770   *
00771   * @param[in]  u32DebounceEn       Tamper pin de-bounce enable
00772   *                                 - \ref RTC_TAMPER_DEBOUNCE_ENABLE
00773   *                                 - \ref RTC_TAMPER_DEBOUNCE_DISABLE
00774   *
00775   * @return     None
00776   *
00777   * @details    This API is used to enable the tamper pin detect function with specify trigger condition.
00778   */
00779 void RTC_StaticTamperEnable(uint32_t u32TamperSelect, uint32_t u32DetecLevel, uint32_t u32DebounceEn)
00780 {
00781     uint32_t i;
00782     uint32_t u32Reg;
00783     uint32_t u32TmpReg;
00784     RTC_WaitAccessEnable();
00785     u32Reg = RTC->TAMPCTL;
00786     u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | (u32DetecLevel << RTC_TAMPCTL_TAMP0LV_Pos) |
00787                  (u32DebounceEn << RTC_TAMPCTL_TAMP0DBEN_Pos));
00788 
00789     for (i = 0ul; i < MAX_TAMPER_PIN_NUM; i++) {
00790         if (u32TamperSelect & (0x1ul << i)) {
00791             u32Reg &= ~((RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP0LV_Msk | RTC_TAMPCTL_TAMP0DBEN_Msk) << (i * 4ul));
00792             u32Reg |= (u32TmpReg << (i * 4ul));
00793         }
00794     }
00795 
00796     RTC_WaitAccessEnable();
00797     RTC->TAMPCTL = u32Reg;
00798 }
00799 
00800 /**
00801   * @brief      Static Tamper Disable
00802   *
00803   * @param[in]  u32TamperSelect     Tamper pin select. Possible options are
00804   *                                 - \ref RTC_TAMPER5_SELECT
00805   *                                 - \ref RTC_TAMPER4_SELECT
00806   *                                 - \ref RTC_TAMPER3_SELECT
00807   *                                 - \ref RTC_TAMPER2_SELECT
00808   *                                 - \ref RTC_TAMPER1_SELECT
00809   *                                 - \ref RTC_TAMPER0_SELECT
00810   *
00811   * @return     None
00812   *
00813   * @details    This API is used to disable the static tamper pin detect.
00814   */
00815 void RTC_StaticTamperDisable(uint32_t u32TamperSelect)
00816 {
00817     uint32_t i;
00818     uint32_t u32Reg;
00819     uint32_t u32TmpReg;
00820     RTC_WaitAccessEnable();
00821     u32Reg = RTC->TAMPCTL;
00822     u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk);
00823 
00824     for (i = 0ul; i < MAX_TAMPER_PIN_NUM; i++) {
00825         if (u32TamperSelect & (0x1ul << i)) {
00826             u32Reg &= ~(u32TmpReg << (i * 4ul));
00827         }
00828     }
00829 
00830     RTC_WaitAccessEnable();
00831     RTC->TAMPCTL = u32Reg;
00832 }
00833 
00834 /**
00835   * @brief      Dynamic Tamper Detect
00836   *
00837   * @param[in]  u32PairSel          Tamper pin detection enable. Possible options are
00838   *                                 - \ref RTC_PAIR0_SELECT
00839   *                                 - \ref RTC_PAIR1_SELECT
00840   *                                 - \ref RTC_PAIR2_SELECT
00841   *
00842   * @param[in]  u32DebounceEn       Tamper pin de-bounce enable
00843   *                                 - \ref RTC_TAMPER_DEBOUNCE_ENABLE
00844   *                                 - \ref RTC_TAMPER_DEBOUNCE_DISABLE
00845   *
00846   *  @param[in]  u32Pair1Source     Dynamic Pair 1 Input Source Select
00847   *                                 0: Pair 1 source select tamper 2
00848   *                                 1: Pair 1 source select tamper 0
00849   *
00850   *  @param[in]  u32Pair2Source     Dynamic Pair 2 Input Source Select
00851   *                                 0: Pair 2 source select tamper 4
00852   *                                 1: Pair 2 source select tamper 0
00853   *
00854   * @return     None
00855   *
00856   * @details    This API is used to enable the dynamic tamper.
00857   */
00858 void RTC_DynamicTamperEnable(uint32_t u32PairSel, uint32_t u32DebounceEn, uint32_t u32Pair1Source, uint32_t u32Pair2Source)
00859 {
00860     uint32_t i;
00861     uint32_t u32Reg;
00862     uint32_t u32TmpReg;
00863     uint32_t u32Tamper2Debounce, u32Tamper4Debounce;
00864     RTC_WaitAccessEnable();
00865     u32Reg = RTC->TAMPCTL;
00866     u32Tamper2Debounce = u32Reg & RTC_TAMPCTL_TAMP2DBEN_Msk;
00867     u32Tamper4Debounce = u32Reg & RTC_TAMPCTL_TAMP4DBEN_Msk;
00868     u32Reg &= ~(RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_TAMP2EN_Msk |
00869                 RTC_TAMPCTL_TAMP3EN_Msk | RTC_TAMPCTL_TAMP4EN_Msk | RTC_TAMPCTL_TAMP5EN_Msk);
00870     u32Reg &= ~(RTC_TAMPCTL_DYN1ISS_Msk | RTC_TAMPCTL_DYN2ISS_Msk);
00871     u32Reg |= ((u32Pair1Source & 0x1ul) << RTC_TAMPCTL_DYN1ISS_Pos) | ((u32Pair2Source & 0x1ul) << RTC_TAMPCTL_DYN2ISS_Pos);
00872 
00873     if (u32DebounceEn) {
00874         u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk |
00875                      RTC_TAMPCTL_TAMP0DBEN_Msk | RTC_TAMPCTL_TAMP1DBEN_Msk | RTC_TAMPCTL_DYNPR0EN_Msk);
00876     } else {
00877         u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_DYNPR0EN_Msk);
00878     }
00879 
00880     for (i = 0ul; i < MAX_PAIR_NUM; i++) {
00881         if (u32PairSel & (0x1ul << i)) {
00882             u32Reg &= ~((RTC_TAMPCTL_TAMP0DBEN_Msk | RTC_TAMPCTL_TAMP1DBEN_Msk) << (i * 8ul));
00883             u32Reg |= (u32TmpReg << (i * 8ul));
00884         }
00885     }
00886 
00887     if ((u32Pair1Source) && (u32PairSel & RTC_PAIR1_SELECT)) {
00888         u32Reg &= ~RTC_TAMPCTL_TAMP2EN_Msk;
00889         u32Reg |= u32Tamper2Debounce;
00890     }
00891 
00892     if ((u32Pair2Source) && (u32PairSel & RTC_PAIR2_SELECT)) {
00893         u32Reg &= ~RTC_TAMPCTL_TAMP4EN_Msk;
00894         u32Reg |= u32Tamper4Debounce;
00895     }
00896 
00897     RTC_WaitAccessEnable();
00898     RTC->TAMPCTL = u32Reg;
00899 }
00900 
00901 /**
00902   * @brief      Dynamic Tamper Disable
00903   *
00904   * @param[in]  u32PairSel          Tamper pin detection enable. Possible options are
00905   *                                 - \ref RTC_PAIR0_SELECT
00906   *                                 - \ref RTC_PAIR1_SELECT
00907   *                                 - \ref RTC_PAIR2_SELECT
00908   *
00909   * @return     None
00910   *
00911   * @details    This API is used to disable the dynamic tamper.
00912   */
00913 void RTC_DynamicTamperDisable(uint32_t u32PairSel)
00914 {
00915     uint32_t i;
00916     uint32_t u32Reg;
00917     uint32_t u32TmpReg;
00918     uint32_t u32Tamper2En = 0ul, u32Tamper4En = 0ul;
00919     RTC_WaitAccessEnable();
00920     u32Reg = RTC->TAMPCTL;
00921 
00922     if ((u32Reg & RTC_TAMPCTL_DYN1ISS_Msk) && (u32PairSel & RTC_PAIR1_SELECT)) {
00923         u32Tamper2En = u32Reg & RTC_TAMPCTL_TAMP2EN_Msk;
00924     }
00925 
00926     if ((u32Reg & RTC_TAMPCTL_DYN2ISS_Msk) && (u32PairSel & RTC_PAIR2_SELECT)) {
00927         u32Tamper4En = u32Reg & RTC_TAMPCTL_TAMP4EN_Msk;
00928     }
00929 
00930     u32TmpReg = (RTC_TAMPCTL_TAMP0EN_Msk | RTC_TAMPCTL_TAMP1EN_Msk | RTC_TAMPCTL_DYNPR0EN_Msk);
00931 
00932     for (i = 0ul; i < MAX_PAIR_NUM; i++) {
00933         if (u32PairSel & (0x1ul << i)) {
00934             u32Reg &= ~(u32TmpReg << ((i * 8ul)));
00935         }
00936     }
00937 
00938     u32Reg |= (u32Tamper2En | u32Tamper4En);
00939     RTC_WaitAccessEnable();
00940     RTC->TAMPCTL = u32Reg;
00941 }
00942 
00943 /**
00944   * @brief      Config dynamic tamper
00945   *
00946   * @param[in]  u32ChangeRate       The dynamic tamper output change rate
00947   *                                 - \ref RTC_2POW10_CLK
00948   *                                 - \ref RTC_2POW11_CLK
00949   *                                 - \ref RTC_2POW12_CLK
00950   *                                 - \ref RTC_2POW13_CLK
00951   *                                 - \ref RTC_2POW14_CLK
00952   *                                 - \ref RTC_2POW15_CLK
00953   *                                 - \ref RTC_2POW16_CLK
00954   *                                 - \ref RTC_2POW17_CLK
00955   *
00956   * @param[in]  u32SeedReload       Reload new seed or not
00957   *                                 0: not reload new seed
00958   *                                 1: reload new seed
00959   *
00960   * @param[in]  u32RefPattern       Reference pattern
00961   *                                 - \ref REF_RANDOM_PATTERN
00962   *                                 - \ref REF_PREVIOUS_PATTERN
00963   *                                 - \ref REF_SEED
00964   *
00965   * @param[in]  u32Seed             Seed Value (0x0 ~ 0xFFFFFFFF)
00966   *
00967   * @return     None
00968   *
00969   * @details    This API is used to config dynamic tamper setting.
00970   */
00971 void RTC_DynamicTamperConfig(uint32_t u32ChangeRate, uint32_t u32SeedReload, uint32_t u32RefPattern, uint32_t u32Seed)
00972 {
00973     uint32_t u32Reg;
00974     RTC_WaitAccessEnable();
00975     u32Reg = RTC->TAMPCTL;
00976     u32Reg &= ~(RTC_TAMPCTL_DYNSRC_Msk | RTC_TAMPCTL_SEEDRLD_Msk | RTC_TAMPCTL_DYNRATE_Msk);
00977     u32Reg |= (u32ChangeRate) | ((u32SeedReload & 0x1ul) << RTC_TAMPCTL_SEEDRLD_Pos) |
00978               ((u32RefPattern & 0x3ul) << RTC_TAMPCTL_DYNSRC_Pos);
00979     RTC_WaitAccessEnable();
00980     RTC->TAMPSEED = u32Seed; /* need set seed value before re-load seed */
00981     RTC_WaitAccessEnable();
00982     RTC->TAMPCTL = u32Reg;
00983 }
00984 
00985 /*@}*/ /* end of group RTC_EXPORTED_FUNCTIONS */
00986 
00987 /*@}*/ /* end of group RTC_Driver */
00988 
00989 /*@}*/ /* end of group Standard_Driver */
00990 
00991 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/