Skip to content

Commit 7eb0c08

Browse files
committed
lib: update embedded-hal into 1.0.0
Signed-off-by: Zhouqi Jiang <[email protected]>
1 parent fb6c239 commit 7eb0c08

File tree

7 files changed

+98
-118
lines changed

7 files changed

+98
-118
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ edition = "2021"
1313
targets = ["riscv64gc-unknown-none-elf"]
1414

1515
[dependencies]
16-
embedded-hal = { version = "0.2.7", features = ["unproven"] }
16+
embedded-hal = "1.0.0"
17+
embedded-io = "0.6.1"
1718
nb = "1"
1819
k210-pac = "0.2.0"
1920
bitflags = "1.3"

src/gpio.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! General Purpose Input/Output (GPIO)
22
3-
use crate::bit_utils::{u32_bit_is_clear, u32_bit_is_set, u32_set_bit, u32_toggle_bit};
3+
use crate::bit_utils::{u32_bit_is_clear, u32_bit_is_set, u32_set_bit};
44
use crate::fpioa::{IoPin, Mode, Pull};
55
use crate::pac;
66
use crate::sysctl::{self, APB0};
77
use core::marker::PhantomData;
8-
use embedded_hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin, ToggleableOutputPin};
8+
use embedded_hal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin};
99

1010
/// Extension trait to split a GPIO peripheral into independent pins
1111
pub trait GpioExt {
@@ -186,17 +186,20 @@ impl<GPIO: GpioIndex, PIN: IoPin, MODE: Active> Gpio<GPIO, PIN, MODE> {
186186
}
187187
}
188188

