Skip to content
This repository was archived by the owner on Nov 30, 2022. It is now read-only.

Commit 5518293

Browse files
authored
Merge pull request #122 from RCasatta/no_std2
Add no_std embedded test in CI and fix for no_std
2 parents 451fe87 + 9d08cbd commit 5518293

File tree

7 files changed

+123
-15
lines changed

7 files changed

+123
-15
lines changed

.github/workflows/rust.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,25 @@ jobs:
8080
DO_FEATURE_MATRIX: true
8181
run: ./contrib/test.sh
8282

83+
Embedded:
84+
runs-on: ubuntu-latest
85+
steps:
86+
- name: Checkout
87+
uses: actions/checkout@v2
88+
- name: Set up QEMU
89+
run: sudo apt update && sudo apt install qemu-system-arm
90+
- name: Checkout Toolchain
91+
uses: actions-rs/toolchain@v1
92+
with:
93+
profile: minimal
94+
toolchain: nightly
95+
override: true
96+
components: rust-src
97+
target: thumbv7m-none-eabi
98+
- name: Run
99+
env:
100+
RUSTFLAGS: "-C link-arg=-Tlink.x"
101+
CARGO_TARGET_THUMBV7M_NONE_EABI_RUNNER: "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
102+
run: cd embedded && cargo run --target thumbv7m-none-eabi
103+
104+

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ Cargo.lock
88
fuzz/hfuzz_target
99
fuzz/hfuzz_workspace
1010

11+
#embedded
12+
embedded/.cargo
13+
1114
*~

embedded/Cargo.toml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[package]
2+
authors = ["Riccardo Casatta <[email protected]>"]
3+
edition = "2018"
4+
readme = "README.md"
5+
name = "embedded"
6+
version = "0.1.0"
7+
8+
[dependencies]
9+
cortex-m = "0.6.0"
10+
cortex-m-rt = "0.6.10"
11+
cortex-m-semihosting = "0.3.3"
12+
panic-halt = "0.2.0"
13+
alloc-cortex-m = "0.4.1"
14+
bitcoin_hashes = { path="../", default-features = false }
15+
16+
[[bin]]
17+
name = "embedded"
18+
test = false
19+
bench = false
20+
21+
[profile.release]
22+
codegen-units = 1 # better optimizations
23+
debug = true # symbols are nice and they don't increase the size on Flash
24+
lto = true # better optimizations

embedded/memory.x

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
MEMORY
2+
{
3+
FLASH : ORIGIN = 0x00000000, LENGTH = 256K
4+
RAM : ORIGIN = 0x20000000, LENGTH = 64K
5+
}

