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