Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial support for the Rust Embedded embedded-hal #540

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ version = "0.1.0"
# more information.
rust-version = "1.74"

[features]
rust_embedded = [
"embedded-hal",
"libtock_platform/rust_embedded",
"libtock_gpio/rust_embedded"
]

[dependencies]
libtock_adc = { path = "apis/adc" }
libtock_air_quality = { path = "apis/air_quality" }
Expand All @@ -38,6 +45,8 @@ libtock_runtime = { path = "runtime" }
libtock_sound_pressure = { path = "apis/sound_pressure" }
libtock_temperature = { path = "apis/temperature" }

embedded-hal = { version = "1.0", optional = true }

[build-dependencies]
libtock_build_scripts = { path = "build_scripts" }

Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ This script does the following steps for you:
- create a TAB (tock application bundle)
- if you have a J-Link compatible board connected: flash this TAB to your board (using tockloader)

### Enabling rust-embedded support

libtock-rs can be built to be compatible with the rust-embedded
[embedded_hal](https://docs.rs/embedded-hal/1.0.0/embedded_hal/index.html) by
including the following when running `make`
alistair23 marked this conversation as resolved.
Show resolved Hide resolved

```shell
FEATURES=rust_embedded
```
alistair23 marked this conversation as resolved.
Show resolved Hide resolved

If using libtock-rs or a sub-crate as a cargo dependency the `rust_embedded`
can also be enabled via Cargo.

### Building a generic TAB (Tock Application Bundle) file

To build your example for a variety of boards you can use
Expand Down
4 changes: 4 additions & 0 deletions apis/gpio/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ repository = "https://www.github.com/tock/libtock-rs"
rust-version.workspace = true
description = "libtock gpio driver"

[features]
rust_embedded = ["embedded-hal"]

[dependencies]
libtock_platform = { path = "../../platform" }
embedded-hal = { version = "1.0", optional = true }

[dev-dependencies]
libtock_unittest = { path = "../../unittest" }
16 changes: 16 additions & 0 deletions apis/gpio/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,22 @@ impl<S: Syscalls> Gpio<S> {
}
}

#[cfg(feature = "rust_embedded")]
impl<'a, S: Syscalls> embedded_hal::digital::ErrorType for OutputPin<'a, S> {
type Error = ErrorCode;
}

#[cfg(feature = "rust_embedded")]
impl<'a, S: Syscalls> embedded_hal::digital::OutputPin for OutputPin<'a, S> {
fn set_low(&mut self) -> Result<(), Self::Error> {
self.clear()
}

fn set_high(&mut self) -> Result<(), Self::Error> {
self.set()
}
}

#[cfg(test)]
mod tests;

Expand Down
6 changes: 6 additions & 0 deletions platform/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ name = "libtock_platform"
repository = "https://www.github.com/tock/libtock/rs"
rust-version.workspace = true
version = "0.1.0"

[features]
rust_embedded = ["embedded-hal"]

[dependencies]
embedded-hal = { version = "1.0", optional = true }
8 changes: 8 additions & 0 deletions platform/src/error_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,11 @@ impl TryFrom<u32> for ErrorCode {
}
}
}

#[cfg(feature = "rust_embedded")]
impl embedded_hal::digital::Error for ErrorCode {
fn kind(&self) -> embedded_hal::digital::ErrorKind {
use embedded_hal::digital::ErrorKind;
ErrorKind::Other
}
}
Loading