WXK
2023-08-30 72d5e74bf1969d0a1429a14a4f409eddf938d66d
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*******************************************************************************
 * File Name         : RTC.c
 * Description       :
 * Author            : hido.ltd
 *******************************************************************************/
 
/*******************************************************************************
 *                              Include Files                                  *
 *******************************************************************************/
#include "RTC.h"
#include "stm32l0xx_hal.h"
 
/*******************************************************************************
*                                  Macro                                      *
*******************************************************************************/
 
/*******************************************************************************
*                             Type Definition                                 *
*******************************************************************************/
 
/*******************************************************************************
 *                             Local Variable                                  *
 *******************************************************************************/
static RTC_HandleTypeDef *l_pstRTC = NULL;
 
 /*******************************************************************************
  *                        Local Function Declaration                           *
  *******************************************************************************/
 
 /*******************************************************************************
  *                             Local Function                                  *
  *******************************************************************************/
 /*******************************************************************************
  * Function Name     :
  * Description       :
  * Input             :
  * Output            :
  * Return            :
  * Author            : hido.ltd
  * Modified Date:    : 2018Äê8ÔÂ2ÈÕ
  *******************************************************************************/
 
 /*******************************************************************************
  *                             Global Function                                 *
  *******************************************************************************/
/*******************************************************************************
 * Function Name     : RTC_Init
 * Description       : RTC³õʼ»¯
 * Input             : _pRTC                         RTCÃèÊöÖ¸Õë
 * Output            : None
 * Return            : None
 * Author            : hido.ltd
 *******************************************************************************/
void RTC_Init(void *_pRTC)
{
    l_pstRTC = (RTC_HandleTypeDef *)_pRTC;
}
 
/*******************************************************************************
 * Function Name     : RTC_GetDateTime
 * Description       : ´ÓRTCÖлñÈ¡ÈÕÆÚ¼°Ê±¼ä
 * Input             : None
 * Output            : _pstDateTime                  ÈÕÆÚʱ¼ä
 * Return            : HIDO_ERR               Ê§°Ü
 *                     HIDO_OK                ³É¹¦
 * Author            : hido.ltd
 *******************************************************************************/
HIDO_INT32 RTC_GetDateTime(ST_RTCDateTime *_pstDateTime)
{
    RTC_TimeTypeDef stTime;
    RTC_DateTypeDef stDate;
 
    /* ÐèÒªÏÈ»ñȡʱ¼ä */
    if (HAL_RTC_GetTime(l_pstRTC, &stTime, RTC_FORMAT_BIN) != HAL_OK)
    {
        return HIDO_ERR;
    }
 
    /* ÔÙ»ñÈ¡ÈÕÆÚ */
    if (HAL_RTC_GetDate(l_pstRTC, &stDate, RTC_FORMAT_BIN) != HAL_OK)
    {
        return HIDO_ERR;
    }
 
    _pstDateTime->m_u32Year = stDate.Year + 2000;
    _pstDateTime->m_u8Month = RTC_Bcd2ToByte(stDate.Month);
    _pstDateTime->m_u8Day = stDate.Date;
 
    _pstDateTime->m_u8Hour = stTime.Hours;
    _pstDateTime->m_u8Min = stTime.Minutes;
    _pstDateTime->m_u8Sec = stTime.Seconds;
 
    return HIDO_OK;
}
 
/*******************************************************************************
 * Function Name     : RTC_SetDateTime
 * Description       : ÉèÖÃÈÕÆÚ¼°Ê±¼ä
 * Input             : _pstDateTime                  ÈÕÆÚʱ¼ä
 * Output            : None
 * Return            : HIDO_ERR               Ê§°Ü
 *                     HIDO_OK                ³É¹¦
 * Author            : hido.ltd
 *******************************************************************************/
HIDO_INT32 RTC_SetDateTime(ST_RTCDateTime *_pstDateTime)
{
    RTC_TimeTypeDef stTime;
    RTC_DateTypeDef stDate;
 
    stDate.Year = _pstDateTime->m_u32Year - 2000;
    stDate.Month = RTC_ByteToBcd2(_pstDateTime->m_u8Month);
    stDate.Date = _pstDateTime->m_u8Day;
    stDate.WeekDay = RTC_WEEKDAY_MONDAY;
 
    stTime.Hours = _pstDateTime->m_u8Hour;
    stTime.Minutes = _pstDateTime->m_u8Min;
    stTime.Seconds = _pstDateTime->m_u8Sec;
    stTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
    stTime.StoreOperation = RTC_STOREOPERATION_RESET;
 
    /* ÉèÖÃʱ¼ä */
    if (HAL_RTC_SetTime(l_pstRTC, &stTime, RTC_FORMAT_BIN) != HAL_OK)
    {
        return HIDO_ERR;
    }
 
    /* ÉèÖÃÈÕÆÚ */
    if (HAL_RTC_SetDate(l_pstRTC, &stDate, RTC_FORMAT_BIN) != HAL_OK)
    {
        return HIDO_ERR;
    }
 
    return HIDO_OK;
}
 
