Skip to content

Add defmt #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[target.thumbv8m.main-none-eabihf]
runner = 'arm-none-eabi-gdb'
runner = 'probe-rs run --connect-under-reset'
rustflags = [
# LLD (shipped with the Rust toolchain) is used as the default linker
"-C", "link-arg=-Tlink.x",
"-C", "link-arg=-Tdefmt.x",
]

[build]
Expand Down
9 changes: 6 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ jobs:
- stm32h562
- stm32h563
- stm32h573
logger:
- log
- defmt
env: # Peripheral Feature flags
FLAGS: rt,log
FLAGS: rt

steps:
- uses: actions/checkout@v4
Expand All @@ -42,6 +45,6 @@ jobs:
- name: Install thumbv8m rust target
run: rustup target add thumbv8m.main-none-eabihf
- name: Build
run: cargo build --verbose --release --examples --target thumbv8m.main-none-eabihf --features ${{ matrix.mcu }},${{ env.FLAGS }}
run: cargo build --verbose --release --examples --target thumbv8m.main-none-eabihf --features ${{ matrix.mcu }},${{ matrix.logger }},${{ env.FLAGS }}
- name: Test
run: cargo test --lib --target x86_64-unknown-linux-gnu --features ${{ matrix.mcu }},${{ env.FLAGS }}
run: cargo test --lib --target x86_64-unknown-linux-gnu --features ${{ matrix.mcu }},${{ matrix.logger }},${{ env.FLAGS }}
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ cortex-m = { version = "^0.7.7", features = ["critical-section-single-core"] }
stm32h5 = { package = "stm32h5", version = "0.16.0" }
fugit = "0.3.7"
embedded-hal = "1.0.0"
defmt = { version = "0.3.8", optional = true }
defmt = { version = "1.0.0", optional = true }
paste = "1.0.15"
log = { version = "0.4.20", optional = true}

[dev-dependencies]
log = { version = "0.4.20"}
cortex-m-rt = "0.7.3"
defmt-rtt = { version = "0.4.0" }
panic-halt = "0.2.0"
panic-rtt-target = { version = "0.1.0", features = ["cortex-m"] }
cfg-if = "1.0.0"
Expand All @@ -76,6 +77,7 @@ lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
cortex-m-log = { version = "0.8.0", features = ["itm", "semihosting", "log-integration"] }
cortex-m-semihosting = "0.5.0"
panic-itm = { version = "~0.4.1" }
panic-probe = "0.3.2"
panic-semihosting = "0.6"

[profile.release]
Expand All @@ -86,7 +88,6 @@ opt-level = "s" # optimize for binary size

[[example]]
name = "blinky"
required-features = ["stm32h503"]

[[example]]
name = "i2c"
Expand Down
5 changes: 3 additions & 2 deletions examples/blinky.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use cortex_m_rt::entry;
use embedded_hal::delay::DelayNs;
use fugit::SecsDurationU32;
use stm32h5xx_hal::{delay::Delay, pac, prelude::*};
use utilities::logger::info;

#[entry]
fn main() -> ! {
Expand All @@ -31,10 +32,10 @@ fn main() -> ! {

loop {
led.set_low();
log::info!("Off");
info!("Off");
delay.delay_ms(duration);
led.set_high();
log::info!("On");
info!("On");
delay.delay_ms(duration);
}
}
22 changes: 22 additions & 0 deletions examples/utilities/logger.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
#![cfg_attr(feature = "log-itm", allow(unsafe_code))]

cfg_if::cfg_if! {
if #[cfg(feature = "defmt")] {
#[allow(unused_imports)]
pub use defmt::{info, trace, warn, debug, error};

} else {
#[allow(unused_imports)]
pub use log::{info, trace, warn, debug, error};
}
}

cfg_if::cfg_if! {
if #[cfg(any(feature = "log-itm"))] {
use panic_itm as _;
Expand Down Expand Up @@ -32,6 +43,17 @@ cfg_if::cfg_if! {
}

}
else if #[cfg(feature = "defmt")] {
use defmt_rtt as _; // global logger
use panic_probe as _;
#[allow(unused_imports)]
pub use defmt::Logger;
#[allow(unused_imports)]
pub use defmt::println;

#[allow(dead_code)]
pub fn init() {}
}
else if #[cfg(any(feature = "log-rtt"))] {
use panic_rtt_target as _;

Expand Down
1 change: 1 addition & 0 deletions src/gpio/dynamic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum Dynamic {

/// Error for [DynamicPin]
#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum PinModeError {
/// For operations unsupported in current mode
IncorrectMode,
Expand Down
1 change: 1 addition & 0 deletions src/rcc/pll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ const VCO_RANGE_WIDE: VcoRange = VcoRange {
};

#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
struct PllOutput {
ck: u32,
div: u32,
Expand Down