Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adapt to embedded-hal 1.0.0-alpha.1 #21

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ categories = ["embedded", "no-std"]
nb = "0.1"

[dependencies.embedded-hal]
version = "0.2.3"
features = ["unproven"]
version = "=1.0.0-alpha.1"

[dev-dependencies.stm32f1xx-hal]
version = "0.5.3"
Expand Down
4 changes: 2 additions & 2 deletions examples/i2c-eeprom24x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ fn main() -> ! {
for addr in addrs.iter() {
eeprom.write_byte(*addr, byte).unwrap();
// need to wait before next write
block!(delay.wait()).ok();
block!(delay.try_wait()).ok();
}

loop {
for addr in addrs.iter() {
let _ = eeprom.read_byte(*addr).unwrap();
block!(delay.wait()).ok();
block!(delay.try_wait()).ok();
}
}
}
2 changes: 1 addition & 1 deletion examples/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn main() -> ! {

loop {
for byte in b"Hello, World!\r\n" {
block!(serial.write(*byte)).unwrap();
block!(serial.try_write(*byte)).unwrap();
}

delay.delay_ms(1000u16);
Expand Down
23 changes: 11 additions & 12 deletions src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@
```
*/

use embedded_hal::blocking::i2c::{Read, Write, WriteRead};
use embedded_hal::digital::v2::{InputPin, OutputPin};
use embedded_hal::digital::{InputPin, OutputPin};
use embedded_hal::timer::{CountDown, Periodic};
use nb::block;

Expand Down Expand Up @@ -118,7 +117,7 @@ where
self.set_scl_high()?;
self.wait_for_clk();

let ack = self.sda.is_low().map_err(Error::Bus)?;
let ack = self.sda.try_is_low().map_err(Error::Bus)?;

self.set_scl_low()?;
self.set_sda_low()?;
Expand All @@ -136,7 +135,7 @@ where
self.set_scl_high()?;
self.wait_for_clk();

if self.sda.is_high().map_err(Error::Bus)? {
if self.sda.try_is_high().map_err(Error::Bus)? {
byte |= 1 << (7 - bit_offset);
}

Expand Down Expand Up @@ -201,27 +200,27 @@ where

#[inline]
fn set_scl_high(&mut self) -> Result<(), crate::i2c::Error<E>> {
self.scl.set_high().map_err(Error::Bus)
self.scl.try_set_high().map_err(Error::Bus)
}

#[inline]
fn set_scl_low(&mut self) -> Result<(), crate::i2c::Error<E>> {
self.scl.set_low().map_err(Error::Bus)
self.scl.try_set_low().map_err(Error::Bus)
}

#[inline]
fn set_sda_high(&mut self) -> Result<(), crate::i2c::Error<E>> {
self.sda.set_high().map_err(Error::Bus)
self.sda.try_set_high().map_err(Error::Bus)
}

#[inline]
fn set_sda_low(&mut self) -> Result<(), crate::i2c::Error<E>> {
self.sda.set_low().map_err(Error::Bus)
self.sda.try_set_low().map_err(Error::Bus)
}

#[inline]
fn wait_for_clk(&mut self) {
block!(self.clk.wait()).ok();
block!(self.clk.try_wait()).ok();
}

#[inline]
Expand All @@ -242,7 +241,7 @@ where
{
type Error = crate::i2c::Error<E>;

fn write(&mut self, addr: u8, output: &[u8]) -> Result<(), Self::Error> {
fn try_write(&mut self, addr: u8, output: &[u8]) -> Result<(), Self::Error> {
if output.is_empty() {
return Ok(());
}
Expand All @@ -269,7 +268,7 @@ where
{
type Error = crate::i2c::Error<E>;

fn read(&mut self, addr: u8, input: &mut [u8]) -> Result<(), Self::Error> {
fn try_read(&mut self, addr: u8, input: &mut [u8]) -> Result<(), Self::Error> {
if input.is_empty() {
return Ok(());
}
Expand All @@ -296,7 +295,7 @@ where
{
type Error = crate::i2c::Error<E>;

fn write_read(&mut self, addr: u8, output: &[u8], input: &mut [u8]) -> Result<(), Self::Error> {
fn try_write_read(&mut self, addr: u8, output: &[u8], input: &mut [u8]) -> Result<(), Self::Error> {
if output.is_empty() || input.is_empty() {
return Err(Error::InvalidData);
}
Expand Down
22 changes: 11 additions & 11 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! The timer must be configured to twice the desired communication frequency.
//!

use embedded_hal::digital::v2::{InputPin, OutputPin};
use embedded_hal::digital::{InputPin, OutputPin};
use embedded_hal::serial;
use embedded_hal::timer::{CountDown, Periodic};
use nb::block;
Expand Down Expand Up @@ -45,7 +45,7 @@ where

#[inline]
fn wait_for_timer(&mut self) {
block!(self.timer.wait()).ok();
block!(self.timer.try_wait()).ok();
}
}

Expand All @@ -57,25 +57,25 @@ where
{
type Error = crate::serial::Error<E>;

fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
fn try_write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
let mut data_out = byte;
self.tx.set_low().map_err(Error::Bus)?; // start bit
self.tx.try_set_low().map_err(Error::Bus)?; // start bit
self.wait_for_timer();
for _bit in 0..8 {
if data_out & 1 == 1 {
self.tx.set_high().map_err(Error::Bus)?;
self.tx.try_set_high().map_err(Error::Bus)?;
} else {
self.tx.set_low().map_err(Error::Bus)?;
self.tx.try_set_low().map_err(Error::Bus)?;
}
data_out >>= 1;
self.wait_for_timer();
}
self.tx.set_high().map_err(Error::Bus)?; // stop bit
self.tx.try_set_high().map_err(Error::Bus)?; // stop bit
self.wait_for_timer();
Ok(())
}

fn flush(&mut self) -> nb::Result<(), Self::Error> {
fn try_flush(&mut self) -> nb::Result<(), Self::Error> {
Ok(())
}
}
Expand All @@ -88,14 +88,14 @@ where
{
type Error = crate::serial::Error<E>;

fn read(&mut self) -> nb::Result<u8, Self::Error> {
fn try_read(&mut self) -> nb::Result<u8, Self::Error> {
let mut data_in = 0;
// wait for start bit
while self.rx.is_high().map_err(Error::Bus)? {}
while self.rx.try_is_high().map_err(Error::Bus)? {}
self.wait_for_timer();
for _bit in 0..8 {
data_in <<= 1;
if self.rx.is_high().map_err(Error::Bus)? {
if self.rx.try_is_high().map_err(Error::Bus)? {
data_in |= 1
}
self.wait_for_timer();
Expand Down
22 changes: 11 additions & 11 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

pub use embedded_hal::spi::{MODE_0, MODE_1, MODE_2, MODE_3};

use embedded_hal::digital::v2::{InputPin, OutputPin};
use embedded_hal::digital::{InputPin, OutputPin};
use embedded_hal::spi::{FullDuplex, Mode, Polarity};
use embedded_hal::timer::{CountDown, Periodic};
use nb::block;
Expand Down Expand Up @@ -83,8 +83,8 @@ where
};

match mode.polarity {
Polarity::IdleLow => spi.sck.set_low(),
Polarity::IdleHigh => spi.sck.set_high(),
Polarity::IdleLow => spi.sck.try_set_low(),
Polarity::IdleHigh => spi.sck.try_set_high(),
}
.unwrap_or(());

Expand All @@ -97,7 +97,7 @@ where
}

fn read_bit(&mut self) -> nb::Result<(), crate::spi::Error<E>> {
let is_miso_high = self.miso.is_high().map_err(Error::Bus)?;
let is_miso_high = self.miso.try_is_high().map_err(Error::Bus)?;
let shifted_value = self.read_val.unwrap_or(0) << 1;
if is_miso_high {
self.read_val = Some(shifted_value | 1);
Expand All @@ -109,17 +109,17 @@ where

#[inline]
fn set_clk_high(&mut self) -> Result<(), crate::spi::Error<E>> {
self.sck.set_high().map_err(Error::Bus)
self.sck.try_set_high().map_err(Error::Bus)
}

#[inline]
fn set_clk_low(&mut self) -> Result<(), crate::spi::Error<E>> {
self.sck.set_low().map_err(Error::Bus)
self.sck.try_set_low().map_err(Error::Bus)
}

#[inline]
fn wait_for_timer(&mut self) {
block!(self.timer.wait()).ok();
block!(self.timer.try_wait()).ok();
}
}

Expand All @@ -133,24 +133,24 @@ where
type Error = crate::spi::Error<E>;

#[inline]
fn read(&mut self) -> nb::Result<u8, Self::Error> {
fn try_read(&mut self) -> nb::Result<u8, Self::Error> {
match self.read_val {
Some(val) => Ok(val),
None => Err(nb::Error::Other(crate::spi::Error::NoData)),
}
}

fn send(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
fn try_send(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
for bit_offset in 0..8 {
let out_bit = match self.bit_order {
BitOrder::MSBFirst => (byte >> (7 - bit_offset)) & 0b1,
BitOrder::LSBFirst => (byte >> bit_offset) & 0b1,
};

if out_bit == 1 {
self.mosi.set_high().map_err(Error::Bus)?;
self.mosi.try_set_high().map_err(Error::Bus)?;
} else {
self.mosi.set_low().map_err(Error::Bus)?;
self.mosi.try_set_low().map_err(Error::Bus)?;
}

match self.mode {
Expand Down