Skip to content

Commit 879e4e3

Browse files
committed
Simplify serial read() error handling
Clearing the error bits once should suffice Signed-off-by: Daniel Egger <[email protected]>
1 parent 14e9452 commit 879e4e3

File tree

2 files changed

+9
-24
lines changed

2 files changed

+9
-24
lines changed

CHANGELOG.md

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

1010
### Changed
1111

12+
- Remove duplicate error bits clearing in serial `read()` implementation
1213
- Optimize SPI implementation
1314
- Use `pac` instead of `stm32` for PAC access and soft-deprecate the former
1415
- Updated stm32f0 dependency to v0.10 (breaking change)

src/serial.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -573,37 +573,21 @@ fn read(usart: *const SerialRegisterBlock) -> nb::Result<u8, Error> {
573573
// NOTE(unsafe) write accessor for atomic writes with no side effects
574574
let icr = unsafe { &(*usart).icr };
575575

576-
let err = if isr.pe().bit_is_set() {
576+
if isr.pe().bit_is_set() {
577577
icr.write(|w| w.pecf().set_bit());
578-
nb::Error::Other(Error::Parity)
578+
Err(nb::Error::Other(Error::Parity))
579579
} else if isr.fe().bit_is_set() {
580580
icr.write(|w| w.fecf().set_bit());
581-
nb::Error::Other(Error::Framing)
581+
Err(nb::Error::Other(Error::Framing))
582582
} else if isr.nf().bit_is_set() {
583583
icr.write(|w| w.ncf().set_bit());
584-
nb::Error::Other(Error::Noise)
584+
Err(nb::Error::Other(Error::Noise))
585585
} else if isr.ore().bit_is_set() {
586586
icr.write(|w| w.orecf().set_bit());
587-
nb::Error::Other(Error::Overrun)
587+
Err(nb::Error::Other(Error::Overrun))
588588
} else if isr.rxne().bit_is_set() {
589-
return Ok(unsafe { (*usart).rdr.read().rdr().bits() as u8 });
589+
Ok(unsafe { (*usart).rdr.read().rdr().bits() as u8 })
590590
} else {
591-
return Err(nb::Error::WouldBlock);
592-
};
593-
594-
// NOTE(unsafe) atomic write with no side effects other than clearing the errors we've just handled
595-
unsafe {
596-
(*usart).icr.write(|w| {
597-
w.pecf()
598-
.set_bit()
599-
.fecf()
600-
.set_bit()
601-
.ncf()
602-
.set_bit()
603-
.orecf()
604-
.set_bit()
605-
})
606-
};
607-
608-
Err(err)
591+
Err(nb::Error::WouldBlock)
592+
}
609593
}

0 commit comments

Comments
 (0)