Skip to content
This repository was archived by the owner on Apr 12, 2021. It is now read-only.
Open
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
21 changes: 12 additions & 9 deletions .cargo/config
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@

[target.thumbv7em-none-eabihf]
rustflags = [
"-C", "link-arg=-mthumb",
"-C", "link-arg=-Tnrf52dk-sys.ld",
"-C", "link-arg=-mcpu=cortex-m4",
"-C", "link-arg=--specs=nano.specs",

"-C", "link-arg=-Wl,--gc-sections",
"-C", "link-arg=-Wl,--start-group",
"-C", "link-arg=-Wl,--end-group",
"-C", "link-arg=-Wl,--build-id=none",
# "-C", "link-arg=-Tlink.x",
"-C", "link-arg=-Tcustom.x",
# "-C", "link-arg=-Tmemory.x",

"-C", "link-arg=-mcpu=cortex-m4",
"-C", "link-arg=-mthumb",
"-C", "link-arg=-mabi=aapcs",
"-C", "link-arg=-mfloat-abi=hard",
"-C", "link-arg=-mfpu=fpv4-sp-d16",

"-C", "link-arg=--specs=nano.specs",

# "-C", "link-arg=-Wl,--gc-sections",
# "-C", "link-arg=-Wl,--start-group",
# "-C", "link-arg=-Wl,--end-group",
# "-C", "link-arg=-Wl,--build-id=none",
]

[build]
Expand Down
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ repository = "https://github.com/jamesmunns/nrf52dk-sys"
description = "nrf52 support using the nRF5-SDK and SoftDevice S132"

[dependencies]
cortex-m = "0.1.6"
r0 = "0.2.0"
cortex-m = "0.5.2"
cortex-m-rt = "0.5.0"
cortex-m-semihosting = "0.3.0"
panic-semihosting = "0.3.0"

[build-dependencies]
gcc = "0.3.46"
Expand Down
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,30 @@ This project aims to be a reference on how to combine C and Rust components, in

This project requires the following tools before building:

