1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
|
/*************************************************************************************
* Copyright (C) 2019, Huada Semiconductor Co.,Ltd All rights reserved.
*
* This software is owned and published by:
* Huada Semiconductor Co.,Ltd ("HDSC").
*
* BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
* BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
*
* This software contains source code for use with HDSC
* components. This software is licensed by HDSC to be adapted only
* for use in systems utilizing HDSC components. HDSC shall not be
* responsible for misuse or illegal use of this software for devices not
* supported herein. HDSC is providing this software "AS IS" and will
* not be responsible for issues arising from incorrect user implementation
* of the software.
*
* Disclaimer:
* HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
* REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
* ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
* WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
* WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
* WARRANTY OF NONINFRINGEMENT.
* HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
* NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
* LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
* LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
* INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
* SAVINGS OR PROFITS,
* EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
* INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
* FROM, THE SOFTWARE.
*
* This software may be replicated in part or whole for the licensed use,
* with the restriction that this Disclaimer and Copyright notice must be
* included with each copy of this software, whether used in part or whole,
* at all times.
*/
/******************************************************************************/
/** \file rtc.c
**
** RTC function driver API.
** @link SampleGroup Some description @endlink
**
** - 2019-04-10 First version
**
******************************************************************************/
/******************************************************************************/
/* Include files */
/******************************************************************************/
#include "hc32l196_rtc.h"
/**
******************************************************************************
** \addtogroup RtcGroup
******************************************************************************/
//@{
/******************************************************************************/
/* Local pre-processor symbols/macros ('#define') */
/******************************************************************************/
/******************************************************************************/
/* Local function prototypes ('const') */
/******************************************************************************/
const uint8_t Leap_Month_Base[] = {3,6,0,3,5,1,3,6,2,4,0,2};
const uint8_t NonLeap_Month_Base[] = {4,0,0,3,5,1,3,6,2,4,0,2};
const uint8_t Cnst_Month_Tbl[12]={0x31,0x28,0x31,0x30,0x31,0x30,0x31,0x31,0x30,0x31,0x30,0x31};
/******************************************************************************/
/* Local function prototypes ('static') */
/******************************************************************************/
/******************************************************************************/
/* Local variable prototypes ('static') */
/******************************************************************************/
/*****************************************************************************
* Function implementation - global ('extern') and local ('static')
*****************************************************************************/
/**
******************************************************************************
** \brief RTC计数器的使能或停止
**
** @param NewState : TRUE 或 FALSE
** \retval 无
**
******************************************************************************/
void Rtc_Cmd(boolean_t NewState)
{
SetBit((uint32_t)(&(M0P_RTC->CR0)), 7, NewState);
}
/**
******************************************************************************
** \brief RTC计数器启动等待函数,如启动RTC计数器后立即进入低功耗模式,
** 进入低功耗模式之前需执行此函数,以确保RTC已启动完成
**
** @param NewState : TRUE 或 FALSE
** \retval 无
**
******************************************************************************/
void Rtc_StartWait(void)
{
M0P_RTC->CR1_f.WAIT = 1;
while (M0P_RTC->CR1_f.WAITF != 1) //等待直到WAITF=1
{
;
}
M0P_RTC->CR1_f.WAIT = 0;
while (M0P_RTC->CR1_f.WAITF != 0) //等待直到WAITF=0
{
;
}
}
/**
******************************************************************************
** \brief RTC的1Hz输出的使能或停止
** @param pricision : RtcHz1selGeneralPricision 或 RtcHz1selHighPricision
** @param NewState : Hz1o_Disable 或 HZ1o_Enable
** \retval 无
**
******************************************************************************/
void Rtc_Hz1Cmd(en_rtc_hz1sel_t pricision, boolean_t NewState)
{
SetBit((uint32_t)(&(M0P_RTC->CR0)), 6, pricision); //设置普通精度或者高精度1Hz输出
SetBit((uint32_t)(&(M0P_RTC->CR0)), 5, NewState); //设置1Hz输出使能或禁止
}
/**
******************************************************************************
** \brief 设置周期中断的类型(PRDSEL)及其所选类型的时间(PRDS或PRDX)
**
** @param pstCyc: 根据结构体的定义设置PRDSEL、PRDS与PRDX
** \retval Ok、Error 或 ErrorInvalidParameter
**
******************************************************************************/
en_result_t Rtc_SetCyc(stc_rtc_cyccfg_t* pstCyc)
{
en_result_t enRet = Error;
M0P_RTC->CR0_f.PRDSEL = pstCyc->rtcPrdsel;
if(pstCyc->rtcPrdsel == RtcPrds)
{
M0P_RTC->CR0_f.PRDS = pstCyc->rtcPrds;
}
else if(pstCyc->rtcPrdsel == RtcPrdx)
{
if(pstCyc->rtcPrdx>=64)
{
enRet = ErrorInvalidParameter;
return enRet;
}
M0P_RTC->CR0_f.PRDX = pstCyc->rtcPrdx;
}
else
{
;
}
enRet = Ok;
return enRet;
}
/**
******************************************************************************
** \brief RTC闹钟中断的使能或停止
**
** @param NewState : TRUE 或 FALSE
** \retval 无
**
******************************************************************************/
void Rtc_AlmIeCmd(boolean_t NewState)
{
SetBit((uint32_t)(&(M0P_RTC->CR1)), 3, 0); //清除周期中断标志位
SetBit((uint32_t)(&(M0P_RTC->CR1)), 4, 0); //清除周期中断标志位
SetBit((uint32_t)(&(M0P_RTC->CR1)), 6, NewState);
}
/**
******************************************************************************
** \brief RTC闹钟的使能或停止
**
** @param NewState : Almen_Disable 或 Almen_Enable
** \retval 无
**
******************************************************************************/
void Rtc_AlmEnCmd(boolean_t NewState)
{
SetBit((uint32_t)(&(M0P_RTC->CR1)), 7, NewState);
}
/**
******************************************************************************
** \brief 获取RTC闹钟中断状态位
**
** @param 无
** \retval TRUE 或 FALSE
**
******************************************************************************/
boolean_t Rtc_GetAlmfItStatus(void)
{
return GetBit((uint32_t)(&(M0P_RTC->CR1)), 4);
}
/**
******************************************************************************
** \brief 清除RTC闹钟中断状态位
**
** @param 无
** \retval 无
**
******************************************************************************/
void Rtc_ClearAlmfItStatus(void)
{
SetBit((uint32_t)(&(M0P_RTC->CR1)), 4, 0);
}
/**
******************************************************************************
** \brief 清除RTC周期中断状态位
**
** @param 无
** \retval 无
**
******************************************************************************/
void Rtc_ClearPrdfItStatus(void)
{
SetBit((uint32_t)(&(M0P_RTC->CR1)), 3, 0);
}
/**
******************************************************************************
** \brief 获取RTC周期中断状态位
**
** @param 无
** \retval TRUE 或 FALSE
**
******************************************************************************/
boolean_t Rtc_GetPridItStatus(void)
{
return GetBit((uint32_t)(&(M0P_RTC->CR1)), 3);
}
/**
******************************************************************************
** \brief 配置RTC的误差补偿寄存器
**
** @param CompValue:数值的范围为:32-256
** @param NewStatus: RtcCompenDisable 或 RtcAmCompenEnable
** \retval Ok ErrorInvalidParameter
**
******************************************************************************/
en_result_t Rtc_CompCfg(uint16_t CompVlue, en_rtc_compen_t NewStatus)
{
en_result_t enRet = Error;
if(CompVlue<=256)
{
M0P_RTC->COMPEN_f.EN = NewStatus;
M0P_RTC->COMPEN_f.CR = CompVlue;
}
else
{
enRet = ErrorInvalidParameter;
}
return enRet;
}
/**
******************************************************************************
** \brief RTC根据日期计算周数
**
** \param pu8buf时间数据
** \param u8limit_min最小值
** \param u8limit_max最大值
**
** \retval Error 错误,Ok校验正确
**
******************************************************************************/
en_result_t Check_BCD_Format(uint8_t u8data,uint8_t u8limit_min, uint8_t u8limit_max)
{
if (((u8data & 0x0F) > 0x09) || ((u8data & 0xF0) > 0x90)
||(u8data > u8limit_max) || (u8data < u8limit_min))
{
return Error;
}
return Ok;
}
/**
******************************************************************************
** \brief RTC 平、闰年检测
**
** \param u8year:年十进制低两位:0-99
**
** \retval 1:闰年 0:平年
**
******************************************************************************/
uint8_t Rtc_CheckLeapYear(uint8_t u8year)
{
uint16_t tmp;
tmp=2000+u8year;
if((((tmp % 4)==0) && ((tmp % 100) !=0))|| ((tmp % 400) ==0))
{
return 1;
}
else
{
return 0;
}
}
/**
******************************************************************************
** \brief RTC根据年获取二月的天数
**
** \param [in] u8month月份,u8year年份
**
** \retval u8day天数:28或29
**
******************************************************************************/
uint8_t Get_Month2_Day( uint8_t u8year)
{
uint8_t u8day = 0;
u8day = 28;
if(Rtc_CheckLeapYear(u8year) == 1)
{
u8day++;
}
return u8day;
}
/**
******************************************************************************
** \brief RTC获取时间函数
**
** \param time: 用于存放读取自时间寄存器的时间数据,格式为BCD码格式
**
** \retval Ok 获取正常
** \retval ErrorTimeout 时间溢出错误
******************************************************************************/
en_result_t Rtc_ReadDateTime(stc_rtc_time_t* time)
{
uint32_t u32TimeOut;
ASSERT(NULL != pstcTimeDate);
u32TimeOut = 1000;
if(1 == M0P_RTC->CR0_f.START)
{
M0P_RTC->CR1_f.WAIT = 1;
while(u32TimeOut--)
{
if(M0P_RTC->CR1_f.WAITF)
{
break;
}
}
if(u32TimeOut==0)
{
return ErrorTimeout;
}
}
time->u8Second = M0P_RTC->SEC;
time->u8Minute = M0P_RTC->MIN;
if(1 == M0P_RTC->CR0_f.AMPM)
{
time->u8Hour = M0P_RTC->HOUR;
}
else
{
time->u8Hour = M0P_RTC->HOUR&0x1f;
}
time->u8Day = M0P_RTC->DAY;
time->u8DayOfWeek = M0P_RTC->WEEK;
time->u8Month = M0P_RTC->MON;
time->u8Year = M0P_RTC->YEAR;
M0P_RTC->CR1_f.WAIT = 0;
if(1 == M0P_RTC->CR0_f.START)
{
while(M0P_RTC->CR1_f.WAITF)
{}
}
return Ok;
}
/**
******************************************************************************
** \brief 向RTC时间寄存器写入时间
**
** \param time: 存放时间的结构体,各个时间均为BCD码格式
**
** \retval ErrorTimeout 或 Ok
**
******************************************************************************/
en_result_t Rtc_SetTime(stc_rtc_time_t* time)
{
en_result_t enRet = Ok;
uint16_t u16TimeOut;
u16TimeOut = 1000;
if(M0P_RTC->CR0_f.START == 1)
{
M0P_RTC->CR1_f.WAIT = 1;
while(--u16TimeOut)
{
if(M0P_RTC->CR1_f.WAITF)
{
break;
}
}
if(u16TimeOut==0)
{
return ErrorTimeout;
}
}
M0P_RTC->SEC = time->u8Second;
M0P_RTC->MIN = time->u8Minute;
M0P_RTC->HOUR = time->u8Hour;
M0P_RTC->DAY = time->u8Day;
M0P_RTC->MON = time->u8Month;
M0P_RTC->YEAR = time->u8Year;
M0P_RTC->WEEK = time->u8DayOfWeek;
M0P_RTC->CR1_f.WAIT = 0;
if(M0P_RTC->CR0_f.START == 1)
{
while(M0P_RTC->CR1_f.WAITF)
{}
}
enRet = Ok;
return enRet;
}
/**
******************************************************************************
** \brief RTC闹钟中断时间获取
**
** \param pstcAlarmTime:存放闹钟时间寄存器数据:秒 分 时 周
**
** \retval 无
**
******************************************************************************/
void Rtc_GetAlarmTime(stc_rtc_alarmtime_t* pstcAlarmTime)
{
pstcAlarmTime->RtcAlarmSec = M0P_RTC->ALMSEC;
pstcAlarmTime->RtcAlarmMinute = M0P_RTC->ALMMIN;
pstcAlarmTime->RtcAlarmHour = M0P_RTC->ALMHOUR;
pstcAlarmTime->RtcAlarmWeek = M0P_RTC->ALMWEEK;
}
/**
******************************************************************************
** \brief RTC闹钟设置
**
** \param [in] pstcAlarmTime闹钟时间:秒 分 时 周
**
** \retval Ok 设置正常
**
******************************************************************************/
en_result_t Rtc_SetAlarmTime(stc_rtc_alarmtime_t* pstcAlarmTime)
{
en_result_t enRet = Ok;
// ASSERT(NULL != pstcAlarmTime);
Rtc_AlmEnCmd(FALSE); //闹钟禁止以后再设置闹钟时间
enRet = Check_BCD_Format(pstcAlarmTime->RtcAlarmSec,0x00,0x59);
if(M0P_RTC->CR0_f.AMPM == RtcAm)
{
enRet = Check_BCD_Format(pstcAlarmTime->RtcAlarmHour,0x00,0x12);
}
else
{
enRet = Check_BCD_Format(pstcAlarmTime->RtcAlarmHour,0x00,0x24);
}
if(enRet != Ok)
{
return enRet;
}
enRet = Check_BCD_Format(pstcAlarmTime->RtcAlarmMinute,0x00,0x59);
if(enRet != Ok)
{
return enRet;
}
if(enRet != Ok)
{
return enRet;
}
M0P_RTC->ALMSEC = pstcAlarmTime->RtcAlarmSec & 0x7f;
M0P_RTC->ALMMIN = pstcAlarmTime->RtcAlarmMinute & 0x7f;
M0P_RTC->ALMHOUR = pstcAlarmTime->RtcAlarmHour & 0x3f;
M0P_RTC->ALMWEEK = pstcAlarmTime->RtcAlarmWeek;
Rtc_AlmEnCmd(TRUE); //闹钟许可
enRet = Ok;
return enRet;
}
/**
******************************************************************************
** \brief 初始化RTC
**
** @param Rtc_InitStruct 存放stc_rtc_initstruct_t类型的结构体
** \retval 无
**
******************************************************************************/
void Rtc_Init(stc_rtc_initstruct_t* Rtc_InitStruct)
{
Rtc_Cmd(FALSE);
M0P_RTC->CR0_f.AMPM = Rtc_InitStruct->rtcAmpm; //实时时钟小时的时制
Rtc_SetCyc(&Rtc_InitStruct->rtcPrdsel); //设置周期中断的类型(PRDSEL)及其所选类型的时间(PRDS或PRDX)
M0P_RTC->CR1_f.CKSEL = Rtc_InitStruct->rtcClksrc; //实时时钟RTC的时钟源
Rtc_CompCfg(Rtc_InitStruct->rtcCompValue, Rtc_InitStruct->rtcCompen); //配置时钟误差补偿寄存器
Rtc_SetTime(&Rtc_InitStruct->rtcTime); //设置初始时钟
}
|