forked from SZV10X_Software/SZV103_FM33A0xxEV_SiZhu

jinlicong
2024-04-30 ee3c90168f81ce19529904d80f9c9de4fed23bb8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "rtc.h"
#include "extern_rtc.h"
#include "gpio.h"
#include "uart.h"
 
 
 
__IO RTC_TimeDateTypeDef calendar_g = {0};
 
void RTC_IRQHandler(void)
{
//    if(__SYS_DELAY_SEC_FLAG_GET(sys_active_time) == SET && LargeCurrent_LimitingProcess() == 3)
//        SysWakeUp_ClockCfg();
//    if( RTC_ISR_SEC_IF_Chk() == SET)//²éѯÃëÖӶϱêÖ¾ÊÇ·ñÖÃÆð
//    {
//        RTC_ISR_SEC_IF_Clr();        //Çå³ýÃëÖжϱêÖ¾
//        sys_time_g.sys_run_period = SET;
//        Get_Sys_Rtc_Time(&calendar_g);
//    }    
}
 
 
void RTC_Init(void)
{    
    CMU_PERCLK_SetableEx(RTCCLK, ENABLE);  //RTC×ÜÏßʱÖÓʹÄÜ
    //RTCʹÓÃXTLF/RCHF£¬Éϵç×Ô¶¯ÆðÕñ£¬²»ÐèҪʹÄܹ¤×÷ʱÖÓ
    
  RTC_ISR_SEC_IF_Clr();        //Çå³ýÃëÖжϱêÖ¾                                            
    RTC_IER_SEC_IE_Setable(ENABLE);//´ò¿ªRTCÃëÖжϠ   
    
    NVIC_DisableIRQ(RTC_IRQn);                //NVICÖжϿØÖÆÆ÷ÅäÖÃ
    NVIC_SetPriority(RTC_IRQn,1);
    NVIC_EnableIRQ(RTC_IRQn);    
    
    RTC_CR_RTC_EN_Setable(ENABLE);  //ʹÄÜRTC_A
}
 
 
/******************************************
 * func:    Get_Sys_Rtc_Time
 * desc:    »ñȡϵͳRTCʱ¼ä
 * input:   pOutput_timedata £º»ñȡϵͳʱ¼äÖ¸Õ루BCD¸ñʽ£©
 * output:  none
 * return:  none
 *****************************************/
ErrorStatus_STM32 Get_Sys_Rtc_Time(__IO RTC_TimeDateTypeDef * pOutput_timedata)
{
    uint08 n, i;
    ErrorStatus_STM32 Result = ERROR_1;
 
    RTC_TimeDateTypeDef TempTime1,TempTime2;
    
    for(n=0 ;n<3; n++)
    {
        RTC_TimeDate_GetEx(&TempTime1);//¶ÁÒ»´Îʱ¼ä
        RTC_TimeDate_GetEx(&TempTime2);//ÔÙ¶ÁÒ»´Îʱ¼ä
        
        for(i=0; i<7; i++)//Á½ÕßÒ»ÖÂ, ±íʾ¶ÁÈ¡³É¹¦
        {
            if(((uint08*)(&TempTime1))[i] != ((uint08*)(&TempTime2))[i]) break;
        }
        if(i == 7)
        {
            Result = SUCCESS_0;
            memcpy((uint08*)(pOutput_timedata), (uint08*)(&TempTime1), 7);//¶ÁÈ¡ÕýÈ·Ôò¸üÐÂеÄʱ¼ä        
//            memcpy(&sys_realtime_data_g.time_para_s,&TempTime1,7);
            break;
        }
    }
    return Result;
}
 
/******************************************
 * func:    Set_Sys_Rtc_Time
 * desc:    ÉèÖÃϵͳRTCʱ¼ä
 * input:   pInput_timedata£ºÐ´ÈëµÄϵͳʱ¼äÖ¸Õ루BCD¸ñʽ£©
 * output:  none
 * return:  none
 *****************************************/
ErrorStatus_STM32 Set_Sys_Rtc_Time(uint8_t year,uint8_t month,uint8_t day,uint8_t hour,uint8_t min,uint8_t sec,uint8_t week)
{
    uint08 n, i;
    ErrorStatus_STM32 Result;
    RTC_TimeDateTypeDef TempTime1;
    RTC_TimeDateTypeDef real_time = {year,month,day,hour,min,sec,week};
    for(n=0 ;n<3; n++)
    {
        RTC_WER_Write(RTC_WRITE_ENABLE );        //½â³ýRTCд±£»¤
        RTC_TimeDate_SetEx(&real_time);            //ÉèÖÃRTC
        RTC_WER_Write(RTC_WRITE_DISABLE );    //´ò¿ªRTCд±£»¤
        
        Result = Get_Sys_Rtc_Time(&TempTime1);    //¶ÁȡȷÈÏÉèÖýá¹û
        if(Result == SUCCESS_0)
        {
            Result = ERROR_1;
            for(i=0; i<7; i++)//Á½ÕßÒ»ÖÂ, ±íʾÉèÖóɹ¦
            {
                if(((uint08*)(&TempTime1))[i] != ((uint08*)(&real_time))[i]) break;
            }
            if(i == 7)
            {
                Result = SUCCESS_0;
                break;
            }
        }
    }
    return Result;
}
 
void rtc_setalarm(RTC_AlarmTmieTypeDef* para)
{
    RTC_AlarmTime_SetEx(para);    //ÉèÖÃÄÖÖÓʱ¼ä    
    
    RTC_ISR_ALARM_IF_Clr();        //Çå³ýÄÖÖÓÖжϱêÖ¾                                            
    RTC_IER_ALARM_IE_Setable(ENABLE);//´ò¿ªÄÖÖÓÖжϠ   
    RTC_ALARM_ALMEN_Setable(ENABLE);//ÄÖÖÓ¹¦ÄÜʹÄÜ
}