embedded/src/main.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#![feature(alloc_error_handler)]
2+
#![no_std]
3+
#![no_main]
4+
5+
#[macro_use]
6+
extern crate bitcoin_hashes;
7+
8+
extern crate alloc;
9+
10+
use alloc_cortex_m::CortexMHeap;
11+
use bitcoin_hashes::{sha256, Hash, HashEngine};
12+
use core::alloc::Layout;
13+
use core::str::FromStr;
14+
use cortex_m::asm;
15+
use cortex_m_rt::entry;
16+
use cortex_m_semihosting::{debug, hprintln};
17+
use panic_halt as _;
18+
19+
hash_newtype!(TestType, sha256::Hash, 32, doc = "test");
20+
21+
// this is the allocator the application will use
22+
#[global_allocator]
23+
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
24+
25+
const HEAP_SIZE: usize = 1024; // in bytes
26+
27+
#[entry]
28+
fn main() -> ! {
29+
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }
30+
31+
let mut engine = TestType::engine();
32+
engine.input(b"abc");
33+
let hash = TestType::from_engine(engine);
34+
35+
let hash_check =
36+
TestType::from_str("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad")
37+
.unwrap();
38+
hprintln!("hash:{} hash_check:{}", hash, hash_check).unwrap();
39+
if hash == hash_check {
40+
debug::exit(debug::EXIT_SUCCESS);
41+
} else {
42+
debug::exit(debug::EXIT_FAILURE);
43+
}
44+
45+
loop {}
46+
}
47+
48+
// define what happens in an Out Of Memory (OOM) condition
49+
#[alloc_error_handler]
50+
fn alloc_error(_layout: Layout) -> ! {
51+
asm::bkpt();
52+
53+
loop {}
54+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#![cfg_attr(all(test, feature = "unstable"), feature(test))]
3838
#[cfg(all(test, feature = "unstable"))] extern crate test;
3939

40-
#[cfg(any(test, feature="std"))] pub extern crate core;
40+
#[cfg(any(test, feature="std"))] extern crate core;
4141
#[cfg(feature="serde")] pub extern crate serde;
4242
#[cfg(all(test,feature="serde"))] extern crate serde_test;
4343

src/util.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ macro_rules! hex_fmt_impl(
2929
hex_fmt_impl!($imp, $ty, );
3030
);
3131
($imp:ident, $ty:ident, $($gen:ident: $gent:ident),*) => (
32-
impl<$($gen: $gent),*> $crate::core::fmt::$imp for $ty<$($gen),*> {
33-
fn fmt(&self, f: &mut $crate::core::fmt::Formatter) -> $crate::core::fmt::Result {
32+
impl<$($gen: $gent),*> ::core::fmt::$imp for $ty<$($gen),*> {
33+
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
3434
use $crate::hex::{format_hex, format_hex_reverse};
3535
if $ty::<$($gen),*>::DISPLAY_BACKWARD {
3636
format_hex_reverse(&self.0, f)
@@ -49,37 +49,37 @@ macro_rules! index_impl(
4949
index_impl!($ty, );
5050
);
5151
($ty:ident, $($gen:ident: $gent:ident),*) => (
52-
impl<$($gen: $gent),*> $crate::core::ops::Index<usize> for $ty<$($gen),*> {
52+
impl<$($gen: $gent),*> ::core::ops::Index<usize> for $ty<$($gen),*> {
5353
type Output = u8;
5454
fn index(&self, index: usize) -> &u8 {
5555
&self.0[index]
5656
}
5757
}
5858

59-
impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::Range<usize>> for $ty<$($gen),*> {
59+
impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::Range<usize>> for $ty<$($gen),*> {
6060
type Output = [u8];
61-
fn index(&self, index: $crate::core::ops::Range<usize>) -> &[u8] {
61+
fn index(&self, index: ::core::ops::Range<usize>) -> &[u8] {
6262
&self.0[index]
6363
}
6464
}
6565

66-
impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeFrom<usize>> for $ty<$($gen),*> {
66+
impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeFrom<usize>> for $ty<$($gen),*> {
6767
type Output = [u8];
68-
fn index(&self, index: $crate::core::ops::RangeFrom<usize>) -> &[u8] {
68+
fn index(&self, index: ::core::ops::RangeFrom<usize>) -> &[u8] {
6969
&self.0[index]
7070
}
7171
}
7272

73-
impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeTo<usize>> for $ty<$($gen),*> {
73+
impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeTo<usize>> for $ty<$($gen),*> {
7474
type Output = [u8];
75-
fn index(&self, index: $crate::core::ops::RangeTo<usize>) -> &[u8] {
75+
fn index(&self, index: ::core::ops::RangeTo<usize>) -> &[u8] {
7676
&self.0[index]
7777
}
7878
}
7979

80-
impl<$($gen: $gent),*> $crate::core::ops::Index<$crate::core::ops::RangeFull> for $ty<$($gen),*> {
80+
impl<$($gen: $gent),*> ::core::ops::Index<::core::ops::RangeFull> for $ty<$($gen),*> {
8181
type Output = [u8];
82-
fn index(&self, index: $crate::core::ops::RangeFull) -> &[u8] {
82+
fn index(&self, index: ::core::ops::RangeFull) -> &[u8] {
8383
&self.0[index]
8484
}
8585
}
@@ -93,19 +93,19 @@ macro_rules! borrow_slice_impl(
9393
borrow_slice_impl!($ty, );
9494
);
9595
($ty:ident, $($gen:ident: $gent:ident),*) => (
96-
impl<$($gen: $gent),*> $crate::core::borrow::Borrow<[u8]> for $ty<$($gen),*> {
96+
impl<$($gen: $gent),*> ::core::borrow::Borrow<[u8]> for $ty<$($gen),*> {
9797
fn borrow(&self) -> &[u8] {
9898
&self[..]
9999
}
100100
}
101101

102-
impl<$($gen: $gent),*> $crate::core::convert::AsRef<[u8]> for $ty<$($gen),*> {
102+
impl<$($gen: $gent),*> ::core::convert::AsRef<[u8]> for $ty<$($gen),*> {
103103
fn as_ref(&self) -> &[u8] {
104104
&self[..]
105105
}
106106
}
107107

108-
impl<$($gen: $gent),*> $crate::core::ops::Deref for $ty<$($gen),*> {
108+
impl<$($gen: $gent),*> ::core::ops::Deref for $ty<$($gen),*> {
109109
type Target = [u8];
110110

111111
fn deref(&self) -> &Self::Target {

0 commit comments

Comments
 (0)