diff options
| author | Corey Schuhen <[email protected]> | 2024-03-14 21:13:19 +1000 |
|---|---|---|
| committer | Corey Schuhen <[email protected]> | 2024-03-28 09:32:08 +1000 |
| commit | 32065d7719e8dd2f5da7787d4b7edf3109c632ba (patch) | |
| tree | 638afc709f97a0f340779f2c381e2036b1db8c20 | |
| parent | fcfcfce4007768578c3a6a1b744a5efd97f14376 (diff) | |
BXCAN: Cut out more that wasn't required from BXCAN crate.
| -rw-r--r-- | embassy-stm32/src/can/bx/mod.rs | 125 | ||||
| -rw-r--r-- | embassy-stm32/src/can/bxcan.rs | 7 |
2 files changed, 12 insertions, 120 deletions
diff --git a/embassy-stm32/src/can/bx/mod.rs b/embassy-stm32/src/can/bx/mod.rs index 121da1bb2..5ea0471c9 100644 --- a/embassy-stm32/src/can/bx/mod.rs +++ b/embassy-stm32/src/can/bx/mod.rs | |||
| @@ -602,54 +602,14 @@ where | |||
| 602 | unsafe { Tx::<I>::conjure(self.canregs).abort(mailbox) } | 602 | unsafe { Tx::<I>::conjure(self.canregs).abort(mailbox) } |
| 603 | } | 603 | } |
| 604 | 604 | ||
| 605 | /// Returns a received frame if available. | ||
| 606 | /// | ||
| 607 | /// This will first check FIFO 0 for a message or error. If none are available, FIFO 1 is | ||
| 608 | /// checked. | ||
| 609 | /// | ||
| 610 | /// Returns `Err` when a frame was lost due to buffer overrun. | ||
| 611 | pub fn receive(&mut self) -> nb::Result<Frame, OverrunError> { | ||
| 612 | // Safety: We have a `&mut self` and have unique access to the peripheral. | ||
| 613 | let mut rx0 = unsafe { Rx0::<I>::conjure(self.canregs) }; | ||
| 614 | let mut rx1 = unsafe { Rx1::<I>::conjure(self.canregs) }; | ||
| 615 | |||
| 616 | match rx0.receive() { | ||
| 617 | Err(nb::Error::WouldBlock) => rx1.receive(), | ||
| 618 | result => result, | ||
| 619 | } | ||
| 620 | } | ||
| 621 | |||
| 622 | /// Returns a reference to the RX FIFO 0. | ||
| 623 | pub fn rx0(&mut self) -> Rx0<I> { | ||
| 624 | // Safety: We take `&mut self` and the return value lifetimes are tied to `self`'s lifetime. | ||
| 625 | unsafe { Rx0::conjure(self.canregs) } | ||
| 626 | } | ||
| 627 | |||
| 628 | /// Returns a reference to the RX FIFO 1. | ||
| 629 | pub fn rx1(&mut self) -> Rx1<I> { | ||
| 630 | // Safety: We take `&mut self` and the return value lifetimes are tied to `self`'s lifetime. | ||
| 631 | unsafe { Rx1::conjure(self.canregs) } | ||
| 632 | } | ||
| 633 | 605 | ||
| 634 | pub(crate) fn split_by_ref(&mut self) -> (Tx<I>, Rx0<I>, Rx1<I>) { | 606 | pub(crate) fn split_by_ref(&mut self) -> (Tx<I>, Rx<I>) { |
| 635 | // Safety: We take `&mut self` and the return value lifetimes are tied to `self`'s lifetime. | 607 | // Safety: We take `&mut self` and the return value lifetimes are tied to `self`'s lifetime. |
| 636 | let tx = unsafe { Tx::conjure(self.canregs) }; | 608 | let tx = unsafe { Tx::conjure(self.canregs) }; |
| 637 | let rx0 = unsafe { Rx0::conjure(self.canregs) }; | 609 | let rx0 = unsafe { Rx::conjure() }; |
| 638 | let rx1 = unsafe { Rx1::conjure(self.canregs) }; | 610 | (tx, rx0) |
| 639 | (tx, rx0, rx1) | ||
| 640 | } | ||
| 641 | |||
| 642 | /// Consumes this `Can` instance and splits it into transmitting and receiving halves. | ||
| 643 | pub fn split(self) -> (Tx<I>, Rx0<I>, Rx1<I>) { | ||
| 644 | // Safety: `Self` is not `Copy` and is destroyed by moving it into this method. | ||
| 645 | unsafe { | ||
| 646 | ( | ||
| 647 | Tx::conjure(self.canregs), | ||
| 648 | Rx0::conjure(self.canregs), | ||
| 649 | Rx1::conjure(self.canregs), | ||
| 650 | ) | ||
| 651 | } | ||
| 652 | } | 611 | } |
| 612 | |||
| 653 | } | 613 | } |
| 654 | 614 | ||
| 655 | impl<I: FilterOwner> Can<I> { | 615 | impl<I: FilterOwner> Can<I> { |
| @@ -662,7 +622,7 @@ impl<I: FilterOwner> Can<I> { | |||
| 662 | } | 622 | } |
| 663 | } | 623 | } |
| 664 | 624 | ||
| 665 | /// Interface to the CAN transmitter part. | 625 | /// Marker for Tx half |
| 666 | pub struct Tx<I> { | 626 | pub struct Tx<I> { |
| 667 | _can: PhantomData<I>, | 627 | _can: PhantomData<I>, |
| 668 | canregs: crate::pac::can::Can, | 628 | canregs: crate::pac::can::Can, |
| @@ -844,87 +804,20 @@ where | |||
| 844 | } | 804 | } |
| 845 | } | 805 | } |
| 846 | 806 | ||
| 847 | /// Interface to receiver FIFO 0. | 807 | /// Marker for Rx half |
| 848 | pub struct Rx0<I> { | 808 | pub struct Rx<I> { |
| 849 | _can: PhantomData<I>, | ||
| 850 | canregs: crate::pac::can::Can, | ||
| 851 | } | ||
| 852 | |||
| 853 | impl<I> Rx0<I> | ||
| 854 | where | ||
| 855 | I: Instance, | ||
| 856 | { | ||
| 857 | unsafe fn conjure(canregs: crate::pac::can::Can) -> Self { | ||
| 858 | Self { | ||
| 859 | _can: PhantomData, | ||
| 860 | canregs, | ||
| 861 | } | ||
| 862 | } | ||
| 863 | |||
| 864 | /// Returns a received frame if available. | ||
| 865 | /// | ||
| 866 | /// Returns `Err` when a frame was lost due to buffer overrun. | ||
| 867 | pub fn receive(&mut self) -> nb::Result<Frame, OverrunError> { | ||
| 868 | receive_fifo(self.canregs, 0) | ||
| 869 | } | ||
| 870 | } | ||
| 871 | |||
| 872 | /// Interface to receiver FIFO 1. | ||
| 873 | pub struct Rx1<I> { | ||
| 874 | _can: PhantomData<I>, | 809 | _can: PhantomData<I>, |
| 875 | canregs: crate::pac::can::Can, | ||
| 876 | } | 810 | } |
| 877 | 811 | ||
| 878 | impl<I> Rx1<I> | 812 | impl<I> Rx<I> |
| 879 | where | 813 | where |
| 880 | I: Instance, | 814 | I: Instance, |
| 881 | { | 815 | { |
| 882 | unsafe fn conjure(canregs: crate::pac::can::Can) -> Self { | 816 | unsafe fn conjure() -> Self { |
| 883 | Self { | 817 | Self { |
| 884 | _can: PhantomData, | 818 | _can: PhantomData, |
| 885 | canregs, | ||
| 886 | } | 819 | } |
| 887 | } | 820 | } |
| 888 | |||
| 889 | /// Returns a received frame if available. | ||
| 890 | /// | ||
| 891 | /// Returns `Err` when a frame was lost due to buffer overrun. | ||
| 892 | pub fn receive(&mut self) -> nb::Result<Frame, OverrunError> { | ||
| 893 | receive_fifo(self.canregs, 1) | ||
| 894 | } | ||
| 895 | } | ||
| 896 | |||
| 897 | fn receive_fifo(canregs: crate::pac::can::Can, fifo_nr: usize) -> nb::Result<Frame, OverrunError> { | ||
| 898 | assert!(fifo_nr < 2); | ||
| 899 | let rfr = canregs.rfr(fifo_nr); | ||
| 900 | let rx = canregs.rx(fifo_nr); | ||
| 901 | |||
| 902 | //let rfr = &can.rfr[fifo_nr]; | ||
| 903 | //let rx = &can.rx[fifo_nr]; | ||
| 904 | |||
| 905 | // Check if a frame is available in the mailbox. | ||
| 906 | let rfr_read = rfr.read(); | ||
| 907 | if rfr_read.fmp() == 0 { | ||
| 908 | return Err(nb::Error::WouldBlock); | ||
| 909 | } | ||
| 910 | |||
| 911 | // Check for RX FIFO overrun. | ||
| 912 | if rfr_read.fovr() { | ||
| 913 | rfr.write(|w| w.set_fovr(true)); | ||
| 914 | return Err(nb::Error::Other(OverrunError { _priv: () })); | ||
| 915 | } | ||
| 916 | |||
| 917 | // Read the frame. | ||
| 918 | let id = IdReg(rx.rir().read().0); | ||
| 919 | let mut data = [0xff; 8]; | ||
| 920 | data[0..4].copy_from_slice(&rx.rdlr().read().0.to_ne_bytes()); | ||
| 921 | data[4..8].copy_from_slice(&rx.rdhr().read().0.to_ne_bytes()); | ||
| 922 | let len = rx.rdtr().read().dlc(); | ||
| 923 | |||
| 924 | // Release the mailbox. | ||
| 925 | rfr.write(|w| w.set_rfom(true)); | ||
| 926 | |||
| 927 | Ok(Frame::new(Header::new(id.id(), len, id.rtr()), &data).unwrap()) | ||
| 928 | } | 821 | } |
| 929 | 822 | ||
| 930 | /// Identifies one of the two receive FIFOs. | 823 | /// Identifies one of the two receive FIFOs. |
diff --git a/embassy-stm32/src/can/bxcan.rs b/embassy-stm32/src/can/bxcan.rs index 017c5110d..9d2b8797e 100644 --- a/embassy-stm32/src/can/bxcan.rs +++ b/embassy-stm32/src/can/bxcan.rs | |||
| @@ -296,8 +296,8 @@ impl<'d, T: Instance> Can<'d, T> { | |||
| 296 | /// | 296 | /// |
| 297 | /// Useful for doing separate transmit/receive tasks. | 297 | /// Useful for doing separate transmit/receive tasks. |
| 298 | pub fn split<'c>(&'c mut self) -> (CanTx<'d, T>, CanRx<'d, T>) { | 298 | pub fn split<'c>(&'c mut self) -> (CanTx<'d, T>, CanRx<'d, T>) { |
| 299 | let (tx, rx0, rx1) = self.can.split_by_ref(); | 299 | let (tx, rx) = self.can.split_by_ref(); |
| 300 | (CanTx { tx }, CanRx { rx0, rx1 }) | 300 | (CanTx { tx }, CanRx { rx}) |
| 301 | } | 301 | } |
| 302 | } | 302 | } |
| 303 | 303 | ||
| @@ -401,8 +401,7 @@ impl<'d, T: Instance> CanTx<'d, T> { | |||
| 401 | /// CAN driver, receive half. | 401 | /// CAN driver, receive half. |
| 402 | #[allow(dead_code)] | 402 | #[allow(dead_code)] |
| 403 | pub struct CanRx<'d, T: Instance> { | 403 | pub struct CanRx<'d, T: Instance> { |
| 404 | rx0: crate::can::bx::Rx0<BxcanInstance<'d, T>>, | 404 | rx: crate::can::bx::Rx<BxcanInstance<'d, T>>, |
| 405 | rx1: crate::can::bx::Rx1<BxcanInstance<'d, T>>, | ||
| 406 | } | 405 | } |
| 407 | 406 | ||
| 408 | impl<'d, T: Instance> CanRx<'d, T> { | 407 | impl<'d, T: Instance> CanRx<'d, T> { |
