diff options
| author | Grant Miller <[email protected]> | 2022-03-14 16:24:43 -0500 |
|---|---|---|
| committer | Grant Miller <[email protected]> | 2022-03-14 16:53:50 -0500 |
| commit | 6e00c5885459927ab79aa31bf9b0c1564d70c35b (patch) | |
| tree | 20b00fe176c4218a1f87cdde212ea3dd4ba0b3ce | |
| parent | f0b62bc8e06b3d75e13fd8e36738104923d56174 (diff) | |
Make all functions generic over word size
| -rw-r--r-- | embassy-stm32/src/spi/mod.rs | 48 |
1 files changed, 27 insertions, 21 deletions
diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index 24819513c..3b39f0fd2 100644 --- a/embassy-stm32/src/spi/mod.rs +++ b/embassy-stm32/src/spi/mod.rs | |||
| @@ -407,11 +407,11 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { | |||
| 407 | self.current_word_size = word_size; | 407 | self.current_word_size = word_size; |
| 408 | } | 408 | } |
| 409 | 409 | ||
| 410 | pub async fn write(&mut self, data: &[u8]) -> Result<(), Error> | 410 | pub async fn write<W: Word>(&mut self, data: &[W]) -> Result<(), Error> |
| 411 | where | 411 | where |
| 412 | Tx: TxDma<T>, | 412 | Tx: TxDma<T>, |
| 413 | { | 413 | { |
| 414 | self.set_word_size(WordSize::EightBit); | 414 | self.set_word_size(W::WORDSIZE); |
| 415 | unsafe { | 415 | unsafe { |
| 416 | T::REGS.cr1().modify(|w| { | 416 | T::REGS.cr1().modify(|w| { |
| 417 | w.set_spe(false); | 417 | w.set_spe(false); |
| @@ -445,12 +445,12 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { | |||
| 445 | Ok(()) | 445 | Ok(()) |
| 446 | } | 446 | } |
| 447 | 447 | ||
| 448 | pub async fn read(&mut self, data: &mut [u8]) -> Result<(), Error> | 448 | pub async fn read<W: Word>(&mut self, data: &mut [W]) -> Result<(), Error> |
| 449 | where | 449 | where |
| 450 | Tx: TxDma<T>, | 450 | Tx: TxDma<T>, |
| 451 | Rx: RxDma<T>, | 451 | Rx: RxDma<T>, |
| 452 | { | 452 | { |
| 453 | self.set_word_size(WordSize::EightBit); | 453 | self.set_word_size(W::WORDSIZE); |
| 454 | unsafe { | 454 | unsafe { |
| 455 | T::REGS.cr1().modify(|w| { | 455 | T::REGS.cr1().modify(|w| { |
| 456 | w.set_spe(false); | 456 | w.set_spe(false); |
| @@ -494,7 +494,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { | |||
| 494 | Ok(()) | 494 | Ok(()) |
| 495 | } | 495 | } |
| 496 | 496 | ||
| 497 | pub async fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error> | 497 | pub async fn transfer<W: Word>(&mut self, read: &mut [W], write: &[W]) -> Result<(), Error> |
| 498 | where | 498 | where |
| 499 | Tx: TxDma<T>, | 499 | Tx: TxDma<T>, |
| 500 | Rx: RxDma<T>, | 500 | Rx: RxDma<T>, |
| @@ -503,7 +503,7 @@ impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { | |||
| 503 | let (_, tx_len) = slice_ptr_parts(write); | 503 | let (_, tx_len) = slice_ptr_parts(write); |
| 504 | assert_eq!(rx_len, tx_len); | 504 | assert_eq!(rx_len, tx_len); |
| 505 | 505 | ||
| 506 | self.set_word_size(WordSize::EightBit); | 506 | self.set_word_size(W::WORDSIZE); |
| 507 | unsafe { | 507 | unsafe { |
| 508 | T::REGS.cr1().modify(|w| { | 508 | T::REGS.cr1().modify(|w| { |
| 509 | w.set_spe(false); | 509 | w.set_spe(false); |
| @@ -840,24 +840,30 @@ mod eh1 { | |||
| 840 | } | 840 | } |
| 841 | } | 841 | } |
| 842 | 842 | ||
| 843 | impl<'d, T: Instance> embedded_hal_1::spi::blocking::SpiBusRead<u8> for Spi<'d, T, NoDma, NoDma> { | 843 | impl<'d, T: Instance, W: Word> embedded_hal_1::spi::blocking::SpiBusRead<W> |
| 844 | fn read(&mut self, words: &mut [u8]) -> Result<(), Self::Error> { | 844 | for Spi<'d, T, NoDma, NoDma> |
| 845 | { | ||
| 846 | fn read(&mut self, words: &mut [W]) -> Result<(), Self::Error> { | ||
| 845 | self.blocking_read(words) | 847 | self.blocking_read(words) |
| 846 | } | 848 | } |
| 847 | } | 849 | } |
| 848 | 850 | ||
| 849 | impl<'d, T: Instance> embedded_hal_1::spi::blocking::SpiBusWrite<u8> for Spi<'d, T, NoDma, NoDma> { | 851 | impl<'d, T: Instance, W: Word> embedded_hal_1::spi::blocking::SpiBusWrite<W> |
| 850 | fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { | 852 | for Spi<'d, T, NoDma, NoDma> |
| 853 | { | ||
| 854 | fn write(&mut self, words: &[W]) -> Result<(), Self::Error> { | ||
| 851 | self.blocking_write(words) | 855 | self.blocking_write(words) |
| 852 | } | 856 | } |
| 853 | } | 857 | } |
| 854 | 858 | ||
| 855 | impl<'d, T: Instance> embedded_hal_1::spi::blocking::SpiBus<u8> for Spi<'d, T, NoDma, NoDma> { | 859 | impl<'d, T: Instance, W: Word> embedded_hal_1::spi::blocking::SpiBus<W> |
| 856 | fn transfer(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Self::Error> { | 860 | for Spi<'d, T, NoDma, NoDma> |
| 861 | { | ||
| 862 | fn transfer(&mut self, read: &mut [W], write: &[W]) -> Result<(), Self::Error> { | ||
| 857 | self.blocking_transfer(read, write) | 863 | self.blocking_transfer(read, write) |
| 858 | } | 864 | } |
| 859 | 865 | ||
| 860 | fn transfer_in_place(&mut self, words: &mut [u8]) -> Result<(), Self::Error> { | 866 | fn transfer_in_place(&mut self, words: &mut [W]) -> Result<(), Self::Error> { |
| 861 | self.blocking_transfer_in_place(words) | 867 | self.blocking_transfer_in_place(words) |
| 862 | } | 868 | } |
| 863 | } | 869 | } |
| @@ -885,32 +891,32 @@ cfg_if::cfg_if! { | |||
| 885 | } | 891 | } |
| 886 | } | 892 | } |
| 887 | 893 | ||
| 888 | impl<'d, T: Instance, Tx: TxDma<T>, Rx> embedded_hal_async::spi::SpiBusWrite<u8> | 894 | impl<'d, T: Instance, Tx: TxDma<T>, Rx, W: Word> embedded_hal_async::spi::SpiBusWrite<W> |
| 889 | for Spi<'d, T, Tx, Rx> | 895 | for Spi<'d, T, Tx, Rx> |
| 890 | { | 896 | { |
| 891 | type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | 897 | type WriteFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; |
| 892 | 898 | ||
| 893 | fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { | 899 | fn write<'a>(&'a mut self, data: &'a [W]) -> Self::WriteFuture<'a> { |
| 894 | self.write(data) | 900 | self.write(data) |
| 895 | } | 901 | } |
| 896 | } | 902 | } |
| 897 | 903 | ||
| 898 | impl<'d, T: Instance, Tx: TxDma<T>, Rx: RxDma<T>> embedded_hal_async::spi::SpiBusRead<u8> | 904 | impl<'d, T: Instance, Tx: TxDma<T>, Rx: RxDma<T>, W: Word> embedded_hal_async::spi::SpiBusRead<W> |
| 899 | for Spi<'d, T, Tx, Rx> | 905 | for Spi<'d, T, Tx, Rx> |
| 900 | { | 906 | { |
| 901 | type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | 907 | type ReadFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; |
| 902 | 908 | ||
| 903 | fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { | 909 | fn read<'a>(&'a mut self, data: &'a mut [W]) -> Self::ReadFuture<'a> { |
| 904 | self.read(data) | 910 | self.read(data) |
| 905 | } | 911 | } |
| 906 | } | 912 | } |
| 907 | 913 | ||
| 908 | impl<'d, T: Instance, Tx: TxDma<T>, Rx: RxDma<T>> embedded_hal_async::spi::SpiBus<u8> | 914 | impl<'d, T: Instance, Tx: TxDma<T>, Rx: RxDma<T>, W: Word> embedded_hal_async::spi::SpiBus<W> |
| 909 | for Spi<'d, T, Tx, Rx> | 915 | for Spi<'d, T, Tx, Rx> |
| 910 | { | 916 | { |
| 911 | type TransferFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; | 917 | type TransferFuture<'a> = impl Future<Output = Result<(), Self::Error>> + 'a where Self: 'a; |
| 912 | 918 | ||
| 913 | fn transfer<'a>(&'a mut self, rx: &'a mut [u8], tx: &'a [u8]) -> Self::TransferFuture<'a> { | 919 | fn transfer<'a>(&'a mut self, rx: &'a mut [W], tx: &'a [W]) -> Self::TransferFuture<'a> { |
| 914 | self.transfer(rx, tx) | 920 | self.transfer(rx, tx) |
| 915 | } | 921 | } |
| 916 | 922 | ||
| @@ -918,7 +924,7 @@ cfg_if::cfg_if! { | |||
| 918 | 924 | ||
| 919 | fn transfer_in_place<'a>( | 925 | fn transfer_in_place<'a>( |
| 920 | &'a mut self, | 926 | &'a mut self, |
| 921 | words: &'a mut [u8], | 927 | words: &'a mut [W], |
| 922 | ) -> Self::TransferInPlaceFuture<'a> { | 928 | ) -> Self::TransferInPlaceFuture<'a> { |
| 923 | // TODO: Implement async version | 929 | // TODO: Implement async version |
| 924 | let result = self.blocking_transfer_in_place(words); | 930 | let result = self.blocking_transfer_in_place(words); |
| @@ -995,7 +1001,7 @@ pub(crate) mod sealed { | |||
| 995 | } | 1001 | } |
| 996 | } | 1002 | } |
| 997 | 1003 | ||
| 998 | pub trait Word: Copy + 'static + sealed::Word + Default {} | 1004 | pub trait Word: Copy + 'static + sealed::Word + Default + crate::dma::Word {} |
| 999 | 1005 | ||
| 1000 | impl Word for u8 {} | 1006 | impl Word for u8 {} |
| 1001 | impl Word for u16 {} | 1007 | impl Word for u16 {} |
