Skip to content

Commit dcf5d94

Browse files
committed
Initial support for the Rust Embedded embedded-hal
This is very initial support for the embedded_hal [1]. This allows general embedded Rust crates to be built on top of libtock-rs with minimal porting effort. This currently just stubs out the support and implements the `digital::OutputPin` trait for GPIO pins. 1: https://docs.rs/embedded-hal/1.0.0/embedded_hal/index.html Signed-off-by: Alistair Francis <[email protected]>
1 parent 552ff2f commit dcf5d94

File tree

6 files changed

+56
-0
lines changed

6 files changed

+56
-0
lines changed

Cargo.toml

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ version = "0.1.0"
1515
# more information.
1616
rust-version = "1.74"
1717

18+
[features]
19+
rust_embedded = [
20+
"embedded-hal",
21+
"libtock_platform/rust_embedded",
22+
"libtock_gpio/rust_embedded"
23+
]
24+
1825
[dependencies]
1926
libtock_adc = { path = "apis/adc" }
2027
libtock_air_quality = { path = "apis/air_quality" }
@@ -38,6 +45,8 @@ libtock_runtime = { path = "runtime" }
3845
libtock_sound_pressure = { path = "apis/sound_pressure" }
3946
libtock_temperature = { path = "apis/temperature" }
4047

48+
embedded-hal = { version = "1.0", optional = true }
49+
4150
[build-dependencies]
4251
libtock_build_scripts = { path = "build_scripts" }
4352

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ This script does the following steps for you:
6363
- create a TAB (tock application bundle)
6464
- if you have a J-Link compatible board connected: flash this TAB to your board (using tockloader)
6565

66+
### Enabling rust-embedded support
67+
68+
libtock-rs can be built to be compatible with the rust-embedded
69+
[embedded_hal](https://docs.rs/embedded-hal/1.0.0/embedded_hal/index.html) by
70+
including the following when running `make`
71+
72+
```shell
73+
FEATURES=rust_embedded
74+
```
75+
76+
If using libtock-rs or a sub-crate as a cargo dependency the `rust_embedded`
77+
can also be enabled via Cargo.
78+
6679
### Building a generic TAB (Tock Application Bundle) file
6780

6881
To build your example for a variety of boards you can use

apis/gpio/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ repository = "https://www.github.com/tock/libtock-rs"
88
rust-version.workspace = true
99
description = "libtock gpio driver"
1010

11+
[features]
12+
rust_embedded = ["embedded-hal"]
13+
1114
[dependencies]
1215
libtock_platform = { path = "../../platform" }
16+
embedded-hal = { version = "1.0", optional = true }
1317

1418
[dev-dependencies]
1519
libtock_unittest = { path = "../../unittest" }

apis/gpio/src/lib.rs

+16
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ impl<S: Syscalls> Gpio<S> {
231231
}
232232
}
233233

234+
#[cfg(feature = "rust_embedded")]
235+
impl<'a, S: Syscalls> embedded_hal::digital::ErrorType for OutputPin<'a, S> {
236+
type Error = ErrorCode;
237+
}
238+
239+
#[cfg(feature = "rust_embedded")]
240+
impl<'a, S: Syscalls> embedded_hal::digital::OutputPin for OutputPin<'a, S> {
241+
fn set_low(&mut self) -> Result<(), Self::Error> {
242+
self.clear()
243+
}
244+
245+
fn set_high(&mut self) -> Result<(), Self::Error> {
246+
self.set()
247+
}
248+
}
249+
234250
#[cfg(test)]
235251
mod tests;
236252

platform/Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ name = "libtock_platform"
1212
repository = "https://www.github.com/tock/libtock/rs"
1313
rust-version.workspace = true
1414
version = "0.1.0"
15+
16+
[features]
17+
rust_embedded = ["embedded-hal"]
18+
19+
[dependencies]
20+
embedded-hal = { version = "1.0", optional = true }

platform/src/error_code.rs

+8
Original file line numberDiff line numberDiff line change
@@ -283,3 +283,11 @@ impl TryFrom<u32> for ErrorCode {
283283
}
284284
}
285285
}
286+
287+
#[cfg(feature = "rust_embedded")]
288+
impl embedded_hal::digital::Error for ErrorCode {
289+
fn kind(&self) -> embedded_hal::digital::ErrorKind {
290+
use embedded_hal::digital::ErrorKind;
291+
ErrorKind::Other
292+
}
293+
}

0 commit comments

Comments
 (0)