Tool | Recommended Version | Link/Install
:--- | :------------------ | :---
Clang | 3.9 | [debian/ubuntu](http://apt.llvm.org/) or [source](http://releases.llvm.org/download.html)
arm-none-eabi-gcc | 6.1 | [Current Version](https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads)
Rust (nightly) | nightly-2017-11-15 | [rustup.rs](https://www.rustup.rs/)
Rust source | nightly-2017-11-15 | `rustup component add rust-src`
Xargo | 0.3.8 | `cargo install xargo --vers 0.3.8`
Bindgen | 0.31.3 | `cargo install bindgen --vers 0.31.3`
| Tool | Recommended Version | Link/Install |
| :---------------- | :------------------ | :---------------------------------------------------------------------------------------- |
| Clang | 3.9 | [debian/ubuntu](http://apt.llvm.org/) or [source](http://releases.llvm.org/download.html) |
| arm-none-eabi-gcc | 6.1 | [Current Version](https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads) |
| Rust (nightly) | nightly-2017-11-15 | [rustup.rs](https://www.rustup.rs/) |
| Bindgen | 0.31.3 | `cargo install bindgen --vers 0.31.3` |

If you would like more detailed installation instructions, please look at [The Detailed Setup Instructions](./SETUP.md).

If you use `docker`, please see the debian based [Dockerfile](./Dockerfile).

Additionally, the following tools are required to run or debug the firmware:

Tool | Recommended Version | Link/Install
--- | --- | ---
SoftDevice | S132-v4.0.2 | [Nordic Download](http://www.nordicsemi.com/eng/nordic/Products/nRF52832/S132-SD-v4/58803)
JLink | v6.16 | [JLink Download](https://www.segger.com/downloads/jlink)
| Tool | Recommended Version | Link/Install |
| ---------- | ------------------- | ------------------------------------------------------------------------------------------ |
| SoftDevice | S132-v4.0.2 | [Nordic Download](http://www.nordicsemi.com/eng/nordic/Products/nRF52832/S132-SD-v4/58803) |
| JLink | v6.16 | [JLink Download](https://www.segger.com/downloads/jlink) |

## Building

```text
git clone --recursive https://github.com/jamesmunns/nrf52dk-sys
cd nrf52dk-sys
xargo build --example blinky
cargo build --example blinky
Compiling core v0.0.0 (file:///root/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore)
Finished release [optimized] target(s) in 14.8 secs
Updating registry `https://github.com/rust-lang/crates.io-index`
Expand Down Expand Up @@ -114,7 +112,6 @@ JLinkGDBServer -device NRF52832_XXAA -if SWD -speed 4000
# Waiting for GDB connection...
```


Then, in another terminal:

```text
Expand Down
87 changes: 47 additions & 40 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
extern crate gcc;

use std::collections::{HashMap, HashSet};
use gcc::Build;
use std::collections::{HashMap, HashSet};

use std::env;
use std::path::PathBuf;
use std::process::Command;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process::Command;

fn main() {
let outdir = PathBuf::from(env::var("OUT_DIR").unwrap());
let out = PathBuf::from(env::var("OUT_DIR").unwrap());

// Put the linker script somewhere the linker can find it
File::create(out.join("memory.x"))
.unwrap()
.write_all(include_bytes!("memory.x"))
.unwrap();
println!("cargo:rustc-link-search={}", out.display());

// If any of these files/folders change, we should regenerate
// the whole C + bindings component
println!("cargo:rerun-if-changed=memory.x");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=bindings.h");

// We're going to generate app_config.h from their feature
// selection, so let's extract that from the env.
Expand All @@ -23,13 +36,7 @@ fn main() {
}
})
.collect();

write_app_config(&outdir, &features);

// If any of these files/folders change, we should regenerate
// the whole C + bindings component
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=bindings.h");
write_app_config(&out, &features);

let mut info = SdkInfo::default();
info.add_from_path(&PathBuf::from("nRF5-sdk"));
Expand All @@ -42,15 +49,15 @@ fn main() {
println!("cargo:rerun-if-changed={}", hdr.display());
}

process_linker_file(&outdir);
generate_ble(&outdir, &info);
make_c_deps(&outdir, &info, &features);
// process_linker_file(&out);
generate_ble(&out, &info);
make_c_deps(&out, &info, &features);
}

/// Emit app_config.h based on the enabled features. This is used
/// to override things in sdk_config.h
fn write_app_config(outdir: &PathBuf, features: &HashSet<String>) {
let mut app_config = File::create(outdir.join("app_config.h")).unwrap();
fn write_app_config(out: &PathBuf, features: &HashSet<String>) {
let mut app_config = File::create(out.join("app_config.h")).unwrap();
for feature in features.iter() {
writeln!(app_config, "#define {}_ENABLED 1", feature).ok();
}
Expand Down Expand Up @@ -97,27 +104,26 @@ impl SdkInfo {
}
}

fn process_linker_file(out: &PathBuf) {
// Copy over the target specific linker script
File::create(out.join("nrf52dk-sys.ld"))
.unwrap()
.write_all(include_bytes!("nrf52dk-sys.ld"))
.unwrap();

// Also copy the nrf general linker script
File::create(out.join("nrf5x_common.ld"))
.unwrap()
.write_all(include_bytes!("nrf5x_common.ld"))
.unwrap();
// fn process_linker_file(out: &PathBuf) {
// // Copy over the target specific linker script
// File::create(out.join("nrf52dk-sys.ld"))
// .unwrap()
// .write_all(include_bytes!("nrf52dk-sys.ld"))
// .unwrap();

println!("cargo:rustc-link-search={}", out.display());
}
// // Also copy the nrf general linker script
// File::create(out.join("nrf5x_common.ld"))
// .unwrap()
// .write_all(include_bytes!("nrf5x_common.ld"))
// .unwrap();

// println!("cargo:rustc-link-search={}", out.display());
// }

fn make_c_deps(out_path: &PathBuf, info: &SdkInfo, features: &HashSet<String>) {
fn make_c_deps(out: &PathBuf, info: &SdkInfo, features: &HashSet<String>) {
let mut config = Build::new();

config.out_dir(out_path);
config.out_dir(out);

for f in FLAGS {
config.flag(f);
Expand All @@ -134,16 +140,16 @@ fn make_c_deps(out_path: &PathBuf, info: &SdkInfo, features: &HashSet<String>) {
}
}

// out_path is where we find the app_config.h that we generate
// out is where we find the app_config.h that we generate
// from the enabled features
config.include(out_path);
config.include(out);
for i in info.dirs.iter() {
config.include(i);
}

config.compile("libnrf.a");

println!("cargo:rustc-link-search={}", out_path.display());
println!("cargo:rustc-link-search={}", out.display());
println!("cargo:rustc-link-lib=static=nrf");
}

Expand Down Expand Up @@ -180,9 +186,9 @@ fn generate_ble(out: &PathBuf, info: &SdkInfo) {
cmd.arg("--ctypes-prefix=ctypes");
cmd.arg("--with-derive-default");
cmd.arg("--verbose");
cmd.arg("--blacklist-type");
cmd.arg("--blacklist-type");
cmd.arg("IRQn_Type");
cmd.arg("--blacklist-type");
cmd.arg("--blacklist-type");
cmd.arg("__va_list");
// This type wraps a mutable void pointer, and we cannot safely impl Copy.
cmd.arg("--output");
Expand Down Expand Up @@ -225,9 +231,7 @@ fn generate_ble(out: &PathBuf, info: &SdkInfo) {
cmd.arg("-target");
cmd.arg(env::var("TARGET").unwrap());

assert!(cmd.status()
.expect("failed to build BLE libs")
.success());
assert!(cmd.status().expect("failed to build BLE libs").success());
}

/// Build SRC_TO_FEAT into something using PathBufs
Expand Down Expand Up @@ -267,13 +271,16 @@ static FLAGS: &[&str] = &[
"-mfpu=fpv4-sp-d16",
"-ffunction-sections",
"-fdata-sections",
"-fno-pic",
"-fno-strict-aliasing",
"-fno-builtin",
// the headers are riddled with unused parameters and emit
// hundreds of warnings: suppress them.
"-Wno-unused-parameter",
"-Wno-sign-compare",
"-Wno-missing-field-initializers",
"-Wno-expansion-to-defined",
"-Wimplicit-fallthrough=0",
"--short-enums",
];

Expand Down
14 changes: 14 additions & 0 deletions custom.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SEARCH_DIR(.)

GROUP(-lgcc -lc -lnosys)

SECTIONS
{
.heap (COPY):
{
PROVIDE(end = .);
KEEP(*(.heap*))
} > RAM
} INSERT AFTER .bss;

INCLUDE "link.x"
Loading