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