Skip to content

Commit

Permalink
Starting on RTIC version of loopback-fw.
Browse files Browse the repository at this point in the history
Currently it operates a USB Serial port.
  • Loading branch information
jonathanpallant committed Dec 11, 2024
1 parent ff4df13 commit 9d7a18a
Show file tree
Hide file tree
Showing 8 changed files with 847 additions and 5 deletions.
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ pushd loopback-fw
cargo build --target=thumbv7em-none-eabihf --release
cargo fmt --check
popd
pushd loopback-fw-rtic
cargo build --target=thumbv7em-none-eabihf --release
cargo fmt --check
popd
popd

# Only build the templates (they will panic at run-time due to the use of todo!)
Expand Down Expand Up @@ -105,5 +109,6 @@ cp -r ./.cargo "${OUTPUT_NAME}/"
cp -r ./tools "${OUTPUT_NAME}/"
cp ./nrf52-code/puzzle-fw/target/thumbv7em-none-eabihf/release/puzzle-fw "${OUTPUT_NAME}/nrf52-code/boards/dongle-fw/puzzle-fw"
cp ./nrf52-code/loopback-fw/target/thumbv7em-none-eabihf/release/loopback-fw "${OUTPUT_NAME}/nrf52-code/boards/dongle-fw/loopback-fw"
cp ./nrf52-code/loopback-fw-rtic/target/thumbv7em-none-eabihf/release/loopback-fw-rtic "${OUTPUT_NAME}/nrf52-code/boards/dongle-fw/loopback-fw-rtic"
find "${OUTPUT_NAME}" -name target -type d -print0 | xargs -0 rm -rf
zip -r "${OUTPUT_NAME}.zip" "${OUTPUT_NAME}"
1 change: 1 addition & 0 deletions nrf52-code/boards/dk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ hal = { package = "nrf52840-hal", version = "0.18.0" }
[features]
advanced = []
radio = []
usbd = []
28 changes: 23 additions & 5 deletions nrf52-code/boards/dk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use cortex_m_semihosting::debug;
use embedded_hal::digital::{OutputPin, StatefulOutputPin};
#[cfg(feature = "advanced")]
use grounded::uninit::GroundedArrayCell;
#[cfg(feature = "radio")]
#[cfg(any(feature = "radio", feature = "usbd"))]
use grounded::uninit::GroundedCell;
#[cfg(feature = "radio")]
pub use hal::ieee802154;
Expand All @@ -44,14 +44,18 @@ pub mod peripheral;
#[cfg(feature = "advanced")]
pub mod usbd;

#[cfg(feature = "radio")]
#[cfg(any(feature = "radio", feature = "usbd"))]
struct ClockSyncWrapper<H, L, LSTAT> {
clocks: Clocks<H, L, LSTAT>,
}

#[cfg(feature = "radio")]
#[cfg(any(feature = "radio", feature = "usbd"))]
unsafe impl<H, L, LSTAT> Sync for ClockSyncWrapper<H, L, LSTAT> {}

/// Our USB Device
#[cfg(feature = "usbd")]
pub type UsbDevice = hal::usbd::Usbd<hal::usbd::UsbPeripheral<'static>>;

/// Components on the board
pub struct Board {
/// LEDs
Expand All @@ -71,6 +75,9 @@ pub struct Board {
/// USB control endpoint 0
#[cfg(feature = "advanced")]
pub ep0in: Ep0In,
/// A configured USB Device
#[cfg(feature = "usbd")]
pub usbd: UsbDevice,
}

/// All LEDs on the board
Expand Down Expand Up @@ -300,7 +307,7 @@ pub fn init() -> Result<Board, Error> {
// NOTE: this branch runs at most once
#[cfg(feature = "advanced")]
static EP0IN_BUF: GroundedArrayCell<u8, 64> = GroundedArrayCell::const_init();
#[cfg(feature = "radio")]
#[cfg(any(feature = "radio", feature = "usbd"))]
// We need the wrapper to make this type Sync, as it contains raw pointers
static CLOCKS: GroundedCell<
ClockSyncWrapper<
Expand All @@ -317,7 +324,7 @@ pub fn init() -> Result<Board, Error> {
let clocks = clocks.start_lfclk();
let _clocks = clocks.enable_ext_hfosc();
// extend lifetime to `'static`
#[cfg(feature = "radio")]
#[cfg(any(feature = "radio", feature = "usbd"))]
let clocks = unsafe {
let clocks_ptr = CLOCKS.get();
clocks_ptr.write(ClockSyncWrapper { clocks: _clocks });
Expand Down Expand Up @@ -370,6 +377,15 @@ pub fn init() -> Result<Board, Error> {
radio
};

#[cfg(feature = "usbd")]
{
defmt::debug!("Enabling SOF interrupts...");
periph.USBD.inten.modify(|_r, w| {
w.sof().set_bit();
w
});
}

Ok(Board {
leds: Leds {
_1: Led { inner: led1pin },
Expand All @@ -386,6 +402,8 @@ pub fn init() -> Result<Board, Error> {
power: periph.POWER,
#[cfg(feature = "advanced")]
ep0in: unsafe { Ep0In::new(&EP0IN_BUF) },
#[cfg(feature = "usbd")]
usbd: UsbDevice::new(hal::usbd::UsbPeripheral::new(periph.USBD, clocks)),
})
}

Expand Down
23 changes: 23 additions & 0 deletions nrf52-code/loopback-fw-rtic/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[target.thumbv7em-none-eabihf]
# set custom cargo runner to flash & run on embedded target when we call `cargo run`
# for more information, check out https://github.com/probe-rs
runner = [
"probe-rs",
"run",
"--chip",
"nRF52840_xxAA",
"--allow-erase-all",
"--log-format",
"{t} {[{L}]%bold} {s} {({c:bold} {fff}:{l:1})%dimmed}"
]
# Or use "{[{L}]%bold} {s} {({c:bold} {fff}:{l:1})%dimmed}" to disable timestamps

linker = "flip-link" # adds stack overflow protection
rustflags = [
"-C", "link-arg=-Tlink.x", # use the cortex-m-rt linker script
"-C", "link-arg=-Tdefmt.x", # defmt support
]

[build]
# cross-compile to this target
target = "thumbv7em-none-eabihf" # = ARM Cortex-M4 with FPU
1 change: 1 addition & 0 deletions nrf52-code/loopback-fw-rtic/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
Loading

0 comments on commit 9d7a18a

Please sign in to comment.