@@ -36,13 +36,6 @@ use embedded_hal::prelude::*;
36
36
#[ allow( unused) ]
37
37
use crate :: { gpio:: * , rcc:: Clocks , time:: Bps } ;
38
38
39
- /// Interrupt event
40
- pub enum Event {
41
- /// New data has been received
42
- Rxne ,
43
- /// New data can be sent
44
- Txe ,
45
- }
46
39
47
40
/// Serial error
48
41
#[ derive( Debug ) ]
@@ -59,6 +52,16 @@ pub enum Error {
59
52
_Extensible,
60
53
}
61
54
55
+ /// Interrupt event
56
+ pub enum Event {
57
+ /// New data has been received
58
+ Rxne ,
59
+ /// New data can be sent
60
+ Txe ,
61
+ /// Idle line state detected
62
+ Idle ,
63
+ }
64
+
62
65
pub trait TxPin < USART > { }
63
66
pub trait RxPin < USART > { }
64
67
@@ -178,22 +181,52 @@ macro_rules! usart {
178
181
// NOTE(unsafe) This executes only during initialisation
179
182
let rcc = unsafe { & ( * crate :: stm32:: RCC :: ptr( ) ) } ;
180
183
181
- /* Enable clock for USART */
184
+ // Enable clock for USART
182
185
rcc. $apbenr. modify( |_, w| w. $usartXen( ) . set_bit( ) ) ;
183
186
184
187
// Calculate correct baudrate divisor on the fly
185
188
let brr = clocks. pclk( ) . 0 / baud_rate. 0 ;
186
189
usart. brr. write( |w| unsafe { w. bits( brr) } ) ;
187
190
188
- /* Reset other registers to disable advanced USART features */
191
+ // Reset other registers to disable advanced USART features
189
192
usart. cr2. reset( ) ;
190
193
usart. cr3. reset( ) ;
191
194
192
- /* Enable transmission and receiving */
193
- usart. cr1. modify( |_, w| unsafe { w . bits ( 0xD ) } ) ;
195
+ // Enable transmission and receiving
196
+ usart. cr1. modify( |_, w| w . te ( ) . set_bit ( ) . re ( ) . set_bit ( ) . ue ( ) . set_bit ( ) ) ;
194
197
195
198
Serial { usart, pins }
196
199
}
200
+
201
+ /// Starts listening for an interrupt event
202
+ pub fn listen( & mut self , event: Event ) {
203
+ match event {
204
+ Event :: Rxne => {
205
+ self . usart. cr1. modify( |_, w| w. rxneie( ) . set_bit( ) )
206
+ } ,
207
+ Event :: Txe => {
208
+ self . usart. cr1. modify( |_, w| w. txeie( ) . set_bit( ) )
209
+ } ,
210
+ Event :: Idle => {
211
+ self . usart. cr1. modify( |_, w| w. idleie( ) . set_bit( ) )
212
+ } ,
213
+ }
214
+ }
215
+
216
+ /// Stop listening for an interrupt event
217
+ pub fn unlisten( & mut self , event: Event ) {
218
+ match event {
219
+ Event :: Rxne => {
220
+ self . usart. cr1. modify( |_, w| w. rxneie( ) . clear_bit( ) )
221
+ } ,
222
+ Event :: Txe => {
223
+ self . usart. cr1. modify( |_, w| w. txeie( ) . clear_bit( ) )
224
+ } ,
225
+ Event :: Idle => {
226
+ self . usart. cr1. modify( |_, w| w. idleie( ) . clear_bit( ) )
227
+ } ,
228
+ }
229
+ }
197
230
}
198
231
) +
199
232
}
0 commit comments