Skip to content

Commit c9221f1

Browse files
committed
embedded-hal v1
1 parent 0ec64ba commit c9221f1

File tree

16 files changed

+810
-888
lines changed

16 files changed

+810
-888
lines changed

e310x-hal/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ edition = "2021"
1111
rust-version = "1.76"
1212

1313
[dependencies]
14-
embedded-hal = { version = "0.2.6", features = ["unproven"] }
14+
embedded-hal = { version = "1.0.0" }
15+
embedded-hal-nb = { version = "1.0.0" }
1516
nb = "1.0.0"
1617
riscv = { version = "0.12.1", features = ["critical-section-single-hart"] }
1718
e310x = { path = "../e310x", version = "0.12.0", features = ["rt", "critical-section"] }

e310x-hal/src/delay.rs

Lines changed: 7 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::clock::Clocks;
44
use e310x::CLINT;
5-
use embedded_hal::blocking::delay::{DelayMs, DelayUs};
5+
use embedded_hal::delay::DelayNs;
66
use riscv::register::mip;
77

88
/// Machine timer (mtime) as a busyloop delay provider
@@ -18,68 +18,16 @@ impl Delay {
1818
}
1919
}
2020

21-
impl DelayUs<u32> for Delay {
22-
fn delay_us(&mut self, us: u32) {
23-
let ticks = (us as u64) * TICKS_PER_SECOND / 1_000_000;
21+
impl DelayNs for Delay {
22+
fn delay_ns(&mut self, ns: u32) {
23+
let ticks = (ns as u64) * TICKS_PER_SECOND / 1_000_000_000;
2424

2525
let mtime = CLINT::mtimer().mtime;
2626
let t = mtime.read() + ticks;
2727
while mtime.read() < t {}
2828
}
2929
}
3030

31-
// This is a workaround to allow `delay_us(42)` construction without specifying a type.
32-
impl DelayUs<i32> for Delay {
33-
#[inline(always)]
34-
fn delay_us(&mut self, us: i32) {
35-
assert!(us >= 0);
36-
self.delay_us(us as u32);
37-
}
38-
}
39-
40-
impl DelayUs<u16> for Delay {
41-
#[inline(always)]
42-
fn delay_us(&mut self, us: u16) {
43-
self.delay_us(u32::from(us));
44-
}
45-
}
46-
47-
impl DelayUs<u8> for Delay {
48-
#[inline(always)]
49-
fn delay_us(&mut self, us: u8) {
50-
self.delay_us(u32::from(us));
51-
}
52-
}
53-
54-
impl DelayMs<u32> for Delay {
55-
fn delay_ms(&mut self, ms: u32) {
56-
self.delay_us(ms * 1000);
57-
}
58-
}
59-
60-
// This is a workaround to allow `delay_ms(42)` construction without specifying a type.
61-
impl DelayMs<i32> for Delay {
62-
#[inline(always)]
63-
fn delay_ms(&mut self, ms: i32) {
64-
assert!(ms >= 0);
65-
self.delay_ms(ms as u32);
66-
}
67-
}
68-
69-
impl DelayMs<u16> for Delay {
70-
#[inline(always)]
71-
fn delay_ms(&mut self, ms: u16) {
72-
self.delay_ms(u32::from(ms));
73-
}
74-
}
75-
76-
impl DelayMs<u8> for Delay {
77-
#[inline(always)]
78-
fn delay_ms(&mut self, ms: u8) {
79-
self.delay_ms(u32::from(ms));
80-
}
81-
}
82-
8331
/// Machine timer (mtime) as a sleep delay provider using mtimecmp
8432
pub struct Sleep {
8533
clock_freq: u32,
@@ -94,9 +42,9 @@ impl Sleep {
9442
}
9543
}
9644

97-
impl DelayMs<u32> for Sleep {
98-
fn delay_ms(&mut self, ms: u32) {
99-
let ticks = (ms as u64) * (self.clock_freq as u64) / 1000;
45+
impl DelayNs for Sleep {
46+
fn delay_ns(&mut self, ns: u32) {
47+
let ticks = (ns as u64) * u64::from(self.clock_freq) / 1_000_000_000;
10048
let t = CLINT::mtimer().mtime.read() + ticks;
10149

10250
CLINT::mtimecmp0().write(t);
@@ -121,26 +69,3 @@ impl DelayMs<u32> for Sleep {
12169
CLINT::mtimer_disable();
12270
}
12371
}
124-
125-
// This is a workaround to allow `delay_ms(42)` construction without specifying a type.
126-
impl DelayMs<i32> for Sleep {
127-
#[inline(always)]
128-
fn delay_ms(&mut self, ms: i32) {
129-
assert!(ms >= 0);
130-
self.delay_ms(ms as u32);
131-
}
132-
}
133-
134-
impl DelayMs<u16> for Sleep {
135-
#[inline(always)]
136-
fn delay_ms(&mut self, ms: u16) {
137-
self.delay_ms(u32::from(ms));
138-
}
139-
}
140-
141-
impl DelayMs<u8> for Sleep {
142-
#[inline(always)]
143-
fn delay_ms(&mut self, ms: u8) {
144-
self.delay_ms(u32::from(ms));
145-
}
146-
}

e310x-hal/src/gpio.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ macro_rules! gpio {
143143
use core::marker::PhantomData;
144144
use core::convert::Infallible;
145145

146-
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin,
147-
ToggleableOutputPin};
146+
use embedded_hal::digital::{InputPin, OutputPin, StatefulOutputPin, ErrorType};
148147
use e310x::$GPIOX;
149148
use super::{Unknown, IOF0, IOF1, Drive, Floating, GpioExt, Input, Invert,
150149
NoInvert, Output, PullUp, Regular, PinIndex, PeripheralAccess};
@@ -195,7 +194,6 @@ macro_rules! gpio {
195194
$PXi { _mode: PhantomData }
196195
}
197196

198-
199197
/// Configures the pin to serve as alternate function 1 (AF1)
200198
pub fn into_iof1(self) -> $PXi<IOF1<NoInvert>> {
201199
$GPIOX::set_out_xor(Self::INDEX, false);
@@ -212,7 +210,6 @@ macro_rules! gpio {
212210
$PXi { _mode: PhantomData }
213211
}
214212

215-
216213
/// Configures the pin to serve as inverted alternate function 1 (AF1)
217214
pub fn into_inverted_iof1(self) -> $PXi<IOF1<Invert>> {
218215
$GPIOX::set_out_xor(Self::INDEX, true);
@@ -276,47 +273,52 @@ macro_rules! gpio {
276273
}
277274
}
278275

279-
impl<MODE> InputPin for $PXi<Input<MODE>> {
276+
impl<MODE> ErrorType for $PXi<Input<MODE>> {
280277
type Error = Infallible;
278+
}
281279

282-
fn is_high(&self) -> Result<bool, Infallible> {
283-
Ok($GPIOX::input_value(Self::INDEX))
284-
285-
}
286-
287-
fn is_low(&self) -> Result<bool, Infallible> {
288-
Ok(!self.is_high()?)
289-
}
280+
impl<MODE> ErrorType for $PXi<Output<MODE>> {
281+
type Error = Infallible;
290282
}
291283

292-
impl<MODE> StatefulOutputPin for $PXi<Output<MODE>> {
293-
fn is_set_high(&self) -> Result<bool, Infallible> {
284+
impl<MODE> InputPin for $PXi<Input<MODE>> {
285+
#[inline]
286+
fn is_high(&mut self) -> Result<bool, Self::Error> {
294287
Ok($GPIOX::input_value(Self::INDEX))
295288
}
296289

297-
fn is_set_low(&self) -> Result<bool, Infallible> {
298-
Ok(!self.is_set_high()?)
290+
#[inline]
291+
fn is_low(&mut self) -> Result<bool, Self::Error> {
292+
Ok(!self.is_high()?)
299293
}
300294
}
301295

302296
impl<MODE> OutputPin for $PXi<Output<MODE>> {
303-
type Error = Infallible;
304-
297+
#[inline]
305298
fn set_high(&mut self) -> Result<(), Infallible> {
306299
$GPIOX::set_output_value(Self::INDEX, true);
307300
Ok(())
308301
}
309302

303+
#[inline]
310304
fn set_low(&mut self) -> Result<(), Infallible> {
311305
$GPIOX::set_output_value(Self::INDEX, false);
312306
Ok(())
313307
}
314308
}
315309

316-
impl<MODE> ToggleableOutputPin for $PXi<Output<MODE>> {
317-
type Error = Infallible;
310+
impl<MODE> StatefulOutputPin for $PXi<Output<MODE>> {
311+
#[inline]
312+
fn is_set_high(&mut self) -> Result<bool, Infallible> {
313+
Ok($GPIOX::input_value(Self::INDEX))
314+
}
315+
316+
#[inline]
317+
fn is_set_low(&mut self) -> Result<bool, Infallible> {
318+
Ok(!self.is_set_high()?)
319+
}
318320

319-
/// Toggles the pin state.
321+
#[inline]
320322
fn toggle(&mut self) -> Result<(), Infallible> {
321323
$GPIOX::toggle_pin(Self::INDEX);
322324
Ok(())

0 commit comments

Comments
 (0)