#![cfg_attr(not(feature = "hal"), no_std)]
#[macro_use]
extern crate log;
extern crate embedded_hal;
#[cfg(feature = "mock")]
extern crate std;
#[cfg(feature = "mock")]
pub mod mock;
#[cfg(feature = "ffi")]
extern crate libc;
#[cfg(feature = "ffi")]
pub mod ffi;
#[cfg(feature = "serde")]
extern crate serde;
#[cfg(feature = "toml")]
extern crate toml;
#[cfg(feature = "simplelog")]
extern crate simplelog;
#[cfg(feature = "hal-linux")]
extern crate linux_embedded_hal;
#[cfg(feature = "hal-cp2130")]
extern crate driver_cp2130;
#[cfg(feature = "hal")]
pub mod hal;
pub mod wrapper;
pub trait ManagedChipSelect {}
pub trait Hal<E>:
PrefixWrite<Error = E>
+ PrefixRead<Error = E>
+ embedded_hal::spi::SpiDevice<u8, Error = E>
+ Busy<Error = E>
+ Ready<Error = E>
+ Reset<Error = E>
+ embedded_hal::delay::DelayNs
{
}
impl<T, E> Hal<E> for T where
T: PrefixWrite<Error = E>
+ PrefixRead<Error = E>
+ embedded_hal::spi::SpiDevice<u8, Error = E>
+ Busy<Error = E>
+ Ready<Error = E>
+ Reset<Error = E>
+ embedded_hal::delay::DelayNs
{
}
pub trait PrefixRead {
type Error;
fn prefix_read(&mut self, prefix: &[u8], data: &mut [u8]) -> Result<(), Self::Error>;
}
pub trait PrefixWrite {
type Error;
fn prefix_write(&mut self, prefix: &[u8], data: &[u8]) -> Result<(), Self::Error>;
}
pub type Transaction<'a> = embedded_hal::spi::Operation<'a, u8>;
pub trait ChipSelect {
type Error;
fn set_cs(&mut self, state: PinState) -> Result<(), Self::Error>;
}
pub trait Busy {
type Error;
fn get_busy(&mut self) -> Result<PinState, Self::Error>;
}
pub trait Reset {
type Error;
fn set_reset(&mut self, state: PinState) -> Result<(), Self::Error>;
}
pub trait Ready {
type Error;
fn get_ready(&mut self) -> Result<PinState, Self::Error>;
}
#[derive(Debug, Clone, PartialEq)]
pub enum Error<SpiError, PinError> {
Spi(SpiError),
Pin(PinError),
Aborted,
}
impl<SpiError, PinError> embedded_hal::spi::Error for Error<SpiError, PinError>
where
SpiError: core::fmt::Debug,
PinError: core::fmt::Debug,
{
fn kind(&self) -> embedded_hal::spi::ErrorKind {
embedded_hal::spi::ErrorKind::Other
}
}
impl<SpiError, PinError> embedded_hal::digital::Error for Error<SpiError, PinError>
where
SpiError: core::fmt::Debug,
PinError: core::fmt::Debug,
{
fn kind(&self) -> embedded_hal::digital::ErrorKind {
embedded_hal::digital::ErrorKind::Other
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum PinState {
Low,
High,
}
use embedded_hal::spi::{Operation, SpiDevice};
impl<T> PrefixWrite for T
where
T: SpiDevice<u8>,
<T as embedded_hal::spi::ErrorType>::Error: core::fmt::Debug,
{
type Error = <T as embedded_hal::spi::ErrorType>::Error;
fn prefix_write(&mut self, prefix: &[u8], data: &[u8]) -> Result<(), Self::Error> {
let mut ops = [Operation::Write(prefix), Operation::Write(data)];
self.transaction(&mut ops)?;
Ok(())
}
}
impl<T> PrefixRead for T
where
T: SpiDevice<u8>,
<T as embedded_hal::spi::ErrorType>::Error: core::fmt::Debug,
{
type Error = <T as embedded_hal::spi::ErrorType>::Error;
fn prefix_read<'a>(&mut self, prefix: &[u8], data: &'a mut [u8]) -> Result<(), Self::Error> {
let mut ops = [Operation::Write(prefix), Operation::TransferInPlace(data)];
self.transaction(&mut ops)?;
Ok(())
}
}