diff options
| author | xoviat <[email protected]> | 2021-03-02 00:32:23 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-03-02 00:32:23 +0100 |
| commit | 9626aee7dbe5fe454a014beee82383bc3f102a91 (patch) | |
| tree | 6d155c12dd0563056874ecc97d57c6ac521bcf5c /embassy-traits/src | |
| parent | 084b64053a562797fa3d88bb7dde9ddd8db792f1 (diff) | |
Move traits to separate crate.
Diffstat (limited to 'embassy-traits/src')
| -rw-r--r-- | embassy-traits/src/flash.rs | 51 | ||||
| -rw-r--r-- | embassy-traits/src/gpio.rs | 48 | ||||
| -rw-r--r-- | embassy-traits/src/lib.rs | 10 | ||||
| -rw-r--r-- | embassy-traits/src/uart.rs | 15 |
4 files changed, 124 insertions, 0 deletions
diff --git a/embassy-traits/src/flash.rs b/embassy-traits/src/flash.rs new file mode 100644 index 000000000..145cc7684 --- /dev/null +++ b/embassy-traits/src/flash.rs | |||
| @@ -0,0 +1,51 @@ | |||
| 1 | use core::future::Future; | ||
| 2 | use core::pin::Pin; | ||
| 3 | |||
| 4 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
| 5 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 6 | #[non_exhaustive] | ||
| 7 | pub enum Error { | ||
| 8 | Failed, | ||
| 9 | AddressMisaligned, | ||
| 10 | BufferMisaligned, | ||
| 11 | } | ||
| 12 | |||
| 13 | pub trait Flash { | ||
| 14 | type ReadFuture<'a>: Future<Output = Result<(), Error>>; | ||
| 15 | type WriteFuture<'a>: Future<Output = Result<(), Error>>; | ||
| 16 | type ErasePageFuture<'a>: Future<Output = Result<(), Error>>; | ||
| 17 | |||
| 18 | /// Reads data from the flash device. | ||
| 19 | /// | ||
| 20 | /// address must be a multiple of self.read_size(). | ||
| 21 | /// buf.len() must be a multiple of self.read_size(). | ||
| 22 | fn read<'a>(self: Pin<&'a mut Self>, address: usize, buf: &'a mut [u8]) | ||
| 23 | -> Self::ReadFuture<'a>; | ||
| 24 | |||
| 25 | /// Writes data to the flash device. | ||
| 26 | /// | ||
| 27 | /// address must be a multiple of self.write_size(). | ||
| 28 | /// buf.len() must be a multiple of self.write_size(). | ||
| 29 | fn write<'a>(self: Pin<&'a mut Self>, address: usize, buf: &'a [u8]) -> Self::WriteFuture<'a>; | ||
| 30 | |||
| 31 | /// Erases a single page from the flash device. | ||
| 32 | /// | ||
| 33 | /// address must be a multiple of self.erase_size(). | ||
| 34 | fn erase<'a>(self: Pin<&'a mut Self>, address: usize) -> Self::ErasePageFuture<'a>; | ||
| 35 | |||
| 36 | /// Returns the total size, in bytes. | ||
| 37 | /// This is not guaranteed to be a power of 2. | ||
| 38 | fn size(&self) -> usize; | ||
| 39 | |||
| 40 | /// Returns the read size in bytes. | ||
| 41 | /// This is guaranteed to be a power of 2. | ||
| 42 | fn read_size(&self) -> usize; | ||
| 43 | |||
| 44 | /// Returns the write size in bytes. | ||
| 45 | /// This is guaranteed to be a power of 2. | ||
| 46 | fn write_size(&self) -> usize; | ||
| 47 | |||
| 48 | /// Returns the erase size in bytes. | ||
| 49 | /// This is guaranteed to be a power of 2. | ||
| 50 | fn erase_size(&self) -> usize; | ||
| 51 | } | ||
diff --git a/embassy-traits/src/gpio.rs b/embassy-traits/src/gpio.rs new file mode 100644 index 000000000..4c3feac21 --- /dev/null +++ b/embassy-traits/src/gpio.rs | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | use core::future::Future; | ||
| 2 | use core::pin::Pin; | ||
| 3 | |||
| 4 | /// Wait for a pin to become high. | ||
| 5 | pub trait WaitForHigh { | ||
| 6 | type Future<'a>: Future<Output = ()> + 'a; | ||
| 7 | |||
| 8 | /// Wait for a pin to become high. | ||
| 9 | /// | ||
| 10 | /// If the pin is already high, the future completes immediately. | ||
| 11 | /// Otherwise, it completes when it becomes high. | ||
| 12 | fn wait_for_high<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; | ||
| 13 | } | ||
| 14 | |||
| 15 | /// Wait for a pin to become low. | ||
| 16 | pub trait WaitForLow { | ||
| 17 | type Future<'a>: Future<Output = ()> + 'a; | ||
| 18 | |||
| 19 | /// Wait for a pin to become low. | ||
| 20 | /// | ||
| 21 | /// If the pin is already low, the future completes immediately. | ||
| 22 | /// Otherwise, it completes when it becomes low. | ||
| 23 | fn wait_for_low<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; | ||
| 24 | } | ||
| 25 | |||
| 26 | /// Wait for a rising edge (transition from low to high) | ||
| 27 | pub trait WaitForRisingEdge { | ||
| 28 | type Future<'a>: Future<Output = ()> + 'a; | ||
| 29 | |||
| 30 | /// Wait for a rising edge (transition from low to high) | ||
| 31 | fn wait_for_rising_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; | ||
| 32 | } | ||
| 33 | |||
| 34 | /// Wait for a falling edge (transition from high to low) | ||
| 35 | pub trait WaitForFallingEdge { | ||
| 36 | type Future<'a>: Future<Output = ()> + 'a; | ||
| 37 | |||
| 38 | /// Wait for a falling edge (transition from high to low) | ||
| 39 | fn wait_for_falling_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; | ||
| 40 | } | ||
| 41 | |||
| 42 | /// Wait for any edge (any transition, high to low or low to high) | ||
| 43 | pub trait WaitForAnyEdge { | ||
| 44 | type Future<'a>: Future<Output = ()> + 'a; | ||
| 45 | |||
| 46 | /// Wait for any edge (any transition, high to low or low to high) | ||
| 47 | fn wait_for_any_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>; | ||
| 48 | } | ||
diff --git a/embassy-traits/src/lib.rs b/embassy-traits/src/lib.rs new file mode 100644 index 000000000..55af46cff --- /dev/null +++ b/embassy-traits/src/lib.rs | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | #![cfg_attr(not(feature = "std"), no_std)] | ||
| 2 | #![feature(generic_associated_types)] | ||
| 3 | #![feature(const_fn)] | ||
| 4 | #![feature(const_fn_fn_ptr_basics)] | ||
| 5 | #![feature(const_option)] | ||
| 6 | #![allow(incomplete_features)] | ||
| 7 | |||
| 8 | pub mod flash; | ||
| 9 | pub mod gpio; | ||
| 10 | pub mod uart; | ||
diff --git a/embassy-traits/src/uart.rs b/embassy-traits/src/uart.rs new file mode 100644 index 000000000..b40b9e9bd --- /dev/null +++ b/embassy-traits/src/uart.rs | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | use core::future::Future; | ||
| 2 | |||
| 3 | #[derive(Copy, Clone, Debug, Eq, PartialEq)] | ||
| 4 | #[cfg_attr(feature = "defmt", derive(defmt::Format))] | ||
| 5 | #[non_exhaustive] | ||
| 6 | pub enum Error { | ||
| 7 | Other, | ||
| 8 | } | ||
| 9 | |||
| 10 | pub trait Uart { | ||
| 11 | type ReceiveFuture<'a>: Future<Output = Result<(), Error>>; | ||
| 12 | type SendFuture<'a>: Future<Output = Result<(), Error>>; | ||
| 13 | fn receive<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReceiveFuture<'a>; | ||
| 14 | fn send<'a>(&'a mut self, buf: &'a [u8]) -> Self::SendFuture<'a>; | ||
| 15 | } | ||
