rtc.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /******************************************************************************
  2. * Copyright (C) 2017, 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. ** - 2017-05-10 Cathy First Version
  50. **
  51. *****************************************************************************/
  52. #ifndef __RTC_H__
  53. #define __RTC_H__
  54. /*****************************************************************************
  55. * Include files
  56. *****************************************************************************/
  57. #include "ddl.h"
  58. #include "interrupts_hc32l136.h"
  59. #ifdef __cplusplus
  60. extern "C"
  61. {
  62. #endif
  63. /**
  64. ******************************************************************************
  65. ** \defgroup RtcGroup Real Time Clock (RTC)
  66. **
  67. ******************************************************************************/
  68. //@{
  69. /******************************************************************************/
  70. /* Global pre-processor symbols/macros ('#define') */
  71. /******************************************************************************/
  72. /******************************************************************************
  73. * Global type definitions
  74. ******************************************************************************/
  75. /**
  76. ******************************************************************************
  77. ** \brief rtc时钟源选择
  78. *****************************************************************************/
  79. typedef enum en_rtc_clk
  80. {
  81. RtcClk32768 = 0u, ///<外部32.768k
  82. RtcClk32768_1 = 1u, ///<外部32.768k
  83. RtcClk32 = 2u, ///<内部RC32
  84. RtcClk32_1 = 3u, ///<内部RC32
  85. RtcClkHxt128 = 4u, ///<外部晶振4M
  86. RtcClkHxt256 = 5u, ///<外部晶振8M
  87. RtcClkHxt512 = 6u, ///<外部晶振16M
  88. RtcClkHxt1024 = 7u, ///<外部晶振32M
  89. }en_rtc_clk_t;
  90. /**
  91. ******************************************************************************
  92. ** \brief rtc周期中断方式选择
  93. *****************************************************************************/
  94. typedef enum en_rtc_cyc
  95. {
  96. RtcPrads = 0u, ///<月、天、时分秒
  97. RtcPradx = 1u, ///<step 0.5s
  98. }en_rtc_cyc_t;
  99. /**
  100. ******************************************************************************
  101. ** \brief rtc 12h制或24h制方式选择
  102. *****************************************************************************/
  103. typedef enum en_rtc_ampm
  104. {
  105. Rtc12h = 0u, ///<12h
  106. Rtc24h = 1u, ///<24h
  107. }en_rtc_ampm_t;
  108. /**
  109. ******************************************************************************
  110. ** \brief prds中断周期
  111. *****************************************************************************/
  112. typedef enum en_rtc_cycprds
  113. {
  114. Rtc_None = 0u, ///<无周期中断
  115. Rtc_05S = 1u, ///<0.5S中断
  116. Rtc_1S = 2u, ///<1s
  117. Rtc_1Min = 3u, ///<1min
  118. Rtc_1H = 4u, ///<1h
  119. Rtc_1Day = 5u, ///<1d
  120. Rtc_1Mon = 6u, ///<1月
  121. Rtc_1Mon_1 = 7u, ///<1月
  122. }en_rtc_cycprds_t;
  123. /**
  124. ******************************************************************************
  125. ** \brief rtc周期中断总配置
  126. *****************************************************************************/
  127. typedef struct stc_rtc_cyc_sel
  128. {
  129. en_rtc_cyc_t enCyc_sel; ///<周期类型配置
  130. en_rtc_cycprds_t enPrds_sel;///<周期配置
  131. uint8_t u8Prdx;
  132. }stc_rtc_cyc_sel_t;
  133. /**
  134. ******************************************************************************
  135. ** \brief 闹钟源配置
  136. *****************************************************************************/
  137. typedef struct stc_rtc_alarmset
  138. {
  139. uint8_t u8Minute; ///<闹钟分钟
  140. uint8_t u8Hour; ///<闹钟小时
  141. uint8_t u8Week; ///<闹钟周
  142. }stc_rtc_alarmset_t;
  143. /**
  144. ******************************************************************************
  145. ** \brief 闹钟中断使能设置
  146. *****************************************************************************/
  147. typedef enum en_rtc_alarmirq
  148. {
  149. Rtc_AlarmInt_Disable = 0u,///<闹钟中断禁止
  150. Rtc_AlarmInt_Enable = 1u,///<闹钟中断使能
  151. }en_rtc_alarmirq_t;
  152. /**
  153. ******************************************************************************
  154. ** \brief rtc 1hz补偿功能开启设置
  155. *****************************************************************************/
  156. typedef enum en_rtc_compen_en
  157. {
  158. Rtc_Comp_Disable = 0u,///<时钟补偿禁止
  159. Rtc_Comp_Enable = 1u,///<时钟补偿使能
  160. }en_rtc_compen_en_t;
  161. /**
  162. ******************************************************************************
  163. ** \brief rtc计数功能使能设置
  164. *****************************************************************************/
  165. typedef enum en_rtc_count_en
  166. {
  167. Rtc_Count_Disable = 0u,///<计数禁止
  168. Rtc_Count_Enable = 1u,///<计数使能
  169. }en_rtc_count_en_t;
  170. /**
  171. ******************************************************************************
  172. ** \brief rtc计数模式还是读写模式状态
  173. *****************************************************************************/
  174. typedef enum en_rtc_status
  175. {
  176. RtcRunStatus = 0u, ///<计数状态
  177. RtcRdWStatus = 1u, ///<读写状态
  178. }en_rtc_status_t;
  179. /**
  180. ******************************************************************************
  181. ** \brief rtc 中断请求标志
  182. *****************************************************************************/
  183. typedef enum en_rtc_status_irq
  184. {
  185. RtcAlmf = 0u, ///<闹钟中断请求
  186. RtcPrdf = 1u, ///<周期中断请求
  187. }en_rtc_status_irq_t;
  188. /**
  189. ******************************************************************************
  190. ** \brief rtc时钟年、月、日、时、分、秒读写结构
  191. *****************************************************************************/
  192. typedef struct stc_rtc_time
  193. {
  194. uint8_t u8Second; ///<秒
  195. uint8_t u8Minute; ///<分
  196. uint8_t u8Hour; ///<时
  197. uint8_t u8DayOfWeek; ///<周
  198. uint8_t u8Day; ///<日
  199. uint8_t u8Month; ///<月
  200. uint8_t u8Year; ///<年
  201. } stc_rtc_time_t;
  202. /**
  203. ******************************************************************************
  204. ** \brief rtc功能描述
  205. ******************************************************************************/
  206. typedef enum en_rtc_func
  207. {
  208. RtcCount = 0u, ///< RTC计数使能
  209. RtcAlarmEn = 1u, ///< RTC闹钟使能
  210. Rtc_ComenEn = 2u, ///<RTC补偿使能
  211. Rtc1HzOutEn = 3u, ///<使能1hz输出
  212. }en_rtc_func_t;
  213. /**
  214. ******************************************************************************
  215. ** \brief rtc 闹钟及周期中断处理函数
  216. *****************************************************************************/
  217. typedef struct stc_rtc_irq_cb
  218. {
  219. func_ptr_t pfnAlarmIrqCb; ///<闹钟中断服务函数
  220. func_ptr_t pfnTimerIrqCb; ///<周期中断服务函数
  221. }stc_rtc_irq_cb_t, stc_rtc_intern_cb_t;
  222. /**
  223. ******************************************************************************
  224. ** \brief rtc 总体配置结构体
  225. *****************************************************************************/
  226. typedef struct stc_rtc_config
  227. {
  228. en_rtc_clk_t enClkSel; ///<时钟源配置
  229. en_rtc_ampm_t enAmpmSel; ///<时制配置
  230. stc_rtc_cyc_sel_t* pstcCycSel; ///<周期配置
  231. stc_rtc_time_t* pstcTimeDate; ///<时间日期初值配置
  232. stc_rtc_irq_cb_t* pstcIrqCb; ///<中断服务函数
  233. boolean_t bTouchNvic; ///<NVIC中断配置
  234. } stc_rtc_config_t;
  235. //rtc 计数时钟源选择
  236. en_result_t Rtc_SelClk(en_rtc_clk_t enClk);
  237. //rtc 计数周期设置
  238. en_result_t Rtc_SetCyc(stc_rtc_cyc_sel_t* pstcCyc);
  239. //rtc ampm模式设置
  240. en_result_t Rtc_SetAmPm(en_rtc_ampm_t enMode);
  241. //rtc时制模式获取
  242. boolean_t Rtc_GetHourMode(void);
  243. //rtc 闹钟相关配置
  244. en_result_t Rtc_SetAlarmTime(stc_rtc_alarmset_t* pstcAlarmTime);
  245. en_result_t Rtc_GetAlarmTime(stc_rtc_alarmset_t* pstcAlarmTime);
  246. //1hz 输出模式及补偿值设置
  247. en_result_t Rtc_Set1HzMode(boolean_t bMode);
  248. en_result_t Rtc_SetCompCr(uint16_t u16Cr);
  249. //周计算
  250. uint8_t Rtc_CalWeek(uint8_t* pu8Date);
  251. //判断是否闰年
  252. uint8_t Rtc_CheckLeapYear(uint8_t u8year);
  253. //12时制上午或下午读取
  254. boolean_t Rtc_RDAmPm(void);
  255. //rtc 读写时间计数器
  256. en_result_t Rtc_WriteDateTime(stc_rtc_time_t* pstcTimeDate,boolean_t bUpdateTime,
  257. boolean_t bUpdateDate);
  258. en_result_t Rtc_ReadDateTime(stc_rtc_time_t* pstcTimeDate);
  259. //格式转换函数
  260. uint8_t Change_DateTimeFormat(uint8_t u8sr);
  261. //时间格式检查函数
  262. en_result_t Rtc_CheckDateTimeFormat(uint8_t* pu8TimeDate,uint8_t u8Mode);
  263. //数据大小判断函数
  264. en_result_t Check_BCD_Format(uint8_t u8data, uint8_t u8limit_min, uint8_t u8limit_max);
  265. //获取某年某月最大天数
  266. uint8_t Get_Month_Max_Day(uint8_t u8month, uint8_t u8year);
  267. //rtc 读取当前状态(读写状态或计数状态),中断请求状态、中断清除状态
  268. en_result_t Rtc_EnAlarmIrq(en_rtc_alarmirq_t enIrqEn);
  269. boolean_t Rtc_RDStatus(void);
  270. boolean_t Rtc_GetIrqStatus(en_rtc_status_irq_t enIrqSel);
  271. en_result_t Rtc_ClrIrqStatus(en_rtc_status_irq_t enIrqSel);
  272. //rtc功能使能禁止函数
  273. en_result_t Rtc_EnableFunc(en_rtc_func_t enFunc);
  274. en_result_t Rtc_DisableFunc(en_rtc_func_t enFunc);
  275. //rtc初始化、禁止函数
  276. en_result_t Rtc_Init(stc_rtc_config_t* pstcRtcConfig);
  277. en_result_t Rtc_DeInit(void);
  278. //@} // RtcGroup
  279. #ifdef __cplusplus
  280. #endif
  281. #endif /* __RTC_H__ */
  282. /******************************************************************************
  283. * EOF (not truncated)
  284. *****************************************************************************/