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