Skip to content

Commit 4c81088

Browse files
committed
Added interupt enabling/disabling support to USART ports
Signed-off-by: Daniel Egger <daniel@eggers-club.de>
1 parent 1924bb1 commit 4c81088

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Added
1111

1212
- Added ADC helper functions to read more intuitive values (#22) - @HarkonenBade
13+
- Added interrupt enabling/disabling support to USART ports
1314

1415
### Changed
1516

src/serial.rs

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@ use embedded_hal::prelude::*;
3636
#[allow(unused)]
3737
use crate::{gpio::*, rcc::Clocks, time::Bps};
3838

39-
/// Interrupt event
40-
pub enum Event {
41-
/// New data has been received
42-
Rxne,
43-
/// New data can be sent
44-
Txe,
45-
}
4639

4740
/// Serial error
4841
#[derive(Debug)]
@@ -59,6 +52,16 @@ pub enum Error {
5952
_Extensible,
6053
}
6154

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+
6265
pub trait TxPin<USART> {}
6366
pub trait RxPin<USART> {}
6467

@@ -178,22 +181,52 @@ macro_rules! usart {
178181
// NOTE(unsafe) This executes only during initialisation
179182
let rcc = unsafe { &(*crate::stm32::RCC::ptr()) };
180183

181-
/* Enable clock for USART */
184+
// Enable clock for USART
182185
rcc.$apbenr.modify(|_, w| w.$usartXen().set_bit());
183186

184187
// Calculate correct baudrate divisor on the fly
185188
let brr = clocks.pclk().0 / baud_rate.0;
186189
usart.brr.write(|w| unsafe { w.bits(brr) });
187190

188-
/* Reset other registers to disable advanced USART features */
191+
// Reset other registers to disable advanced USART features
189192
usart.cr2.reset();
190193
usart.cr3.reset();
191194

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());
194197

195198
Serial { usart, pins }
196199
}
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+
}
197230
}
198231
)+
199232
}

0 commit comments

Comments
 (0)