28
28
29
29
#include <nuttx/arch.h>
30
30
#include <nuttx/irq.h>
31
+ #include <nuttx/spinlock.h>
31
32
#include <nuttx/timers/rtc.h>
32
33
33
34
#ifdef CONFIG_RTC_ALARM
@@ -136,6 +137,7 @@ static void rtc_pmnotify(struct pm_callback_s *cb, enum pm_state_e pmstate);
136
137
137
138
#ifdef CONFIG_RTC_ALARM
138
139
static alarmcb_t g_alarmcb ;
140
+ static spinlock_t g_alarmcb_lock = SP_UNLOCKED ;
139
141
#endif
140
142
141
143
#ifdef CONFIG_RTC_SAVE_DEFAULT
@@ -581,6 +583,7 @@ int up_rtc_settime(const struct timespec *ts)
581
583
#ifdef CONFIG_RTC_ALARM
582
584
int up_rtc_setalarm (const struct timespec * ts , alarmcb_t callback )
583
585
{
586
+ irqstate_t flags ;
584
587
struct tm * tp ;
585
588
586
589
if (g_alarmcb )
@@ -589,9 +592,12 @@ int up_rtc_setalarm(const struct timespec *ts, alarmcb_t callback)
589
592
}
590
593
591
594
tp = gmtime (& ts -> tv_sec );
595
+
592
596
#ifdef CONFIG_RTC_DIV
593
597
tm_divider (tp , CONFIG_RTC_DIV_M , CONFIG_RTC_DIV_N );
594
598
#endif /* CONFIG_RTC_DIV */
599
+
600
+ flags = spin_lock_irqsave (& g_alarmcb_lock );
595
601
g_alarmcb = callback ;
596
602
#if 0
597
603
llinfo ("SETALARM (%04d/%02d/%02d %02d:%02d:%02d)\n" ,
@@ -623,6 +629,8 @@ int up_rtc_setalarm(const struct timespec *ts, alarmcb_t callback)
623
629
624
630
modifyreg8 (RTC_RTCINT , 1 << RTC_RTCINT_SET , 1 << RTC_RTCINT_AIE );
625
631
632
+ spin_unlock_irqrestore (& g_alarmcb_lock , flags );
633
+
626
634
return OK ;
627
635
}
628
636
@@ -633,14 +641,14 @@ int up_rtc_setalarm(const struct timespec *ts, alarmcb_t callback)
633
641
int up_rtc_cancelalarm (void )
634
642
{
635
643
irqstate_t flags ;
636
- flags = enter_critical_section ( );
644
+ flags = spin_lock_irqsave ( & g_alarmcb_lock );
637
645
g_alarmcb = NULL ;
638
646
639
647
/* Disable IRQ */
640
648
641
649
putreg8 (0 , RTC_RTCINT );
642
650
643
- leave_critical_section ( flags );
651
+ spin_unlock_irqrestore ( & g_alarmcb_lock , flags );
644
652
return 0 ;
645
653
}
646
654
0 commit comments