Skip to content

Commit dd14acc

Browse files
committed
Update to embedded-hal v1.0.0-alpha.1
1 parent eaefada commit dd14acc

18 files changed

+333
-237
lines changed

Cargo.toml

+5-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,11 @@ default-features = false
4848
version = "1.0.2"
4949

5050
[dependencies.embedded-hal]
51-
features = ["unproven"]
52-
version = "0.2.3"
51+
# TODO: Update version on next release.
52+
# version = "0.2.3"
53+
version = "=1.0.0-alpha.1"
54+
# git = "https://github.com/rust-embedded/embedded-hal"
55+
# branch = "v1.0.0-alpha.1"
5356

5457
[dev-dependencies]
5558
panic-semihosting = "0.5.3"

examples/delay-blinky.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ fn main() -> ! {
3131

3232
loop {
3333
// On for 1s, off for 1s.
34-
led.set_high().unwrap();
35-
delay.delay_ms(1000_u32);
36-
led.set_low().unwrap();
37-
delay.delay_ms(1000_u32);
34+
led.try_set_high().unwrap();
35+
delay.try_delay_ms(1000_u32).unwrap();
36+
led.try_set_low().unwrap();
37+
delay.try_delay_ms(1000_u32).unwrap();
3838
}
3939
}
4040

examples/pwm.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ fn main() -> ! {
2424

2525
let pwm = pwm::tim1(dp.TIM1, channels, clocks, 20u32.khz());
2626
let (mut ch1, _ch2) = pwm;
27-
let max_duty = ch1.get_max_duty();
28-
ch1.set_duty(max_duty / 2);
29-
ch1.enable();
27+
let max_duty = ch1.try_get_max_duty().unwrap();
28+
ch1.try_set_duty(max_duty / 2);
29+
ch1.try_enable();
3030
}
3131

3232
loop {

examples/rng-display.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn main() -> ! {
9999
}
100100
disp.flush().unwrap();
101101
//delay a little while between refreshes so the display is readable
102-
delay_source.delay_ms(100u8);
102+
delay_source.try_delay_ms(100u8).unwrap();
103103
}
104104
}
105105

examples/timer-syst.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//! Start and stop a periodic system timer.
1+
//! Start and stop a periodic system timer.try_
22
//!
33
//! This example should run on all stm32f4xx boards but it was tested with
44
//! stm32f4-discovery board (model STM32F407G-DISC1).
@@ -36,29 +36,29 @@ fn main() -> ! {
3636

3737
hprintln!("hello!").unwrap();
3838
// wait until timer expires
39-
nb::block!(timer.wait()).unwrap();
39+
nb::block!(timer.try_wait()).unwrap();
4040
hprintln!("timer expired 1").unwrap();
4141

4242
// the function syst() creates a periodic timer, so it is automatically
4343
// restarted
44-
nb::block!(timer.wait()).unwrap();
44+
nb::block!(timer.try_wait()).unwrap();
4545
hprintln!("timer expired 2").unwrap();
4646

4747
// cancel current timer
48-
timer.cancel().unwrap();
48+
timer.try_cancel().unwrap();
4949

5050
// start it again
51-
timer.start(24.hz());
52-
nb::block!(timer.wait()).unwrap();
51+
timer.try_start(24.hz());
52+
nb::block!(timer.try_wait()).unwrap();
5353
hprintln!("timer expired 3").unwrap();
5454

55-
timer.cancel().unwrap();
56-
let cancel_outcome = timer.cancel();
55+
timer.try_cancel().unwrap();
56+
let cancel_outcome = timer.try_cancel();
5757
assert_eq!(cancel_outcome, Err(timer::Error::Disabled));
5858
hprintln!("ehy, you cannot cancel a timer two times!").unwrap();
5959
// this time the timer was not restarted, therefore this function should
6060
// wait forever
61-
nb::block!(timer.wait()).unwrap();
61+
nb::block!(timer.try_wait()).unwrap();
6262
// you should never see this print
6363
hprintln!("if you see this there is something wrong").unwrap();
6464
panic!();

src/adc.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ macro_rules! adc_pins {
2626
$(
2727
impl Channel<stm32::$adc> for $pin {
2828
type ID = u8;
29-
fn channel() -> u8 { $chan }
29+
const CHANNEL: Self::ID = $chan;
3030
}
3131
)+
3232
};
@@ -665,7 +665,7 @@ macro_rules! adc {
665665
}
666666

667667
let vref_cal = VrefCal::get().read();
668-
let vref_samp = self.read(&mut Vref).unwrap(); //This can't actually fail, it's just in a result to satisfy hal trait
668+
let vref_samp = self.try_read(&mut Vref).unwrap(); //This can't actually fail, it's just in a result to satisfy hal trait
669669

670670
self.calibrated_vdda = (VDDA_CALIB * u32::from(vref_cal)) / u32::from(vref_samp);
671671
if !vref_en {
@@ -873,7 +873,7 @@ macro_rules! adc {
873873
}
874874
});
875875

876-
let channel = CHANNEL::channel();
876+
let channel = CHANNEL::CHANNEL;
877877

