embassy-rs / nrf-softdevice

Apache License 2.0
269 stars 80 forks source link

Including in a separate Cargo project #168

Open olalonde opened 1 year ago

olalonde commented 1 year ago

It would be helpful to have some documentation on how to use this from a separate project. I tried to move out the examples/ but it doesn't build...

$ cargo run --bin ble_bas_peripheral
    Updating crates.io index
error: failed to get `nrf-softdevice` as a dependency of package `nrf-softdevice-examples v0.1.0 (/Users/redacted/code/nordic/myapp)`

Caused by:
  failed to load source for dependency `nrf-softdevice`

Caused by:
  Unable to update /Users/redacted/code/nordic/nrf-softdevice

Caused by:
  found a virtual manifest at `/Users/redacted/code/nordic/nrf-softdevice/Cargo.toml` instead of a package manifest

I tried removing path = "../nrf-softdevice" and path = "../nrf-softdevice-s140" from Cargo.toml but I have more errors:

$ cargo run --bin ble_bas_peripheral
    Updating crates.io index
error: failed to select a version for the requirement `embassy-nrf = "^0.1.0"`
candidate versions found which didn't match: 0.0.0
location searched: crates.io index
required by package `nrf-softdevice-examples v0.1.0 (/Users/redacted/code/nordic/myapp)`

Help appreciated.

olalonde commented 1 year ago

I modifed the Cargo.toml a bit:

[package]
authors = ["Dario Nieuwenhuis <dirbaio@dirbaio.net>"]
edition = "2021"
name = "nrf-softdevice-examples"
version = "0.1.0"

[features]
default = [
    "ble-l2cap",
    "ble-gatt-server",
    "ble-gatt-client",
    "ble-sec",
]

ble-l2cap = ["nrf-softdevice/ble-l2cap"]
ble-gatt-server = ["nrf-softdevice/ble-gatt-server"]
ble-gatt-client = ["nrf-softdevice/ble-gatt-client"]
ble-sec = ["nrf-softdevice/ble-sec"]

[dependencies]
embassy-executor = { version = "0.1.1", features = ["nightly", "defmt", "integrated-timers"]}
embassy-time = { version = "0.1.0", features = ["nightly", "defmt", "defmt-timestamp-uptime"]}
embassy-sync = { version = "0.1.0" }
embassy-nrf = { version = "0.1.0", features = [ "nightly", "defmt", "nrf52840", "gpiote", "time-driver-rtc1" ]}
cortex-m = "0.7.2"
cortex-m-rt = "0.7.0"
defmt = "0.3"
defmt-rtt = "0.4.0"
panic-probe = { version = "0.3", features= ["print-defmt"] }
nrf-softdevice = { version = "0.1.0", features = ["defmt", "nrf52840", "s140", "ble-peripheral", "ble-central", "critical-section-impl"] }
nrf-softdevice-s140 = { version = "0.1.1" }
embedded-storage = "0.3.0"
embedded-storage-async = "0.3.0"
futures = { version = "0.3.5", default-features = false }
fixed = "1.2.0"
heapless = "0.7.1"
atomic-pool = "1.0.0"
static_cell = "1.0.0"

[patch.crates-io]
embassy-nrf = { git = "https://github.com/embassy-rs/embassy", tag = "embassy-executor-v0.1.1" }
nrf-softdevice = { git = "https://github.com/embassy-rs/nrf-softdevice" }

[patch."https://github.com/embassy-rs/embassy"]
embassy-executor = "0.1.1"
embassy-macros = "0.1.0"
embassy-sync = "0.1.0"
embassy-time = "0.1.0"

[[bin]]
name = "ble_bas_peripheral"
required-features = ["ble-gatt-server"]

[[bin]]
name = "ble_bas_peripheral_notify"
required-features = ["ble-gatt-server"]

[[bin]]
name = "ble_bas_central"
required-features = ["ble-gatt-client"]

[[bin]]
name = "ble_peripheral_onoff"
required-features = ["ble-gatt-server"]

[[bin]]
name = "flash"
required-features = []

but now I'm getting some compilation errors:

$ cargo build
   Compiling defmt-macros v0.3.3
   Compiling darling_macro v0.13.4
   Compiling cortex-m-rt-macros v0.7.0
   Compiling num_enum_derive v0.5.7
   Compiling nrf-softdevice-s140 v0.1.1
error: cannot find macro `asm` in this scope
    --> /Users/redacted/.cargo/registry/src/github.com-1ecc6299db9ec823/nrf-softdevice-s140-0.1.1/src/bindings.rs:1663:5
     |
1663 |     asm!("svc 44",
     |     ^^^
     |
     = help: consider importing this macro:
             core::arch::asm

error: cannot find macro `asm` in this scope
    --> /Users/redacted/.cargo/registry/src/github.com-1ecc6299db9ec823/nrf-softdevice-s140-0.1.1/src/bindings.rs:1684:5
     |
1684 |     asm!("svc 45",
     |     ^^^
     |
     = help: consider importing this macro:
             core::arch::asm

......
sureshjoshi commented 1 year ago

Not sure if it's still helpful, but here's what I use for my S340 fork. I'm only just learning Rust, so maybe there is a way to shorten this, but 🤷🏽

[package]
name = "myproject"
version = "0.0.1"
description = "Firmware for myproject"
authors = [
    "SJ <me@sureshjoshi.com>"
]
edition = "2021"
publish = false

[features]
default = [
    "nightly",
    "ble-l2cap",
    "ble-gatt-server",
    "ble-gatt-client",
    "ble-sec",
]
nightly = ["embassy-executor/nightly", "embassy-nrf/nightly", "embedded-io/async"]

ble-l2cap = ["nrf-softdevice/ble-l2cap"]
ble-gatt-server = ["nrf-softdevice/ble-gatt-server"]
ble-gatt-client = ["nrf-softdevice/ble-gatt-client"]
ble-sec = ["nrf-softdevice/ble-sec"]

[dependencies]
embassy-futures = { git = "https://github.com/embassy-rs/embassy" }
embassy-executor = { git = "https://github.com/embassy-rs/embassy", features = ["defmt", "integrated-timers"] }
embassy-nrf = { git = "https://github.com/embassy-rs/embassy", features = ["defmt", "nrf52840", "time-driver-rtc1", "gpiote", "unstable-pac"] }
embassy-sync = { git = "https://github.com/embassy-rs/embassy", features = ["defmt"] }
embassy-time = { git = "https://github.com/embassy-rs/embassy", features = ["defmt", "defmt-timestamp-uptime"] }
embedded-io = "0.4.0"

nrf-softdevice = { git = "ssh://myprivatefork", features = ["defmt", "nrf52840", "s340", "ble-peripheral", "ble-central", "critical-section-impl"] }
nrf-softdevice-s340 = { git = "ssh://myprivatefork" }

defmt = "0.3"
defmt-rtt = "0.4"

cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
cortex-m-rt = "0.7.0"
panic-probe = { version = "0.3", features = ["print-defmt"] }

I'm sure instead of nrf-softdevice-s340 = { git = "ssh://myprivatefork" } you could do nrf-softdevice-s340 = "0.1.1"

materoy commented 1 year ago

Build with nightly version of rust, check rust-toolchain.toml

sureshjoshi commented 1 year ago

@olalonde Did this ever work out for you?