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