hc32l196_rtc.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /******************************************************************************
  2. * Copyright (C) 2019, Huada Semiconductor Co.,Ltd All rights reserved.
  3. *
  4. * This software is owned and published by:
  5. * Huada Semiconductor Co.,Ltd ("HDSC").
  6. *
  7. * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
  8. * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
  9. *
  10. * This software contains source code for use with HDSC
  11. * components. This software is licensed by HDSC to be adapted only
  12. * for use in systems utilizing HDSC components. HDSC shall not be
  13. * responsible for misuse or illegal use of this software for devices not
  14. * supported herein. HDSC is providing this software "AS IS" and will
  15. * not be responsible for issues arising from incorrect user implementation
  16. * of the software.
  17. *
  18. * Disclaimer:
  19. * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
  20. * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
  21. * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
  22. * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
  23. * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
  24. * WARRANTY OF NONINFRINGEMENT.
  25. * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
  26. * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
  27. * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
  28. * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
  29. * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
  30. * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
  31. * SAVINGS OR PROFITS,
  32. * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  33. * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
  34. * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
  35. * FROM, THE SOFTWARE.
  36. *
  37. * This software may be replicated in part or whole for the licensed use,
  38. * with the restriction that this Disclaimer and Copyright notice must be
  39. * included with each copy of this software, whether used in part or whole,
  40. * at all times.
  41. */
  42. /*****************************************************************************/
  43. /** \file rtc.h
  44. **
  45. ** Headerfile for RTC functions
  46. **
  47. **
  48. ** History:
  49. ** - 2019-04-10 First Version
  50. **
  51. *****************************************************************************/
  52. #ifndef __RTC_H__
  53. #define __RTC_H__
  54. /*****************************************************************************
  55. * Include files
  56. *****************************************************************************/
  57. #include "ddl.h"
  58. #ifdef __cplusplus
  59. extern "C"
  60. {
  61. #endif
  62. /**
  63. ******************************************************************************
  64. ** \defgroup RtcGroup Real Time Clock (RTC)
  65. **
  66. ******************************************************************************/
  67. //@{
  68. /******************************************************************************/
  69. /* Global pre-processor symbols/macros ('#define') */
  70. /******************************************************************************/
  71. /******************************************************************************
  72. * Global type definitions
  73. ******************************************************************************/
  74. /**
  75. ******************************************************************************
  76. ** \brief rtc周期中断的时间间隔 RTC_CR0 PRDS
  77. *****************************************************************************/
  78. typedef enum
  79. {
  80. RtcNone = 0u, //无周期中断
  81. Rtc05S = 1u, //0.5S中断
  82. Rtc1S = 2u, //1秒
  83. Rtc1Min = 3u, //1分钟
  84. Rtc1H = 4u, //1小时
  85. Rtc1Day = 5u, //1天
  86. Rtc1Mon = 6u //1月
  87. }en_rtc_prds_t;
  88. /**
  89. ******************************************************************************
  90. ** \brief rtc 12h制或24h制方式选择 RTC_CR0 AMPM
  91. *****************************************************************************/
  92. typedef enum
  93. {
  94. RtcAm = 0u, //12小时制
  95. RtcPm = 1u //24小时制
  96. }en_rtc_ampm_t;
  97. /**
  98. ******************************************************************************
  99. ** \brief 普通精度与高精度1Hz输出 RTC_CR0 HZ1SEL
  100. *****************************************************************************/
  101. typedef enum
  102. {
  103. RtcHz1selGeneralPricision = 0u, //普通精度1Hz输出
  104. RtcHz1selHighPricision = 1u //高精度1Hz输出
  105. }en_rtc_hz1sel_t;
  106. /**
  107. ******************************************************************************
  108. ** \brief 周期中断选择 RTC_CR0 PRDSEL
  109. *****************************************************************************/
  110. typedef enum
  111. {
  112. RtcPrds = 0u, //使用PRDS所设定的周期中断事件间隔
  113. RtcPrdx = 1u //使用PRDX所设定的周期中断事件间隔
  114. }en_rtc_prdsel_t;
  115. /**
  116. ******************************************************************************
  117. ** \brief rtc的时钟选择 RTC_CR1 CKSEL
  118. *****************************************************************************/
  119. typedef enum
  120. {
  121. RtcClkXtl = 0u, //外部低速时钟XTL 32.768k
  122. RtcClkRcl = 2u, //内部低速时钟RCL 32k
  123. RtcClkXth128 = 4u, //外部晶振4M XTH/128
  124. RtcClkXth256 = 5u, //外部晶振8M XTH/256
  125. RtcClkXth512 = 6u, //外部晶振16M XTH/512
  126. RtcClkXth1024 = 7u //外部晶振32M XTH/1024
  127. }en_rtc_cksel_t;
  128. /**
  129. ******************************************************************************
  130. ** \brief 时钟误差补偿使能或禁止 RTC_COMPEN EN
  131. *****************************************************************************/
  132. typedef enum
  133. {
  134. RtcCompenDisable = 0u,
  135. RtcCompenEnable = 1u
  136. }en_rtc_compen_t;
  137. /**
  138. ******************************************************************************
  139. ** \brief 配置PRD中断使能及其周期类型
  140. *****************************************************************************/
  141. typedef struct
  142. {
  143. en_rtc_prdsel_t rtcPrdsel;
  144. uint8_t rtcPrdx;
  145. en_rtc_prds_t rtcPrds;
  146. }stc_rtc_cyccfg_t;
  147. /**
  148. ******************************************************************************
  149. ** \brief 闹钟源配置
  150. *****************************************************************************/
  151. typedef struct
  152. {
  153. uint8_t RtcAlarmSec; //闹钟秒钟
  154. uint8_t RtcAlarmMinute; //闹钟分钟
  155. uint8_t RtcAlarmHour; //闹钟小时
  156. uint8_t RtcAlarmWeek; //闹钟周
  157. }stc_rtc_alarmtime_t;
  158. /**
  159. ******************************************************************************
  160. ** \brief 时间
  161. *****************************************************************************/
  162. /**
  163. ******************************************************************************
  164. ** \brief rtc时钟年、月、日、时、分、秒读写结构
  165. *****************************************************************************/
  166. typedef struct stc_rtc_time
  167. {
  168. uint8_t u8Second; //时间:秒
  169. uint8_t u8Minute; //时间:分
  170. uint8_t u8Hour; //时间:时
  171. uint8_t u8DayOfWeek; //时间:周
  172. uint8_t u8Day; //时间:日
  173. uint8_t u8Month; //时间:月
  174. uint8_t u8Year; //时间:年
  175. } stc_rtc_time_t;
  176. /**
  177. ******************************************************************************
  178. ** \brief 初始化RTC的结构体
  179. *****************************************************************************/
  180. typedef struct
  181. {
  182. en_rtc_ampm_t rtcAmpm; //小时的时制
  183. stc_rtc_cyccfg_t rtcPrdsel; //确定PRDS或者PRDX所设定的周期中断时间间隔类型
  184. en_rtc_cksel_t rtcClksrc; //实时时钟的时钟源
  185. en_rtc_compen_t rtcCompen; //时钟误差补偿使能与禁止
  186. uint16_t rtcCompValue; //使能补偿的情况下,补偿值取值范围为:0-255
  187. stc_rtc_time_t rtcTime; //要写入时间寄存器的时间
  188. }stc_rtc_initstruct_t;
  189. /******************************************************************************
  190. Global function prototypes (definition in C source)
  191. *******************************************************************************/
  192. //RTC计数器的使能或停止
  193. extern void Rtc_Cmd(boolean_t NewState);
  194. //RTC计数器启动等待函数
  195. extern void Rtc_StartWait(void);
  196. //RTC的1Hz输出的使能或停止
  197. extern void Rtc_Hz1Cmd(en_rtc_hz1sel_t pricision, boolean_t NewState);
  198. //设置周期中断的类型(PRDSEL)及其所选类型的时间(PRDS或PRDX)
  199. extern en_result_t Rtc_SetCyc(stc_rtc_cyccfg_t* pstCyc);
  200. //RTC闹钟中断的使能或停止
  201. extern void Rtc_AlmIeCmd(boolean_t NewState);
  202. //RTC闹钟的使能或停止
  203. extern void Rtc_AlmEnCmd(boolean_t NewState);
  204. //获取RTC闹钟中断状态位
  205. extern boolean_t Rtc_GetAlmfItStatus(void);
  206. //清除RTC闹钟中断状态位
  207. extern void Rtc_ClearAlmfItStatus(void);
  208. //清除RTC周期中断状态位
  209. extern void Rtc_ClearPrdfItStatus(void);
  210. //获取RTC周期中断状态位
  211. extern boolean_t Rtc_GetPridItStatus(void);
  212. //配置RTC的误差补偿寄存器
  213. extern en_result_t Rtc_CompCfg(uint16_t CompVlue, en_rtc_compen_t NewStatus);
  214. //RTC根据日期计算周数
  215. extern en_result_t Check_BCD_Format(uint8_t u8data,uint8_t u8limit_min, uint8_t u8limit_max);
  216. //RTC获取时间函数
  217. extern en_result_t Rtc_ReadDateTime(stc_rtc_time_t* time);
  218. //向RTC时间寄存器写入时间
  219. extern en_result_t Rtc_SetTime(stc_rtc_time_t* time);
  220. //RTC闹钟中断时间获取
  221. extern void Rtc_GetAlarmTime(stc_rtc_alarmtime_t* pstcAlarmTime);
  222. //RTC闹钟设置
  223. extern en_result_t Rtc_SetAlarmTime(stc_rtc_alarmtime_t* pstcAlarmTime);
  224. //初始化RTC
  225. extern void Rtc_Init(stc_rtc_initstruct_t* Rtc_InitStruct);
  226. #endif /* __RTC_H__ */
  227. /******************************************************************************
  228. * EOF (not truncated)
  229. *****************************************************************************/