Skip to content

Commit 7723d1f

Browse files
committed
fix[gd32][rtc]: Fix the initialization logic of the RTC clock and the issue of default triggering of the alarm interrupt.
1 parent 6f10e55 commit 7723d1f

File tree

1 file changed

+25
-58
lines changed

1 file changed

+25
-58
lines changed

bsp/gd32/arm/libraries/gd32_drivers/drv_rtc.c

Lines changed: 25 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
#elif defined(BSP_USING_ALARM0)
3030
#define BSP_ALARM_FLAG RTC_FLAG_ALARM0
3131
#define BSP_RTC_ALARM RTC_ALARM0
32+
#define BSP_RTC_INT_ALARM RTC_INT_ALARM0
3233
#elif defined(BSP_USING_ALARM1)
3334
#define BSP_ALARM_FLAG RTC_FLAG_ALARM1
3435
#define BSP_RTC_ALARM RTC_ALARM1
36+
#define BSP_RTC_INT_ALARM RTC_INT_ALARM1
3537
#endif
3638
#endif
3739

@@ -100,7 +102,6 @@ void RTC_Alarm_IRQHandler(void)
100102
LOG_D("RTC Alarm0 triggered");
101103
}
102104

103-
/* Check if alarm1 interrupt occurred */
104105
if (rtc_flag_get(RTC_FLAG_ALARM1) != RESET)
105106
{
106107
/* Clear alarm flag */
@@ -118,33 +119,8 @@ void RTC_Alarm_IRQHandler(void)
118119
LOG_D("RTC Alarm1 triggered");
119120
}
120121

121-
122122
rt_interrupt_leave();
123123
}
124-
125-
/**
126-
* @brief Initialize RTC alarm interrupt
127-
*/
128-
static void rtc_alarm_interrupt_init(void)
129-
{
130-
/* Clear any pending alarm flag first */
131-
rtc_flag_clear(RTC_FLAG_ALARM0);
132-
rtc_flag_clear(RTC_FLAG_ALARM1);
133-
134-
/* Clear EXTI line 17 flag */
135-
exti_flag_clear(EXTI_17);
136-
137-
/* Configure EXTI line 17 for RTC alarm interrupt */
138-
exti_init(EXTI_17, EXTI_INTERRUPT, EXTI_TRIG_RISING);
139-
140-
/* Enable RTC alarm interrupt */
141-
rtc_interrupt_enable(RTC_INT_ALARM0);
142-
143-
/* Enable RTC Alarm global interrupt in NVIC */
144-
nvic_irq_enable(RTC_Alarm_IRQn, 0, 0);
145-
146-
LOG_D("RTC alarm interrupt initialized with EXTI configuration");
147-
}
148124
#endif /* RT_USING_ALARM */
149125

150126
static rt_err_t gd_rtc_init(void)
@@ -183,40 +159,31 @@ static rt_err_t gd_rtc_init(void)
183159

184160
#if defined (RTC_CLOCK_SOURCE_LXTAL)
185161
/* Use LSE (32.768kHz) as RTC clock source */
162+
rcu_osci_on(RCU_LXTAL);
186163
if (rcu_osci_stab_wait(RCU_LXTAL) != SUCCESS)
187164
{
188-
rcu_osci_on(RCU_LXTAL);
189-
if (rcu_osci_stab_wait(RCU_LXTAL) != SUCCESS)
190-
{
191-
LOG_E("LSE oscillator failed to stabilize");
192-
return -RT_ERROR;
193-
}
165+
LOG_E("LSE oscillator failed to stabilize");
166+
return -RT_ERROR;
194167
}
195168
rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);
196169
LOG_D("RTC clock source: LSE (32.768kHz)");
197170
#elif defined(RTC_CLOCK_SOURCE_IRC32K)
198171
/* Use LSI (40kHz) as RTC clock source */
172+
rcu_osci_on(RCU_IRC32K);
199173
if (rcu_osci_stab_wait(RCU_IRC32K) != SUCCESS)
200174
{
201-
rcu_osci_on(RCU_IRC32K);
202-
if (rcu_osci_stab_wait(RCU_IRC32K) != SUCCESS)
203-
{
204-
LOG_E("LSI oscillator failed to stabilize");
205-
return -RT_ERROR;
206-
}
175+
LOG_E("LSI oscillator failed to stabilize");
176+
return -RT_ERROR;
207177
}
208178
rcu_rtc_clock_config(RCU_RTCSRC_IRC32K);
209179
LOG_D("RTC clock source: LSI (32kHz)");
210180
#elif defined(RTC_CLOCK_SOURCE_IRC40K)
211181
/* Use LSI (40kHz) as RTC clock source */
182+
rcu_osci_on(RCU_IRC40K);
212183
if (rcu_osci_stab_wait(RCU_IRC40K) != SUCCESS)
213184
{
214-
rcu_osci_on(RCU_IRC40K);
215-
if (rcu_osci_stab_wait(RCU_IRC40K) != SUCCESS)
216-
{
217-
LOG_E("LSI oscillator failed to stabilize");
218-
return -RT_ERROR;
219-
}
185+
LOG_E("LSI oscillator failed to stabilize");
186+
return -RT_ERROR;
220187
}
221188
rcu_rtc_clock_config(RCU_RTCSRC_IRC40K);
222189
LOG_D("RTC clock source: LSI (40kHz)");
@@ -243,11 +210,6 @@ static rt_err_t gd_rtc_init(void)
243210
LOG_D("RTC set to default time: 2024-01-01 00:00:00");
244211
}
245212

