1 /** 2 ****************************************************************************** 3 * @file rtl8721d_rtc.h 4 * @author 5 * @version V1.0.0 6 * @date 2016-05-17 7 * @brief This file contains all the functions prototypes for the RTC firmware 8 * library. 9 ****************************************************************************** 10 * @attention 11 * 12 * This module is a confidential and proprietary property of RealTek and 13 * possession or use of this module requires written permission of RealTek. 14 * 15 * Copyright(c) 2015, Realtek Semiconductor Corporation. All rights reserved. 16 ****************************************************************************** 17 */ 18 19 #ifndef _RTL8721D_RTC_H_ 20 #define _RTL8721D_RTC_H_ 21 22 /** @addtogroup AmebaD_Periph_Driver 23 * @{ 24 */ 25 26 /** @defgroup RTC 27 * @brief RTC driver modules 28 * @{ 29 */ 30 31 /** @addtogroup RTC 32 * @verbatim 33 ***************************************************************************************** 34 * Introduction 35 ***************************************************************************************** 36 * The real-time clock (RTC) is an independent BCD timer/counter. 37 * One 32-bit register contains the seconds, minutes, hours (12 or 24-hour format) expressed in binary coded decimal format (BCD). 38 * and days expressed in binary format. 39 * Daylight saving time compensation can also be performed. 40 * One programmable alarm contains seconds, minutes, hours and days with interrupt function, 41 * and can be triggered by any combination of the time fields. 42 * A digital calibration feature is available to compensate for some deviation. 43 * After backup domain reset, all RTC registers are protected against possible parasitic 44 * write accesses. 45 * Support a programmable register for RTC trigger SDM32K periodical calibration with active and power save mode. 46 * As long as the supply voltage remains in the operating range, the RTC never stops, 47 * regardless of the device status (Run mode, low power mode or under reset). 48 * 49 ***************************************************************************************** 50 * How to use RTC 51 ***************************************************************************************** 52 * To use the RTC, the following steps are mandatory: 53 * 54 * 1. Initialize the RTC Clock Source. 55 * RCC_PeriphClockSource_RTC() 56 * 57 * 2. Fill the variable RTC_InitStruct with default parameters, or set the desired parameters manually: 58 * RTC_StructInit(&RTC_InitStruct) 59 * 60 * 3. configure the RTC peripheral with the corresponding configurations contained in RTC_InitStruct: 61 * Call RTC_Init(&RTC_InitStruct) 62 * 63 * 4. Fill the variable RTC_TimeStruct with default parameters() (Time = 00d:00h:00min:00sec) 64 * RTC_TimeStructInit(&RTC_TimeStruct) 65 * setting the desired calendar time parameters manually. 66 * 67 * 5. Set calendar time: 68 * RTC_SetTime(RTC_Format_BIN, &RTC_TimeStruct). 69 * 70 * 6. If RTC_OUT output is needed, the pinmux configuration is as follows: 71 * Pinmux_Config(PinName, PINMUX_FUNCTION_RTCOUT) 72 * 73 * 74 ***************************************************************************************** 75 * How to use Alarm 76 ***************************************************************************************** 77 * To use the Alarm, the following steps are mandatory: 78 * 79 * 1. Configure the RTC as described in the first part of this driver. 80 * 81 * 2. Fill the variable RTC_AlarmStruct of type RTC_AlarmTypeDef with default parameters 82 * RTC_AlarmStructInit(&RTC_AlarmStruct) 83 * or setting the desired Alarm time parameters manually. 84 * 85 * 3. configure Alarm: 86 * RTC_SetAlarm(RTC_Format_BIN, &RTC_AlarmStruct) 87 * 88 * 4. Enable alarm and alarm interrupt. 89 * RTC_AlarmCmd(ENABLE) 90 * 91 * 5. Enable IRQ as follows: 92 * InterruptRegister(RTC_IntHandler, RTC_IRQ, NULL, 4); 93 * InterruptEn(RTC_IRQ, 4); 94 * 95 * @endverbatim 96 */ 97 98 /* Exported types ------------------------------------------------------------*/ 99 100 /** @defgroup RTC_Exported_Types RTC Exported Types 101 * @{ 102 */ 103 104 /** 105 * @brief RTC Init structure definition 106 */ 107 typedef struct { 108 u32 RTC_HourFormat; /*!< Specifies the RTC Hour Format. 109 This parameter can be a value of @ref RTC_Hour_Formats */ 110 111 u32 RTC_AsynchPrediv; /*!< Specifies the RTC Asynchronous Predivider value. 112 This parameter must be a value of @ref RTC_Asynchronous_Predivider */ 113 114 u32 RTC_SynchPrediv; /*!< Specifies the RTC Synchronous Predivider value. 115 This parameter must be a value of @ref RTC_Synchronous_Predivider */ 116 117 u32 RTC_DayThreshold; /*!< Specifies the RTC Day Threshold value. 118 This parameter must be a value of @ref RTC_Day_Threshold */ 119 } RTC_InitTypeDef; 120 121 /** 122 * @brief RTC Time structure definition 123 */ 124 typedef struct 125 { 126 u16 RTC_Days; /*!< Day in binary format 9bits 0~0x1FF */ 127 128 u8 RTC_Hours; /*!< Specifies the RTC Time Hour. 129 This parameter must be set to a value in the 1-12 range 130 if the RTC_HourFormat_12 is selected or 0-23 range if 131 the RTC_HourFormat_24 is selected. */ 132 133 u8 RTC_Minutes; /*!< Specifies the RTC Time Minutes. 134 This parameter must be set to a value in the 0-59 range. */ 135 136 u8 RTC_Seconds; /*!< Specifies the RTC Time Seconds. 137 This parameter must be set to a value in the 0-59 range. */ 138 139 u8 RTC_H12_PMAM; /*!< Specifies the RTC AM/PM Time. 140 This parameter can be a value of @ref RTC_AM_PM_Definitions */ 141 }RTC_TimeTypeDef; 142 143 /** 144 * @brief RTC Alarm structure definition 145 */ 146 typedef struct 147 { 148 RTC_TimeTypeDef RTC_AlarmTime; /*!< Specifies the RTC Alarm Time members. */ 149 150 u32 RTC_AlarmMask; /*!< Specifies the RTC Alarm1 Masks(H:M:S). 151 This parameter can be a value of @ref RTC_AlarmMask1_Definitions */ 152 u32 RTC_Alarm2Mask; /*!< Specifies the RTC Alarm2 Masks Day). 153 This parameter can be a value of @ref RTC_AlarmMask2_Definitions */ 154 }RTC_AlarmTypeDef; 155 /** 156 * @} 157 */ 158 159 /* Exported constants --------------------------------------------------------*/ 160 161 /** @defgroup RTC_Exported_Constants RTC Exported Constants 162 * @{ 163 */ 164 165 /** @defgroup RTC_Input_parameter_format_definitions 166 * @{ 167 */ 168 #define RTC_Format_BIN ((u32)0x000000000) 169 #define RTC_Format_BCD ((u32)0x000000001) 170 #define IS_RTC_FORMAT(FORMAT) (((FORMAT) == RTC_Format_BIN) || ((FORMAT) == RTC_Format_BCD)) 171 /** 172 * @} 173 */ 174 175 /** @defgroup RTC_Hour_Formats 176 * @{ 177 */ 178 #define RTC_HourFormat_24 ((u32)0x00000000) 179 #define RTC_HourFormat_12 ((u32)0x00000080) 180 #define IS_RTC_HOUR_FORMAT(FORMAT) (((FORMAT) == RTC_HourFormat_12) || \ 181 ((FORMAT) == RTC_HourFormat_24)) 182 /** 183 * @} 184 */ 185 186 /** @defgroup RTC_Asynchronous_Predivider 187 * @{ 188 */ 189 #define IS_RTC_ASYNCH_PREDIV(PREDIV) ((PREDIV) <= 0x1FF) 190 /** 191 * @} 192 */ 193 194 /** @defgroup RTC_Synchronous_Predivider 195 * @{ 196 */ 197 #define IS_RTC_SYNCH_PREDIV(PREDIV) ((PREDIV) <= 0x1FF) 198 /** 199 * @} 200 */ 201 202 /** @defgroup RTC_Day_Threshold 203 * @{ 204 */ 205 #define RTC_DAYTHRES_MSK ((u32)0xFF800000) 206 #define IS_RTC_DAY_THRES(DAYS) ((DAYS) <= 0x1FF) 207 /** 208 * @} 209 */ 210 211 /** @defgroup RTC_Time_Definitions 212 * @{ 213 */ 214 #define IS_RTC_HOUR12(HOUR) (((HOUR) > 0) && ((HOUR) <= 12)) 215 #define IS_RTC_HOUR24(HOUR) ((HOUR) <= 23) 216 #define IS_RTC_MINUTES(MINUTES) ((MINUTES) <= 59) 217 #define IS_RTC_SECONDS(SECONDS) ((SECONDS) <= 59) 218 /** 219 * @} 220 */ 221 222 /** @defgroup RTC_AM_PM_Definitions 223 * @{ 224 */ 225 #define RTC_H12_AM ((u8)0x00) //AM or 24-hour format 226 #define RTC_H12_PM ((u8)0x01) //PM 227 #define IS_RTC_H12_AMPM(PM) (((PM) == RTC_H12_AM) || ((PM) == RTC_H12_PM)) 228 /** 229 * @} 230 */ 231 232 /** @defgroup RTC_AlarmMask1_Definitions 233 * @{ 234 */ 235 #define RTC_AlarmMask_None ((u32)0x00000000) 236 #define RTC_AlarmMask_Hours ((u32)0x00800000) 237 #define RTC_AlarmMask_Minutes ((u32)0x00008000) 238 #define RTC_AlarmMask_Seconds ((u32)0x00000080) 239 #define RTC_AlarmMask_All ((u32)0x00808080) 240 #define IS_ALARM_MASK(MASK) (((MASK) & 0x7F7F7F) == 0) 241 /** 242 * @} 243 */ 244 245 /** @defgroup RTC_AlarmMask2_Definitions 246 * @{ 247 */ 248 #define RTC_Alarm2Mask_None ((u32)0x00000000) 249 #define RTC_Alarm2Mask_Days ((u32)0x00000200) 250 #define IS_ALARM2_MASK(MASK) (((MASK) & ~RTC_Alarm2Mask_Days) == 0) 251 /** 252 * @} 253 */ 254 255 /** @defgroup RTC_Alarms_Definitions 256 * @{ 257 */ 258 #define RTC_Alarm ((u32)0x00000100) 259 #define IS_RTC_ALARM(ALARM) ((ALARM) == RTC_Alarm) 260 #define IS_RTC_CMD_ALARM(ALARM) (((ALARM) & RTC_Alarm) != (u32)0) 261 /** 262 * @} 263 */ 264 265 /** @defgroup RTC_Alarms_Interrupt_Definitions 266 * @{ 267 */ 268 #define RTC_Alarm_IntEn ((u32)0x00001000) 269 /** 270 * @} 271 */ 272 273 /** @defgroup RTC_DayLightSaving_Definitions 274 * @{ 275 */ 276 #define RTC_DayLightSaving_SUB1H ((u32)0x00000002) 277 #define RTC_DayLightSaving_ADD1H ((u32)0x00000001) 278 #define IS_RTC_DAYLIGHT_SAVING(SAVE) (((SAVE) == RTC_DayLightSaving_SUB1H) || \ 279 ((SAVE) == RTC_DayLightSaving_ADD1H)) 280 281 #define RTC_StoreOperation_Reset ((u32)0x00000000) 282 #define RTC_StoreOperation_Set ((u32)0x00000004) 283 #define IS_RTC_STORE_OPERATION(OPERATION) (((OPERATION) == RTC_StoreOperation_Reset) || \ 284 ((OPERATION) == RTC_StoreOperation_Set)) 285 /** 286 * @} 287 */ 288 289 /** @defgroup RTC_Output_selection_Definitions 290 * @{ 291 */ 292 #define RTC_Output_Disable ((u32)0x00000000) 293 #define RTC_Output_Alarm ((u32)0x00000020) ////wakeup 294 #define RTC_Output_clkspre ((u32)0x00000040) ////1Hz 295 #define RTC_Output_clkapre ((u32)0x00000060) ////256Hz 296 297 #define IS_RTC_OUTPUT(OUTPUT) (((OUTPUT) == RTC_Output_Disable) || \ 298 ((OUTPUT) == RTC_Output_Alarm) || \ 299 ((OUTPUT) == RTC_Output_clkspre) || \ 300 ((OUTPUT) == RTC_Output_clkapre)) 301 /** 302 * @} 303 */ 304 305 /** @defgroup RTC_Smooth_Calibration_Definitions 306 * @{ 307 */ 308 #define RTC_CalibPeriod_1MIN ((u32)0x00000000) 309 #define RTC_CalibPeriod_2MIN ((u32)0x00010000) 310 #define RTC_CalibPeriod_3MIN ((u32)0x00020000) 311 #define RTC_CalibPeriod_4MIN ((u32)0x00030000) 312 #define RTC_CalibPeriod_5MIN ((u32)0x00040000) 313 #define RTC_CalibPeriod_6MIN ((u32)0x00050000) 314 #define RTC_CalibPeriod_7MIN ((u32)0x00060000) 315 #define RTC_CalibPeriod_8MIN ((u32)0x00070000) 316 317 #define IS_RTC_CALIB_PERIOD(DCP) (((DCP) == RTC_CalibPeriod_1MIN) || \ 318 ((DCP) == RTC_CalibPeriod_2MIN) || \ 319 ((DCP) == RTC_CalibPeriod_3MIN) || \ 320 ((DCP) == RTC_CalibPeriod_4MIN) || \ 321 ((DCP) == RTC_CalibPeriod_5MIN) || \ 322 ((DCP) == RTC_CalibPeriod_6MIN) || \ 323 ((DCP) == RTC_CalibPeriod_7MIN) || \ 324 ((DCP) == RTC_CalibPeriod_8MIN)) 325 326 #define RTC_Calib_Disable ((u32)0x00000000) 327 #define RTC_Calib_Enable ((u32)0x00008000) 328 329 #define IS_RTC_CALIB_ENABLE(DCE) (((DCE) == RTC_Calib_Disable) || \ 330 ((DCE) == RTC_Calib_Enable)) 331 332 #define RTC_CalibSign_Positive ((u32)0x00000000) 333 #define RTC_CalibSign_Negative ((u32)0x00004000) 334 335 #define IS_RTC_CALIB_SIGN(SIGN) (((SIGN) == RTC_CalibSign_Positive) || \ 336 ((SIGN) == RTC_CalibSign_Negative)) 337 338 #define IS_RTC_CALIB_VALUE(VALUE) ((VALUE) <= 0x7F) 339 /** 340 * @} 341 */ 342 343 /** @defgroup RTC_32K_Auto_Calibration_Definitions 344 * @{ 345 */ 346 #define RTC_32K_AUTOCAL_DISABLE ((u32)0x00000000) 347 #define RTC_32K_AUTOCAL_MINUTES ((u32)0x00000001) 348 #define RTC_32K_AUTOCAL_HOURS ((u32)0x00000002) 349 #define RTC_32K_AUTOCAL_DAYS ((u32)0x00000003) 350 351 #define IS_RTC_32K_AUTOCAL_SIGN(SIGN) (((SIGN) == RTC_32K_AUTOCAL_DISABLE) || \ 352 ((SIGN) == RTC_32K_AUTOCAL_MINUTES) || \ 353 ((SIGN) == RTC_32K_AUTOCAL_HOURS) || \ 354 ((SIGN) == RTC_32K_AUTOCAL_DAYS)) 355 /** 356 * @} 357 */ 358 359 /** 360 * @} 361 */ 362 363 /** @defgroup RTC_Exported_Functions RTC Exported Functions 364 * @{ 365 */ 366 _LONG_CALL_ u32 RTC_BypassShadowCmd(u32 NewState); 367 _LONG_CALL_ void RTC_StructInit(RTC_InitTypeDef* RTC_InitStruct); 368 _LONG_CALL_ u32 RTC_Init(RTC_InitTypeDef* RTC_InitStruct); 369 _LONG_CALL_ void RTC_TimeStructInit(RTC_TimeTypeDef* RTC_TimeStruct); 370 _LONG_CALL_ u32 RTC_SetTime(u32 RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct); 371 _LONG_CALL_ void RTC_GetTime(u32 RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct); 372 _LONG_CALL_ void RTC_DayIntClear(void); 373 _LONG_CALL_ u32 RTC_DayIntCmd(u32 NewState); 374 _LONG_CALL_ u32 RTC_DayThresSet(u32 DayThres); 375 _LONG_CALL_ u32 RTC_DayThresGet(void); 376 _LONG_CALL_ u32 RTC_SetAlarm(u32 RTC_Format, RTC_AlarmTypeDef* RTC_AlarmStruct); 377 _LONG_CALL_ void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct); 378 _LONG_CALL_ void RTC_GetAlarm(u32 RTC_Format, RTC_AlarmTypeDef* RTC_AlarmStruct); 379 _LONG_CALL_ void RTC_AlarmCmd(u32 NewState); 380 _LONG_CALL_ void RTC_AlarmClear(void); 381 _LONG_CALL_ u32 RTC_DayLightSavingConfig(u32 RTC_DayLightSaving, u32 RTC_StoreOperation); 382 _LONG_CALL_ u32 RTC_GetStoreOperation(void); 383 _LONG_CALL_ u32 RTC_OutputConfig(u32 RTC_Output); 384 _LONG_CALL_ u32 RTC_SmoothCalibConfig(u32 CalibSign, u32 Value, u32 CalibPeriod, u32 Calib_Enable); 385 _LONG_CALL_ u32 RTC_32KAutoCalibConfig(u32 Cal_Period, u32 Unit_Sel); 386 /** 387 * @} 388 */ 389 390 /* Registers Definitions --------------------------------------------------------*/ 391 /**************************************************************************//** 392 * @defgroup RTC_Register_Definitions RTC Register Definitions 393 * @{ 394 *****************************************************************************/ 395 396 /**************************************************************************//** 397 * @defgroup RTC_TR 398 * @{ 399 *****************************************************************************/ 400 #define RTC_TR_DAY ((u32)0xFF800000) 401 #define RTC_TR_PM ((u32)0x00400000) 402 #define RTC_TR_HT ((u32)0x00300000) 403 #define RTC_TR_HU ((u32)0x000F0000) 404 #define RTC_TR_MNT ((u32)0x00007000) 405 #define RTC_TR_MNU ((u32)0x00000F00) 406 #define RTC_TR_ST ((u32)0x00000070) 407 #define RTC_TR_SU ((u32)0x0000000F) 408 /** @} */ 409 410 /**************************************************************************//** 411 * @defgroup RTC_CR 412 * @{ 413 *****************************************************************************/ 414 #define RTC_CR_DOVTHIE ((u32)0x00010000) 415 #define RTC_CR_ALRAIE ((u32)0x00001000) 416 #define RTC_CR_ALRAE ((u32)0x00000100) 417 #define RTC_CR_FMT ((u32)0x00000080) 418 #define RTC_CR_OSEL ((u32)0x00000060) 419 #define RTC_CR_BYPSHAD ((u32)0x00000008) 420 #define RTC_CR_BCK ((u32)0x00000004) 421 #define RTC_CR_SUB1H ((u32)0x00000002) 422 #define RTC_CR_ADD1H ((u32)0x00000001) 423 /** @} */ 424 425 /**************************************************************************//** 426 * @defgroup RTC_ISR 427 * @{ 428 *****************************************************************************/ 429 #define RTC_ISR_RECALPF ((u32)0x00010000) 430 #define RTC_ISR_DOVTHF ((u32)0x00008000) 431 #define RTC_ISR_ALRAF ((u32)0x00000100) 432 433 #define RTC_ISR_INIT ((u32)0x00000080) 434 #define RTC_ISR_INITF ((u32)0x00000040) 435 #define RTC_ISR_RSF ((u32)0x00000020) 436 #define RTC_ISR_INITS ((u32)0x00000010) 437 438 #define RTC_ISR_ALMWF ((u32)0x00000001) 439 /** @} */ 440 441 /**************************************************************************//** 442 * @defgroup RTC_PRER 443 * @{ 444 *****************************************************************************/ 445 #define RTC_PRER_PREDIV_A ((u32)0x01FF0000) 446 #define RTC_PRER_PREDIV_S ((u32)0x000001FF) 447 /** @} */ 448 449 /**************************************************************************//** 450 * @defgroup RTC_CALIBR 451 * @{ 452 *****************************************************************************/ 453 #define RTC_CALIBR_CALP ((u32)0x00070000) 454 #define RTC_CALIBR_DCE ((u32)0x00008000) 455 #define RTC_CALIBR_DCS ((u32)0x00004000) 456 #define RTC_CALIBR_DC ((u32)0x0000007F) 457 /** @} */ 458 459 /**************************************************************************//** 460 * @defgroup RTC_ALRMAR 461 * @{ 462 *****************************************************************************/ 463 #define RTC_ALRMAR_MSK2 ((u32)0x00800000) 464 #define RTC_ALRMAR_PM ((u32)0x00400000) 465 #define RTC_ALRMAR_HT ((u32)0x00300000) 466 #define RTC_ALRMAR_HU ((u32)0x000F0000) 467 468 #define RTC_ALRMAR_MSK1 ((u32)0x00008000) 469 #define RTC_ALRMAR_MNT ((u32)0x00007000) 470 #define RTC_ALRMAR_MNU ((u32)0x00000F00) 471 472 #define RTC_ALRMAR_MSK0 ((u32)0x00000080) 473 #define RTC_ALRMAR_ST ((u32)0x00000070) 474 #define RTC_ALRMAR_SU ((u32)0x0000000F) 475 /** @} */ 476 477 /**************************************************************************//** 478 * @defgroup RTC_ALRMBR 479 * @{ 480 *****************************************************************************/ 481 #define RTC_ALRMBR_MSK3 ((u32)0x00000200) 482 #define RTC_ALRMBR_DT ((u32)0x000001FF) 483 /** @} */ 484 485 /**************************************************************************//** 486 * @defgroup RTC_WPR 487 * @{ 488 *****************************************************************************/ 489 #define RTC_WPR_KEY ((u32)0x000000FF) 490 /** @} */ 491 492 /**************************************************************************//** 493 * @defgroup RTC_CLKACALR 494 * @{ 495 *****************************************************************************/ 496 #define RTC_32K_AUTOCAL_SEL ((u32)0x00000003) 497 #define RTC_AUTOCAL_THRES_MSK ((u32)0x000000FC) 498 499 #define IS_RTC_32K_AUTOCALIB_THRES(THRES) (THRES <= 0x3F) 500 /** @} */ 501 /** @} */ 502 503 /** 504 * @} 505 */ 506 507 /** 508 * @} 509 */ 510 511 /* Masks Definition */ 512 #define RTC_TR_RESERVED_MASK ((u32)0xFFFF7F7F) 513 #define INITMODE_TIMEOUT ((u32) 0x00010000) 514 #define SYNCHRO_TIMEOUT ((u32) 0x00020000) 515 #define RECALPF_TIMEOUT ((u32) 0x00020000) 516 #define ALARMDIS_TIMEOUT ((u32) 0x00020000) 517 518 #endif //_RTL8721D_RTC_H_ 519 520 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/ 521