EDA365欢迎您!
您需要 登录 才可以下载或查看,没有帐号?注册
x
x% Q/ y5 M1 d/************************************************************/ # G; {/ k O7 G+ M' |
/******* 样例使用说明 *************/ $ y: M9 E# I+ `- A) n, T! p% X4 H
/******* 版本历史 ************/ - u) y, l6 \3 B' }7 w! s
/******* 日期 版本 负责人 IAR MDK ***********/
) m# k" P/ T* ^$ Z/***** 017-07-14 0.1 CJ 7.70 5.14 **********/
* r7 ?5 K+ y4 |3 ^/******* QQ:3386208645 ************/ ( d" f! l }. `. W
1 R( C7 m5 s& p# X% Q
/******* 功能描述 ***********/
$ K! Z( U% w* \4 }/ l% S/ e/* 本样例主要涉及时钟模块计数寄存器的写入和读取**********/ 4 ~% ?2 h. |# |1 R, H# \
/*说明: *************/ 0 V& b$ D0 E4 x+ Z1 J, ^
/*计数初值写入计数寄存器 *************/
" n8 l$ U4 i0 C- `0 n/*启动计数 *************/ + N* A9 F( J' ^3 X3 w
/*读取计数寄存器数值 *************/
7 g# ^3 u! m- _$ n9 d2 A/******* 测试环境 *************/ $ P) i' _3 R" {( A7 p
/* 测试用板: *************/ 1 j( a1 ?% w! G- K
/* SK-L110-TSSOP20 V10 *************/ & n8 ]* X. I: p. l y3 |5 |3 A
/*******辅助工具: ************/
& l( X! E0 f& {/ ~& ~+ F2 s/* JLINK调试工具 *************/ 9 l. P( A% Q# [& E8 ^
/************************************************************/ . N- \# K. t" ]2 t
#define IS_VALID_CLK(x) (RtcClk32768 == (x)||\ 6 Z. v, a/ M9 I/ `' b2 |+ P
RtcClk32768_1== (x)||\
1 I/ g& J+ _( p% v1 a' k RtcClk32== (x)||\ 4 Z4 r: D9 l5 K7 y
RtcClk32_1== (x)||\
! v) J: [/ }. \9 B+ n+ z RtcClkHxt128 ==(x)||\ ) O* i, q% \2 @+ X; e$ ^
RtcClkHxt256 == (x)||\ 2 _8 L9 m) [% Q3 y
RtcClkHxt512 == (x)||\
6 h* y D* `5 a+ p4 {& {$ U RtcClkHxt1024 == (x))
6 R: E" ^2 j. i' _, E1 P. X6 F
( ^, k* j6 ?# n; m }. O6 {#define IS_VALID_CYCSEL(x) (RtcPrads == (x)||\ * D' Y( n7 B$ e( d( ~
RtcPradx==(x)) 2 Y$ d! x7 z6 m
: p1 B" _3 n9 R Q' o9 e#define IS_VALID_PRDS(x) (Rtc_None == (x)||\
, F3 a9 ~2 C4 p7 r. R+ q' m5 F Rtc_05S == (x)||\
; Q0 ?% i1 s! t- P G. g4 x; ]; B Rtc_1S == (x)||\ 0 b( @$ r# t" c7 `% H: C
Rtc_1Min == (x)||\
/ G7 @" `! r; G% d2 ~6 |( z Rtc_1H == (x)||\ + j% K& \+ d- [: _
Rtc_1Day ==(x)||\
1 k# S) k; w8 y1 _* y. h1 i8 o1 s# V8 x Rtc_1Mon ==(x)||\ % x+ S- u! \" G
Rtc_1Mon_1 ==(x))
: x( U+ Q: M6 Y 7 B X9 m- K( t9 J5 {- [3 D( z0 E
#define IS_VALID_IRQ_SEL(x) (RtcPrdf== (x) ||\
' L( f5 {& D- s5 g( q RtcAlmf== (x)) ; a! H |1 J0 g/ N% N
. k, v A8 b1 ?#define IS_VALID_FUNC(x) ((RtcCount<=(x))&&\ 3 f# z: a* @" k8 ?
(Rtc1HzOutEn>=(x)))
) b$ g$ c8 ^" \#define CkDateTime 0x7F : O7 a: v3 @; I
#define CkDate 0x78 * a! C2 c! }$ h2 P4 D% y5 X
#define CkTime 0x07
2 I1 o' h$ q4 P% |* r, {#define RTC_TIMEOUT 1000//test 1s
8 q0 q1 C% [6 V# g#define T1_PORT (3) * Y2 A$ G ^4 }$ {; u
#define T1_PIN (3) : ~0 j+ }, o9 |& n) h, {$ p
uint8_t cyccnt=0;
* R* M' g. n/ w7 n& U# [8 xuint8_t u8Alarmcnt=0;
5 R; R( e' w- r7 d& a# d/ h7 G5 i- U3 _uint8_t flg=0;
0 }. e( G. P2 M7 K: G; i; v& `stc_rtc_time_t stcReadTime;
5 \1 u- p6 U. H4 { ; j9 s4 ^/ ~4 [- n) w) O
/********************************************************************************** \brief RTC计数时钟选择 ) @5 T1 w( G5 A; v* F
** ** \param [in] enClk时钟源
" f) X" {! p7 ~- d5 ?7 W: g** ** \retval Ok
8 h' |1 ]$ e6 ^+ G4 F) {4 D P******************************************************************************/
$ `, _: z$ M( Men_result_t Rtc_SelClk(en_rtc_clk_t enClk)
- L8 h! n! ~8 ]/ ]# R( Z{
" J% c, Y5 D" K, O! b0 l en_result_t enRet = Error; 5 H, g$ G7 Z4 G
ASSERT(IS_VALID_CLK(enClk));
L" \1 p* d! x9 c/ Z- P, A M0P_RTC->CR1_f.CKSEL = enClk; 8 k t: }! p% M( a3 e+ g* b+ ]
enRet = Ok;
1 |& T& W) E. H" N- C. Q6 m9 T, r$ t) t return enRet; 0 F% Y! u4 C, I
}
. _( ^: D6 L: R/**
2 B( A. c8 ?, [5 o ******************************************************************************
9 r( d. l& Q; P' i' D' B **\brief RTC周期中断方式选择
! _$ Q+ l9 B" j# A ** 9 r5 i2 l* L. w; _ E9 _) L
**\param [in] pstccCyc周期中断方式及周期间隔选择 4 G9 y" m9 a$ f* S+ C j! O* ?
**
. {, i+ y, x9 k" w' X5 D5 E% A0 ` **\retval Ok 9 m7 [- K: x: ^: b5 a- q
** , W3 k( V$ R, |' G! b' p
******************************************************************************/
( e) X: I$ _+ b2 Yen_result_t Rtc_SetCyc(stc_rtc_cyc_sel_t*pstcCyc)
8 A1 m2 p. L+ A# K0 w# j' f$ [3 x& Q6 D{ ( Z9 Y3 h `# X0 k" L
en_result_t enRet = Error;
7 G" ~9 R7 m8 H, B0 h ASSERT(IS_VALID_CYCSEL(pstcCyc->enCyc_sel));
# _4 g# @' ]; Q9 F8 f9 K8 g ASSERT(IS_VALID_PRDS(pstcCyc->enPrds_sel));
8 _& T" G7 D: S; _( k0 q M0P_RTC->CR0_f.PRDSEL= pstcCyc->enCyc_sel;
: A. ]% @- ?5 ]" a4 i j8 D9 H9 ] if(pstcCyc->enCyc_sel) , }" X8 |% J9 Y% k9 {% x1 }' O
{
' k0 |5 L: ~' R' f+ m M0P_RTC->CR0_f.PRDX = pstcCyc->u8Prdx;
$ e8 b- w: z5 ^ } + j5 c& S" G) d, Y
else
' J5 H5 [* D: g3 t q6 A: H E {
( L- X* \1 `4 ? @8 H M0P_RTC->CR0_f.PRDS= pstcCyc->enPrds_sel; : E: @$ q$ a/ c4 H7 }& p p
}
; y( b: U6 w \( f! n; k enRet = Ok;
# t. O$ p- r- S8 C returnenRet; 1 ^, {7 O, F+ |! I ]6 I2 \: m- q
}
2 S! J2 _" U) j- Y# F! ^/**
. k/ C7 } h |3 _/ N( D) i/ J ****************************************************************************** ; e5 G) _* q$ X- v
**\brief RTC时制选择 ! o' y+ W) `- g9 q) t
** 6 P+ v: ?$ Z; G& F& j+ ^( m0 H" n' |
**\param [in] bmode是12时制or24时制
' X. }! l! }, Z+ @% w3 h" u **
8 Z+ b2 Z& k/ @: W! q. F/ D& J( l2 X* t **\retval Ok 设置正常 ) P" c7 G$ N1 Y2 Q: ^6 [
**\retval ErrorInvalidParameter 设置异常
% g" e4 C3 A; C. ^0 ]. ~; ~ ******************************************************************************/
m% ?6 K& ]3 ~* u% A* R+ ]en_result_t Rtc_SetAmPm(en_rtc_ampm_tenMode)
1 O4 `3 V0 u) m, U6 v, U{
4 s2 L: q( l z! r) N& o en_result_t enRet = Error;
! V1 j7 q4 h0 x switch(enMode)
# Y2 s3 L# L6 U" { ^ {
$ K+ J/ ?9 j1 k* R) j" k1 N. ~ case 0: ! \/ _7 v7 K: q2 ~ g
case 1: : n6 R( G1 H: u0 ]% w! @ ]
M0P_RTC->CR0_f.AMPM = enMode;
f) G+ B! A0 n" ~/ K break; % S A! z! V5 n: V$ X
default:
3 `6 H/ w. U6 ? s return ErrorInvalidParameter; - p0 \9 N6 b p& f
}
2 Y# N* g- H9 C$ a, r) ? enRet = Ok;
" D' ^$ c$ R' i) p8 ] return enRet;
; C) M1 x' C" N1 i} , D9 S% ?3 V& f
/** , @' r( r5 S1 h% O# p8 X4 p! j
******************************************************************************
4 ]5 f8 D2 S& K$ | **\brief RTC闹钟中断设置
9 k% h! n5 M/ C9 W+ `* a0 U **
; {* C8 e }4 }6 ]' g **\param [in] pstcAlarmTime闹钟时间时、分、周
9 U( U# m) t: R- J# i- w **
; P! v( ?' s F# L9 Q3 n! |' f: w **\retval Ok 设置正常
5 `1 X9 d" y+ N8 q1 w ** " X; o) U# y* D4 A/ ?) D4 c' D$ i
******************************************************************************/
8 F1 U* H( p4 ^6 {en_result_tRtc_SetAlarmTime(stc_rtc_alarmset_t* pstcAlarmTime)
7 v! ~. l+ e1 {9 W{ ( }( P: I5 v3 C [" ?. v# d" B b
en_result_t enRet = Ok; ( |& Y5 K' u, g3 a6 b1 E5 f
ASSERT(NULL != pstcAlarmTime); , _) h/ M, ?5 c9 z$ e$ G
if(Rtc12h == M0P_RTC->CR0_f.AMPM) : @- R( `( g! U/ @3 G$ N+ X# s7 V+ N
{
0 M- p; y& P7 H$ t enRet = Check_BCD_Format(pstcAlarmTime->u8Hour,0x00,0x12); 7 N" }) } M% ]* s0 l* o! E. S
}
. ^ `9 k* N3 j else ; O! s. z) c8 }; n. o$ u5 c
{ $ P/ N8 `$ i1 O/ A, W
enRet = Check_BCD_Format(pstcAlarmTime->u8Hour,0x00,0x24);
' b- l( `' k+ ~9 j5 E }
3 c8 x( A, v9 [ if(enRet != Ok) $ K4 [8 C7 M, }7 J
return enRet; % e9 I% t* |$ `* b1 M( v6 ?
enRet = Check_BCD_Format(pstcAlarmTime->u8Minute,0x00,0x59); % b7 w4 w' C. ^0 i6 ^) b9 g* A) w$ r
if(enRet != Ok)
W. H* v: |' A+ ~* d; V return enRet;
+ _; O0 L- i3 W; d3 {0 F$ n3 d, M6 p //enRet = Check_BCD_Format(pstcAlarmTime->u8Week,0x00,0x06);
. ~6 x: x$ ^3 v9 R2 [! M if(enRet != Ok)
- t# w' J3 T5 c# n6 m: W0 R return enRet; & R/ ^4 s- c3 b) c
M0P_RTC->ALMHOUR = pstcAlarmTime->u8Hour;
V1 y& G, L8 |2 c7 |+ V7 N$ t M0P_RTC->ALMMIN = pstcAlarmTime->u8Minute; ) {6 v8 f' i2 l9 b4 G# L5 y; M9 I
M0P_RTC->ALMWEEK = pstcAlarmTime->u8Week;
& o9 _4 R1 j Y7 n$ f1 b: Y enRet = Ok;
2 C% I) r; T8 a6 z! F! i7 E7 }' W return enRet; ( ]# m. T7 F) x/ t5 w
} 9 E; R: w! Y" Z e/ U( u
/**
& b& X7 Y/ h" i* g. g' [) C ******************************************************************************
# c2 l# C, l/ z4 d **\brief RTC闹钟中断时间获取
5 n6 S4 R+ C( {2 c, w7 i **
4 _2 h* n( q2 w **\param [in] pstcAlarmTime闹钟时间时、分、周
- C6 w0 [) `- ~% r" d **
9 }7 G& J. o0 b8 f# m* ]9 ^ **\retval Ok 设置正常
& o. G/ e" }$ @0 ^6 a! U$ L* n ** 6 I1 t3 n9 F8 k
******************************************************************************/
% l% }% o& @/ A) Hen_result_tRtc_GetAlarmTime(stc_rtc_alarmset_t* pstcAlarmTime) O: j1 d3 c: S6 T) b% [) V
{ 2 Y7 ^" |! m6 I- }4 l* O
en_result_t enRet = Error;
8 y& k$ v& {+ `: ^ ASSERT(NULL != pstcAlarmTime); 2 k' e. G3 E: M# d" _) Y0 j, N3 }
pstcAlarmTime->u8Minute = M0P_RTC->ALMMIN; 1 P) [2 ]) A* H4 G/ |
pstcAlarmTime->u8Hour = M0P_RTC->ALMHOUR; 2 K6 S1 `& p5 ?% j- k \" ?' g: v4 _
pstcAlarmTime->u8Week = M0P_RTC->ALMWEEK;
# e. O: [( X1 W& P! k3 M+ S8 n3 C enRet = Ok;
+ `. Z c+ U8 _& F. v2 H L return enRet; " D3 U: `& r* V* d6 _ t
}
+ k7 t! {( p; \" b, C7 X" s/** 5 y5 z5 _6 v3 k3 m. T6 z" U
****************************************************************************** / D! G) }, o- {' J& D3 C
**\brief RTC 1hz模式选择
1 o3 G: \: m0 A6 T$ Y1 c ** . J( }, k! g- Y3 C
**\param [in] bmode 高精度和普通精度 3 q8 Y# x5 D+ w4 s( i6 X0 |
** - c* U' A7 d. k; r
**\retval Ok 设置正常
/ B P+ l6 b3 u% m) f ** ! @6 q9 m2 X% B
******************************************************************************/ ( D! \, _/ |5 l0 E/ D& ~) v& v
en_result_t Rtc_Set1HzMode(boolean_t bMode) & a; G& y. o# H0 W& l3 L
{
0 d/ V% }9 ~3 b6 q8 x$ {1 a en_result_t enRet = Error; 3 J% D6 I# G+ O# ~6 C2 e) Q8 K
M0P_RTC->CR0_f.HZ1SEL = bMode;
$ ~4 ?; l+ |; q+ T& m) O: ^ enRet = Ok;
: D0 v' L6 e6 v' @6 |( m% v/ h return enRet;
1 J6 y6 w7 R& m; ^ K7 p9 R} + `1 I( V( g# C7 [+ b* A
/**
, N! V: t0 T, x% M' j ****************************************************************************** % H8 \2 Z( y' q, O
**\brief RTC 1hz补偿值设置 - g! S- W6 _3 T$ R' `% A" U
** - [# @ I( a7 n! x1 a. b
**\param [in] u16Cr 补偿值 ! V' E! n$ x8 {
** . \6 V, U- K$ d( [3 B# ?
**\retval Ok 设置正常 ! C& L# v. M/ r5 W' H! Y
** / R: ?3 M; ]: M+ a
******************************************************************************/ / e: q. @. k/ a$ x5 w
en_result_t Rtc_SetCompCr(uint16_t u16Cr)
% {- F& ^8 J7 D2 F{
0 E6 @% C# h4 h C b5 l( @' r6 Z+ | en_result_t enRet = Error; ) X4 \- B) G& u+ d z
M0P_RTC->COMPEN_f.CR = u16Cr;
2 O0 O t+ |& ?4 h5 j enRet = Ok; 0 t6 u) i" O; v, {6 N2 Z
return enRet;
0 r! _5 O. e+ M/ E} + d3 r: ~9 k4 I; }* ~& k
/** ) } `( t# {+ L$ o/ \
****************************************************************************** ' i& j% T+ X1 N# O3 R
**\brief RTC 功能使能设置
- [9 X/ U$ K: V; t/ B **
" ~- }* ?$ G' _; j1 z **\param [in] enFunc 功能选择
" O# t. ^7 @0 Q _ **
+ m: I5 a* { U/ `$ A. z j **\retval Ok 设置正常
8 `# T2 `& [( {0 S$ y **\retval ErrorInvalidParameter 设置异常
" s" w) `( J/ `* o% O ******************************************************************************/
3 f' ~1 f' \- J9 Xen_result_t Rtc_EnableFunc(en_rtc_func_tenFunc) / G5 J+ |+ \0 m3 I( D8 _
{ / E9 s5 R: y4 `1 S2 P5 D
ASSERT(IS_VALID_FUNC(enFunc)); 4 [- a1 x5 T' t/ o7 `7 r( `( g0 S
switch(enFunc) , w' m' T& }0 {. h5 K% k
{
: J* V, G' I4 Y5 @' a L! _4 D case RtcCount:
9 ?- T1 r+ t6 j% j M0P_RTC->CR0_f.START = 1u; 2 m7 \. X8 [. f! R0 W
break;
5 ~& S, I6 ]* T* H. b* z case RtcAlarmEn:
& ]1 e' |/ E4 l+ ?0 ^ M0P_RTC->CR1_f.ALMEN = 1u;
. {+ ~* {. p1 o, p4 Y$ M! ` break; " ^. r- j5 { k- l5 j6 y
case Rtc_ComenEn: ! w' x! O% {- _
M0P_RTC->COMPEN_f.EN = 1u;
) v6 w* |' D# n0 H0 S+ [/ `! Y7 b. W: m break;
+ T- p: g# u! [7 [6 d; T case Rtc1HzOutEn:
% u0 r$ @6 H1 h7 b) V M0P_RTC->CR0_f.HZ1OE = 1u; + W L+ k9 g% g
break;
1 D3 u! `( y. `9 k( p0 b# O default:
. I. {. I9 H5 d$ b5 ~ return ErrorInvalidParameter;
+ q. @& e7 l4 F) @# u+ {% l! P0 k } ! J! Y0 n/ f3 E7 i9 T6 \+ ~
return Ok; * {& f3 q I3 A/ H0 ^$ I6 E. @& H
} ( P0 l6 [( ^8 @+ C1 f
/**
0 |- O% ~" Q2 K ****************************************************************************** 8 f# G' `3 r' O! @" O* ~9 j
**\brief RTC 功能禁止设置 4 N5 m7 z% x; m A B; }) N' m
** ) O% i1 J {& f7 {( z
**\param [in] enFunc 功能选择 & J9 L# k% ^& @$ D u6 X; [; p
** % ?, \$ G6 i/ T9 c" | n4 h1 j' F, l3 B
**\retval Ok 设置正常
% S8 k- \7 f) X$ [* w8 B **\retval ErrorInvalidParameter 设置异常
0 C) G7 q* m: b0 b, i ******************************************************************************/
0 i6 _* g5 ]6 s- a$ W; a- v6 h% Cen_result_t Rtc_DisableFunc(en_rtc_func_tenFunc)
$ l( E. E; N9 x1 Z, ~; A{ e, S* W, p' @1 G, ]/ C
ASSERT(IS_VALID_FUNC(enFunc));
{1 K' n- ]2 W5 A# t. y1 g switch(enFunc)
9 L& ^ l9 R1 c { - o* i* L8 b/ x3 u, l( G, ~/ d
case RtcCount:
! y, J! J5 }- g; R M0P_RTC->CR0_f.START = 0u;
1 U6 i. Q+ ]) F$ c/ V. b8 l break;
3 ~' J3 x, } v* m; v; v* y+ ? case RtcAlarmEn:
# J! `" R; C4 T" d2 Y7 `( _ @ M0P_RTC->CR1_f.ALMEN = 0u;
. c+ z6 }9 z) a, v! H7 J break; 4 z5 C# @) T& }+ [
case Rtc_ComenEn:
* d0 F6 t& K1 C+ J' ^ M0P_RTC->COMPEN_f.EN = 0u;
1 R, P9 `2 ?0 d! s5 G break;
$ |0 ]9 Y1 z! S$ M/ F0 |0 z3 X case Rtc1HzOutEn: 4 R- L# z0 @" F4 o$ L9 c
M0P_RTC->CR0_f.HZ1OE = 0u;
% X" C% u+ L! q break;
( D' I$ V3 L8 E! o1 D& X default: " N! b' E9 s) Q- Z% J9 ~8 D
return ErrorInvalidParameter;
& B. L K4 f' s5 C/ `9 U- i5 r5 V }
5 p0 n- |( I6 ~4 h- k, Q return Ok; " A( R7 n5 y+ T c% v5 G
}
D4 g" j n. K Vuint8_t Change_DateTimeFormat(uint8_t u8sr) ' X" I/ q& v) z; L# w. b! H9 w3 h
{ 6 u2 j0 S {5 t8 O
uint8_tu8de=0;
: S: Y3 R1 p+ z# }1 Z& j' }/ W# R2 \) M while(u8sr>=0x10) 8 y+ T+ J/ q' s/ E$ B" w
{
% `# D" x" X6 X' G! Z u8de+=10; - A4 g$ S4 j. ]) @
u8sr-=0x10;
- X- p, B- G+ c3 I# N+ z* _ } 6 t+ b0 x# u% ~& I- b$ u
u8de+= u8sr; 1 `8 B: l9 s& R6 y6 m
return(u8de);
4 U1 S! {- U4 @1 \5 ~$ i}
; v: _$ g5 _3 M. J/**
& g J \2 p2 P8 l" S9 T+ ^ ****************************************************************************** 0 m4 B5 c! |5 s3 ~
**\brief RTC 平、闰年检测 ; I8 W f' S! J: f: y$ w
**
) b1 V6 x' g$ L **\param [in] u8year 年十进制低两位 9 K9 n3 V6 m# E# K6 p+ s
** ( _9 R, i: a3 [3 D
**\retval 1 闰年 r" s' ~ ]; ]* |, m9 n) g
**\retval 0 平年 . ~$ D4 l5 R/ F* v6 C+ ^* r
******************************************************************************/ c, d$ e: t1 X4 z) F, Y
uint8_t Rtc_CheckLeapYear(uint8_t u8year) 2 Z& o7 z- P% ^9 l; e" x [( k# x( h
{
2 u- u& {* Q& h/ T uint8_t u8year_shl,u8year_shr;
7 n: w! L" p! M# t) `8 P* J" _4 D u8year_shl = u8year>>2; 6 q8 x9 b0 p* C' E6 s
u8year_shr = u8year_shl<<2; / e. k5 ]; G' k0 U6 j" r7 V1 @) N
if(u8year== u8year_shr)
+ J* i( g; U2 H2 y1 Q# b { 0 _8 m" c* Y! S- J
return 1; : x0 G# L2 x7 c) I3 e% D# @
} _: y9 ~! S" `5 _9 r
else
3 i5 B% p1 r" P! }: A { - w9 q( d' ` n' ~& Z
return 0;
, \ Z6 C+ s) E+ l5 A } 8 N/ h S2 _ e1 y Q3 y# S
}
; ]0 L P( X$ U6 \: \/** & l7 U: ~9 F( ?2 s& e. T( x5 B- v1 O
******************************************************************************
& g" _7 w3 ]" @2 Z9 }0 Y9 V) _ **\brief RTC根据日期计算周数 8 k& v& i6 F, o( G3 D. M/ M
** ' T) |$ c: i! B
**\param [in] pu8Date日期 3 D, N! U1 @/ X: X
** . K' }. _# G; E8 V0 F* e1 p7 _
**\retval week 周数 " C h( f+ A! Y9 ?$ K% C
** 2 C# O* q9 n, u$ ?# q
******************************************************************************/
$ i4 n4 C% o2 K& X* p) ]& k9 @uint8_t Rtc_CalWeek(uint8_t* pu8Date) 0 \5 c* b7 m8 F9 L" R _* T
{
A3 B% v) u6 R uint8_t u8week; & T: o* j* r( n. Y! [
if((Rtc_CheckLeapYear(Change_DateTimeFormat(*(pu8Date+2)))==1)) 0 Q2 ]( S9 s4 N8 ]/ {7 u( g
{ / ^* X0 A+ x; N9 @8 _8 @
u8week=(Change_DateTimeFormat(*(pu8Date+2))+Change_DateTimeFormat(*(pu8Date+2))/4+Leap_Month_Base[Change_DateTimeFormat(*(pu8Date+1))-1]+Change_DateTimeFormat(*(pu8Date))+2)%7;
! X5 R! B: g3 j* A& W0 k6 X: [ }
: I6 f7 L- _7 }8 D else 9 X9 F4 |% S4 Q ? ~2 |
{ + t9 E; U0 M% a* Q! q4 O3 G$ c
u8week= (Change_DateTimeFormat(*(pu8Date+2))+Change_DateTimeFormat(*(pu8Date+2))/4+NonLeap_Month_Base[Change_DateTimeFormat(*(pu8Date+1))-1]+Change_DateTimeFormat(*(pu8Date))+2)%7; 5 `5 \, L) O- B5 U8 _
}
$ l8 u; B+ U% z- W5 V0 A- C return u8week; / g2 w; f7 O; v3 F0 E6 H, K& ^( V9 @
} / m' r, V X2 `4 Y6 O8 ~7 K/ n h
/**
8 j1 T& ^4 q+ h$ m" B. X5 \ ******************************************************************************
5 y- Y& F9 i& ~: b6 n0 [ **\brief RTC根据年月获取天数
4 Z0 j' Y7 N3 E7 |0 Z; ?9 w ** 8 A. v) X4 N7 b7 ]6 K0 ]- C
**\param [in] u8month月份,u8year年份 3 S% R. w. P% K5 v9 g! ~
**
7 \" j+ M9 o; r8 F* D$ f5 J **\retval u8day天数 6 X7 A! F. l) A2 o
**
3 Y: ]2 N0 |7 `8 l3 H4 C ******************************************************************************/ , s2 F+ H6 [* ^; b+ z" S
uint8_t Get_Month_Max_Day(uint8_t u8month,uint8_t u8year) ) ? h O0 ]1 k
{
+ ~% W/ N* J; L+ K( Z5 Y uint8_tu8day = 0; 6 Y( f) S; e1 `3 h3 }, R
# i5 Q( F2 w0 x$ x( H l/ y u8day= Cnst_Month_Tbl[u8month - 1]; $ ?( \6 y4 r6 A' K, K, I% r0 @" l
if((u8month== 2) && ((u8year % 4) == 0))
$ s: S: y; P( l2 | {
" ~: h" o& }2 B" y: N# U5 D u8day++;
: k; P: D$ ^- e7 `& Z } 0 F1 ^/ d7 p" s+ h v: q
return(u8day);//day的格式是bcd码,例如;日为31天,day=0x31 & d* P$ o7 ?- s8 R4 `; l
} & D% e" ]! e& ~# E" q
/**
. q& v2 ^ y- h$ k& H1 j m ****************************************************************************** 7 ]5 D8 ^0 @6 U$ T6 S$ I
**\brief RTC根据日期计算周数
: s; h& u h0 B0 j: U* _ E ** ) l+ x' J$ h! d& z! {/ ?8 c
**\param [in] pu8buf日期时间数据,u8len检查数据长度,u8limit_min最小值,u8limit_max最大值
; `. U5 G% k$ g ^8 F2 }5 ^ **
" Q u {0 J+ _9 u% T **\retval Error 错误,Ok校验正确
' R! K/ K+ H/ P% s **
8 O/ b% o0 x! Z; b/ q5 ^" U ******************************************************************************/ # l2 B$ O$ ~5 i& {5 j% U
en_result_t Check_BCD_Format(uint8_tu8data,uint8_t u8limit_min, uint8_t u8limit_max)
# q3 y) [# L Q& X! K4 u{ , [! `% o. I) b( `" c
5 W* \, M: Q( y' I, X' T if (((u8data & 0x0F) > 0x09) || ((u8data & 0xF0) > 0x90) + p7 d4 b3 N) g$ y
||(u8data > u8limit_max) || (u8data < u8limit_min)) % J7 j9 }3 E `: P& f. y
{
5 m8 I6 L4 P/ P1 c5 n& N5 Q. P' ^ return Error; 0 i. ^" M" _4 t9 T4 X! G
} $ G& A( c" M, n( q% E% r2 o$ C& K. K
return Ok;
6 c7 {, r$ v \5 \* r, L0 K% L! ^( q, J} ! @( ?+ _5 B: w% u$ D5 r3 }
/**
0 [: r1 f' l8 H* @% c- S8 @: s ******************************************************************************
# F) v8 z+ u: n h! @+ V z0 U; d **\brief RTC时间格式检测
: d6 I* q0 y d d7 x ** # y0 v$ [% l8 n
**\param [in] pu8TimeDate日期时间数据,u8Mode检测模式
: i$ \% i8 J9 \/ e. ? **
$ J9 J$ W% x, B **\retval enRet校验结果
. ^3 g- l0 A7 D d. q' S% Y0 Z) b ** 7 N5 o0 _! |1 S$ L w
******************************************************************************/ " ~* d/ U r U* s- w+ v3 h5 Q' y
en_result_tRtc_CheckDateTimeFormat(uint8_t* pu8TimeDate,uint8_t u8Mode) + y8 k1 C& Y- O- q) t: |
{ : b' o, C F/ d0 B$ w
uint8_t u8i=0;
, y+ t3 p1 C6 _ uint8_t u8mon_max_day = 0x28; 8 A% d4 C+ M3 f; y" a
uint8_t u8date[3];
- | ]/ C% k' ]0 I# C( v en_result_t enRet;
' B8 z$ `3 G1 s/ A& J) m( z, C) o while(u8i<7)
r& `) [' \& D! t {
( W/ |+ U( @1 f/ I$ m if(u8Mode&&(1<<u8i)) ) N1 t! k7 K5 A' \$ O$ }
{
! O& H0 P1 [5 X$ F" e switch(u8i)
. J4 Z/ G8 S( v) f* \( ^; i" G F( h {
( R. `+ V! T3 _) [, t" h0 B case 0:
8 o& I$ D( R1 y3 q+ o enRet = Check_BCD_Format(*pu8TimeDate,0x00,0x59);//秒 0 M, I: }5 r% I J% c d! t
break; - `" \5 p1 I, d M ^8 ]
case 1:
1 [4 m3 o4 w. k4 g5 O Q+ Z enRet =Check_BCD_Format(*pu8TimeDate,0x00,0x59);//分 . D4 W# h9 H) p! x% }3 D- R: i
break; : d, z* y( t: o! l4 S6 M" v( X
case 2: 0 q! D) j& f9 f0 V5 X7 B( T/ ~8 a
if(Rtc12h == M0P_RTC->CR0_f.AMPM) % ~. M- f9 X6 s8 p
enRet =Check_BCD_Format(*pu8TimeDate,0x00,0x12);//时 3 V4 k+ S1 V% N/ ]* \% s& f
else
0 J; g, q. V+ C8 C8 K enRet =Check_BCD_Format(*pu8TimeDate,0x00,0x24);
% o& e) `$ U' [ break;
. S1 g8 V: p6 g5 x case 3:
( T# o. _. p( j: ^' R/ I2 f enRet =Check_BCD_Format(*pu8TimeDate,0x00,0x06);
) B8 @" b( @: C' W- v4 } break; 3 h5 v. \6 t- f* B" Q/ y* `3 w5 Z
case 4: 6 K( C5 O7 o7 n* J
enRet =Check_BCD_Format(*pu8TimeDate,0x00,0x31);
' r" a. l0 _2 O u8date[0] = *pu8TimeDate; 7 _7 F4 H" W3 A4 N$ x# j" P1 t
break; 6 n( l, q, I1 G3 h. [' h, r
case 5:
1 ~5 _; G; D0 V7 ~" M j enRet =Check_BCD_Format(*pu8TimeDate,0x00,0x12);
* Z4 F6 [' Z6 [: M8 w- d/ X u8date[1] = *pu8TimeDate;
& h$ }; T6 w. w# r/ T2 r! ? break; 0 x9 b' s( U" s
case 6: " q! k" s' U( Y5 R6 I+ G! _
enRet =Check_BCD_Format(*pu8TimeDate,0x00,0x99);
, o. X1 p0 ?1 J7 j u8date[2] = *pu8TimeDate; 0 L+ _$ u5 n/ y. _' t+ a2 @
break;
! V% A. D F# `2 k }
# U1 \& u h0 e& w' \ pu8TimeDate++;
& \0 x' v6 m' i* P' i }
0 A+ f5 P. [% U. V( i0 e if(enRet!=Ok)
# |* ^! S) |( G4 [1 h return enRet; ! [/ g1 v( d$ J) B( q
u8i++; - r" M. h3 e3 p6 a2 @, M
} % p- G, c \6 Y! d% Z/ {" R
if((u8Mode&0x10)&&(u8Mode&0x20)) 2 w; Y+ R* P7 R& z
{ % D _- e' V$ P/ N
if(u8Mode&0x40) : G: ]7 g/ C- |/ L0 R7 `! U, F
u8mon_max_day= Get_Month_Max_Day(Change_DateTimeFormat(u8date[1]),Change_DateTimeFormat(u8date[2])); 0 S/ \0 Y# Q2 d) M) F5 a* m
else ; @0 ~8 S6 G" `
u8mon_max_day= Get_Month_Max_Day(Change_DateTimeFormat(u8date[1]), 1); 3 y, Y: p+ I: z" W P
if(u8date[0]>u8mon_max_day)
$ b4 c5 H0 r" q( t4 N4 {4 O returnError;
3 z/ s5 ~5 [; Y/ g0 l4 s }
. K9 V4 y/ D; B7 V2 C4 S if((u8Mode&0x10)&&(!(u8Mode&0x20)))
( e K7 J4 m+ l, f {
& Z* j; H* T# \4 \; m if(u8date[0]>0x28)
5 c. q5 j' g6 ?/ X' `, j returnError;
% y' X; @* G9 Z1 Z }
P4 L2 h% {' n6 Z0 d5 c enRet = Ok;
! y/ V! F2 Y' J* Q v return(enRet);
" U% r E7 N% W: _& U}
5 }8 Y% m. h, z/ [' z) l/**
9 q; H$ s& N9 b9 ]) d ****************************************************************************** 5 U" N! ?( D/ e, B) k
**\brief RTC设置时间函数
- Q& d2 P. Z: U. ]/ C# { ** ' d$ n' z% ]% c$ X' @
**\param [in] pstcTimeDate日期时间数据、bUpdateTime是否更改时间、bUpdateDate是否更改日期 " i' n+ |3 p {. q; s. }/ k0 `3 W
** . ?$ `# r! C* G8 s& p* [3 T5 j
**\retval Ok 设置正常
$ u- i8 f2 \; k **\retval ErrorTimeout 时间溢出错误 ! m5 Z7 W' \) W% v6 ~
******************************************************************************/
c- C' s9 z yen_result_tRtc_WriteDateTime(stc_rtc_time_t* pstcTimeDate,boolean_t bUpdateTime, 2 @' x6 R: P& Y8 V
boolean_t bUpdateDate) 9 Z. z. d- a- B; l4 D, W4 G
{
6 C8 k& `3 x. Z& {+ h& C int32_t u32TimeOut; " A1 f8 P l6 ~
u32TimeOut = RTC_TIMEOUT; 0 {# X1 M4 U+ Q6 Y ^
uint8_t* pu8TimeDate;
( p3 N/ {- M- S3 r" w en_result_t enRet = Ok; / @/ P% g2 ?. {3 g6 t: G+ G
pu8TimeDate = &pstcTimeDate->u8Second; 5 @4 p* O" }. O
ASSERT(NULL != pstcTimeDate); ( }# x! i$ U/ g! ]. A
if(1 == M0P_RTC->CR0_f.START) 0 W( g8 l7 E1 Z# `6 g
{ : W1 p' @: r) h- ^# _' M( b9 R
M0P_RTC->CR1_f.WAIT = 1;
5 V/ W* f K6 E( C while(--u32TimeOut) ' G. v; g z0 p3 t- ?5 n& {: I
{
+ v8 s* \0 g, ?0 P0 c. i% v if(M0P_RTC->CR1_f.WAITF)
e% [' a k* b { , { r1 ~$ U5 R$ z6 c
break; 1 j* q0 @ V8 A
}
, t2 E$ y% G% c8 l( ? }
; `4 H7 Y$ ~0 A" K/ O; K4 R' E: m } if(u32TimeOut==0)
L, ?# C! L$ c$ ^$ y w5 \* Z { 6 b) o/ }% } {9 }. C5 Z5 r
return ErrorTimeout; ! S" k3 H" X( D: `+ B
}
$ G* }9 Y3 V$ Q2 v }
! t2 w/ X0 H6 P6 S$ E7 r3 _4 Z) j/ ` if(TRUE == bUpdateTime)
, n3 @! h4 A: I, m {
2 S/ A8 `2 B7 `, C7 a3 P6 Z8 G enRet = Rtc_CheckDateTimeFormat(pu8TimeDate,CkTime); 0 \5 [7 j1 e! [) Y L6 E6 s
if(enRet != Ok)
. s/ i. M$ u; z return enRet;
0 ]4 ]2 m/ F: z6 t" j M0P_RTC->SEC = pstcTimeDate->u8Second;
; c+ l4 m }9 h+ V# O. {4 { M0P_RTC->MIN = pstcTimeDate->u8Minute;
{- ?' f6 a% d3 J M0P_RTC->HOUR = pstcTimeDate->u8Hour; # u8 S1 I1 [5 Q5 D9 [
} 2 {$ `+ j# F- h. e a! _
if(TRUE == bUpdateDate)
. E" A! A% ?' g7 J { ! c6 W! s/ |9 w& B2 D1 a. }2 G9 I6 F
enRet = Rtc_CheckDateTimeFormat(pu8TimeDate,CkDate);
1 x/ t+ n! T6 j* @. P1 R' T. i if(enRet != Ok)
( d6 o4 l% t% q. D) I' Z+ f( D! ? return enRet; $ L( q8 x5 d$ v9 q1 p2 {/ Q
M0P_RTC->DAY = pstcTimeDate->u8Day;
& m! [$ y5 d; b2 i; \ M0P_RTC->MON = pstcTimeDate->u8Month; 1 N1 j9 x) x& i0 L8 ]' I: D
M0P_RTC->YEAR = pstcTimeDate->u8Year;
( h$ ~" D/ h6 i+ { M0P_RTC->WEEK = pstcTimeDate->u8DayOfWeek;
7 p. @: R2 r. `$ [) R- p* F }
% |; w% b8 K+ P& d M0P_RTC->CR1_f.WAIT = 0;
" j% i- {' I' s' {0 h' d if(1 == M0P_RTC->CR0_f.START) 4 x& J6 T8 g ~/ L! n" [8 z/ L0 ^
while(M0P_RTC->CR1_f.WAITF);
( O4 w+ T/ E5 v7 y6 @ return enRet; l) B* ~5 ^7 r
}
2 P! y9 `7 a5 F3 y/**
4 G8 u$ e7 c: v2 x7 @4 [6 i ******************************************************************************
. Z/ g4 I. S3 b: E2 N **\brief RTC获取时间函数 4 p! A4 j8 a7 B: `: m3 X$ u
**
5 p- b+ M; }0 M **\param [in] pstcTimeDate日期时间数据 . @. k9 y1 p! y
** 6 |' f' x' [9 ~
**\retval Ok 获取正常 9 q: W( |3 ]6 b: A! [+ r" X
**\retval ErrorTimeout 时间溢出错误 # ]( X: t; Y; T
******************************************************************************/ ( @, `# g- z; J4 ?1 |/ Z- y
en_result_tRtc_ReadDateTime(stc_rtc_time_t* pstcTimeDate) 6 s s! i' h+ g9 J7 @6 l4 s1 Q; h
{ $ _% s2 p. N5 h5 q
uint32_t u32TimeOut;
9 D4 i* a: d# O6 j uint8_t u8DayOfWeek, u8BcdSec,u8BcdMin, u8BcdHour, u8Day, u8Month, u8Year; # H+ X: e6 d3 D' H3 p
, t% k. @4 A) d& P: \
ASSERT(NULL != pstcTimeDate);
$ U2 c# x8 f2 s( o8 A4 r u32TimeOut = RTC_TIMEOUT;
3 [5 s$ j5 n2 M9 r9 D if(1 == M0P_RTC->CR0_f.START)
) g0 j, O( S- ~7 `$ |6 P {
' C- L/ `; ~# s7 e/ b M0P_RTC->CR1_f.WAIT = 1; 9 V' L1 _$ j! N6 `* ? i
while(u32TimeOut--)
) H; q& E* y7 I0 d {
k' L! d* J! M3 z' N* v" Q3 [" t if(M0P_RTC->CR1_f.WAITF)
0 Z7 D, p0 }# _ {
7 @" I! W' o- s0 y8 R break; ! k; M: h6 i q V" w- h* N: g1 j5 l
} 4 H! i( @ K. g* b* ]& g# c+ M
} 9 H$ }3 h! A5 o8 _
if(u32TimeOut==0)
* q2 v$ p1 ]' P, t& a* ]& v M. s {
2 d" h! `$ E7 T9 d1 f' V$ |8 t return ErrorTimeout; ( |$ S0 ]! b6 C6 p& L$ ?' j
}
. [; I. F: p% s: h } + i0 r/ C- f1 |, l" o+ E u
u8BcdSec = M0P_RTC->SEC;
! R6 U$ G$ m. |4 h* C u8BcdMin = M0P_RTC->MIN;
F. f, q3 e) f% v" m4 m u8BcdHour = M0P_RTC->HOUR; 5 Q) z' Y9 H* K( G! }7 x5 g
u8Day = M0P_RTC->DAY;
4 F) ?1 ~( ]$ v( ]- T# w6 V u8Month = M0P_RTC->MON; 5 m% U; [/ K) C& H
u8Year = M0P_RTC->YEAR; % G, T2 W$ e0 d I5 e, \* ]
u8DayOfWeek = M0P_RTC->WEEK;
: `4 w1 r A% i( c) ]! g& G2 J7 t 5 J/ j) D6 [# ~% ~ J2 i8 R
pstcTimeDate->u8Second = u8BcdSec;//BcdToDec(u8BcdSec);
! s2 V0 i1 k3 O" G$ @ pstcTimeDate->u8Minute = u8BcdMin;//BcdToDec(u8BcdMin); $ C4 I% r7 X: W4 V) q. O
pstcTimeDate->u8Hour =u8BcdHour;//BcdToDec(u8BcdHour); - R( ^! k* H$ k# e5 v6 M! H
pstcTimeDate->u8Day =u8Day;//BcdToDec(u8Day); % E' p, k. a$ g+ H5 a
pstcTimeDate->u8Month =u8Month;//BcdToDec(u8Month);
+ u+ D) E0 A/ g6 | pstcTimeDate->u8Year =u8Year;//BcdToDec(u8Year);
+ T: b: E+ h/ L' J: A+ a0 i T pstcTimeDate->u8DayOfWeek = u8DayOfWeek;//BcdToDec(u8DayOfWeek); % u. t0 Q) @3 d& ^& z6 a$ Y; u
M0P_RTC->CR1_f.WAIT = 0; 1 Z4 A+ G' f: l# d P3 u2 O' h# K( l
if(1 == M0P_RTC->CR0_f.START) 2 A+ Y+ y2 S8 f
while(M0P_RTC->CR1_f.WAITF); 8 v6 w& z0 M6 e y
return Ok; " D& f0 t8 U% v
} 0 N+ H2 l4 R: p4 \2 }
/**
) } f" D K2 U0 P6 Z( v: J9 l( U9 }9 R ******************************************************************************
) b7 `! l/ W/ F) L1 Z **\brief RTC计数or读写状态获取
9 [* S" \: ~$ p# U- N **
( L; h9 } ~# u. j0 E- \7 ~9 L **\param [in] 无
* M1 Y1 t0 D* z" \$ H: {# l ** . _9 T: g( @7 F
**\retval 计数or读写状态 7 x; p V. X0 G6 ~
**
! l B* F3 ^) m$ R# Z ******************************************************************************/ 4 I( I4 X( L6 |, S/ E# Q$ A
boolean_t Rtc_RDStatus(void)
; J" n& V7 s: d% L8 K+ F{
. `9 N: k$ v$ l7 Q7 S% W boolean_t bRet; # L/ j1 g4 g2 b8 j+ j- B
bRet = M0P_RTC->CR1_f.WAITF;
$ ~9 Y" x7 K, i! |4 s) j% v$ ^ return bRet;
- T1 ]# ~3 i$ S+ F6 J& h}
* h9 G9 |7 w% i5 u1 c1 M/** # t2 N) l! U4 T+ U) f
******************************************************************************
$ \" x9 I' I& c; I g **\brief RTC闹钟中断使能 ; [, Q: {3 x( T; O; g
**
$ V- c/ Y2 k0 |! N; }. f( U **\param [in] enordis中断使能or禁止
( F" A9 O, {3 O8 V5 n" V- K **
- U# q. u8 @# v _( j/ h; L s **\retval Ok设置成功 & D0 K' n; U/ `( b
**
& u4 E' Z+ J3 y% w" s ******************************************************************************/
) |& P) j( C" aen_result_tRtc_EnAlarmIrq(en_rtc_alarmirq_t enIrqEn) & I6 E+ u: |! w% f5 a
{ @: [' d3 P& c* J' `- {4 m
en_result_t enRet = Error;
1 q" z7 i1 J& Y* \ M0P_RTC->CR1_f.ALMIE = enIrqEn; 0 D' F1 a+ g$ W! r& f+ E1 I5 ?$ m
Rtc_ClrIrqStatus(RtcAlmf);//使能中断后清除中断请求标记
, M$ {9 u9 q* A: p5 y) l& [ enRet = Ok;
8 k6 j8 R! \1 N- {, t return enRet; 6 s' P) C7 Y1 }
} $ |4 S4 A. e: }; N
/** . v5 Y, f( v1 b" D
****************************************************************************** 3 @: O, Z0 o& c% ?
**\brief RTC中断请求状态获取 0 a# s! O! p7 \. |
** 0 h6 @6 D; [9 D* l
**\param [in] enIrqSel获取哪种中断请求 1 M2 B/ P! Z/ ^. k, j2 @, F
** " V1 u3 ~" h3 t4 [* G
**\retval 中断请求状态 1 B/ \- @5 S) |4 A3 J% m
**
& [. n6 m- S6 U$ D T ******************************************************************************/ * m! N; [+ t& v9 ~& g* j/ j& N
boolean_t Rtc_GetIrqStatus(en_rtc_status_irq_tenIrqSel) " ~. y4 P- p+ }5 A( P
{
0 m; y _0 w Y, P8 n2 ^ boolean_t bRet = FALSE;
5 R# p& B/ y9 l% y! B/ N ASSERT(IS_VALID_IRQ_SEL(enIrqSel));
+ M+ u$ u+ W. j. ^' n switch(enIrqSel)
, u% L7 b3 E' _) }- i) {" [) O- Q' Y {
; y9 [, }. u' B- @* v$ ?% K case RtcPrdf:
, t. X6 W0 H" o+ e2 b8 O: { (M0P_RTC->CR1_f.PRDF == 1)?(bRet = TRUE) : (bRet = FALSE);
. C4 k/ M% G: j) c, z; T break; 3 ]1 S Y; Z/ Y. S! N+ L
case RtcAlmf :
+ Q* Q+ V% {) Z" w' O n (M0P_RTC->CR1_f.ALMF == 1)?(bRet = TRUE) : (bRet = FALSE);
# m* h4 ` d$ R5 q7 `2 G$ P break;
5 D; L* L8 R, E; B( P% o& F default:
F: `, Q+ O7 t3 I2 f6 \1 n break;
2 u" }) ^0 U- c8 E0 E# k }
2 G( r+ V& s: t$ A return bRet; 7 K9 U8 U4 A' f0 c; P+ H
} , \0 V- }' c- s
/**
2 _( ?" @+ e4 P o/ V ****************************************************************************** ( j: ?4 V4 I! e. ?
**\brief RTC中断请求清除
8 d% ^+ u& t5 F W **
9 W I; {% W3 N2 c. ^9 d) ? **\param [in] enIrqSel清除哪种中断请求
; U% Y/ J- X, V j" N **
' @( y3 |5 \) p$ d" Q! c **\retval Ok 清除成功 5 {$ n# A2 G% F
**\retval ErrorInvalidParameter 清除失败 0 H3 J Q) ~# B- Z
******************************************************************************/
# f: h( w( K' q$ h* } den_result_tRtc_ClrIrqStatus(en_rtc_status_irq_t enIrqSel)
6 n( E: a) n% U6 i$ O4 C{
+ c5 |6 f' x3 j8 G8 {2 M! O ASSERT(IS_VALID_IRQ_SEL(enIrqSel));
0 U5 N% r2 c9 z, A2 U switch(enIrqSel) . s; V$ o- K: I: g5 @
{
@; K1 u4 ^$ x7 T" a2 i case RtcPrdf:
) G) K S2 u5 Y! U" c( r9 } M0P_RTC->CR1_f.PRDF = 0; 1 M X, V; n/ I( q
break; 2 b! X4 H% G' k" ~) H2 g
case RtcAlmf: ) {9 q* u" p+ Y- B; b
M0P_RTC->CR1_f.ALMF = 0; 4 }4 H9 Z1 v. u/ Y' W+ T, Q- j
break; % n& L- B' K3 Y9 K1 b
default: : U- o! S0 ?# l9 |4 r9 s
return ErrorInvalidParameter;
/ o5 t! @/ _ A* s7 B% i } , l- Y; G5 {6 `7 G: x$ r
return Ok; 5 G& e8 g F( C
}
( q9 Z1 \/ X) r0 G5 V* Q. m . E8 Y& V" X9 Y, W) V& @# i
/** 1 O( C* v; X% E7 F1 Z4 f9 K
****************************************************************************** 4 u3 X, }6 W% B& D+ k, `
**\brief RTC中断处理函数接口获取 - a8 c, }, Y# ?, N( }# A
** + X, w/ v9 n) r
**\param [in] 无
! g& f( u7 S' _( F ** 7 G4 A2 V0 X! K# p
**\retval 接口函数地址
6 N$ z* f' w1 H# f4 ?% |# T! P& X+ A3 ` **
# C0 D/ X# Z$ l; F ******************************************************************************/ S r+ x) K& ~3 L) w: j2 F
static stc_rtc_intern_cb_t*RtcGetInternDataCb(void)
8 B: R/ T n+ ^. f4 \{
( |3 e3 g' b+ M! t' \- P return &stcRtcIrqCb; 9 ~. ~# P# c! d: N
} 3 ]: _: e6 u% T! ], P9 F
/** - ]4 y1 w' K8 ]0 t$ s
******************************************************************************
8 a3 X1 i: y2 W7 a8 I3 m **\brief RTC总体初始化函数
5 m* I* N5 U2 D# N/ Y( L$ Y( C ** , [% V2 N3 K: B3 C# L( l$ j
**\param [in] pstcRtcConfig初始化结构
( X' o5 y7 [- l A! _. V ** 7 b1 ?: M, v; D. n- @2 C4 H
**\retval Ok初始化成功 0 D+ E% H; `- u4 D6 B$ T. N* n/ d
**\retval ErrorInvalidParameter 初始化错误
* ]5 R, c3 B4 E& p9 } ******************************************************************************/ , Q! f8 p* e* a. Q' P+ a0 K# _8 V
en_result_t Rtc_Init(stc_rtc_config_t*pstcRtcConfig) : a, W, o8 `# i/ [' M* v9 ?
{
! _! z6 c1 Y" r- ~6 M) o5 Z3 R4 j en_result_t enRet = Error;
& T. Y6 |. Q4 u. ~, n( M6 q) e stc_rtc_intern_cb_t* pstcRtcInternCb;
- v0 }5 x v" k/ @2 o" d9 P8 m if(NULL == pstcRtcConfig)
+ Q, `3 D/ u/ [ u8 P$ u return Error; ( ]; p1 O4 I" \; R6 t* |- Q
pstcRtcInternCb = RtcGetInternDataCb(); , q4 C, h# i$ H& A+ g4 a
enRet = Rtc_SelClk(pstcRtcConfig->enClkSel); ) E ^5 x$ c" t( z0 N+ {) z
enRet = Rtc_SetAmPm(pstcRtcConfig->enAmpmSel);
+ e) k( j" e$ w( p0 ~9 V* w if(enRet != Ok) 4 {- ^2 r- i) {1 O- F4 l1 M) E
{ . u" i4 D- ?' r& l# U
return enRet;
" J9 ^8 H7 f% U( S1 ^" C } 1 m" i1 s. ^5 ]. p* _( g
if(NULL != pstcRtcConfig->pstcCycSel)
7 d9 z# v" k0 ]# } I, D {
. s& Y n7 Y& j+ ~$ G9 a if(Ok != Rtc_SetCyc(pstcRtcConfig->pstcCycSel)) ) Z2 w" }% l4 O) Q; j1 O) H9 h
return Error;
& [# R# B+ v' `' P/ W' F& v$ @" d } . a4 N( X/ B# v" O, ?1 {# {7 ^
if(NULL != pstcRtcConfig->pstcTimeDate) 8 z: j% @8 H- p/ ?8 i! P! Q/ s1 V
{
E& S& Z) v% G3 q- f) M if(Ok != Rtc_WriteDateTime(pstcRtcConfig->pstcTimeDate,TRUE,TRUE))
% m! W1 ? ]' R! u7 @5 |8 l3 I return Error;
$ L( k3 a6 d- Q3 t } & O- d$ ^/ G0 E1 v W+ K
if(NULL != pstcRtcConfig->pstcIrqCb) 3 H# N6 |& S3 W3 x: y/ t4 L
{
6 x3 }* U# j; I& Q. m, A: c pstcRtcInternCb->pfnAlarmIrqCb =pstcRtcConfig->pstcIrqCb->pfnAlarmIrqCb;
7 R2 @3 `& Q; |. L, M pstcRtcInternCb->pfnTimerIrqCb =pstcRtcConfig->pstcIrqCb->pfnTimerIrqCb; 6 h8 u. J: F+ _& S9 [
}
0 \0 W5 P ?' R* M3 R1 F1 M if(TRUE == pstcRtcConfig->bTouchNvic)
( l! z- V7 h! f" X/ [ { # ^. W1 h B' h6 K9 M& {4 p4 c4 t
EnableNvic(RTC_IRQn,DDL_IRQ_LEVEL_DEFAULT,TRUE); ) C: i1 y1 h5 e; O6 _ o4 y
} ' U8 m/ O, l8 ?7 ^$ [! R8 e# E8 `
else
/ T% j$ R; B7 l/ r {
( w" R- e4 V6 H# d. n EnableNvic(RTC_IRQn,DDL_IRQ_LEVEL_DEFAULT,FALSE);
" ]6 o G* l* X5 R8 o3 [+ ~ }
2 r$ g2 P: i" ^/ D6 u return enRet;
6 P1 a; K& A3 @. k}
: D/ ~1 Z; ]' M% q/**
8 c! }( l7 ^3 ^, w' U ****************************************************************************** 8 N5 K! g) S2 N9 Q4 r& a
**\brief RTC计数禁止函数
/ S8 D: z2 A/ j& @ ** % n& p6 A# h7 r4 X5 \0 H1 {& ?
**\param [in] 无 4 V/ [* n) v- K) V/ k6 Y3 x) K3 J2 d
**
) f6 e0 V/ ~- l. [ **\retval Ok禁止设置成功
% @' o8 T A- r8 S! i* | **
2 |' r( w. k# D9 p+ |/ W ******************************************************************************/
2 g7 _( b8 \ _2 s& Den_result_t Rtc_DeInit(void)
& Y3 u3 y) b* P. u% e{ ) L9 f8 ?! o5 A5 N' |. d0 Z- w
EnableNvic(RTC_IRQn,DDL_IRQ_LEVEL_DEFAULT,FALSE);
5 w) N- E: G! F( \ ` Rtc_DisableFunc(RtcCount);
$ o6 k. s- w. }# | Rtc_DisableFunc(RtcAlarmEn); 8 X% l- ?# M& A
Rtc_DisableFunc(Rtc_ComenEn); + `9 H1 W3 l4 y: c# _/ ~& ]
Rtc_DisableFunc(Rtc1HzOutEn);
/ e/ h$ h& {! J" E( Q; e return Ok;
$ U0 G/ i+ ]5 @8 E} 5 G5 Q' ~3 L, Y% V$ ]+ `
/**
% ^( f9 h4 G/ q* n9 K1 I ****************************************************************************** ! d; N- X* U- `# `6 l
**\brief RTC中断处理函数 / K7 Q1 g) x0 v* D1 b. t1 S: [
** / t: W6 K( r8 O+ h5 @
**\param [in] 无
8 \. o+ J% f+ Y; A* R* d! o **
) R& v9 g8 ~: X- Y **\retval 无 3 h0 x6 T3 F" `3 y" o$ R
** 2 f8 Y* w7 |8 u( Z7 q- ]7 ^7 W- L. M
******************************************************************************/
. A7 X3 ~! Y; E: ^6 K4 avoid Rtc_IRQHandler(void)
' M/ M: G: f2 o5 r' T- W0 N# f. V+ F2 _{ 0 A4 x! l4 ~& O
stc_rtc_intern_cb_t* pstcRtcInternCb; : p! }. |: j5 ], Q# ], O" B
pstcRtcInternCb = RtcGetInternDataCb() ; . M) @/ O$ ]$ i
if(TRUE == M0P_RTC->CR1_f.ALMF) ' f {9 S$ _7 i1 d5 s. h
{
3 n" p! r9 B3 g# j+ ^: z: i M0P_RTC->CR1_f.ALMF = 0u;
* @, m3 W. F; g' T% ~ if(NULL != pstcRtcInternCb->pfnAlarmIrqCb) 5 p0 Y2 W* m4 q5 k7 ?9 b
{ ; o7 z9 l2 E$ h8 f F
pstcRtcInternCb->pfnAlarmIrqCb();
4 g$ o$ C& ~- `' j( i# {7 g. c } ! `3 I2 |% ?% ^/ b9 G% }
}
8 J* o# K, q- `& ?5 g- h if(TRUE == M0P_RTC->CR1_f.PRDF)
* S1 H B) G, r, L9 Y2 e5 r5 q {
" ?: @8 v6 M7 b% G M0P_RTC->CR1_f.PRDF = 0;
3 I" c0 \/ a/ E if(NULL != pstcRtcInternCb->pfnTimerIrqCb) 4 d7 J% |# x; J- W. N4 q E* d
{ 2 D, K- q- K! Z2 p8 D6 ~
pstcRtcInternCb->pfnTimerIrqCb();
9 z, u$ G( M2 D3 A }
/ c6 |* S3 ?( r2 a; M9 n! u" } } 8 W0 P9 _) O' x, X/ w7 L& U0 w
}
v5 e+ k: o# ^. P4 r2 u( {" X% x% f 9 l- K% N6 t S- u
. W) K" r3 s/ u' o* u# @static void RtcCycCb(void) , |( J( [; X7 i2 P3 W$ f
{
! p: }2 h3 L: {5 d# }7 R( v cyccnt++; , q" K0 ?& b5 _3 Y5 `$ [* m
flg = ~flg; % Q8 B7 m) B D7 L3 q9 f5 D5 c
Gpio_SetIO(3,3,flg); ' y5 x( U# x, }: o! e) T
}
, B% D7 A( j( O. {+ H; j, Zstatic void RtcAlarmCb(void)
$ v' L3 I8 _; K1 n' i+ J# C& _1 @{ ; L' n& j3 B' y m- V$ T" |( e4 y
u8Alarmcnt++;
1 _5 q! L, f" p3 ~/ g3 g+ _}
5 j1 L: l! O' a; B6 ?( x9 Q6 {int32_t main(void) : U0 O) U' x% F) Q5 Y: m
{ ; n2 ?1 u5 b( K) F
stc_rtc_config_t stcRtcConfig;
3 s' ]+ m9 e2 U3 W/ w9 K. U3 g F n stc_rtc_irq_cb_t stcIrqCb;
' M0 Y3 Y) [9 d0 b1 Z stc_rtc_time_t stcTime;
8 Z1 [* {3 ?1 o# J) g* ~ m stc_rtc_alarmset_t stcAlarm;
j# x) F- A; z stc_rtc_cyc_sel_t stcCycSel; 9 o- W1 g3 l& z+ b0 @- z* D
DDL_ZERO_STRUCT(stcRtcConfig); ; ?* H9 {9 O* ~6 e
DDL_ZERO_STRUCT(stcIrqCb); + P: S" m t5 s9 s7 o, \, w
DDL_ZERO_STRUCT(stcAlarm); ! g) Q: [2 G8 q; P
DDL_ZERO_STRUCT(stcTime); 8 W3 Z: U0 p' E- j1 `' q
DDL_ZERO_STRUCT(stcCycSel);
! k# l: c0 _% |% [! f# q% D
! c; T$ D# `# y; A Clk_SetPeripheralGate(ClkPeripheralGpio,TRUE);//使能GPIO时钟 ( r, T! I. _; F$ S- F
2 U: ~+ ?- |' a4 n, c
9 S( g" b( t9 {6 ]" p0 V Gpio_SetAnalog(1,4,1);//配置成模拟端口即为晶振口
3 ` y; F' J% X" r1 L Gpio_SetAnalog(1,5,1); - L J: a9 _+ Q* h$ \
Clk_Enable(ClkXTL, TRUE);//开启外部晶振32.768
w6 o2 f: ~4 Q2 A //Clk_Enable(ClkRCL, TRUE); O8 t' w7 `7 P# v1 ~8 I* Y
//Clk_Enable(ClkXTH, TRUE);
$ r" r5 m" {% @7 r u6 B Clk_SetPeripheralGate(ClkPeripheralRtc,TRUE);//使能rtc时钟 8 d8 }! F4 }% t/ u- Z
5 e* s( N9 l4 v$ _
stcRtcConfig.enClkSel = RtcClk32768;//RtcClkHxt1024;//RtcClk32;// ; U& n: b1 g: o" @, S+ ^
stcRtcConfig.enAmpmSel = Rtc24h;//Rtc12h;//
' a- N" p# K& j6 t- k
( s/ ~( ^8 t) y* V, X stcTime.u8Year = 0x17; 6 _2 N7 I2 X) a) v2 R/ l
stcTime.u8Month = 0x06; ' }: f% l2 \" k4 \
stcTime.u8Day = 0x07;
3 W; z# ^' g% A# _; T4 g stcTime.u8Hour = 0x12; + L7 u; i2 m, E& X r, e- x7 b0 h
stcTime.u8Minute = 0x59; ' e! X+ ?/ Z3 ?% v' ~ ~3 s( y
stcTime.u8Second = 0x59; 7 ^/ s# ~ i+ F1 t; f- R0 T1 f
stcTime.u8DayOfWeek = Rtc_CalWeek(&stcTime.u8Day);
1 F. @' \0 m; F+ R; a, m- R7 | stcRtcConfig.pstcTimeDate = &stcTime; 6 z# J# P8 [' |2 C
4 z9 q5 Z$ |) ~# m stcIrqCb.pfnAlarmIrqCb = RtcAlarmCb; " t) G" c0 s- r/ K! _/ C5 q$ J/ ?
stcIrqCb.pfnTimerIrqCb = RtcCycCb;
- h9 K" M# D3 ?, {9 V stcRtcConfig.pstcIrqCb = &stcIrqCb;
5 Z+ P U/ K4 R4 A8 I% C2 {* n stcRtcConfig.bTouchNvic = FALSE;
6 F, F0 [6 s" B+ h . t4 w3 e4 ~+ ^7 d
Rtc_DisableFunc(RtcCount); & S/ L* f0 L$ A1 U" p: c
Rtc_Init(&stcRtcConfig);
) v0 J, R& V ^ W" h6 q7 \) y' {
/ P B$ p2 ^% R! [ \. a) D) t: N9 Q
5 r% i# o" B; f/ L7 ^0 z1 }0 A- w Rtc_EnableFunc(RtcCount);
# q I/ e; ^. D6 P Rtc_ReadDateTime(&stcReadTime); 5 H0 n" @ h7 a+ R) u
while (1); A: L! c/ |; u2 ]
/ `! J( J0 F% [9 {9 B& r
} # u. M% W4 I* G3 p9 {( Y, d. c5 x
0 ~* \$ j5 ?( n2 ]' {% a ^/ z/******************************************************************************
9 y( q5 k7 ]# b *EOF (not truncated) : A# i4 t- ]* r2 @
******************************************************************************/ 0 k: ^6 S; _& ?+ X0 w2 {8 |# a4 j
' c' V. {: i( J7 O& I |