878878
//Set the channel in the right sequence field
879879
match sequence {
@@ -976,7 +976,7 @@ macro_rules! adc {
976976
{
977977
type Error = ();
978978

979-
fn read(&mut self, pin: &mut PIN) -> nb::Result<u16, Self::Error> {
979+
fn try_read(&mut self, pin: &mut PIN) -> nb::Result<u16, Self::Error> {
980980
let enabled = self.is_enabled();
981981
if !enabled {
982982
self.enable();

src/delay.rs

+27-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Delays
22
33
use cast::u32;
4+
use core::convert::Infallible;
5+
46
use cortex_m::peripheral::syst::SystClkSource;
57
use cortex_m::peripheral::SYST;
68

@@ -28,25 +30,33 @@ impl Delay {
2830
}
2931

3032
impl DelayMs<u32> for Delay {
31-
fn delay_ms(&mut self, ms: u32) {
32-
self.delay_us(ms * 1_000);
33+
type Error = Infallible;
34+
35+
fn try_delay_ms(&mut self, ms: u32) -> Result<(), Self::Error> {
36+
self.try_delay_us(ms * 1_000)
3337
}
3438
}
3539

3640
impl DelayMs<u16> for Delay {
37-
fn delay_ms(&mut self, ms: u16) {
38-
self.delay_ms(u32(ms));
41+
type Error = Infallible;
42+
43+
fn try_delay_ms(&mut self, ms: u16) -> Result<(), Self::Error> {
44+
self.try_delay_ms(u32(ms))
3945
}
4046
}
4147

4248
impl DelayMs<u8> for Delay {
43-
fn delay_ms(&mut self, ms: u8) {
44-
self.delay_ms(u32(ms));
49+
type Error = Infallible;
50+
51+
fn try_delay_ms(&mut self, ms: u8) -> Result<(), Self::Error> {
52+
self.try_delay_ms(u32(ms))
4553
}
4654
}
4755

4856
impl DelayUs<u32> for Delay {
49-
fn delay_us(&mut self, us: u32) {
57+
type Error = Infallible;
58+
59+
fn try_delay_us(&mut self, us: u32) -> Result<(), Self::Error> {
5060
// The SysTick Reload Value register supports values between 1 and 0x00FFFFFF.
5161
const MAX_RVR: u32 = 0x00FF_FFFF;
5262

@@ -70,17 +80,23 @@ impl DelayUs<u32> for Delay {
7080

7181
self.syst.disable_counter();
7282
}
83+
84+
Ok(())
7385
}
7486
}
7587

7688
impl DelayUs<u16> for Delay {
77-
fn delay_us(&mut self, us: u16) {
78-
self.delay_us(u32(us))
89+
type Error = Infallible;
90+
91+
fn try_delay_us(&mut self, us: u16) -> Result<(), Self::Error> {
92+
self.try_delay_us(u32(us))
7993
}
8094
}
8195

8296
impl DelayUs<u8> for Delay {
83-
fn delay_us(&mut self, us: u8) {
84-
self.delay_us(u32(us))
97+
type Error = Infallible;
98+
99+
fn try_delay_us(&mut self, us: u8) -> Result<(), Self::Error> {
100+
self.try_delay_us(u32(us))
85101
}
86102
}

src/dwt.rs

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
//! Debug and trace and stuff
22
3+
use core::convert::Infallible;
4+
35
use crate::rcc::Clocks;
46
use crate::time::Hertz;
57
use cortex_m::peripheral::{DCB, DWT};
8+
69
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
710

811
pub trait DwtExt {
@@ -65,13 +68,13 @@ pub struct Delay {
6568
}
6669
impl Delay {
6770
/// Delay for `ClockDuration::ticks`
68-
pub fn delay(duration: ClockDuration) {
71+
pub fn try_delay(duration: ClockDuration) -> Result<(), Infallible> {
6972
let ticks = duration.ticks as u64;
70-
Delay::delay_ticks(DWT::get_cycle_count(), ticks);
73+
Delay::try_delay_ticks(DWT::get_cycle_count(), ticks)
7174
}
7275
/// Delay ticks
7376
/// NOTE DCB and DWT need to be set up for this to work, so it is private
74-
fn delay_ticks(mut start: u32, ticks: u64) {
77+
fn try_delay_ticks(mut start: u32, ticks: u64) -> Result<(), Infallible> {
7578
if ticks < (core::u32::MAX / 2) as u64 {
7679
// Simple delay
7780
let ticks = ticks as u32;
@@ -96,24 +99,30 @@ impl Delay {
9699
while (DWT::get_cycle_count().wrapping_sub(start)) > ticks {}
97100
}
98101
}
102+
103+
Ok(())
99104
}
100105
}
101106

102107
// Implement DelayUs/DelayMs for various integer types
103108
impl<T: Into<u64>> DelayUs<T> for Delay {
104-
fn delay_us(&mut self, us: T) {
109+
type Error = Infallible;
110+
111+
fn try_delay_us(&mut self, us: T) -> Result<(), Self::Error> {
105112
// Convert us to ticks
106113
let start = DWT::get_cycle_count();
107114
let ticks = (us.into() * self.clock.0 as u64) / 1_000_000;
108-
Delay::delay_ticks(start, ticks);
115+
Delay::try_delay_ticks(start, ticks)
109116
}
110117
}
111118
impl<T: Into<u64>> DelayMs<T> for Delay {
112-
fn delay_ms(&mut self, ms: T) {
119+
type Error = Infallible;
120+
121+
fn try_delay_ms(&mut self, ms: T) -> Result<(), Self::Error> {
113122
// Convert ms to ticks
114123
let start = DWT::get_cycle_count();
115124
let ticks = (ms.into() * self.clock.0 as u64) / 1_000;
116-
Delay::delay_ticks(start, ticks);
125+
Delay::try_delay_ticks(start, ticks)
117126
}
118127
}
119128

0 commit comments

Comments
 (0)