Skip to content

Commit

Permalink
Merge pull request #540 from alistair23/alistair/rust_embedded
Browse files Browse the repository at this point in the history
Initial support for the Rust Embedded embedded-hal
  • Loading branch information
jrvanwhy authored May 28, 2024
2 parents 552ff2f + dcf5d94 commit 97a8bea
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
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`

```shell
FEATURES=rust_embedded
```

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
}
}

0 comments on commit 97a8bea

Please sign in to comment.