Move traits to separate crate.
This commit is contained in:
committed by
Dario Nieuwenhuis
parent
084b64053a
commit
9626aee7db
17
embassy-traits/Cargo.toml
Normal file
17
embassy-traits/Cargo.toml
Normal file
@ -0,0 +1,17 @@
|
||||
[package]
|
||||
name = "embassy-traits"
|
||||
version = "0.1.0"
|
||||
authors = ["Dario Nieuwenhuis <dirbaio@dirbaio.net>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
defmt = { version = "0.2.0", optional = true }
|
||||
|
||||
[features]
|
||||
std = []
|
||||
|
||||
defmt-trace = []
|
||||
defmt-debug = []
|
||||
defmt-info = []
|
||||
defmt-warn = []
|
||||
defmt-error = []
|
51
embassy-traits/src/flash.rs
Normal file
51
embassy-traits/src/flash.rs
Normal file
@ -0,0 +1,51 @@
|
||||
use core::future::Future;
|
||||
use core::pin::Pin;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
#[non_exhaustive]
|
||||
pub enum Error {
|
||||
Failed,
|
||||
AddressMisaligned,
|
||||
BufferMisaligned,
|
||||
}
|
||||
|
||||
pub trait Flash {
|
||||
type ReadFuture<'a>: Future<Output = Result<(), Error>>;
|
||||
type WriteFuture<'a>: Future<Output = Result<(), Error>>;
|
||||
type ErasePageFuture<'a>: Future<Output = Result<(), Error>>;
|
||||
|
||||
/// Reads data from the flash device.
|
||||
///
|
||||
/// address must be a multiple of self.read_size().
|
||||
/// buf.len() must be a multiple of self.read_size().
|
||||
fn read<'a>(self: Pin<&'a mut Self>, address: usize, buf: &'a mut [u8])
|
||||
-> Self::ReadFuture<'a>;
|
||||
|
||||
/// Writes data to the flash device.
|
||||
///
|
||||
/// address must be a multiple of self.write_size().
|
||||
/// buf.len() must be a multiple of self.write_size().
|
||||
fn write<'a>(self: Pin<&'a mut Self>, address: usize, buf: &'a [u8]) -> Self::WriteFuture<'a>;
|
||||
|
||||
/// Erases a single page from the flash device.
|
||||
///
|
||||
/// address must be a multiple of self.erase_size().
|
||||
fn erase<'a>(self: Pin<&'a mut Self>, address: usize) -> Self::ErasePageFuture<'a>;
|
||||
|
||||
/// Returns the total size, in bytes.
|
||||
/// This is not guaranteed to be a power of 2.
|
||||
fn size(&self) -> usize;
|
||||
|
||||
/// Returns the read size in bytes.
|
||||
/// This is guaranteed to be a power of 2.
|
||||
fn read_size(&self) -> usize;
|
||||
|
||||
/// Returns the write size in bytes.
|
||||
/// This is guaranteed to be a power of 2.
|
||||
fn write_size(&self) -> usize;
|
||||
|
||||
/// Returns the erase size in bytes.
|
||||
/// This is guaranteed to be a power of 2.
|
||||
fn erase_size(&self) -> usize;
|
||||
}
|
48
embassy-traits/src/gpio.rs
Normal file
48
embassy-traits/src/gpio.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use core::future::Future;
|
||||
use core::pin::Pin;
|
||||
|
||||
/// Wait for a pin to become high.
|
||||
pub trait WaitForHigh {
|
||||
type Future<'a>: Future<Output = ()> + 'a;
|
||||
|
||||
/// Wait for a pin to become high.
|
||||
///
|
||||
/// If the pin is already high, the future completes immediately.
|
||||
/// Otherwise, it completes when it becomes high.
|
||||
fn wait_for_high<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>;
|
||||
}
|
||||
|
||||
/// Wait for a pin to become low.
|
||||
pub trait WaitForLow {
|
||||
type Future<'a>: Future<Output = ()> + 'a;
|
||||
|
||||
/// Wait for a pin to become low.
|
||||
///
|
||||
/// If the pin is already low, the future completes immediately.
|
||||
/// Otherwise, it completes when it becomes low.
|
||||
fn wait_for_low<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>;
|
||||
}
|
||||
|
||||
/// Wait for a rising edge (transition from low to high)
|
||||
pub trait WaitForRisingEdge {
|
||||
type Future<'a>: Future<Output = ()> + 'a;
|
||||
|
||||
/// Wait for a rising edge (transition from low to high)
|
||||
fn wait_for_rising_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>;
|
||||
}
|
||||
|
||||
/// Wait for a falling edge (transition from high to low)
|
||||
pub trait WaitForFallingEdge {
|
||||
type Future<'a>: Future<Output = ()> + 'a;
|
||||
|
||||
/// Wait for a falling edge (transition from high to low)
|
||||
fn wait_for_falling_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>;
|
||||
}
|
||||
|
||||
/// Wait for any edge (any transition, high to low or low to high)
|
||||
pub trait WaitForAnyEdge {
|
||||
type Future<'a>: Future<Output = ()> + 'a;
|
||||
|
||||
/// Wait for any edge (any transition, high to low or low to high)
|
||||
fn wait_for_any_edge<'a>(self: Pin<&'a mut Self>) -> Self::Future<'a>;
|
||||
}
|
10
embassy-traits/src/lib.rs
Normal file
10
embassy-traits/src/lib.rs
Normal file
@ -0,0 +1,10 @@
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
#![feature(generic_associated_types)]
|
||||
#![feature(const_fn)]
|
||||
#![feature(const_fn_fn_ptr_basics)]
|
||||
#![feature(const_option)]
|
||||
#![allow(incomplete_features)]
|
||||
|
||||
pub mod flash;
|
||||
pub mod gpio;
|
||||
pub mod uart;
|
15
embassy-traits/src/uart.rs
Normal file
15
embassy-traits/src/uart.rs
Normal file
@ -0,0 +1,15 @@
|
||||
use core::future::Future;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
||||
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
||||
#[non_exhaustive]
|
||||
pub enum Error {
|
||||
Other,
|
||||
}
|
||||
|
||||
pub trait Uart {
|
||||
type ReceiveFuture<'a>: Future<Output = Result<(), Error>>;
|
||||
type SendFuture<'a>: Future<Output = Result<(), Error>>;
|
||||
fn receive<'a>(&'a mut self, buf: &'a mut [u8]) -> Self::ReceiveFuture<'a>;
|
||||
fn send<'a>(&'a mut self, buf: &'a [u8]) -> Self::SendFuture<'a>;
|
||||
}
|
Reference in New Issue
Block a user