Skip to content

Commit c9c0cb6

Browse files
committed
[WIP] Refactor for embedded-hal v1.0.0-alpha.6 (master)
Blocked on embedded-hal v1.0.0 release (rust-embedded/embedded-hal#177) Signed-off-by: Moritz Scheuren <[email protected]>
1 parent e84d5a3 commit c9c0cb6

File tree

12 files changed

+237
-174
lines changed

12 files changed

+237
-174
lines changed

Cargo.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,30 @@ version = "0.6.0"
1515
features = ["stm32f746", "rt"]
1616

1717
[dependencies]
18-
as-slice = "0.1.0"
18+
as-slice = "0.2.1"
1919
cortex-m = "0.7"
2020
cortex-m-rt = ">=0.6.15, <0.8"
2121
embedded-time = "0.12.0"
22-
nb = "0.1.2"
22+
nb = "1.0.0"
2323
rtcc = "0.2"
2424
stm32f7 = "0.14.0"
25-
micromath = "1.0.0"
25+
micromath = "2.0.0"
2626
synopsys-usb-otg = { version = "0.2.3", features = ["cortex-m"], optional = true }
2727
stm32-fmc = { version = "0.2.0", features = ["sdram"], optional = true }
2828
rand_core = "0.6"
29-
bxcan = ">=0.4, <0.6"
29+
bxcan = "0.6.0"
3030

3131
[dependencies.bare-metal]
32-
version = "0.2.4"
33-
features = ["const-fn"]
32+
version = "1.0.0"
3433

3534
[dependencies.cast]
3635
default-features = false
37-
version = "0.2.2"
36+
version = "0.3.0"
3837

3938
[dependencies.embedded-hal]
40-
features = ["unproven"]
41-
version = "0.2.3"
39+
#version = "1.0.0"
40+
git = "https://github.com/rust-embedded/embedded-hal"
41+
rev = "c7497ba"
4242

4343
[dependencies.void]
4444
default-features = false
@@ -52,7 +52,7 @@ version = "0.4.1"
5252
cortex-m-semihosting = "0.3.3"
5353
panic-halt = "0.2.0"
5454
panic-semihosting = "0.5.2"
55-
embedded-graphics = "0.6.1"
55+
embedded-graphics = "0.7.1"
5656
usb-device = "0.2.5"
5757
usbd-serial = "0.1.0"
5858

src/adc.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::pac::{ADC1, ADC2, ADC3, ADC_COMMON};
1111

1212
use cortex_m::asm::delay;
1313

14-
use embedded_hal::adc::{Channel, OneShot};
14+
use embedded_hal::adc::nb::{Channel, OneShot};
1515

1616
#[derive(Clone, Copy, Debug, PartialEq)]
1717
#[allow(non_camel_case_types)]
@@ -95,7 +95,7 @@ macro_rules! adc_pins {
9595
impl Channel<$ADC> for $pin {
9696
type ID = u8;
9797

98-
fn channel() -> u8 { $chan }
98+
fn channel(&self) -> u8 { $chan }
9999
}
100100
)+
101101
};
@@ -442,8 +442,8 @@ macro_rules! adc_hal {
442442
{
443443
type Error = ();
444444

445-
fn read(&mut self, _pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
446-
let res = self.convert(PIN::channel());
445+
fn read(&mut self, pin: &mut PIN) -> nb::Result<WORD, Self::Error> {
446+
let res = self.convert(PIN::channel(pin));
447447
Ok(res.into())
448448
}
449449
}

src/delay.rs

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
//! Delays
22
3-
use cast::u32;
43
use cortex_m::peripheral::syst::SystClkSource;
54
use cortex_m::peripheral::SYST;
65

7-
use crate::hal::blocking::delay::{DelayMs, DelayUs};
6+
use crate::hal::delay::blocking::DelayUs;
87
use crate::rcc::Clocks;
98

109
/// System timer (SysTick) as a delay provider
@@ -27,26 +26,10 @@ impl Delay {
2726
}
2827
}
2928

30-
impl DelayMs<u32> for Delay {
31-
fn delay_ms(&mut self, ms: u32) {
32-
self.delay_us(ms * 1_000);
33-
}
34-
}
35-
36-
impl DelayMs<u16> for Delay {
37-
fn delay_ms(&mut self, ms: u16) {
38-
self.delay_ms(u32(ms));
39-
}
40-
}
41-
42-
impl DelayMs<u8> for Delay {
43-
fn delay_ms(&mut self, ms: u8) {
44-
self.delay_ms(u32(ms));
45-
}
46-
}
29+
impl DelayUs for Delay {
30+
type Error = ();
4731

48-
impl DelayUs<u32> for Delay {
49-
fn delay_us(&mut self, us: u32) {
32+
fn delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
5033
// The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
5134
const MAX_RVR: u32 = 0x00FF_FFFF;
5235

@@ -70,17 +53,6 @@ impl DelayUs<u32> for Delay {
7053

7154
self.syst.disable_counter();
7255
}
73-
}
74-
}
75-
76-
impl DelayUs<u16> for Delay {
77-
fn delay_us(&mut self, us: u16) {
78-
self.delay_us(u32(us))
79-
}
80-
}
81-
82-
impl DelayUs<u8> for Delay {
83-
fn delay_us(&mut self, us: u8) {
84-
self.delay_us(u32(us))
56+
Ok(())
8557
}
8658
}

src/gpio.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@
4747
use core::convert::Infallible;
4848
use core::marker::PhantomData;
4949

50-
pub use embedded_hal::digital::v2::PinState;
51-
use embedded_hal::digital::v2::{
50+
use embedded_hal::digital::blocking::{
5251
InputPin, IoPin, OutputPin, StatefulOutputPin, ToggleableOutputPin,
5352
};
53+
pub use embedded_hal::digital::PinState;
5454

5555
use crate::pac::{EXTI, SYSCFG};
5656
use crate::rcc::{Enable, APB2};
@@ -450,7 +450,7 @@ impl<MODE, const P: char, const N: u8> Pin<Output<MODE>, P, N> {
450450

451451
#[inline(always)]
452452
pub fn toggle(&mut self) {
453-
if self.is_set_low() {
453+
if self.is_set_low().unwrap() {
454454
self.set_high()
455455
} else {
456456
self.set_low()

src/gpio/erased.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<MODE> ErasedPin<Output<MODE>> {
9999

100100
#[inline(always)]
101101
pub fn toggle(&mut self) {
102-
if self.is_set_low() {
102+
if self.is_set_low().unwrap() {
103103
self.set_high()
104104
} else {
105105
self.set_low()

src/gpio/partially_erased.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<MODE, const P: char> PartiallyErasedPin<Output<MODE>, P> {
8080

8181
#[inline(always)]
8282
pub fn toggle(&mut self) {
83-
if self.is_set_low() {
83+
if self.is_set_low().unwrap() {
8484
self.set_high()
8585
} else {
8686
self.set_low()

src/i2c.rs

Lines changed: 55 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,50 @@ use crate::gpio::gpioc::PC9;
1010
use crate::gpio::gpiof::{PF0, PF1};
1111
use crate::gpio::gpioh::{PH4, PH5, PH7, PH8};
1212
use crate::gpio::AlternateOD;
13-
use crate::hal::blocking::i2c::{Read, Write, WriteRead};
13+
use crate::hal::i2c::{
14+
self,
15+
blocking::{Read, Write, WriteRead},
16+
};
1417
use crate::pac::{DWT, I2C1, I2C2, I2C3};
1518
use crate::rcc::{Clocks, Enable, GetBusFreq, RccBus, Reset};
16-
use nb::Error::{Other, WouldBlock};
17-
use nb::{Error as NbError, Result as NbResult};
19+
use nb;
1820

1921
use cast::u16;
2022

2123
/// I2C error
2224
#[derive(Debug, Eq, PartialEq)]
2325
#[non_exhaustive]
2426
pub enum Error {
25-
/// Bus error
27+
/// Bus error occurred. e.g. A START or a STOP condition is detected and is not
28+
/// located after a multiple of 9 SCL clock pulses.
2629
Bus,
27-
/// Arbitration loss
28-
Arbitration,
29-
/// No ack received
30-
Acknowledge,
31-
/// Overrun/underrun
30+
/// The arbitration was lost, e.g. electrical problems with the clock signal
31+
ArbitrationLoss,
32+
/// A bus operation was not acknowledged, e.g. due to the addressed device not
33+
/// being available on the bus or the device not being ready to process requests
34+
/// at the moment
35+
NoAcknowledge(i2c::NoAcknowledgeSource),
36+
/// The peripheral receive buffer was overrun
3237
Overrun,
33-
/// Bus is busy
34-
Busy,
38+
/// Timeout
39+
Timeout,
3540
// Pec, // SMBUS mode only
3641
// Timeout, // SMBUS mode only
3742
// Alert, // SMBUS mode only
3843
}
3944

45+
impl i2c::Error for Error {
46+
fn kind(&self) -> i2c::ErrorKind {
47+
match *self {
48+
Error::Bus => i2c::ErrorKind::Bus,
49+
Error::ArbitrationLoss => i2c::ErrorKind::ArbitrationLoss,
50+
Error::NoAcknowledge(s) => i2c::ErrorKind::NoAcknowledge(s),
51+
Error::Overrun => i2c::ErrorKind::Overrun,
52+
_ => i2c::ErrorKind::Other,
53+
}
54+
}
55+
}
56+
4057
/// SPI mode. The user should make sure that the requested frequency can be
4158
/// generated considering the buses clocks.
4259
#[derive(Debug, PartialEq)]
@@ -354,20 +371,22 @@ macro_rules! check_status_flag {
354371

355372
if isr.berr().bit_is_set() {
356373
$i2c.icr.write(|w| w.berrcf().set_bit());
357-
Err(Other(Error::Bus))
374+
Err(nb::Error::Other(Error::Bus))
358375
} else if isr.arlo().bit_is_set() {
359376
$i2c.icr.write(|w| w.arlocf().set_bit());
360-
Err(Other(Error::Arbitration))
377+
Err(nb::Error::Other(Error::ArbitrationLoss))
361378
} else if isr.nackf().bit_is_set() {
362379
$i2c.icr.write(|w| w.stopcf().set_bit().nackcf().set_bit());
363-
Err(Other(Error::Acknowledge))
380+
Err(nb::Error::Other(Error::NoAcknowledge(
381+
i2c::NoAcknowledgeSource::Unknown,
382+
)))
364383
} else if isr.ovr().bit_is_set() {
365384
$i2c.icr.write(|w| w.stopcf().set_bit().ovrcf().set_bit());
366-
Err(Other(Error::Overrun))
385+
Err(nb::Error::Other(Error::Overrun))
367386
} else if isr.$flag().$status() {
368387
Ok(())
369388
} else {
370-
Err(WouldBlock)
389+
Err(nb::Error::WouldBlock)
371390
}
372391
}};
373392
}
@@ -376,7 +395,7 @@ macro_rules! busy_wait {
376395
($nb_expr:expr, $exit_cond:expr) => {{
377396
loop {
378397
let res = $nb_expr;
379-
if res != Err(WouldBlock) {
398+
if res != Err(nb::Error::WouldBlock) {
380399
break res;
381400
}
382401
if $exit_cond {
@@ -397,6 +416,17 @@ macro_rules! busy_wait_cycles {
397416
}};
398417
}
399418

419+
// Map non-blocking errors to blocking errors
420+
macro_rules! nbError_to_Error {
421+
($nb_expr:expr) => {{
422+
match $nb_expr {
423+
Ok(()) => {}
424+
Err(nb::Error::WouldBlock) => return Err(Error::Timeout),
425+
Err(nb::Error::Other(error)) => return Err(error),
426+
};
427+
}};
428+
}
429+
400430
// Generate the same code for both I2Cs
401431
macro_rules! hal {
402432
($($I2CX:ident: ($i2cX:ident),)+) => {
@@ -531,25 +561,19 @@ macro_rules! hal {
531561

532562
/// Wait for a byte to be read and return it (ie for RXNE flag
533563
/// to be set)
534-
fn wait_byte_read(&self) -> NbResult<u8, Error> {
564+
fn wait_byte_read(&self) -> Result<u8, Error> {
535565
// Wait until we have received something
536-
busy_wait_cycles!(
537-
check_status_flag!(self.nb.i2c, rxne, is_not_empty),
538-
self.data_timeout
539-
)?;
566+
nbError_to_Error!(busy_wait_cycles!(check_status_flag!(self.nb.i2c, rxne, is_not_empty), self.data_timeout));
540567

541568
Ok(self.nb.i2c.rxdr.read().rxdata().bits())
542569
}
543570

544571
/// Wait the write data register to be empty (ie for TXIS flag
545572
/// to be set) and write the byte to it
546-
fn wait_byte_write(&self, byte: u8) -> NbResult<(), Error> {
573+
fn wait_byte_write(&self, byte: u8) -> Result<(), Error> {
547574
// Wait until we are allowed to send data
548575
// (START has been ACKed or last byte when through)
549-
busy_wait_cycles!(
550-
check_status_flag!(self.nb.i2c, txis, is_empty),
551-
self.data_timeout
552-
)?;
576+
nbError_to_Error!(busy_wait_cycles!(check_status_flag!(self.nb.i2c, txis, is_empty), self.data_timeout));
553577

554578
// Put byte on the wire
555579
self.nb.i2c.txdr.write(|w| w.txdata().bits(byte));
@@ -564,7 +588,7 @@ macro_rules! hal {
564588
}
565589

566590
impl<SCL, SDA> Write for BlockingI2c<$I2CX, SCL, SDA> {
567-
type Error = NbError<Error>;
591+
type Error = Error;
568592

569593
/// Write bytes to I2C. Currently, `bytes.len()` must be less or
570594
/// equal than 255
@@ -592,7 +616,7 @@ macro_rules! hal {
592616
}
593617

594618
impl<SCL, SDA> Read for BlockingI2c<$I2CX, SCL, SDA> {
595-
type Error = NbError<Error>;
619+
type Error = Error;
596620

597621
/// Reads enough bytes from slave with `address` to fill `buffer`
598622
fn read(&mut self, addr: u8, buffer: &mut [u8]) -> Result<(), Self::Error> {
@@ -620,7 +644,7 @@ macro_rules! hal {
620644
}
621645

622646
impl<SCL, SDA> WriteRead for BlockingI2c<$I2CX, SCL, SDA> {
623-
type Error = NbError<Error>;
647+
type Error = Error;
624648

625649
fn write_read(
626650
&mut self,
@@ -642,10 +666,7 @@ macro_rules! hal {
642666

643667
// Wait until the write finishes before beginning to read.
644668
// busy_wait2!(self.nb.i2c, tc, is_complete);
645-
busy_wait_cycles!(
646-
check_status_flag!(self.nb.i2c, tc, is_complete),
647-
self.data_timeout
648-
)?;
669+
nbError_to_Error!(busy_wait_cycles!(check_status_flag!(self.nb.i2c, tc, is_complete), self.data_timeout));
649670

650671
// reSTART and prepare to receive bytes into `buffer`
651672
self.nb.start(addr, buffer.len() as u8, true, true);

0 commit comments

Comments
 (0)