Skip to content

Commit

Permalink
Merge pull request #488 from luojia65/luojia65/1.0.0-fixes
Browse files Browse the repository at this point in the history
lib: amend documents to add periods (.), add inline hints when necessary
  • Loading branch information
Dirbaio authored Aug 23, 2023
2 parents 9228fb2 + d2f099e commit 35e6012
Show file tree
Hide file tree
Showing 23 changed files with 235 additions and 104 deletions.
3 changes: 3 additions & 0 deletions embedded-hal-async/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- Minor document fixes.
- Add #[inline] hints to most of `embedded-hal-async` functions.

## [v1.0.0-rc.1] - 2023-08-15

- Updated `embedded-hal` to version `1.0.0-rc.1`.
Expand Down
6 changes: 4 additions & 2 deletions embedded-hal-async/src/delay.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Delays
//! Delays.

/// Microsecond delay
/// Microsecond delay.
pub trait DelayUs {
/// Pauses execution for at minimum `us` microseconds. Pause can be longer
/// if the implementation requires it due to precision/timing issues.
Expand All @@ -15,10 +15,12 @@ impl<T> DelayUs for &mut T
where
T: DelayUs + ?Sized,
{
#[inline]
async fn delay_us(&mut self, us: u32) {
T::delay_us(self, us).await
}

#[inline]
async fn delay_ms(&mut self, ms: u32) {
T::delay_ms(self, ms).await
}
Expand Down
7 changes: 6 additions & 1 deletion embedded-hal-async/src/digital.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Asynchronous digital I/O
//! Asynchronous digital I/O.
//!
//! # Example
//!
Expand Down Expand Up @@ -49,22 +49,27 @@ pub trait Wait: embedded_hal::digital::ErrorType {
}

impl<T: Wait + ?Sized> Wait for &mut T {
#[inline]
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
T::wait_for_high(self).await
}

#[inline]
async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
T::wait_for_low(self).await
}

#[inline]
async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
T::wait_for_rising_edge(self).await
}

#[inline]
async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
T::wait_for_falling_edge(self).await
}

#[inline]
async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
T::wait_for_any_edge(self).await
}
Expand Down
15 changes: 11 additions & 4 deletions embedded-hal-async/src/i2c.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Async I2C API
//! Async I2C API.
//!
//! This API supports 7-bit and 10-bit addresses. Traits feature an `AddressMode`
//! marker type parameter. Two implementation of the `AddressMode` exist:
Expand All @@ -21,9 +21,9 @@ pub use embedded_hal::i2c::{
AddressMode, Error, ErrorKind, ErrorType, NoAcknowledgeSource, SevenBitAddress, TenBitAddress,
};

/// Async i2c
/// Async I2c.
pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
/// Reads enough bytes from slave with `address` to fill `buffer`
/// Reads enough bytes from slave with `address` to fill `buffer`.
///
/// # I2C Events (contract)
///
Expand All @@ -41,12 +41,13 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
/// - `MAK` = master acknowledge
/// - `NMAK` = master no acknowledge
/// - `SP` = stop condition
#[inline]
async fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
self.transaction(address, &mut [Operation::Read(read)])
.await
}

