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