From be392299bccfe239111f0c50444af50ab0ae606e Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Fri, 9 Apr 2021 15:27:36 +0200 Subject: [PATCH 1/3] add no_std program and build it in github actions --- .github/workflows/embedded.yml | 23 +++++++++++++++++++++ embedded/.cargo/config | 26 ++++++++++++++++++++++++ embedded/Cargo.toml | 37 ++++++++++++++++++++++++++++++++++ embedded/build.rs | 31 ++++++++++++++++++++++++++++ embedded/memory.x | 34 +++++++++++++++++++++++++++++++ embedded/src/main.rs | 30 +++++++++++++++++++++++++++ 6 files changed, 181 insertions(+) create mode 100644 .github/workflows/embedded.yml create mode 100644 embedded/.cargo/config create mode 100644 embedded/Cargo.toml create mode 100644 embedded/build.rs create mode 100644 embedded/memory.x create mode 100644 embedded/src/main.rs diff --git a/.github/workflows/embedded.yml b/.github/workflows/embedded.yml new file mode 100644 index 0000000..112760b --- /dev/null +++ b/.github/workflows/embedded.yml @@ -0,0 +1,23 @@ +name: Test Embedded + +on: + push: + +jobs: + build: + runs-on: ubuntu-latest + steps: + - + name: Checkout + uses: actions/checkout@v2 + - name: Checkout Toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + components: rust-src + target: thumbv7m-none-eabi + - + name: Build + run: cd embedded && cargo build diff --git a/embedded/.cargo/config b/embedded/.cargo/config new file mode 100644 index 0000000..4cdaced --- /dev/null +++ b/embedded/.cargo/config @@ -0,0 +1,26 @@ +[target.thumbv7m-none-eabi] +# uncomment this to make `cargo run` execute programs on QEMU +runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel" + +rustflags = [ + # This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x + # See https://github.com/rust-embedded/cortex-m-quickstart/pull/95 + "-C", "link-arg=--nmagic", + + # LLD (shipped with the Rust toolchain) is used as the default linker + "-C", "link-arg=-Tlink.x", + + # if you run into problems with LLD switch to the GNU linker by commenting out + # this line + # "-C", "linker=arm-none-eabi-ld", + + # if you need to link to pre-compiled C libraries provided by a C toolchain + # use GCC as the linker by commenting out both lines above and then + # uncommenting the three lines below + # "-C", "linker=arm-none-eabi-gcc", + # "-C", "link-arg=-Wl,-Tlink.x", + # "-C", "link-arg=-nostartfiles", +] + +[build] +target = "thumbv7m-none-eabi" # Cortex-M3 diff --git a/embedded/Cargo.toml b/embedded/Cargo.toml new file mode 100644 index 0000000..0f1d459 --- /dev/null +++ b/embedded/Cargo.toml @@ -0,0 +1,37 @@ +[package] +authors = ["Riccardo Casatta "] +edition = "2018" +readme = "README.md" +name = "embedded" +version = "0.1.0" + +[dependencies] +cortex-m = "0.6.0" +cortex-m-rt = "0.6.10" +cortex-m-semihosting = "0.3.3" +panic-halt = "0.2.0" +bitcoin_hashes = { path="../", default-features = false } + +# Uncomment for the panic example. +# panic-itm = "0.4.1" + +# Uncomment for the allocator example. +# alloc-cortex-m = "0.4.0" + +# Uncomment for the device example. +# Update `memory.x`, set target to `thumbv7em-none-eabihf` in `.cargo/config`, +# and then use `cargo build --examples device` to build it. +# [dependencies.stm32f3] +# features = ["stm32f303", "rt"] +# version = "0.7.1" + +# this lets you use `cargo fix`! +[[bin]] +name = "embedded" +test = false +bench = false + +[profile.release] +codegen-units = 1 # better optimizations +debug = true # symbols are nice and they don't increase the size on Flash +lto = true # better optimizations diff --git a/embedded/build.rs b/embedded/build.rs new file mode 100644 index 0000000..d534cc3 --- /dev/null +++ b/embedded/build.rs @@ -0,0 +1,31 @@ +//! This build script copies the `memory.x` file from the crate root into +//! a directory where the linker can always find it at build time. +//! For many projects this is optional, as the linker always searches the +//! project root directory -- wherever `Cargo.toml` is. However, if you +//! are using a workspace or have a more complicated build setup, this +//! build script becomes required. Additionally, by requesting that +//! Cargo re-run the build script whenever `memory.x` is changed, +//! updating `memory.x` ensures a rebuild of the application with the +//! new memory settings. + +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + // Put `memory.x` in our output directory and ensure it's + // on the linker search path. + let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); + File::create(out.join("memory.x")) + .unwrap() + .write_all(include_bytes!("memory.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + + // By default, Cargo will re-run a build script whenever + // any file in the project changes. By specifying `memory.x` + // here, we ensure the build script is only re-run when + // `memory.x` is changed. + println!("cargo:rerun-if-changed=memory.x"); +} diff --git a/embedded/memory.x b/embedded/memory.x new file mode 100644 index 0000000..b271f22 --- /dev/null +++ b/embedded/memory.x @@ -0,0 +1,34 @@ +MEMORY +{ + /* NOTE 1 K = 1 KiBi = 1024 bytes */ + /* TODO Adjust these memory regions to match your device memory layout */ + /* These values correspond to the LM3S6965, one of the few devices QEMU can emulate */ + FLASH : ORIGIN = 0x00000000, LENGTH = 256K + RAM : ORIGIN = 0x20000000, LENGTH = 64K +} + +/* This is where the call stack will be allocated. */ +/* The stack is of the full descending type. */ +/* You may want to use this variable to locate the call stack and static + variables in different memory regions. Below is shown the default value */ +/* _stack_start = ORIGIN(RAM) + LENGTH(RAM); */ + +/* You can use this symbol to customize the location of the .text section */ +/* If omitted the .text section will be placed right after the .vector_table + section */ +/* This is required only on microcontrollers that store some configuration right + after the vector table */ +/* _stext = ORIGIN(FLASH) + 0x400; */ + +/* Example of putting non-initialized variables into custom RAM locations. */ +/* This assumes you have defined a region RAM2 above, and in the Rust + sources added the attribute `#[link_section = ".ram2bss"]` to the data + you want to place there. */ +/* Note that the section will not be zero-initialized by the runtime! */ +/* SECTIONS { + .ram2bss (NOLOAD) : ALIGN(4) { + *(.ram2bss); + . = ALIGN(4); + } > RAM2 + } INSERT AFTER .bss; +*/ diff --git a/embedded/src/main.rs b/embedded/src/main.rs new file mode 100644 index 0000000..f8ce9e1 --- /dev/null +++ b/embedded/src/main.rs @@ -0,0 +1,30 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate bitcoin_hashes; + +// pick a panicking behavior +use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics +// use panic_abort as _; // requires nightly +// use panic_itm as _; // logs messages over ITM; requires ITM support +// use panic_semihosting as _; // logs messages to the host stderr; requires a debugger + +use cortex_m_rt::entry; +use cortex_m_semihosting::{debug, hprintln}; +use bitcoin_hashes::sha256; +use core::str::FromStr; +use bitcoin_hashes::Hash; + +hash_newtype!(TestType, sha256::Hash, 32, doc="test"); + +#[entry] +fn main() -> ! { + hprintln!("Hello world!").unwrap(); + + debug::exit(debug::EXIT_SUCCESS); + + loop { + // your code goes here + } +} From 43cc8ebf435940321a410bd3fb68b36dac7bb2f6 Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Fri, 9 Apr 2021 17:08:37 +0200 Subject: [PATCH 2/3] removes $crate from hash_newtype! macro --- embedded/src/main.rs | 1 - src/lib.rs | 2 +- src/util.rs | 28 ++++++++++++++-------------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/embedded/src/main.rs b/embedded/src/main.rs index f8ce9e1..40a5f5f 100644 --- a/embedded/src/main.rs +++ b/embedded/src/main.rs @@ -13,7 +13,6 @@ use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch use cortex_m_rt::entry; use cortex_m_semihosting::{debug, hprintln}; use bitcoin_hashes::sha256; -use core::str::FromStr; use bitcoin_hashes::Hash; hash_newtype!(TestType, sha256::Hash, 32, doc="test"); diff --git a/src/lib.rs b/src/lib.rs index 9e71c0b..b60f20a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,7 +37,7 @@ #![cfg_attr(all(test, feature = "unstable"), feature(test))] #[cfg(all(test, feature = "unstable"))] extern crate test; -#[cfg(any(test, feature="std"))] pub extern crate core; +#[cfg(any(test, feature="std"))] extern crate core; #[cfg(feature="serde")] pub extern crate serde; #[cfg(all(test,feature="serde"))] extern crate serde_test; diff --git a/src/util.rs b/src/util.rs index e6d4422..80d109b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -29,8 +29,8 @@ macro_rules! hex_fmt_impl( hex_fmt_impl!($imp, $ty, ); ); ($imp:ident, $ty:ident, $($gen:ident: $gent:ident),*) => ( - impl<$($gen: $gent),*> $crate::core::fmt::$imp for $ty<$($gen),*> { - fn fmt(&self, f: &mut $crate::core::fmt::Formatter) -> $crate::core::fmt::Result { + impl<$($gen: $gent),*> ::core::fmt::$imp for $ty<$($gen),*> { + fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result { use $crate::hex::{format_hex, format_hex_reverse}; if $ty::<$($gen),*>::DISPLAY_BACKWARD { format_hex_reverse(&self.0, f) @@ -49,37 +49,37 @@ macro_rules! index_impl( index_impl!($ty, ); ); ($ty:ident, $($gen:ident: $gent:ident),*) => ( - impl<$($gen: $gent),*> $crate::core::ops::Index for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index for $ty<$($gen),*> { type Output = u8; fn index(&self, index: usize) -> &u8 { &self.0[index] } } - impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::Range> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::Range> for $ty<$($gen),*> { type Output = [u8]; - fn index(&self, index: $crate::core::ops::Range) -> &[u8] { + fn index(&self, index: ::core::ops::Range) -> &[u8] { &self.0[index] } } - impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeFrom> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeFrom> for $ty<$($gen),*> { type Output = [u8]; - fn index(&self, index: $crate::core::ops::RangeFrom) -> &[u8] { + fn index(&self, index: ::core::ops::RangeFrom) -> &[u8] { &self.0[index] } } - impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeTo> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeTo> for $ty<$($gen),*> { type Output = [u8]; - fn index(&self, index: $crate::core::ops::RangeTo) -> &[u8] { + fn index(&self, index: ::core::ops::RangeTo) -> &[u8] { &self.0[index] } } - impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeFull> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeFull> for $ty<$($gen),*> { type Output = [u8]; - fn index(&self, index: $crate::core::ops::RangeFull) -> &[u8] { + fn index(&self, index: ::core::ops::RangeFull) -> &[u8] { &self.0[index] } } @@ -93,19 +93,19 @@ macro_rules! borrow_slice_impl( borrow_slice_impl!($ty, ); ); ($ty:ident, $($gen:ident: $gent:ident),*) => ( - impl<$($gen: $gent),*> $crate::core::borrow::Borrow<[u8]> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::borrow::Borrow<[u8]> for $ty<$($gen),*> { fn borrow(&self) -> &[u8] { &self[..] } } - impl<$($gen: $gent),*> $crate::core::convert::AsRef<[u8]> for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::convert::AsRef<[u8]> for $ty<$($gen),*> { fn as_ref(&self) -> &[u8] { &self[..] } } - impl<$($gen: $gent),*> $crate::core::ops::Deref for $ty<$($gen),*> { + impl<$($gen: $gent),*> ::core::ops::Deref for $ty<$($gen),*> { type Target = [u8]; fn deref(&self) -> &Self::Target { From aece2e884dc3037e3862688be2dd535dc36290be Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Tue, 13 Apr 2021 12:12:29 +0200 Subject: [PATCH 3/3] remove config files --- .github/workflows/embedded.yml | 23 ----------------------- .github/workflows/rust.yml | 18 ++++++++++++++++++ .gitignore | 3 +++ embedded/.cargo/config | 26 -------------------------- embedded/Cargo.toml | 14 -------------- embedded/build.rs | 31 ------------------------------- embedded/memory.x | 29 ----------------------------- embedded/src/main.rs | 11 ++--------- 8 files changed, 23 insertions(+), 132 deletions(-) delete mode 100644 .github/workflows/embedded.yml delete mode 100644 embedded/.cargo/config delete mode 100644 embedded/build.rs diff --git a/.github/workflows/embedded.yml b/.github/workflows/embedded.yml deleted file mode 100644 index 112760b..0000000 --- a/.github/workflows/embedded.yml +++ /dev/null @@ -1,23 +0,0 @@ -name: Test Embedded - -on: - push: - -jobs: - build: - runs-on: ubuntu-latest - steps: - - - name: Checkout - uses: actions/checkout@v2 - - name: Checkout Toolchain - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: stable - override: true - components: rust-src - target: thumbv7m-none-eabi - - - name: Build - run: cd embedded && cargo build diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 578a499..8fb8fc4 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -80,3 +80,21 @@ jobs: DO_FEATURE_MATRIX: true run: ./contrib/test.sh + Embedded: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Checkout Toolchain + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: stable + override: true + components: rust-src + target: thumbv7m-none-eabi + - name: Build + env: + RUSTFLAGS: "-C link-arg=-Tlink.x" + run: cd embedded && cargo build --target thumbv7m-none-eabi + diff --git a/.gitignore b/.gitignore index 03eb4d0..84d3dcb 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,7 @@ Cargo.lock fuzz/hfuzz_target fuzz/hfuzz_workspace +#embedded +embedded/.cargo + *~ diff --git a/embedded/.cargo/config b/embedded/.cargo/config deleted file mode 100644 index 4cdaced..0000000 --- a/embedded/.cargo/config +++ /dev/null @@ -1,26 +0,0 @@ -[target.thumbv7m-none-eabi] -# uncomment this to make `cargo run` execute programs on QEMU -runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel" - -rustflags = [ - # This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x - # See https://github.com/rust-embedded/cortex-m-quickstart/pull/95 - "-C", "link-arg=--nmagic", - - # LLD (shipped with the Rust toolchain) is used as the default linker - "-C", "link-arg=-Tlink.x", - - # if you run into problems with LLD switch to the GNU linker by commenting out - # this line - # "-C", "linker=arm-none-eabi-ld", - - # if you need to link to pre-compiled C libraries provided by a C toolchain - # use GCC as the linker by commenting out both lines above and then - # uncommenting the three lines below - # "-C", "linker=arm-none-eabi-gcc", - # "-C", "link-arg=-Wl,-Tlink.x", - # "-C", "link-arg=-nostartfiles", -] - -[build] -target = "thumbv7m-none-eabi" # Cortex-M3 diff --git a/embedded/Cargo.toml b/embedded/Cargo.toml index 0f1d459..6b58afe 100644 --- a/embedded/Cargo.toml +++ b/embedded/Cargo.toml @@ -12,20 +12,6 @@ cortex-m-semihosting = "0.3.3" panic-halt = "0.2.0" bitcoin_hashes = { path="../", default-features = false } -# Uncomment for the panic example. -# panic-itm = "0.4.1" - -# Uncomment for the allocator example. -# alloc-cortex-m = "0.4.0" - -# Uncomment for the device example. -# Update `memory.x`, set target to `thumbv7em-none-eabihf` in `.cargo/config`, -# and then use `cargo build --examples device` to build it. -# [dependencies.stm32f3] -# features = ["stm32f303", "rt"] -# version = "0.7.1" - -# this lets you use `cargo fix`! [[bin]] name = "embedded" test = false diff --git a/embedded/build.rs b/embedded/build.rs deleted file mode 100644 index d534cc3..0000000 --- a/embedded/build.rs +++ /dev/null @@ -1,31 +0,0 @@ -//! This build script copies the `memory.x` file from the crate root into -//! a directory where the linker can always find it at build time. -//! For many projects this is optional, as the linker always searches the -//! project root directory -- wherever `Cargo.toml` is. However, if you -//! are using a workspace or have a more complicated build setup, this -//! build script becomes required. Additionally, by requesting that -//! Cargo re-run the build script whenever `memory.x` is changed, -//! updating `memory.x` ensures a rebuild of the application with the -//! new memory settings. - -use std::env; -use std::fs::File; -use std::io::Write; -use std::path::PathBuf; - -fn main() { - // Put `memory.x` in our output directory and ensure it's - // on the linker search path. - let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); - File::create(out.join("memory.x")) - .unwrap() - .write_all(include_bytes!("memory.x")) - .unwrap(); - println!("cargo:rustc-link-search={}", out.display()); - - // By default, Cargo will re-run a build script whenever - // any file in the project changes. By specifying `memory.x` - // here, we ensure the build script is only re-run when - // `memory.x` is changed. - println!("cargo:rerun-if-changed=memory.x"); -} diff --git a/embedded/memory.x b/embedded/memory.x index b271f22..95de161 100644 --- a/embedded/memory.x +++ b/embedded/memory.x @@ -1,34 +1,5 @@ MEMORY { - /* NOTE 1 K = 1 KiBi = 1024 bytes */ - /* TODO Adjust these memory regions to match your device memory layout */ - /* These values correspond to the LM3S6965, one of the few devices QEMU can emulate */ FLASH : ORIGIN = 0x00000000, LENGTH = 256K RAM : ORIGIN = 0x20000000, LENGTH = 64K } - -/* This is where the call stack will be allocated. */ -/* The stack is of the full descending type. */ -/* You may want to use this variable to locate the call stack and static - variables in different memory regions. Below is shown the default value */ -/* _stack_start = ORIGIN(RAM) + LENGTH(RAM); */ - -/* You can use this symbol to customize the location of the .text section */ -/* If omitted the .text section will be placed right after the .vector_table - section */ -/* This is required only on microcontrollers that store some configuration right - after the vector table */ -/* _stext = ORIGIN(FLASH) + 0x400; */ - -/* Example of putting non-initialized variables into custom RAM locations. */ -/* This assumes you have defined a region RAM2 above, and in the Rust - sources added the attribute `#[link_section = ".ram2bss"]` to the data - you want to place there. */ -/* Note that the section will not be zero-initialized by the runtime! */ -/* SECTIONS { - .ram2bss (NOLOAD) : ALIGN(4) { - *(.ram2bss); - . = ALIGN(4); - } > RAM2 - } INSERT AFTER .bss; -*/ diff --git a/embedded/src/main.rs b/embedded/src/main.rs index 40a5f5f..d242b03 100644 --- a/embedded/src/main.rs +++ b/embedded/src/main.rs @@ -4,12 +4,7 @@ #[macro_use] extern crate bitcoin_hashes; -// pick a panicking behavior -use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics -// use panic_abort as _; // requires nightly -// use panic_itm as _; // logs messages over ITM; requires ITM support -// use panic_semihosting as _; // logs messages to the host stderr; requires a debugger - +use panic_halt as _; use cortex_m_rt::entry; use cortex_m_semihosting::{debug, hprintln}; use bitcoin_hashes::sha256; @@ -23,7 +18,5 @@ fn main() -> ! { debug::exit(debug::EXIT_SUCCESS); - loop { - // your code goes here - } + loop {} }