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