@@ -63,16 +63,26 @@ impl Rtc {
63
63
}
64
64
65
65
pub fn set_alarm ( & mut self , time_seconds : u32 ) {
66
+ // Reset counter
66
67
self . perform_write ( |s| {
67
- // Reset counter
68
68
s. regs . cnth . write ( |w| unsafe { w. bits ( 0 ) } ) ;
69
+ } ) ;
70
+ self . perform_write ( |s| {
69
71
s. regs . cntl . write ( |w| unsafe { w. bits ( 0 ) } ) ;
72
+ } ) ;
70
73
71
- // Set alarm time
72
- s. regs . alrh . write ( |w| unsafe { w. alrh ( ) . bits ( ( time_seconds >> 16 ) as u16 ) } ) ;
73
- s. regs . alrl . write ( |w| unsafe { w. alrl ( ) . bits ( ( time_seconds & 0x0000ffff ) as u16 ) } ) ;
74
+ // Set alarm time
75
+ // See section 18.3.5 for explanation
76
+ let alarm_value = time_seconds - 1 ;
77
+ self . perform_write ( |s| {
78
+ s. regs . alrh . write ( |w| unsafe { w. alrh ( ) . bits ( ( alarm_value >> 16 ) as u16 ) } ) ;
79
+ } ) ;
80
+ self . perform_write ( |s| {
81
+ s. regs . alrl . write ( |w| unsafe { w. alrl ( ) . bits ( ( alarm_value & 0x0000ffff ) as u16 ) } ) ;
82
+ } ) ;
74
83
75
- // Enable alarm interrupt
84
+ // Enable alarm interrupt
85
+ self . perform_write ( |s| {
76
86
s. regs . crh . modify ( |_, w| w. alrie ( ) . set_bit ( ) ) ;
77
87
} )
78
88
}
@@ -84,6 +94,40 @@ impl Rtc {
84
94
( ( self . regs . cnth . read ( ) . bits ( ) << 16 ) as u32 ) + ( self . regs . cntl . read ( ) . bits ( ) as u32 )
85
95
}
86
96
97
+ /**
98
+ Enables the RTC second interrupt
99
+ */
100
+ pub fn listen_seconds ( & mut self ) {
101
+ self . perform_write ( |s| {
102
+ s. regs . crh . modify ( |_, w| w. secie ( ) . set_bit ( ) )
103
+ } )
104
+ }
105
+ /**
106
+ Disables the RTC second interrupt
107
+ */
108
+ pub fn unlisten_seconds ( & mut self ) {
109
+ self . perform_write ( |s| {
110
+ s. regs . crh . modify ( |_, w| w. secie ( ) . clear_bit ( ) )
111
+ } )
112
+ }
113
+ /**
114
+ Clears the RTC second interrupt flag
115
+ */
116
+ pub fn clear_second_flag ( & mut self ) {
117
+ self . perform_write ( |s| {
118
+ s. regs . crl . modify ( |_, w| w. secf ( ) . clear_bit ( ) )
119
+ } )
120
+ }
121
+
122
+ /**
123
+ Clears the RTC alarm interrupt flag
124
+ */
125
+ pub fn clear_alarm_flag ( & mut self ) {
126
+ self . perform_write ( |s| {
127
+ s. regs . crl . modify ( |_, w| w. alrf ( ) . clear_bit ( ) )
128
+ } )
129
+ }
130
+
87
131
88
132
fn perform_write ( & mut self , func : impl Fn ( & mut Self ) ) {
89
133
// This process is documented on page 485 of the stm32f103 manual
@@ -100,8 +144,4 @@ impl Rtc {
100
144
// Wait for the write to be done
101
145
while self . regs . crl . read ( ) . rtoff ( ) . bit ( ) == false { }
102
146
}
103
-
104
- fn clear_alarm_flag ( & mut self ) {
105
- self . perform_write ( |s| s. regs . crl . modify ( |_, w| w. alrf ( ) . clear_bit ( ) ) )
106
- }
107
147
}
0 commit comments