Skip to content

Commit

Permalink
bsp-demo: Add defmt
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanpallant authored and listochkin committed Nov 28, 2024
1 parent 0d83e9f commit 0b322f8
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 36 deletions.
6 changes: 5 additions & 1 deletion example-code/nrf52/bsp_demo/.cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
target = "thumbv7em-none-eabihf"

[target.thumbv7em-none-eabihf]
runner = "probe-run --chip nRF52840_xxAA"
runner = "probe-rs run --chip nRF52840_xxAA --allow-erase-all"
rustflags = [
"-C", "link-arg=-Tlink.x",
"-C", "link-arg=-Tdefmt.x",
]

[env]
DEFMT_LOG = "debug"
40 changes: 21 additions & 19 deletions example-code/nrf52/bsp_demo/.vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
{
"version": "0.2.0",
"configurations": [
{
"preLaunchTask": "${defaultBuildTask}",
"type": "probe-rs-debug",
"request": "launch",
"name": "Run bsp_demo",
"flashingConfig": {
"flashingEnabled": true,
},
"chip": "nrf52840_xxaa",
"coreConfigs": [
{
"programBinary": "./target/thumbv7em-none-eabihf/debug/bsp_demo"
}
]
}
]
}
"version": "0.2.0",
"configurations": [
{
"preLaunchTask": "${defaultBuildTask}",
"type": "probe-rs-debug",
"request": "launch",
"name": "Run bsp_demo",
"flashingConfig": {
"flashingEnabled": true
},
"chip": "nrf52840_xxaa",
"coreConfigs": [
{
"coreIndex": 0,
"rttEnabled": true,
"programBinary": "./target/thumbv7em-none-eabihf/debug/bsp_demo"
}
],
}
]
}
117 changes: 112 additions & 5 deletions example-code/nrf52/bsp_demo/Cargo.lock

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

5 changes: 4 additions & 1 deletion example-code/nrf52/bsp_demo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ edition = "2021"

[dependencies]
cortex-m-rt = "0.6"
cortex-m = "0.6"
cortex-m = { version = "0.7", features = ["critical-section-single-core"] }
nrf52840-dk-bsp = { version = "0.2.0", features = [ "rt" ] }
defmt = "0.3"
defmt-rtt = "0.4"
critical-section = "1.2.0"
43 changes: 33 additions & 10 deletions example-code/nrf52/bsp_demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,50 @@
//! A simple LED blinking demo for nRF52840-DK
//!
//! Demonstrates using a Board Support Package, defmt, and the probe-rs extension for VS Code

#![no_std]
#![no_main]

use core::fmt::Write;
use cortex_m_rt::entry;
use nrf52840_dk_bsp::{hal, Board};

extern crate defmt_rtt;

#[entry]
fn main() -> ! {
let mut nrf52 = Board::take().unwrap();
let mut timer = hal::Timer::new(nrf52.TIMER0);
// We do some hardware set-up first
defmt::info!("Starting up...");
let mut board = Board::take().unwrap();
let mut timer = hal::Timer::new(board.TIMER0);

// Now go into an infinite loop
loop {
writeln!(nrf52.cdc, "On!").unwrap();
nrf52.leds.led_2.enable();
// We're logging over both defmt, and the J-Link's UART to USB Serial
// Port interface.
defmt::debug!("On!");
writeln!(board.cdc, "On!").unwrap();

// Control the LED
board.leds.led_2.enable();

// Wait for 1 second (this is a 1 MHz timer)
timer.delay(1_000_000);
writeln!(nrf52.cdc, "Off!").unwrap();
nrf52.leds.led_2.disable();

// More logging
defmt::debug!("Off!");
writeln!(board.cdc, "Off!").unwrap();

// Control the LED
board.leds.led_2.disable();

// Wait for 1 second again
timer.delay(1_000_000);
}
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
loop {
cortex_m::asm::udf();
}
fn panic(info: &core::panic::PanicInfo) -> ! {
defmt::error!("Panic! {:?}", defmt::Debug2Format(&info));
cortex_m::asm::udf();
}

0 comments on commit 0b322f8

Please sign in to comment.