Skip to content

Commit 4d47cda

Browse files
authored
Merge pull request #344 from stm32-rs/errors
into nb::Error
2 parents 693a4b7 + 7274003 commit 4d47cda

File tree

2 files changed

+27
-33
lines changed

2 files changed

+27
-33
lines changed

src/serial.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -852,13 +852,13 @@ where
852852
}
853853

854854
Err(if sr.pe().bit_is_set() {
855-
nb::Error::Other(Error::Parity)
855+
Error::Parity.into()
856856
} else if sr.fe().bit_is_set() {
857-
nb::Error::Other(Error::Framing)
857+
Error::Framing.into()
858858
} else if sr.nf().bit_is_set() {
859-
nb::Error::Other(Error::Noise)
859+
Error::Noise.into()
860860
} else if sr.ore().bit_is_set() {
861-
nb::Error::Other(Error::Overrun)
861+
Error::Overrun.into()
862862
} else if sr.rxne().bit_is_set() {
863863
// NOTE(unsafe) atomic read from stateless register
864864
return Ok(unsafe { &*USART::ptr() }.dr.read().dr().bits());

src/spi.rs

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -359,20 +359,14 @@ pub struct Spi<SPI, PINS> {
359359

360360
// Implemented by all SPI instances
361361
pub trait Instance:
362-
crate::Sealed + Deref<Target = spi1::RegisterBlock> + rcc::Enable + rcc::Reset
362+
crate::Sealed + Deref<Target = spi1::RegisterBlock> + rcc::Enable + rcc::Reset + rcc::GetBusFreq
363363
{
364-
#[doc(hidden)]
365-
fn pclk_freq(clocks: &Clocks) -> Hertz;
366364
}
367365

368366
// Implemented by all SPI instances
369367
macro_rules! spi {
370-
($SPI:ident: ($spi:ident, $pclk:ident)) => {
371-
impl Instance for $SPI {
372-
fn pclk_freq(clocks: &Clocks) -> Hertz {
373-
clocks.$pclk()
374-
}
375-
}
368+
($SPI:ident: ($spi:ident)) => {
369+
impl Instance for $SPI {}
376370

377371
impl<SCK, MISO, MOSI> Spi<$SPI, (SCK, MISO, MOSI)>
378372
where
@@ -394,20 +388,20 @@ macro_rules! spi {
394388
};
395389
}
396390

397-
spi! { SPI1: (spi1, pclk2) }
398-
spi! { SPI2: (spi2, pclk1) }
391+
spi! { SPI1: (spi1) }
392+
spi! { SPI2: (spi2) }
399393

400394
#[cfg(feature = "spi3")]
401-
spi! { SPI3: (spi3, pclk1) }
395+
spi! { SPI3: (spi3) }
402396

403397
#[cfg(feature = "spi4")]
404-
spi! { SPI4: (spi4, pclk2) }
398+
spi! { SPI4: (spi4) }
405399

406400
#[cfg(feature = "spi5")]
407-
spi! { SPI5: (spi5, pclk2) }
401+
spi! { SPI5: (spi5) }
408402

409403
#[cfg(feature = "spi6")]
410-
spi! { SPI6: (spi6, pclk2) }
404+
spi! { SPI6: (spi6) }
411405

412406
impl<SPI, SCK, MISO, MOSI> Spi<SPI, (SCK, MISO, MOSI)>
413407
where
@@ -424,7 +418,7 @@ where
424418
SPI::reset(rcc);
425419
}
426420

427-
Spi { spi, pins }.init(mode, freq, SPI::pclk_freq(&clocks))
421+
Spi { spi, pins }.init(mode, freq, SPI::get_frequency(&clocks))
428422
}
429423
}
430424

@@ -448,34 +442,34 @@ where
448442
_ => 0b111,
449443
};
450444

451-
// mstr: master configuration
452-
// lsbfirst: MSB first
453-
// ssm: enable software slave management (NSS pin free for other uses)
454-
// ssi: set nss high = master mode
455-
// dff: 8 bit frames
456-
// bidimode: 2-line unidirectional
457-
// spe: enable the SPI bus
458445
self.spi.cr1.write(|w| {
459446
w.cpha()
460447
.bit(mode.phase == Phase::CaptureOnSecondTransition)
461448
.cpol()
462449
.bit(mode.polarity == Polarity::IdleHigh)
450+
// mstr: master configuration
463451
.mstr()
464452
.set_bit()
465453
.br()
466454
.bits(br)
455+
// lsbfirst: MSB first
467456
.lsbfirst()
468457
.clear_bit()
458+
// ssm: enable software slave management (NSS pin free for other uses)
469459
.ssm()
470460
.set_bit()
461+
// ssi: set nss high = master mode
471462
.ssi()
472463
.set_bit()
473464
.rxonly()
474465
.clear_bit()
466+
// dff: 8 bit frames
475467
.dff()
476468
.clear_bit()
469+
// bidimode: 2-line unidirectional
477470
.bidimode()
478471
.clear_bit()
472+
// spe: enable the SPI bus
479473
.spe()
480474
.set_bit()
481475
});
@@ -551,11 +545,11 @@ where
551545
let sr = self.spi.sr.read();
552546

553547
Err(if sr.ovr().bit_is_set() {
554-
nb::Error::Other(Error::Overrun)
548+
Error::Overrun.into()
555549
} else if sr.modf().bit_is_set() {
556-
nb::Error::Other(Error::ModeFault)
550+
Error::ModeFault.into()
557551
} else if sr.crcerr().bit_is_set() {
558-
nb::Error::Other(Error::Crc)
552+
Error::Crc.into()
559553
} else if sr.rxne().bit_is_set() {
560554
// NOTE(read_volatile) read only 1 byte (the svd2rust API only allows
561555
// reading a half-word)
@@ -571,18 +565,18 @@ where
571565
Err(if sr.ovr().bit_is_set() {
572566
// Read from the DR to clear the OVR bit
573567
let _ = self.spi.dr.read();
574-
nb::Error::Other(Error::Overrun)
568+
Error::Overrun.into()
575569
} else if sr.modf().bit_is_set() {
576570
// Write to CR1 to clear MODF
577571
self.spi.cr1.modify(|_r, w| w);
578-
nb::Error::Other(Error::ModeFault)
572+
Error::ModeFault.into()
579573
} else if sr.crcerr().bit_is_set() {
580574
// Clear the CRCERR bit
581575
self.spi.sr.modify(|_r, w| {
582576
w.crcerr().clear_bit();
583577
w
584578
});
585-
nb::Error::Other(Error::Crc)
579+
Error::Crc.into()
586580
} else if sr.txe().bit_is_set() {
587581
// NOTE(write_volatile) see note above
588582
unsafe { ptr::write_volatile(&self.spi.dr as *const _ as *mut u8, byte) }

0 commit comments

Comments
 (0)