Skip to content

Commit 2401726

Browse files
saviourxxmysterywolf
authored andcommitted
[bsp/n32] 修复每次上电都初始化RTC导致上电时间被重置的问题
1 parent 9de975f commit 2401726

File tree

1 file changed

+60
-53
lines changed

1 file changed

+60
-53
lines changed

bsp/n32/libraries/n32_drivers/drv_rtc.c

+60-53
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#ifdef BSP_USING_RTC
1616

17+
#define USER_WRITE_BKP_DAT1_DATA 0xA5A5
18+
1719
uint32_t SynchPrediv, AsynchPrediv;
1820

1921
static rt_err_t n32_rtc_get_timeval(struct timeval *tv)
@@ -105,93 +107,98 @@ static rt_err_t n32_rtc_init(void)
105107

106108
/* Allow access to RTC */
107109
PWR_BackupAccessEnable(ENABLE);
110+
if (USER_WRITE_BKP_DAT1_DATA != BKP_ReadBkpData(BKP_DAT1) )
111+
{
108112

109113
#if defined(SOC_N32G45X) || defined(SOC_N32WB452)
110-
/* Reset Backup */
111-
BKP_DeInit();
114+
/* Reset Backup */
115+
BKP_DeInit();
112116
#endif
113117

114-
/* Disable RTC clock */
115-
RCC_EnableRtcClk(DISABLE);
118+
/* Disable RTC clock */
119+
RCC_EnableRtcClk(DISABLE);
116120

117121
#ifdef BSP_RTC_USING_HSE
118-
/* Enable the HSE OSC */
119-
RCC_EnableLsi(DISABLE);
120-
RCC_ConfigHse(RCC_HSE_ENABLE);
121-
while (RCC_WaitHseStable() == ERROR)
122-
{
123-
}
122+
/* Enable the HSE OSC */
123+
RCC_EnableLsi(DISABLE);
124+
RCC_ConfigHse(RCC_HSE_ENABLE);
125+
while (RCC_WaitHseStable() == ERROR)
126+
{
127+
}
124128
#if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
125-
rt_kprintf("rtc clock source is set hse/128!\n");
126-
RCC_ConfigRtcClk(RCC_RTCCLK_SRC_HSE_DIV128);
129+
rt_kprintf("rtc clock source is set hse/128!\n");
130+
RCC_ConfigRtcClk(RCC_RTCCLK_SRC_HSE_DIV128);
127131
#elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
128-
rt_kprintf("rtc clock source is set hse/32!\n");
129-
RCC_ConfigRtcClk(RCC_RTCCLK_SRC_HSE_DIV32);
132+
rt_kprintf("rtc clock source is set hse/32!\n");
133+
RCC_ConfigRtcClk(RCC_RTCCLK_SRC_HSE_DIV32);
130134
#endif
131135

132136
#if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
133-
SynchPrediv = 0x1E8; // 8M/128 = 62.5KHz
134-
AsynchPrediv = 0x7F; // value range: 0-7F
137+
SynchPrediv = 0x1E8; // 8M/128 = 62.5KHz
138+
AsynchPrediv = 0x7F; // value range: 0-7F
135139
#elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
136-
SynchPrediv = 0x7A0; // 8M/32 = 250KHz
137-
AsynchPrediv = 0x7F; // value range: 0-7F
140+
SynchPrediv = 0x7A0; // 8M/32 = 250KHz
141+
AsynchPrediv = 0x7F; // value range: 0-7F
138142
#endif
139143
#endif /* BSP_RTC_USING_HSE */
140144

141145
#ifdef BSP_RTC_USING_LSE
142-
rt_kprintf("rtc clock source is set lse!\n");
143-
/* Enable the LSE OSC32_IN PC14 */
144-
RCC_EnableLsi(DISABLE); // LSI is turned off here to ensure that only one clock is turned on
146+
rt_kprintf("rtc clock source is set lse!\n");
147+
/* Enable the LSE OSC32_IN PC14 */
148+
RCC_EnableLsi(DISABLE); // LSI is turned off here to ensure that only one clock is turned on
145149

146150
#if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
147-
RCC_ConfigLse(RCC_LSE_ENABLE);
148-
while (RCC_GetFlagStatus(RCC_FLAG_LSERD) == RESET)
149-
{
150-
}
151+
RCC_ConfigLse(RCC_LSE_ENABLE);
152+
while (RCC_GetFlagStatus(RCC_FLAG_LSERD) == RESET)
153+
{
154+
}
151155
#elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
152-
RCC_ConfigLse(RCC_LSE_ENABLE,0x28);
153-
while (RCC_GetFlagStatus(RCC_LDCTRL_FLAG_LSERD) == RESET)
154-
{
155-
}
156+
RCC_ConfigLse(RCC_LSE_ENABLE,0x28);
157+
while (RCC_GetFlagStatus(RCC_LDCTRL_FLAG_LSERD) == RESET)
158+
{
159+
}
156160
#endif
157-
RCC_ConfigRtcClk(RCC_RTCCLK_SRC_LSE);
161+
RCC_ConfigRtcClk(RCC_RTCCLK_SRC_LSE);
158162

159-
SynchPrediv = 0xFF; // 32.768KHz
160-
AsynchPrediv = 0x7F; // value range: 0-7F
163+
SynchPrediv = 0xFF; // 32.768KHz
164+
AsynchPrediv = 0x7F; // value range: 0-7F
161165
#endif /* BSP_RTC_USING_LSE */
162166

163167
#ifdef BSP_RTC_USING_LSI
164-
rt_kprintf("rtc clock source is set lsi!\n");
165-
/* Enable the LSI OSC */
166-
RCC_EnableLsi(ENABLE);
168+
rt_kprintf("rtc clock source is set lsi!\n");
169+
/* Enable the LSI OSC */
170+
RCC_EnableLsi(ENABLE);
167171
#if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
168-
while (RCC_GetFlagStatus(RCC_FLAG_LSIRD) == RESET)
169-
{
170-
}
172+
while (RCC_GetFlagStatus(RCC_FLAG_LSIRD) == RESET)
173+
{
174+
}
171175
#elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
172-
while (RCC_GetFlagStatus(RCC_CTRLSTS_FLAG_LSIRD) == RESET)
173-
{
174-
}
176+
while (RCC_GetFlagStatus(RCC_CTRLSTS_FLAG_LSIRD) == RESET)
177+
{
178+
}
175179
#endif
176-
RCC_ConfigRtcClk(RCC_RTCCLK_SRC_LSI);
180+
RCC_ConfigRtcClk(RCC_RTCCLK_SRC_LSI);
177181

178182
#if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
179-
SynchPrediv = 0x136; // 39.64928KHz
180-
AsynchPrediv = 0x7F; // value range: 0-7F
183+
SynchPrediv = 0x136; // 39.64928KHz
184+
AsynchPrediv = 0x7F; // value range: 0-7F
181185
#elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
182-
SynchPrediv = 0x14A; // 41828Hz
183-
AsynchPrediv = 0x7F; // value range: 0-7F
186+
SynchPrediv = 0x14A; // 41828Hz
187+
AsynchPrediv = 0x7F; // value range: 0-7F
184188
#endif
185189
#endif /* BSP_RTC_USING_LSI */
186190

187-
/* Enable the RTC Clock */
188-
RCC_EnableRtcClk(ENABLE);
189-
RTC_WaitForSynchro();
191+
/* Enable the RTC Clock */
192+
RCC_EnableRtcClk(ENABLE);
193+
RTC_WaitForSynchro();
190194

191-
if (rt_rtc_config() != RT_EOK)
192-
{
193-
rt_kprintf("rtc init failed.\n");
194-
return -RT_ERROR;
195+
if (rt_rtc_config() != RT_EOK)
196+
{
197+
rt_kprintf("rtc init failed.\n");
198+
return -RT_ERROR;
199+
}
200+
201+
BKP_WriteBkpData(BKP_DAT1, USER_WRITE_BKP_DAT1_DATA);
195202
}
196203

197204
return RT_EOK;

0 commit comments

Comments
 (0)