aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/i2c
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2022-01-26 22:39:06 +0100
committerUlf Lilleengen <[email protected]>2022-01-26 22:39:06 +0100
commit4032fc06556312eab27488f05efe1803ade47b45 (patch)
tree0b38343758741e5c4074e86da30867595501f9b6 /embassy-stm32/src/i2c
parentcd36e3f7332d08865e670ca5b515d1c6efa1bf85 (diff)
Support unstable-trait feature for stm32
Diffstat (limited to 'embassy-stm32/src/i2c')
-rw-r--r--embassy-stm32/src/i2c/mod.rs1
-rw-r--r--embassy-stm32/src/i2c/v1.rs6
-rw-r--r--embassy-stm32/src/i2c/v2.rs144
3 files changed, 98 insertions, 53 deletions
diff --git a/embassy-stm32/src/i2c/mod.rs b/embassy-stm32/src/i2c/mod.rs
index 8eee13825..2dcb9b720 100644
--- a/embassy-stm32/src/i2c/mod.rs
+++ b/embassy-stm32/src/i2c/mod.rs
@@ -8,6 +8,7 @@ mod _version;
8use crate::{dma, peripherals}; 8use crate::{dma, peripherals};
9pub use _version::*; 9pub use _version::*;
10 10
11#[derive(Debug)]
11#[cfg_attr(feature = "defmt", derive(defmt::Format))] 12#[cfg_attr(feature = "defmt", derive(defmt::Format))]
12pub enum Error { 13pub enum Error {
13 Bus, 14 Bus,
diff --git a/embassy-stm32/src/i2c/v1.rs b/embassy-stm32/src/i2c/v1.rs
index 6b2c8a35c..aa5268987 100644
--- a/embassy-stm32/src/i2c/v1.rs
+++ b/embassy-stm32/src/i2c/v1.rs
@@ -270,7 +270,7 @@ impl<'d, T: Instance> I2c<'d, T> {
270 } 270 }
271} 271}
272 272
273impl<'d, T: Instance> embedded_hal::blocking::i2c::Read for I2c<'d, T> { 273impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Read for I2c<'d, T> {
274 type Error = Error; 274 type Error = Error;
275 275
276 fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { 276 fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
@@ -278,7 +278,7 @@ impl<'d, T: Instance> embedded_hal::blocking::i2c::Read for I2c<'d, T> {
278 } 278 }
279} 279}
280 280
281impl<'d, T: Instance> embedded_hal::blocking::i2c::Write for I2c<'d, T> { 281impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Write for I2c<'d, T> {
282 type Error = Error; 282 type Error = Error;
283 283
284 fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> { 284 fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> {
@@ -286,7 +286,7 @@ impl<'d, T: Instance> embedded_hal::blocking::i2c::Write for I2c<'d, T> {
286 } 286 }
287} 287}
288 288
289impl<'d, T: Instance> embedded_hal::blocking::i2c::WriteRead for I2c<'d, T> { 289impl<'d, T: Instance> embedded_hal_02::blocking::i2c::WriteRead for I2c<'d, T> {
290 type Error = Error; 290 type Error = Error;
291 291
292 fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> { 292 fn write_read(&mut self, addr: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> {
diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs
index 5b0e5fce2..9c05187a5 100644
--- a/embassy-stm32/src/i2c/v2.rs
+++ b/embassy-stm32/src/i2c/v2.rs
@@ -1,11 +1,9 @@
1use core::cmp; 1use core::cmp;
2use core::future::Future;
3use core::marker::PhantomData; 2use core::marker::PhantomData;
4use core::task::Poll; 3use core::task::Poll;
5 4
6use atomic_polyfill::{AtomicUsize, Ordering}; 5use atomic_polyfill::{AtomicUsize, Ordering};
7use embassy::interrupt::InterruptExt; 6use embassy::interrupt::InterruptExt;
8use embassy::traits::i2c::I2c as I2cTrait;
9use embassy::util::Unborrow; 7use embassy::util::Unborrow;
10use embassy::waitqueue::AtomicWaker; 8use embassy::waitqueue::AtomicWaker;
11use embassy_hal_common::drop::OnDrop; 9use embassy_hal_common::drop::OnDrop;
@@ -735,32 +733,36 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
735 } 733 }
736} 734}
737 735
738impl<'d, T: Instance> embedded_hal::blocking::i2c::Read for I2c<'d, T> { 736mod eh02 {
739 type Error = Error; 737 use super::*;
740 738
741 fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> { 739 impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Read for I2c<'d, T> {
742 self.blocking_read(address, buffer) 740 type Error = Error;
741
742 fn read(&mut self, address: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
743 self.blocking_read(address, buffer)
744 }
743 } 745 }
744}
745 746
746impl<'d, T: Instance> embedded_hal::blocking::i2c::Write for I2c<'d, T> { 747 impl<'d, T: Instance> embedded_hal_02::blocking::i2c::Write for I2c<'d, T> {
747 type Error = Error; 748 type Error = Error;
748 749
749 fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> { 750 fn write(&mut self, address: u8, bytes: &[u8]) -> Result<(), Self::Error> {
750 self.blocking_write(address, bytes) 751 self.blocking_write(address, bytes)
752 }
751 } 753 }
752}
753 754
754impl<'d, T: Instance> embedded_hal::blocking::i2c::WriteRead for I2c<'d, T> { 755 impl<'d, T: Instance> embedded_hal_02::blocking::i2c::WriteRead for I2c<'d, T> {
755 type Error = Error; 756 type Error = Error;
756 757
757 fn write_read( 758 fn write_read(
758 &mut self, 759 &mut self,
759 address: u8, 760 address: u8,
760 bytes: &[u8], 761 bytes: &[u8],
761 buffer: &mut [u8], 762 buffer: &mut [u8],
762 ) -> Result<(), Self::Error> { 763 ) -> Result<(), Self::Error> {
763 self.blocking_write_read(address, bytes, buffer) 764 self.blocking_write_read(address, bytes, buffer)
765 }
764 } 766 }
765} 767}
766 768
@@ -906,38 +908,80 @@ impl Timings {
906 } 908 }
907} 909}
908 910
909impl<'d, T: Instance, TXDMA: super::TxDma<T>, RXDMA: super::RxDma<T>> I2cTrait<u8> 911#[cfg(feature = "unstable-traits")]
910 for I2c<'d, T, TXDMA, RXDMA> 912mod eh1 {
911{ 913 use super::super::{RxDma, TxDma};
912 type Error = super::Error; 914 use super::*;
913 915 use core::future::Future;
914 type WriteFuture<'a> 916
915 where 917 impl embedded_hal_1::i2c::Error for Error {
916 Self: 'a, 918 fn kind(&self) -> embedded_hal_1::i2c::ErrorKind {
917 = impl Future<Output = Result<(), Self::Error>> + 'a; 919 match *self {
918 type ReadFuture<'a> 920 Self::Bus => embedded_hal_1::i2c::ErrorKind::Bus,
919 where 921 Self::Arbitration => embedded_hal_1::i2c::ErrorKind::ArbitrationLoss,
920 Self: 'a, 922 Self::Nack => embedded_hal_1::i2c::ErrorKind::NoAcknowledge(
921 = impl Future<Output = Result<(), Self::Error>> + 'a; 923 embedded_hal_1::i2c::NoAcknowledgeSource::Unknown,
922 type WriteReadFuture<'a> 924 ),
923 where 925 Self::Timeout => embedded_hal_1::i2c::ErrorKind::Other,
924 Self: 'a, 926 Self::Crc => embedded_hal_1::i2c::ErrorKind::Other,
925 = impl Future<Output = Result<(), Self::Error>> + 'a; 927 Self::Overrun => embedded_hal_1::i2c::ErrorKind::Overrun,
926 928 Self::ZeroLengthTransfer => embedded_hal_1::i2c::ErrorKind::Other,
927 fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> { 929 }
928 self.read(address, buffer) 930 }
929 } 931 }
930 932
931 fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> { 933 impl<'d, T: Instance, TXDMA: TxDma<T>, RXDMA: RxDma<T>> embedded_hal_1::i2c::ErrorType
932 self.write(address, bytes) 934 for I2c<'d, T, TXDMA, RXDMA>
935 {
936 type Error = Error;
933 } 937 }
934 938
935 fn write_read<'a>( 939 impl<'d, T: Instance, TXDMA: TxDma<T>, RXDMA: RxDma<T>> embedded_hal_async::i2c::I2c
936 &'a mut self, 940 for I2c<'d, T, TXDMA, RXDMA>
937 address: u8, 941 {
938 bytes: &'a [u8], 942 type ReadFuture<'a>
939 buffer: &'a mut [u8], 943 where
940 ) -> Self::WriteReadFuture<'a> { 944 Self: 'a,
941 self.write_read(address, bytes, buffer) 945 = impl Future<Output = Result<(), Self::Error>> + 'a;
946
947 fn read<'a>(&'a mut self, address: u8, buffer: &'a mut [u8]) -> Self::ReadFuture<'a> {
948 self.read(address, buffer)
949 }
950
951 type WriteFuture<'a>
952 where
953 Self: 'a,
954 = impl Future<Output = Result<(), Self::Error>> + 'a;
955 fn write<'a>(&'a mut self, address: u8, bytes: &'a [u8]) -> Self::WriteFuture<'a> {
956 self.write(address, bytes)
957 }
958
959 type WriteReadFuture<'a>
960 where
961 Self: 'a,
962 = impl Future<Output = Result<(), Self::Error>> + 'a;
963 fn write_read<'a>(
964 &'a mut self,
965 address: u8,
966 bytes: &'a [u8],
967 buffer: &'a mut [u8],
968 ) -> Self::WriteReadFuture<'a> {
969 self.write_read(address, bytes, buffer)
970 }
971
972 type TransactionFuture<'a>
973 where
974 Self: 'a,
975 = impl Future<Output = Result<(), Self::Error>> + 'a;
976
977 fn transaction<'a>(
978 &'a mut self,
979 address: u8,
980 operations: &mut [embedded_hal_async::i2c::Operation<'a>],
981 ) -> Self::TransactionFuture<'a> {
982 let _ = address;
983 let _ = operations;
984 async move { todo!() }
985 }
942 } 986 }
943} 987}