189-
impl<GPIO: GpioIndex, PIN, MODE> InputPin for Gpio<GPIO, PIN, Input<MODE>> {
189+
impl<GPIO: GpioIndex, PIN, MODE> ErrorType for Gpio<GPIO, PIN, MODE> {
190+
// All GPIO operations are infallible.
190191
type Error = core::convert::Infallible;
192+
}
191193

192-
fn is_high(&self) -> Result<bool, Self::Error> {
194+
impl<GPIO: GpioIndex, PIN, MODE> InputPin for Gpio<GPIO, PIN, Input<MODE>> {
195+
fn is_high(&mut self) -> Result<bool, Self::Error> {
193196
Ok(unsafe {
194197
let p = &(*pac::GPIO::ptr()).data_input as *const _ as *const _;
195198
u32_bit_is_set(p, GPIO::INDEX as usize)
196199
})
197200
}
198201

199-
fn is_low(&self) -> Result<bool, Self::Error> {
202+
fn is_low(&mut self) -> Result<bool, Self::Error> {
200203
Ok(unsafe {
201204
let p = &(*pac::GPIO::ptr()).data_input as *const _ as *const _;
202205
u32_bit_is_clear(p, GPIO::INDEX as usize)
@@ -205,8 +208,6 @@ impl<GPIO: GpioIndex, PIN, MODE> InputPin for Gpio<GPIO, PIN, Input<MODE>> {
205208
}
206209

207210
impl<GPIO: GpioIndex, PIN> OutputPin for Gpio<GPIO, PIN, Output> {
208-
type Error = core::convert::Infallible;
209-
210211
fn set_high(&mut self) -> Result<(), Self::Error> {
211212
unsafe {
212213
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *mut _;
@@ -225,29 +226,17 @@ impl<GPIO: GpioIndex, PIN> OutputPin for Gpio<GPIO, PIN, Output> {
225226
}
226227

227228
impl<GPIO: GpioIndex, PIN> StatefulOutputPin for Gpio<GPIO, PIN, Output> {
228-
fn is_set_high(&self) -> Result<bool, Self::Error> {
229+
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
229230
Ok(unsafe {
230231
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *const _;
231232
u32_bit_is_set(p, GPIO::INDEX as usize)
232233
})
233234
}
234235

235-
fn is_set_low(&self) -> Result<bool, Self::Error> {
236+
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
236237
Ok(unsafe {
237238
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *const _;
238239
u32_bit_is_clear(p, GPIO::INDEX as usize)
239240
})
240241
}
241242
}
242-
243-
impl<GPIO: GpioIndex, PIN> ToggleableOutputPin for Gpio<GPIO, PIN, Output> {
244-
type Error = core::convert::Infallible;
245-
246-
fn toggle(&mut self) -> Result<(), Self::Error> {
247-
unsafe {
248-
let p = &(*pac::GPIO::ptr()).data_output as *const _ as *mut _;
249-
u32_toggle_bit(p, GPIO::INDEX as usize);
250-
}
251-
Ok(())
252-
}
253-
}

src/gpiohs.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! High-speed GPIO peripheral (GPIOHS)
22
3-
use crate::bit_utils::{u32_bit_is_clear, u32_bit_is_set, u32_set_bit, u32_toggle_bit};
3+
use crate::bit_utils::{u32_bit_is_clear, u32_bit_is_set, u32_set_bit};
44
use crate::pac::GPIOHS;
55
use core::marker::PhantomData;
6-
use embedded_hal::digital::v2::{InputPin, OutputPin};
6+
use embedded_hal::digital::{ErrorType, InputPin, OutputPin};
77

88
// todo: verify
99

@@ -114,17 +114,20 @@ impl<MODE> Gpiohs0<MODE> {
114114
}
115115
}
116116

117-
impl<MODE> InputPin for Gpiohs0<Input<MODE>> {
117+
impl<MODE> ErrorType for Gpiohs0<MODE> {
118+
// All GPIO operations are infallible.
118119
type Error = core::convert::Infallible;
120+
}
119121

120-
fn is_high(&self) -> Result<bool, Self::Error> {
122+
impl<MODE> InputPin for Gpiohs0<Input<MODE>> {
123+
fn is_high(&mut self) -> Result<bool, Self::Error> {
121124
Ok(unsafe {
122125
let p = &(*GPIOHS::ptr()).input_val as *const _ as *const _;
123126
u32_bit_is_set(p, 0)
124127
})
125128
}
126129

127-
fn is_low(&self) -> Result<bool, Self::Error> {
130+
fn is_low(&mut self) -> Result<bool, Self::Error> {
128131
Ok(unsafe {
129132
let p = &(*GPIOHS::ptr()).input_val as *const _ as *const _;
130133
u32_bit_is_clear(p, 0)
@@ -133,8 +136,6 @@ impl<MODE> InputPin for Gpiohs0<Input<MODE>> {
133136
}
134137

135138
impl<MODE> OutputPin for Gpiohs0<Output<MODE>> {
136-
type Error = core::convert::Infallible;
137-
138139
fn set_high(&mut self) -> Result<(), Self::Error> {
139140
unsafe {
140141
let p = &(*GPIOHS::ptr()).output_val as *const _ as *mut _;
@@ -211,13 +212,6 @@ trait GpiohsAccess {
211212
}
212213
}
213214

214-
fn toggle_pin(index: usize) {
215-
unsafe {
216-
let p = &mut Self::peripheral().output_val as *mut _ as *mut _;
217-
u32_toggle_bit(p, index);
218-
}
219-
}
220-
221215
fn set_pullup_en(index: usize, bit: bool) {
222216
unsafe {
223217
let p = &mut Self::peripheral().pullup_en as *mut _ as *mut _;

src/lib.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ pub mod prelude {
3535
pub use crate::stdout::Write as _k210_hal_stdout_Write;
3636
pub use crate::sysctl::SysctlExt as _k210_hal_sysctl_SysctlExt;
3737
pub use crate::time::U32Ext as _k210_hal_time_U32Ext;
38-
pub use embedded_hal::prelude::*;
3938
}
4039

4140
mod bit_utils {
@@ -49,12 +48,6 @@ mod bit_utils {
4948
}
5049
}
5150

52-
#[inline(always)]
53-
pub(crate) unsafe fn u32_toggle_bit(p: *mut u32, index: usize) {
54-
let mask = 1 << index;
55-
*p ^= mask;
56-
}
57-
5851
#[inline(always)]
5952
pub(crate) unsafe fn u32_bit_is_set(r: *const u32, index: usize) -> bool {
6053
(*r & 1 << index) != 0

src/serial.rs

Lines changed: 54 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
use core::convert::Infallible;
1010
use core::mem;
1111

12-
use embedded_hal::serial;
13-
1412
use crate::clock::Clocks;
1513
use crate::pac::{uart1, UART1, UART2, UART3, UARTHS};
1614
use crate::time::Bps;
@@ -98,44 +96,48 @@ impl Serial<UARTHS> {
9896
}
9997
}
10098

101-
impl serial::Read<u8> for Rx<UARTHS> {
102-
type Error = Infallible;
103-
104-
fn read(&mut self) -> nb::Result<u8, Infallible> {
105-
let rxdata = self.uart.rxdata.read();
99+
impl embedded_io::ErrorType for Rx<UARTHS> {
100+
type Error = core::convert::Infallible;
101+
}
106102

107-
if rxdata.empty().bit_is_set() {
108-
Err(nb::Error::WouldBlock)
109-
} else {
110-
Ok(rxdata.data().bits() as u8)
103+
impl embedded_io::Read for Rx<UARTHS> {
104+
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Infallible> {
105+
while self.uart.rxdata.read().empty().bit_is_set() {
106+
// Block until rxdata available.
107+
core::hint::spin_loop()
108+
}
109+
let len = buf.len();
110+
for slot in buf {
111+
*slot = self.uart.rxdata.read().data().bits();
111112
}
113+
Ok(len)
112114
}
113115
}
114116

115-
impl serial::Write<u8> for Tx<UARTHS> {
116-
type Error = Infallible;
117-
118-
fn write(&mut self, byte: u8) -> nb::Result<(), Infallible> {
119-
let txdata = self.uart.txdata.read();
117+
impl embedded_io::ErrorType for Tx<UARTHS> {
118+
type Error = core::convert::Infallible;
119+
}
120120

121-
if txdata.full().bit_is_set() {
122-
Err(nb::Error::WouldBlock)
123-
} else {
121+
impl embedded_io::Write for Tx<UARTHS> {
122+
fn write(&mut self, bytes: &[u8]) -> Result<usize, Infallible> {
123+
while self.uart.txdata.read().full().bit_is_set() {
124+
// Block until txdata available.
125+
core::hint::spin_loop()
126+
}
127+
for byte in bytes {
124128
unsafe {
125-
(*UARTHS::ptr()).txdata.write(|w| w.data().bits(byte));
129+
self.uart.txdata.write(|w| w.data().bits(*byte));
126130
}
127-
Ok(())
128131
}
132+
Ok(bytes.len())
129133
}
130134

131-
fn flush(&mut self) -> nb::Result<(), Infallible> {
132-
let txdata = self.uart.txdata.read();
133-
134-
if txdata.full().bit_is_set() {
135-
Err(nb::Error::WouldBlock)
136-
} else {
137-
Ok(())
135+
fn flush(&mut self) -> Result<(), Infallible> {
136+
while self.uart.txdata.read().full().bit_is_set() {
137+
// Block until flush complete. If you don't want a block, use embedded_io_async traits instead.
138+
core::hint::spin_loop()
138139
}
140+
Ok(())
139141
}
140142
}
141143

@@ -207,40 +209,43 @@ impl<UART: UartX> Serial<UART> {
207209
}
208210
}
209211

210-
impl<UART: UartX> serial::Read<u8> for Rx<UART> {
211-
type Error = Infallible;
212-
213-
fn read(&mut self) -> nb::Result<u8, Infallible> {
214-
let lsr = self.uart.lsr.read();
212+
impl<UART: UartX> embedded_io::ErrorType for Rx<UART> {
213+
type Error = core::convert::Infallible;
214+
}
215215

216-
if (lsr.bits() & (1 << 0)) == 0 {
216+
impl<UART: UartX> embedded_io::Read for Rx<UART> {
217+
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Infallible> {
218+
while (self.uart.lsr.read().bits() & (1 << 0)) == 0 {
217219
// Data Ready bit
218-
Err(nb::Error::WouldBlock)
219-
} else {
220-
let rbr = self.uart.rbr_dll_thr.read();
221-
Ok((rbr.bits() & 0xff) as u8)
220+
core::hint::spin_loop()
221+
}
222+
let len = buf.len();
223+
for slot in buf {
224+
*slot = (self.uart.rbr_dll_thr.read().bits() & 0xff) as u8;
222225
}
226+
Ok(len)
223227
}
224228
}
225229

226-
impl<UART: UartX> serial::Write<u8> for Tx<UART> {
227-
type Error = Infallible;
228-
229-
fn write(&mut self, byte: u8) -> nb::Result<(), Infallible> {
230-
let lsr = self.uart.lsr.read();
230+
impl<UART: UartX> embedded_io::ErrorType for Tx<UART> {
231+
type Error = core::convert::Infallible;
232+
}
231233

232-
if (lsr.bits() & (1 << 5)) != 0 {
234+
impl<UART: UartX> embedded_io::Write for Tx<UART> {
235+
fn write(&mut self, bytes: &[u8]) -> Result<usize, Infallible> {
236+
while (self.uart.lsr.read().bits() & (1 << 5)) != 0 {
233237
// Transmit Holding Register Empty bit
234-
Err(nb::Error::WouldBlock)
235-
} else {
238+
core::hint::spin_loop();
239+
}
240+
for byte in bytes {
236241
unsafe {
237-
self.uart.rbr_dll_thr.write(|w| w.bits(byte.into()));
242+
self.uart.rbr_dll_thr.write(|w| w.bits(*byte as u32));
238243
}
239-
Ok(())
240244
}
245+
Ok(bytes.len())
241246
}
242247

243-
fn flush(&mut self) -> nb::Result<(), Infallible> {
248+
fn flush(&mut self) -> Result<(), Infallible> {
244249
// TODO
245250
Ok(())
246251
}

src/spi.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use crate::clock::Clocks;
44
use crate::pac::SPI0;
55
use crate::sysctl::{self, APB0};
6-
use core::convert::Infallible;
76
pub use embedded_hal::spi::{Mode, Phase, Polarity};
87

98
///
@@ -64,21 +63,33 @@ impl Spi<SPI0> {
6463
}
6564
}
6665

67-
impl embedded_hal::spi::FullDuplex<u8> for Spi<SPI0> {
68-
/// An enumeration of SPI errors
69-
type Error = Infallible;
66+
impl embedded_hal::spi::ErrorType for Spi<SPI0> {
67+
type Error = core::convert::Infallible;
68+
}
7069

70+
impl embedded_hal::spi::SpiBus<u8> for Spi<SPI0> {
7171
/// Reads the word stored in the shift register
7272
///
7373
/// **NOTE** A word must be sent to the slave before attempting to call this
7474
/// method.
75-
fn read(&mut self) -> nb::Result<u8, Self::Error> {
75+
fn read(&mut self, _words: &mut [u8]) -> Result<(), Self::Error> {
76+
todo!()
77+
}
78+
79+
fn write(&mut self, _words: &[u8]) -> Result<(), Self::Error> {
7680
todo!()
7781
}
7882

79-
/// Sends a word to the slave
80-
fn send(&mut self, word: u8) -> nb::Result<(), Self::Error> {
81-
todo!("{}", word)
83+
fn transfer(&mut self, _read: &mut [u8], _write: &[u8]) -> Result<(), Self::Error> {
84+
todo!()
85+
}
86+
87+
fn transfer_in_place(&mut self, _words: &mut [u8]) -> Result<(), Self::Error> {
88+
todo!()
89+
}
90+
91+
fn flush(&mut self) -> Result<(), Self::Error> {
92+
todo!()
8293
}
8394
}
8495

0 commit comments

Comments
 (0)