246-
#ifdef RT_USING_ALARM
247-
/* Initialize alarm interrupt */
248-
rtc_alarm_interrupt_init();
249-
#endif
250-
251213
LOG_D("RTC initialization successful");
252214
return RT_EOK;
253215
}
@@ -412,7 +374,7 @@ static rt_err_t gd_set_alarm(struct rt_rtc_wkalarm *alarm)
412374

413375
rtc_alarm_struct rtc_alarm;
414376

415-
rtc_alarm_disable(RTC_ALARM0);
377+
rtc_alarm_disable(BSP_RTC_ALARM);
416378
/* Initialize alarm structure */
417379
rtc_alarm.alarm_mask = RTC_ALARM_ALL_MASK;
418380
rtc_alarm.weekday_or_date = RTC_ALARM_DATE_SELECTED;
@@ -425,28 +387,33 @@ static rt_err_t gd_set_alarm(struct rt_rtc_wkalarm *alarm)
425387
/* Configure alarm */
426388
rtc_alarm_config(BSP_RTC_ALARM, &rtc_alarm);
427389

428-
rtc_interrupt_enable(RTC_INT_ALARM0);
429-
rtc_alarm_enable(BSP_RTC_ALARM);
430-
431390
/* Enable or disable alarm */
432391
if (alarm->enable)
433392
{
434393
/* Clear any pending alarm flag first */
435394
rtc_flag_clear(BSP_ALARM_FLAG);
395+
396+
/* Clear EXTI line 17 flag */
436397
exti_flag_clear(EXTI_17);
437398

438-
/* Enable alarm interrupt */
439-
rtc_interrupt_enable(RTC_INT_ALARM0);
399+
/* Enable RTC alarm interrupt */
400+
rtc_interrupt_enable(BSP_RTC_INT_ALARM);
440401

441402
/* Enable alarm */
442403
rtc_alarm_enable(BSP_RTC_ALARM);
443404

444-
LOG_D("RTC Alarm0 enabled with interrupt");
405+
/* Configure EXTI line 17 for RTC alarm interrupt */
406+
exti_init(EXTI_17, EXTI_INTERRUPT, EXTI_TRIG_RISING);
407+
408+
/* Enable RTC Alarm global interrupt in NVIC */
409+
nvic_irq_enable(RTC_Alarm_IRQn, 0, 0);
410+
411+
LOG_D("RTC Alarm enabled with interrupt");
445412
}
446413
else
447414
{
448415
/* Disable alarm interrupt first */
449-
rtc_interrupt_disable(RTC_INT_ALARM0);
416+
rtc_interrupt_disable(BSP_RTC_INT_ALARM);
450417

451418
/* Disable alarm */
452419
rtc_alarm_disable(BSP_RTC_ALARM);
@@ -455,7 +422,7 @@ static rt_err_t gd_set_alarm(struct rt_rtc_wkalarm *alarm)
455422
rtc_flag_clear(BSP_ALARM_FLAG);
456423
exti_flag_clear(EXTI_17);
457424

458-
LOG_D("RTC Alarm0 disabled");
425+
LOG_D("RTC Alarm disabled");
459426
}
460427

461428
LOG_D("RTC: set alarm %02d:%02d:%02d, enable: %d",

0 commit comments

Comments
 (0)