Skip to content

Commit

Permalink
staging
Browse files Browse the repository at this point in the history
Signed-off-by: Jean-Pierre De Jesus DIAZ <me@jeandudey.tech>
  • Loading branch information
jeandudey committed Oct 23, 2023
1 parent f9e2fe7 commit 409d2f8
Show file tree
Hide file tree
Showing 19 changed files with 317 additions and 25 deletions.
94 changes: 94 additions & 0 deletions extmod/foundation-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions extmod/foundation-rust/bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ heapless = { version = "0.8", git = "https://github.com/japaric/heapless", defau
minicbor = { version = "0.19", default-features = false }
passport-platform = { path = "../platform", default-features = false }
secp256k1 = { version = "0.28", features = ["rand"], default-features = false }
spi-nor = { version = "0.1", path = "../spi-nor" }
uuid = { version = "1", default-features = false }
4 changes: 4 additions & 0 deletions extmod/foundation-rust/bindings/src/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
// SPDX-License-Identifier: GPL-3.0-or-later

pub mod spi_nor;
14 changes: 14 additions & 0 deletions extmod/foundation-rust/bindings/src/io/spi_nor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-FileCopyrightText: 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
// SPDX-License-Identifier: GPL-3.0-or-later

#[repr(C)]
pub struct SPI_Flash_t {
flash: passport_platform::io::Flash,
}

/// Read from the SPI NOR flash memory.
pub fn spi_nor_read(dev: &mut spi_nor_t, address: u32, buf: *mut u8, len: usize) {
let buf = unsafe { core::slice::from_raw_parts(buf, len) };
todo!()
//dev.flash.read(address, buf).unwrap()
}
1 change: 1 addition & 0 deletions extmod/foundation-rust/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
#![cfg_attr(all(target_arch = "arm", target_os = "none"), no_std)]
#![allow(non_camel_case_types)]

pub mod io;
pub mod secp256k1;
pub mod ur;
5 changes: 5 additions & 0 deletions extmod/foundation-rust/platform/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
# SPDX-License-Identifier: GPL-3.0-or-later

[package]
name = "passport-platform"
version = "0.1.0"
Expand All @@ -15,9 +18,11 @@ std = [
rand = { version = "0.8.5", default-features = false }
rand_core = { version = "0.6.4", default-features = false }
secp256k1 = { version = "0.28", features = ["rand"], default-features = false }
spi-nor = { version = "0.1.0", path = "../spi-nor" }

[target.'cfg(all(target_arch = "arm", target_os = "none"))'.dependencies]
once_cell = { version = "1", features = ["critical-section"], default-features = false }
cortex-m = { version = "0.7", features = ["cm7", "critical-section-single-core"] }
critical-section = "1"
embedded-hal = "0.2.7"
stm32h753-hal = { version = "0.1", path = "../stm32h753-hal" }
1 change: 1 addition & 0 deletions extmod/foundation-rust/platform/src/stm32/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later

pub mod spi;
pub mod spi_nor;

use crate::stm32::ffi;

Expand Down
18 changes: 10 additions & 8 deletions extmod/foundation-rust/platform/src/stm32/io/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ impl Spi {
pub fn from_index(index: usize) -> Self {
Self(unsafe { &spi_obj[index] as *const _ })
}

fn timeout(&self, words: usize) -> u32 {
// Approximation of len*8*100/baudrate milliseconds.
//
// See ports/stm32/spi.h for more information.
//
// FIXME: Use baudrate for the calculation.
words as u32 + 100
}
}

impl Transfer<u8> for Spi {
Expand All @@ -21,20 +30,13 @@ impl Transfer<u8> for Spi {
&mut self,
words: &'w mut [u8],
) -> Result<&'w [u8], Self::Error> {
// Approximation of len*8*100/baudrate milliseconds.
//
// See ports/stm32/spi.h for more information.
//
// FIXME: Use baudrate for the calculation.
let timeout = words.len() + 100;

unsafe {
spi_transfer(
self.0,
words.len(),
words.as_ptr(),
words.as_mut_ptr(),
timeout as u32,
self.timeout(words.len()),
)
}

Expand Down
7 changes: 7 additions & 0 deletions extmod/foundation-rust/platform/src/stm32/io/spi_nor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
// SPDX-License-Identifier: GPL-3.0-or-later

use crate::io::spi::Spi;
use stm32h753_hal::gpio::{Output, gpioe::PE11};

pub type Flash = Flash<Spi, PE11<Output>>;
9 changes: 9 additions & 0 deletions extmod/foundation-rust/platform/src/unix/io/spi_nor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
// SPDX-License-Identifier: GPL-3.0-or-later


pub type Flash = spi_nor::Flash<EmulatedSpiBus, EmulatedPin>;

pub struct EmulatedSpiBus;

pub struct EmulatedPin;
4 changes: 4 additions & 0 deletions extmod/foundation-rust/platform/src/unix/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// SPDX-FileCopyrightText: © 2023 Foundation Devices, Inc. <hello@foundationdevices.com>
// SPDX-License-Identifier: GPL-3.0-or-later

pub mod io {
pub mod spi_nor;
}

pub mod rand {
pub fn passport_rng() -> rand::rngs::ThreadRng {
rand::thread_rng()
Expand Down
3 changes: 3 additions & 0 deletions extmod/foundation-rust/spi-nor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@ edition = "2021"
bitflags = { version = "2", default-features = false }
embedded-hal = "0.2.1"
nb = "1"

[dev-dependencies]
embedded-hal-mock = "0.9"
Loading

0 comments on commit 409d2f8

Please sign in to comment.