rtc.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /******************************************************************************
  2. * Copyright (C) 2021, Xiaohua Semiconductor Co., Ltd. All rights reserved.
  3. *
  4. * This software component is licensed by XHSC under BSD 3-Clause license
  5. * (the "License"); You may not use this file except in compliance with the
  6. * License. You may obtain a copy of the License at:
  7. * opensource.org/licenses/BSD-3-Clause
  8. *
  9. ******************************************************************************/
  10. /******************************************************************************
  11. * @file rtc.h
  12. *
  13. * @brief Header file for RTC functions
  14. *
  15. * @author MADS Team
  16. *
  17. ******************************************************************************/
  18. #ifndef __RTC_H__
  19. #define __RTC_H__
  20. /*****************************************************************************
  21. * Include files
  22. *****************************************************************************/
  23. #include "ddl.h"
  24. #ifdef __cplusplus
  25. extern "C"
  26. {
  27. #endif
  28. /**
  29. ******************************************************************************
  30. ** \defgroup RtcGroup Real Time Clock (RTC)
  31. **
  32. ******************************************************************************/
  33. //@{
  34. /******************************************************************************/
  35. /* Global pre-processor symbols/macros ('#define') */
  36. /******************************************************************************/
  37. /******************************************************************************
  38. * Global type definitions
  39. ******************************************************************************/
  40. /**
  41. ******************************************************************************
  42. ** \brief rtc周期中断的时间间隔 RTC_CR0 PRDS
  43. *****************************************************************************/
  44. typedef enum
  45. {
  46. RtcNone = 0u, //无周期中断
  47. Rtc05S = 1u, //0.5S中断
  48. Rtc1S = 2u, //1秒
  49. Rtc1Min = 3u, //1分钟
  50. Rtc1H = 4u, //1小时
  51. Rtc1Day = 5u, //1天
  52. Rtc1Mon = 6u //1月
  53. }en_rtc_prds_t;
  54. /**
  55. ******************************************************************************
  56. ** \brief rtc 12h制或24h制方式选择 RTC_CR0 AMPM
  57. *****************************************************************************/
  58. typedef enum
  59. {
  60. RtcAm = 0u, //12小时制
  61. RtcPm = 1u //24小时制
  62. }en_rtc_ampm_t;
  63. /**
  64. ******************************************************************************
  65. ** \brief 普通精度与高精度1Hz输出 RTC_CR0 HZ1SEL
  66. *****************************************************************************/
  67. typedef enum
  68. {
  69. RtcHz1selGeneralPricision = 0u, //普通精度1Hz输出
  70. RtcHz1selHighPricision = 1u //高精度1Hz输出
  71. }en_rtc_hz1sel_t;
  72. /**
  73. ******************************************************************************
  74. ** \brief 周期中断选择 RTC_CR0 PRDSEL
  75. *****************************************************************************/
  76. typedef enum
  77. {
  78. RtcPrds = 0u, //使用PRDS所设定的周期中断事件间隔
  79. RtcPrdx = 1u //使用PRDX所设定的周期中断事件间隔
  80. }en_rtc_prdsel_t;
  81. /**
  82. ******************************************************************************
  83. ** \brief rtc的时钟选择 RTC_CR1 CKSEL
  84. *****************************************************************************/
  85. typedef enum
  86. {
  87. RtcClkXtl = 0u, //外部低速时钟XTL 32.768k
  88. RtcClkRcl = 2u, //内部低速时钟RCL 32k
  89. RtcClkXth128 = 4u, //外部晶振4M XTH/128
  90. RtcClkXth256 = 5u, //外部晶振8M XTH/256
  91. RtcClkXth512 = 6u, //外部晶振16M XTH/512
  92. RtcClkXth1024 = 7u //外部晶振32M XTH/1024
  93. }en_rtc_cksel_t;
  94. /**
  95. ******************************************************************************
  96. ** \brief 时钟误差补偿使能或禁止 RTC_COMPEN EN
  97. *****************************************************************************/
  98. typedef enum
  99. {
  100. RtcCompenDisable = 0u,
  101. RtcCompenEnable = 1u
  102. }en_rtc_compen_t;
  103. /**
  104. ******************************************************************************
  105. ** \brief 配置PRD中断使能及其周期类型
  106. *****************************************************************************/
  107. typedef struct
  108. {
  109. en_rtc_prdsel_t rtcPrdsel;
  110. uint8_t rtcPrdx;
  111. en_rtc_prds_t rtcPrds;
  112. }stc_rtc_cyccfg_t;
  113. /**
  114. ******************************************************************************
  115. ** \brief 闹钟源配置
  116. *****************************************************************************/
  117. typedef struct
  118. {
  119. uint8_t RtcAlarmSec; //闹钟秒钟
  120. uint8_t RtcAlarmMinute; //闹钟分钟
  121. uint8_t RtcAlarmHour; //闹钟小时
  122. uint8_t RtcAlarmWeek; //闹钟周
  123. }stc_rtc_alarmtime_t;
  124. /**
  125. ******************************************************************************
  126. ** \brief 时间
  127. *****************************************************************************/
  128. /**
  129. ******************************************************************************
  130. ** \brief rtc时钟年、月、日、时、分、秒读写结构
  131. *****************************************************************************/
  132. typedef struct stc_rtc_time
  133. {
  134. uint8_t u8Second; //时间:秒
  135. uint8_t u8Minute; //时间:分
  136. uint8_t u8Hour; //时间:时
  137. uint8_t u8DayOfWeek; //时间:周
  138. uint8_t u8Day; //时间:日
  139. uint8_t u8Month; //时间:月
  140. uint8_t u8Year; //时间:年
  141. } stc_rtc_time_t;
  142. /**
  143. ******************************************************************************
  144. ** \brief 初始化RTC的结构体
  145. *****************************************************************************/
  146. typedef struct
  147. {
  148. en_rtc_ampm_t rtcAmpm; //小时的时制
  149. stc_rtc_cyccfg_t rtcPrdsel; //确定PRDS或者PRDX所设定的周期中断时间间隔类型
  150. en_rtc_cksel_t rtcClksrc; //实时时钟的时钟源
  151. en_rtc_compen_t rtcCompen; //时钟误差补偿使能与禁止
  152. uint16_t rtcCompValue; //使能补偿的情况下,补偿值取值范围为:0-255
  153. stc_rtc_time_t rtcTime; //要写入时间寄存器的时间
  154. }stc_rtc_initstruct_t;
  155. /******************************************************************************
  156. Global function prototypes (definition in C source)
  157. *******************************************************************************/
  158. //RTC计数器的使能或停止
  159. extern void Rtc_Cmd(boolean_t NewState);
  160. //RTC计数器启动等待函数
  161. extern void Rtc_StartWait(void);
  162. //RTC的1Hz输出的使能或停止
  163. extern void Rtc_Hz1Cmd(en_rtc_hz1sel_t pricision, boolean_t NewState);
  164. //设置周期中断的类型(PRDSEL)及其所选类型的时间(PRDS或PRDX)
  165. extern en_result_t Rtc_SetCyc(stc_rtc_cyccfg_t* pstCyc);
  166. //RTC闹钟中断的使能或停止
  167. extern void Rtc_AlmIeCmd(boolean_t NewState);
  168. //RTC闹钟的使能或停止
  169. extern void Rtc_AlmEnCmd(boolean_t NewState);
  170. //获取RTC闹钟中断状态位
  171. extern boolean_t Rtc_GetAlmfItStatus(void);
  172. //清除RTC闹钟中断状态位
  173. extern void Rtc_ClearAlmfItStatus(void);
  174. //清除RTC周期中断状态位
  175. extern void Rtc_ClearPrdfItStatus(void);
  176. //获取RTC周期中断状态位
  177. extern boolean_t Rtc_GetPridItStatus(void);
  178. //配置RTC的误差补偿寄存器
  179. extern en_result_t Rtc_CompCfg(uint16_t CompVlue, en_rtc_compen_t NewStatus);
  180. //RTC根据日期计算周数
  181. extern en_result_t Check_BCD_Format(uint8_t u8data,uint8_t u8limit_min, uint8_t u8limit_max);
  182. //RTC获取时间函数
  183. extern en_result_t Rtc_ReadDateTime(stc_rtc_time_t* time);
  184. //向RTC时间寄存器写入时间
  185. extern en_result_t Rtc_SetTime(stc_rtc_time_t* time);
  186. //RTC闹钟中断时间获取
  187. extern void Rtc_GetAlarmTime(stc_rtc_alarmtime_t* pstcAlarmTime);
  188. //RTC闹钟设置
  189. extern en_result_t Rtc_SetAlarmTime(stc_rtc_alarmtime_t* pstcAlarmTime);
  190. //初始化RTC
  191. extern void Rtc_Init(stc_rtc_initstruct_t* Rtc_InitStruct);
  192. #endif /* __RTC_H__ */
  193. /******************************************************************************
  194. * EOF (not truncated)
  195. *****************************************************************************/