/*******************************************************************************
 * Function Name     : RTC_MakeTime
 * Description       : ½«ÄêÔÂÈÕʱ·ÖÃëת³ÉUTCÃëÊý£¬²ÉÓÃLINUXÄÚºËËã·¨
 * Input             : _pstDateTime                  ÈÕÆÚʱ¼ä
 * Output            : None
 * Return            : UTCÃëÊý
 * Author            : hido.ltd
 * Modified Date:    : 2017-09-22
 *******************************************************************************/
HIDO_UINT32 RTC_MakeTime(ST_RTCDateTime *_pstDateTime)
{
    HIDO_UINT32 u32Mon = _pstDateTime->m_u8Month;
    HIDO_UINT32 u32Year = _pstDateTime->m_u32Year;
 
    /* 1..12 -> 11,12,1..10 */
    if (0 >= (HIDO_INT32) (u32Mon -= 2)) {
        u32Mon += 12;  /* Puts Feb last since it has leap day */
        u32Year -= 1;
    }
 
    return ((((HIDO_UINT32)
          (u32Year/4 - u32Year/100 + u32Year/400 + 367*u32Mon/12 + _pstDateTime->m_u8Day) +
          u32Year*365 - 719499
        )*24 + _pstDateTime->m_u8Hour /* now have hours */
      )*60 + _pstDateTime->m_u8Min /* now have minutes */
    )*60 + _pstDateTime->m_u8Sec; /* finally seconds */
}
 
/*******************************************************************************
 * Function Name     : RTC_GetDiffSeconds
 * Description       : ¼ÆËãʱ¼ä_pstDateTime0ºÍʱ¼ä_pstDateTime1µÄÃëÊý²îÖµ
 * Input             : _pstDateTime0                 ÈÕÆÚʱ¼ä0
 *                   : _pstDateTime1                 ÈÕÆÚʱ¼ä1
 * Output            : None
 * Return            : ÃëÊý
 * Author            : hido.ltd
 * Modified Date:    : 2017-09-22
 *******************************************************************************/
HIDO_UINT32 RTC_GetDiffSeconds(ST_RTCDateTime *_pstDateTime0, ST_RTCDateTime *_pstDateTime1)
{
    HIDO_UINT32 u32Sec0 = RTC_MakeTime(_pstDateTime0);
    HIDO_UINT32 u32Sec1 = RTC_MakeTime(_pstDateTime1);
 
    if (u32Sec0 > u32Sec1)
    {
        return u32Sec0 - u32Sec1;
    }
    else
    {
        return u32Sec1 - u32Sec0;
    }
}
 
/*******************************************************************************
 * Function Name     : RTC_GetDiffSeconds
 * Description       : ¼ÆËãʱ¼ä_pstDateTime0ºÍʱ¼ä_pstDateTime1µÄÃëÊý²îÖµ
 * Input             : _pstDateTime0                 ÈÕÆÚʱ¼ä0
 *                   : _pstDateTime1                 ÈÕÆÚʱ¼ä1
 * Output            : None
 * Return            : ÃëÊý
 * Author            : hido.ltd
 *******************************************************************************/
HIDO_UINT32 RTC_GetTimeStamp(void)
{
    ST_RTCDateTime stDateTime;
 
    RTC_GetDateTime(&stDateTime);
 
    return RTC_MakeTime(&stDateTime) - (8 * 3600);
}
 
/*******************************************************************************
 * Function Name     : RTC_SetWakeUp
 * Description       :
 * Input             :
 * Output            :
 * Return            :
 * Author            : hido.ltd
 *******************************************************************************/
HIDO_INT32 RTC_SetWakeUp(HIDO_UINT32 _u32Ms)
{
    /*
     * DIV(16) / LSI(32.768K);
     * 60s / (DVI / LSI) = 122880
     */
 
    __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(l_pstRTC, RTC_FLAG_WUTF);
 
    if (HAL_RTCEx_SetWakeUpTimer_IT(l_pstRTC, _u32Ms / (16 / 32.768), RTC_WAKEUPCLOCK_RTCCLK_DIV16) != HAL_OK)
    {
        return HIDO_ERR;
    }
 
    return HIDO_OK;
}