/// Writes bytes to slave with address `address`
/// Writes bytes to slave with address `address`.
///
/// # I2C Events (contract)
///
Expand All @@ -62,6 +63,7 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
/// - `SAK` = slave acknowledge
/// - `Bi` = ith byte of data
/// - `SP` = stop condition
#[inline]
async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> {
self.transaction(address, &mut [Operation::Write(write)])
.await
Expand Down Expand Up @@ -89,6 +91,7 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
/// - `MAK` = master acknowledge
/// - `NMAK` = master no acknowledge
/// - `SP` = stop condition
#[inline]
async fn write_read(
&mut self,
address: A,
Expand Down Expand Up @@ -123,14 +126,17 @@ pub trait I2c<A: AddressMode = SevenBitAddress>: ErrorType {
}

impl<A: AddressMode, T: I2c<A> + ?Sized> I2c<A> for &mut T {
#[inline]
async fn read(&mut self, address: A, read: &mut [u8]) -> Result<(), Self::Error> {
T::read(self, address, read).await
}

#[inline]
async fn write(&mut self, address: A, write: &[u8]) -> Result<(), Self::Error> {
T::write(self, address, write).await
}

#[inline]
async fn write_read(
&mut self,
address: A,
Expand All @@ -140,6 +146,7 @@ impl<A: AddressMode, T: I2c<A> + ?Sized> I2c<A> for &mut T {
T::write_read(self, address, write, read).await
}

#[inline]
async fn transaction(
&mut self,
address: A,
Expand Down
20 changes: 17 additions & 3 deletions embedded-hal-async/src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub use embedded_hal::spi::{
Error, ErrorKind, ErrorType, Mode, Operation, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3,
};

/// SPI device trait
/// SPI device trait.
///
/// `SpiDevice` represents ownership over a single SPI device on a (possibly shared) bus, selected
/// with a CS (Chip Select) pin.
Expand Down Expand Up @@ -36,6 +36,7 @@ pub trait SpiDevice<Word: Copy + 'static = u8>: ErrorType {
/// This is a convenience method equivalent to `device.read_transaction(&mut [buf])`.
///
/// See also: [`SpiDevice::transaction`], [`SpiDevice::read`]
#[inline]
async fn read(&mut self, buf: &mut [Word]) -> Result<(), Self::Error> {
self.transaction(&mut [Operation::Read(buf)]).await
}
Expand All @@ -45,6 +46,7 @@ pub trait SpiDevice<Word: Copy + 'static = u8>: ErrorType {
/// This is a convenience method equivalent to `device.write_transaction(&mut [buf])`.
///
/// See also: [`SpiDevice::transaction`], [`SpiDevice::write`]
#[inline]
async fn write(&mut self, buf: &[Word]) -> Result<(), Self::Error> {
self.transaction(&mut [Operation::Write(buf)]).await
}
Expand All @@ -54,6 +56,7 @@ pub trait SpiDevice<Word: Copy + 'static = u8>: ErrorType {
/// This is a convenience method equivalent to `device.transaction(|bus| bus.transfer(read, write))`.
///
/// See also: [`SpiDevice::transaction`], [`SpiBus::transfer`]
#[inline]
async fn transfer(&mut self, read: &mut [Word], write: &[Word]) -> Result<(), Self::Error> {
self.transaction(&mut [Operation::Transfer(read, write)])
.await
Expand All @@ -64,38 +67,44 @@ pub trait SpiDevice<Word: Copy + 'static = u8>: ErrorType {
/// This is a convenience method equivalent to `device.transaction(|bus| bus.transfer_in_place(buf))`.
///
/// See also: [`SpiDevice::transaction`], [`SpiBus::transfer_in_place`]
#[inline]
async fn transfer_in_place(&mut self, buf: &mut [Word]) -> Result<(), Self::Error> {
self.transaction(&mut [Operation::TransferInPlace(buf)])
.await
}
}

impl<Word: Copy + 'static, T: SpiDevice<Word> + ?Sized> SpiDevice<Word> for &mut T {
#[inline]
async fn transaction(
&mut self,
operations: &mut [Operation<'_, Word>],
) -> Result<(), Self::Error> {
T::transaction(self, operations).await
}

#[inline]
async fn read(&mut self, buf: &mut [Word]) -> Result<(), Self::Error> {
T::read(self, buf).await
}

#[inline]
async fn write(&mut self, buf: &[Word]) -> Result<(), Self::Error> {
T::write(self, buf).await
}

#[inline]
async fn transfer(&mut self, read: &mut [Word], write: &[Word]) -> Result<(), Self::Error> {
T::transfer(self, read, write).await
}

#[inline]
async fn transfer_in_place(&mut self, buf: &mut [Word]) -> Result<(), Self::Error> {
T::transfer_in_place(self, buf).await
}
}

/// SPI bus
/// SPI bus.
///
/// `SpiBus` represents **exclusive ownership** over the whole SPI bus, with SCK, MOSI and MISO pins.
///
Expand All @@ -110,7 +119,7 @@ pub trait SpiBus<Word: 'static + Copy = u8>: ErrorType {
/// complete. See (the docs on embedded-hal)[embedded_hal::spi] for details on flushing.
async fn read(&mut self, words: &mut [Word]) -> Result<(), Self::Error>;

/// Write `words` to the slave, ignoring all the incoming words
/// Write `words` to the slave, ignoring all the incoming words.
///
/// Implementations are allowed to return before the operation is
/// complete. See (the docs on embedded-hal)[embedded_hal::spi] for details on flushing.
Expand Down Expand Up @@ -144,22 +153,27 @@ pub trait SpiBus<Word: 'static + Copy = u8>: ErrorType {
}

impl<T: SpiBus<Word> + ?Sized, Word: 'static + Copy> SpiBus<Word> for &mut T {
#[inline]
async fn read(&mut self, words: &mut [Word]) -> Result<(), Self::Error> {
T::read(self, words).await
}

#[inline]
async fn write(&mut self, words: &[Word]) -> Result<(), Self::Error> {
T::write(self, words).await
}

#[inline]
async fn transfer(&mut self, read: &mut [Word], write: &[Word]) -> Result<(), Self::Error> {
T::transfer(self, read, write).await
}

#[inline]
async fn transfer_in_place(&mut self, words: &mut [Word]) -> Result<(), Self::Error> {
T::transfer_in_place(self, words).await
}

#[inline]
async fn flush(&mut self) -> Result<(), Self::Error> {
T::flush(self).await
}
Expand Down
3 changes: 3 additions & 0 deletions embedded-hal-bus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

- Minor document fixes.
- Add #[inline] hints to most of `embedded-hal-bus` functions.

## [v0.1.0-rc.1] - 2023-08-15

- Updated `embedded-hal`, `embedded-hal-async` to version `1.0.0-rc.1`.
Expand Down
7 changes: 6 additions & 1 deletion embedded-hal-bus/src/i2c/critical_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub struct CriticalSectionDevice<'a, T> {
}

impl<'a, T> CriticalSectionDevice<'a, T> {
/// Create a new `CriticalSectionDevice`
/// Create a new `CriticalSectionDevice`.
#[inline]
pub fn new(bus: &'a Mutex<RefCell<T>>) -> Self {
Self { bus }
}
Expand All @@ -31,20 +32,23 @@ impl<'a, T> I2c for CriticalSectionDevice<'a, T>
where
T: I2c,
{
#[inline]
fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
critical_section::with(|cs| {
let bus = &mut *self.bus.borrow_ref_mut(cs);
bus.read(address, read)
})
}

#[inline]
fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
critical_section::with(|cs| {
let bus = &mut *self.bus.borrow_ref_mut(cs);
bus.write(address, write)
})
}

#[inline]
fn write_read(
&mut self,
address: u8,
Expand All @@ -57,6 +61,7 @@ where
})
}

#[inline]
fn transaction(
&mut self,
address: u8,
Expand Down
7 changes: 6 additions & 1 deletion embedded-hal-bus/src/i2c/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub struct MutexDevice<'a, T> {
}

impl<'a, T> MutexDevice<'a, T> {
/// Create a new `MutexDevice`
/// Create a new `MutexDevice`.
#[inline]
pub fn new(bus: &'a Mutex<T>) -> Self {
Self { bus }
}
Expand All @@ -29,16 +30,19 @@ impl<'a, T> I2c for MutexDevice<'a, T>
where
T: I2c,
{
#[inline]
fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
let bus = &mut *self.bus.lock().unwrap();
bus.read(address, read)
}

#[inline]
fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
let bus = &mut *self.bus.lock().unwrap();
bus.write(address, write)
}

#[inline]
fn write_read(
&mut self,
address: u8,
Expand All @@ -49,6 +53,7 @@ where
bus.write_read(address, write, read)
}

#[inline]
fn transaction(
&mut self,
address: u8,
Expand Down
7 changes: 6 additions & 1 deletion embedded-hal-bus/src/i2c/refcell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ pub struct RefCellDevice<'a, T> {
}

impl<'a, T> RefCellDevice<'a, T> {
/// Create a new `RefCellDevice`
/// Create a new `RefCellDevice`.
#[inline]
pub fn new(bus: &'a RefCell<T>) -> Self {
Self { bus }
}
Expand All @@ -85,16 +86,19 @@ impl<'a, T> I2c for RefCellDevice<'a, T>
where
T: I2c,
{
#[inline]
fn read(&mut self, address: u8, read: &mut [u8]) -> Result<(), Self::Error> {
let bus = &mut *self.bus.borrow_mut();
bus.read(address, read)
}

#[inline]
fn write(&mut self, address: u8, write: &[u8]) -> Result<(), Self::Error> {
let bus = &mut *self.bus.borrow_mut();
bus.write(address, write)
}

#[inline]
fn write_read(
&mut self,
address: u8,
Expand All @@ -105,6 +109,7 @@ where
bus.write_read(address, write, read)
}

#[inline]
fn transaction(
&mut self,
address: u8,
Expand Down
Loading

0 comments on commit 35e6012

Please sign in to comment.