Skip to content

Commit 2d12cda

Browse files
WratoxHenrik Snömanusbalbin
authored
Add defmt (#41)
Co-authored-by: Henrik Snöman <[email protected]> Co-authored-by: Albin Hedman <[email protected]>
1 parent 5549621 commit 2d12cda

File tree

7 files changed

+38
-8
lines changed

7 files changed

+38
-8
lines changed

.cargo/config.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
[target.thumbv8m.main-none-eabihf]
2-
runner = 'arm-none-eabi-gdb'
2+
runner = 'probe-rs run --connect-under-reset'
33
rustflags = [
44
# LLD (shipped with the Rust toolchain) is used as the default linker
55
"-C", "link-arg=-Tlink.x",
6+
"-C", "link-arg=-Tdefmt.x",
67
]
78

89
[build]

.github/workflows/ci.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ jobs:
2020
- stm32h562
2121
- stm32h563
2222
- stm32h573
23+
logger:
24+
- log
25+
- defmt
2326
env: # Peripheral Feature flags
24-
FLAGS: rt,log
27+
FLAGS: rt
2528

2629
steps:
2730
- uses: actions/checkout@v4
@@ -42,6 +45,6 @@ jobs:
4245
- name: Install thumbv8m rust target
4346
run: rustup target add thumbv8m.main-none-eabihf
4447
- name: Build
45-
run: cargo build --verbose --release --examples --target thumbv8m.main-none-eabihf --features ${{ matrix.mcu }},${{ env.FLAGS }}
48+
run: cargo build --verbose --release --examples --target thumbv8m.main-none-eabihf --features ${{ matrix.mcu }},${{ matrix.logger }},${{ env.FLAGS }}
4649
- name: Test
47-
run: cargo test --lib --target x86_64-unknown-linux-gnu --features ${{ matrix.mcu }},${{ env.FLAGS }}
50+
run: cargo test --lib --target x86_64-unknown-linux-gnu --features ${{ matrix.mcu }},${{ matrix.logger }},${{ env.FLAGS }}

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ cortex-m = { version = "^0.7.7", features = ["critical-section-single-core"] }
6161
stm32h5 = { package = "stm32h5", version = "0.16.0" }
6262
fugit = "0.3.7"
6363
embedded-hal = "1.0.0"
64-
defmt = { version = "0.3.8", optional = true }
64+
defmt = { version = "1.0.0", optional = true }
6565
paste = "1.0.15"
6666
log = { version = "0.4.20", optional = true}
6767

6868
[dev-dependencies]
6969
log = { version = "0.4.20"}
7070
cortex-m-rt = "0.7.3"
71+
defmt-rtt = { version = "0.4.0" }
7172
panic-halt = "0.2.0"
7273
panic-rtt-target = { version = "0.1.0", features = ["cortex-m"] }
7374
cfg-if = "1.0.0"
@@ -76,6 +77,7 @@ lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
7677
cortex-m-log = { version = "0.8.0", features = ["itm", "semihosting", "log-integration"] }
7778
cortex-m-semihosting = "0.5.0"
7879
panic-itm = { version = "~0.4.1" }
80+
panic-probe = "0.3.2"
7981
panic-semihosting = "0.6"
8082

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

8789
[[example]]
8890
name = "blinky"
89-
required-features = ["stm32h503"]
9091

9192
[[example]]
9293
name = "i2c"

examples/blinky.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use cortex_m_rt::entry;
88
use embedded_hal::delay::DelayNs;
99
use fugit::SecsDurationU32;
1010
use stm32h5xx_hal::{delay::Delay, pac, prelude::*};
11+
use utilities::logger::info;
1112

1213
#[entry]
1314
fn main() -> ! {
@@ -31,10 +32,10 @@ fn main() -> ! {
3132

3233
loop {
3334
led.set_low();
34-
log::info!("Off");
35+
info!("Off");
3536
delay.delay_ms(duration);
3637
led.set_high();
37-
log::info!("On");
38+
info!("On");
3839
delay.delay_ms(duration);
3940
}
4041
}

examples/utilities/logger.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
#![cfg_attr(feature = "log-itm", allow(unsafe_code))]
22

3+
cfg_if::cfg_if! {
4+
if #[cfg(feature = "defmt")] {
5+
#[allow(unused_imports)]
6+
pub use defmt::{info, trace, warn, debug, error};
7+
8+
} else {
9+
#[allow(unused_imports)]
10+
pub use log::{info, trace, warn, debug, error};
11+
}
12+
}
13+
314
cfg_if::cfg_if! {
415
if #[cfg(any(feature = "log-itm"))] {
516
use panic_itm as _;
@@ -32,6 +43,17 @@ cfg_if::cfg_if! {
3243
}
3344

3445
}
46+
else if #[cfg(feature = "defmt")] {
47+
use defmt_rtt as _; // global logger
48+
use panic_probe as _;
49+
#[allow(unused_imports)]
50+
pub use defmt::Logger;
51+
#[allow(unused_imports)]
52+
pub use defmt::println;
53+
54+
#[allow(dead_code)]
55+
pub fn init() {}
56+
}
3557
else if #[cfg(any(feature = "log-rtt"))] {
3658
use panic_rtt_target as _;
3759

src/gpio/dynamic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub enum Dynamic {
2525

2626
/// Error for [DynamicPin]
2727
#[derive(Debug, PartialEq, Eq)]
28+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
2829
pub enum PinModeError {
2930
/// For operations unsupported in current mode
3031
IncorrectMode,

src/rcc/pll.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ const VCO_RANGE_WIDE: VcoRange = VcoRange {
6161
};
6262

6363
#[derive(Debug)]
64+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
6465
struct PllOutput {
6566
ck: u32,
6667
div: u32,

0 commit comments

Comments
 (0)