diff --git a/.cargo/config b/.cargo/config.toml similarity index 90% rename from .cargo/config rename to .cargo/config.toml index 18401e9c..55d91200 100644 --- a/.cargo/config +++ b/.cargo/config.toml @@ -5,7 +5,8 @@ [target.'cfg(all(target_arch = "arm", target_os = "none"))'] # uncomment ONE of these three option to make `cargo run` start a GDB session # which option to pick depends on your system -runner = "arm-none-eabi-gdb -q -x openocd.gdb" +runner = "probe-run" +# runner = "arm-none-eabi-gdb -q -x openocd.gdb" # runner = "gdb-multiarch -q -x openocd.gdb" # runner = "gdb -q -x openocd.gdb" @@ -23,6 +24,9 @@ rustflags = [ # "-C", "linker=arm-none-eabi-gcc", # "-C", "link-arg=-Wl,-Tlink.x", # "-C", "link-arg=-nostartfiles", + + # for defmt in examples + "-C", "link-arg=-Tdefmt.x", ] [build] diff --git a/Cargo.toml b/Cargo.toml index c2ceaec0..9a2b4369 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -108,28 +108,21 @@ stm32l4r9 = [ "stm32l4/stm32l4r9" ] # PAC has an L4r9 specific variation stm32l4s9 = [ "stm32l4/stm32l4r9" ] [dev-dependencies] -panic-halt = "0.2.0" -panic-semihosting = "0.5.0" -cortex-m-semihosting = "0.3.5" cortex-m-rt = "0.7" usb-device = "0.2.3" usbd-serial = "0.1.0" -heapless = "0.5" +heapless = "0.7" +defmt = "0.3" +defmt-rtt = "0.3" +panic-probe = { version = "0.3", features = [ "print-defmt" ] } [dev-dependencies.cortex-m-rtic] version = "0.5.9" default-features = false features = ["cortex-m-7"] -[dev-dependencies.panic-rtt-target] -version = "0.1.1" -features = ["cortex-m"] - -[dev-dependencies.rtt-target] -version = "0.2.2" -features = ["cortex-m"] - [profile.dev] +debug = true incremental = false codegen-units = 1 diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 00000000..f6df57a6 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,11 @@ +# Running examples +The easiest way to run examples is with the probe-run cargo extension. +See the [Installation](https://github.com/knurling-rs/probe-run#installation) section of the probe-run readme for details. + +Examples will print messages using RTT for transport. Probe-run will open the debug log once it has completed flashing the firmware. Use probe-run --list-chips and find the correct ID for your MCU + +Running the blinky example on a L433 Nucleo +```sh +# arguments after the "-- " are to tell probe-run which MCU we are flashing the image to +cargo run --features=rt,stm32l433 --example=blinky -- --chip STM32L433RCTx +``` \ No newline at end of file diff --git a/examples/adc.rs b/examples/adc.rs index 8b1f821e..ca699fee 100644 --- a/examples/adc.rs +++ b/examples/adc.rs @@ -1,16 +1,15 @@ #![no_main] #![no_std] -use panic_rtt_target as _; - use cortex_m_rt::entry; -use rtt_target::{rprint, rprintln}; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; use stm32l4xx_hal::{adc::ADC, delay::Delay, pac, prelude::*}; #[entry] fn main() -> ! { - rtt_target::rtt_init_print!(); - rprint!("Initializing..."); + println!("Initializing..."); let cp = pac::CorePeripherals::take().unwrap(); let dp = pac::Peripherals::take().unwrap(); @@ -33,10 +32,10 @@ fn main() -> ! { let mut gpioc = dp.GPIOC.split(&mut rcc.ahb2); let mut a1 = gpioc.pc0.into_analog(&mut gpioc.moder, &mut gpioc.pupdr); - rprintln!(" done."); + println!("Initializing done."); loop { let value = adc.read(&mut a1).unwrap(); - rprintln!("Value: {}", value); + println!("Value: {}", value); } } diff --git a/examples/adc_dma.rs b/examples/adc_dma.rs index 8e9d8016..6d07c02f 100644 --- a/examples/adc_dma.rs +++ b/examples/adc_dma.rs @@ -1,8 +1,10 @@ #![no_main] #![no_std] -use panic_rtt_target as _; -use rtt_target::{rprintln, rtt_init_print}; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; +use rtic::app; use stm32l4xx_hal::{ adc::{DmaMode, SampleTime, Sequence, ADC}, delay::DelayCM, @@ -10,8 +12,6 @@ use stm32l4xx_hal::{ prelude::*, }; -use rtic::app; - const SEQUENCE_LEN: usize = 3; #[app(device = stm32l4xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] @@ -29,9 +29,7 @@ const APP: () = { unsafe { &mut MEMORY } }; - rtt_init_print!(); - - rprintln!("Hello from init!"); + println!("Hello from init!"); let cp = cx.core; let mut dcb = cp.DCB; @@ -90,7 +88,7 @@ const APP: () = { let transfer = cx.resources.transfer; if let Some(transfer_val) = transfer.take() { let (buffer, rx_dma) = transfer_val.wait(); - rprintln!("DMA measurements: {:?}", buffer); + println!("DMA measurements: {:?}", buffer); *transfer = Some(Transfer::from_adc_dma( rx_dma, buffer, diff --git a/examples/blinky.rs b/examples/blinky.rs index f7faf35d..b4c89d3a 100644 --- a/examples/blinky.rs +++ b/examples/blinky.rs @@ -1,61 +1,46 @@ //! Blinks an LED - #![no_std] #![no_main] -extern crate cortex_m; -#[macro_use] -extern crate cortex_m_rt as rt; -extern crate cortex_m_semihosting as sh; -extern crate panic_semihosting; -extern crate stm32l4xx_hal as hal; -// #[macro_use(block)] -// extern crate nb; - -use crate::hal::delay::Delay; -use crate::hal::prelude::*; -use crate::rt::entry; -use crate::rt::ExceptionFrame; - -use crate::sh::hio; -use core::fmt::Write; +use cortex_m_rt::{entry, exception, ExceptionFrame}; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; +use stm32l4xx_hal as hal; // hal +use stm32l4xx_hal::{delay::Delay, prelude::*}; #[entry] fn main() -> ! { - let mut hstdout = hio::hstdout().unwrap(); - - writeln!(hstdout, "Hello, world!").unwrap(); + println!("Hello, world!"); let cp = cortex_m::Peripherals::take().unwrap(); let dp = hal::stm32::Peripherals::take().unwrap(); - let mut flash = dp.FLASH.constrain(); // .constrain(); + let mut flash = dp.FLASH.constrain(); let mut rcc = dp.RCC.constrain(); let mut pwr = dp.PWR.constrain(&mut rcc.apb1r1); // Try a different clock configuration - let clocks = rcc.cfgr.hclk(8.MHz()).freeze(&mut flash.acr, &mut pwr); + let clocks = rcc.cfgr.freeze(&mut flash.acr, &mut pwr); // let clocks = rcc.cfgr // .sysclk(64.MHz()) // .pclk1(32.MHz()) // .freeze(&mut flash.acr); - // let mut gpioc = dp.GPIOC.split(&mut rcc.ahb2); - // let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.afrh); - let mut gpiob = dp.GPIOB.split(&mut rcc.ahb2); let mut led = gpiob .pb3 .into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper); + // simple blink loop counting clock cycles for delays let mut timer = Delay::new(cp.SYST, clocks); loop { - // block!(timer.wait()).unwrap(); timer.delay_ms(1000_u32); led.set_high(); - // block!(timer.wait()).unwrap(); + println!("LED on"); timer.delay_ms(1000_u32); led.set_low(); + println!("LED off"); } } diff --git a/examples/can-loopback.rs b/examples/can-loopback.rs index 40dc939e..982ad388 100644 --- a/examples/can-loopback.rs +++ b/examples/can-loopback.rs @@ -1,7 +1,4 @@ //! Run the bxCAN peripheral in loopback mode. - -#![deny(unsafe_code)] -#![deny(warnings)] #![no_main] #![no_std] @@ -9,17 +6,16 @@ use bxcan::{ filter::Mask32, {Frame, StandardId}, }; -use panic_halt as _; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; use rtic::app; -use rtt_target::{rprintln, rtt_init_print}; use stm32l4xx_hal::{can::Can, prelude::*}; #[app(device = stm32l4xx_hal::stm32, peripherals = true)] const APP: () = { #[init] fn init(cx: init::Context) { - rtt_init_print!(); - let dp = cx.device; let mut flash = dp.FLASH.constrain(); @@ -30,7 +26,7 @@ const APP: () = { // Set the clocks to 80 MHz let _clocks = rcc.cfgr.sysclk(80.MHz()).freeze(&mut flash.acr, &mut pwr); - rprintln!(" - CAN init"); + println!(" - CAN init"); let can = { let rx = @@ -79,12 +75,15 @@ const APP: () = { // Wait for TX to finish while !can.is_transmitter_idle() {} - rprintln!(" - CAN tx complete: {:?}", test_frame); + println!( + " - CAN tx complete: {:?}", + defmt::Debug2Format(&test_frame) + ); // Receive the packet back let r = can.receive(); - rprintln!(" - CAN rx {:?}", r); + println!(" - CAN rx {:?}", defmt::Debug2Format(&r)); assert_eq!(Ok(test_frame), r); } diff --git a/examples/i2c_write.rs b/examples/i2c_write.rs index 2f141535..7f386878 100644 --- a/examples/i2c_write.rs +++ b/examples/i2c_write.rs @@ -1,32 +1,19 @@ //! Blinks an LED - #![no_std] #![no_main] -extern crate cortex_m; -#[macro_use] -extern crate cortex_m_rt as rt; -extern crate cortex_m_semihosting as sh; -extern crate panic_semihosting; -extern crate stm32l4xx_hal as hal; - -use crate::hal::prelude::*; - -use crate::hal::i2c; -use crate::hal::i2c::I2c; -use crate::rt::entry; -use crate::rt::ExceptionFrame; - -use crate::sh::hio; -use core::fmt::Write; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use hal::{ + i2c::{self, I2c}, + prelude::*, +}; +use panic_probe as _; +use stm32l4xx_hal as hal; #[entry] fn main() -> ! { - let mut hstdout = hio::hstdout().unwrap(); - - // writeln!(hstdout, "Hello, world!").unwrap(); - - // let cp = cortex_m::Peripherals::take().unwrap(); let dp = hal::stm32::Peripherals::take().unwrap(); let mut flash = dp.FLASH.constrain(); @@ -56,32 +43,31 @@ fn main() -> ! { &mut rcc.apb1r1, ); - // i2c.write(0x3C, &[0xCC, 0xAA]).unwrap(); let mut buffer = [0u8; 2]; - // 0x08 is version reg - // i2c.write(0x6C, &[0x08],).unwrap(); - // let val = i2c.read(0x36, &mut buffer).unwrap(); + const MAX17048_ADDR: u8 = 0x6C; - i2c.write_read(MAX17048_ADDR, &[0x08], &mut buffer).unwrap(); - let version: u16 = (buffer[0] as u16) << 8 | buffer[1] as u16; - writeln!(hstdout, "Silicon Version: {}", version).ok(); + // read two bytes starting from version register high byte + const MAX17048_VERSION_REG: u8 = 0x08; + i2c.write_read(MAX17048_ADDR, &[MAX17048_VERSION_REG], &mut buffer) + .unwrap(); + let version: u16 = u16::from_be_bytes(buffer); // (buffer[0] as u16) << 8 | buffer[1] as u16; + println!("Silicon Version: {}", version); // let soc: u16 = (buffer[0] as u16) + (buffer[1] as u16 / 256); //& 0xFF00 // let soc: u16 = (buffer[0] as u16) << 8 & 0xFF00 | (buffer[1] as u16) & 0x00FF; - i2c.write_read(MAX17048_ADDR, &[0x04], &mut buffer).unwrap(); - let soc: u16 = (buffer[0] as u16) << 8 | buffer[1] as u16; - writeln!(hstdout, "Batt SoC: {}%", soc / 256).ok(); - - i2c.write_read(MAX17048_ADDR, &[0x02], &mut buffer).unwrap(); - let vlt: u16 = (buffer[0] as u16) << 8 | buffer[1] as u16; - writeln!(hstdout, "Volt: {}", vlt as f32 * 0.000078125).ok(); + const MAX17048_SOC_REG: u8 = 0x04; + i2c.write_read(MAX17048_ADDR, &[MAX17048_SOC_REG], &mut buffer) + .unwrap(); + let soc: u16 = u16::from_be_bytes(buffer); + println!("Batt SoC: {}%", soc / 256); + + const MAX17048_VOLT_REG: u8 = 0x02; + i2c.write_read(MAX17048_ADDR, &[MAX17048_VOLT_REG], &mut buffer) + .unwrap(); + let vlt: u16 = u16::from_be_bytes(buffer); + println!("Volt: {}", vlt as f32 * 0.000078125); loop { continue; } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/irq_button.rs b/examples/irq_button.rs index 60cc27b9..3f7c21cc 100644 --- a/examples/irq_button.rs +++ b/examples/irq_button.rs @@ -1,67 +1,59 @@ #![no_std] #![no_main] -extern crate cortex_m; -extern crate cortex_m_rt as rt; -extern crate panic_semihosting; -extern crate stm32l4xx_hal as hal; - -use crate::hal::{ +use core::{cell::RefCell, ops::DerefMut}; +use cortex_m::{ + interrupt::{free, Mutex}, + peripheral::NVIC, +}; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; +use stm32l4xx_hal::{ gpio::{gpioc::PC13, Edge, ExtiPin, Input, PullUp}, interrupt, prelude::*, stm32, }; -use core::cell::RefCell; -use core::ops::DerefMut; -use cortex_m::{ - interrupt::{free, Mutex}, - peripheral::NVIC, -}; -use rt::entry; // Set up global state. It's all mutexed up for concurrency safety. static BUTTON: Mutex>>>> = Mutex::new(RefCell::new(None)); #[entry] fn main() -> ! { - if let Some(mut dp) = stm32::Peripherals::take() { - dp.RCC.apb2enr.write(|w| w.syscfgen().set_bit()); - - let mut rcc = dp.RCC.constrain(); - let mut flash = dp.FLASH.constrain(); // .constrain(); - let mut pwr = dp.PWR.constrain(&mut rcc.apb1r1); + let mut dp = stm32::Peripherals::take().unwrap(); + dp.RCC.apb2enr.write(|w| w.syscfgen().set_bit()); - rcc.cfgr - .hclk(48.MHz()) - .sysclk(80.MHz()) - .pclk1(24.MHz()) - .pclk2(24.MHz()) - .freeze(&mut flash.acr, &mut pwr); + let mut rcc = dp.RCC.constrain(); + let mut flash = dp.FLASH.constrain(); + let mut pwr = dp.PWR.constrain(&mut rcc.apb1r1); - // Create a button input with an interrupt - let mut gpioc = dp.GPIOC.split(&mut rcc.ahb2); - let mut board_btn = gpioc - .pc13 - .into_pull_up_input(&mut gpioc.moder, &mut gpioc.pupdr); - board_btn.make_interrupt_source(&mut dp.SYSCFG, &mut rcc.apb2); - board_btn.enable_interrupt(&mut dp.EXTI); - board_btn.trigger_on_edge(&mut dp.EXTI, Edge::Falling); - - // Enable interrupts - unsafe { - NVIC::unmask(stm32::Interrupt::EXTI15_10); - } + rcc.cfgr + .hclk(48.MHz()) + .sysclk(80.MHz()) + .pclk1(24.MHz()) + .pclk2(24.MHz()) + .freeze(&mut flash.acr, &mut pwr); - free(|cs| { - BUTTON.borrow(cs).replace(Some(board_btn)); - }); + // Create a button input with an interrupt + let mut gpioc = dp.GPIOC.split(&mut rcc.ahb2); + let mut board_btn = gpioc + .pc13 + .into_pull_up_input(&mut gpioc.moder, &mut gpioc.pupdr); + board_btn.make_interrupt_source(&mut dp.SYSCFG, &mut rcc.apb2); + board_btn.enable_interrupt(&mut dp.EXTI); + board_btn.trigger_on_edge(&mut dp.EXTI, Edge::Falling); - loop { - continue; - } + // Enable interrupts + unsafe { + NVIC::unmask(stm32::Interrupt::EXTI15_10); } + free(|cs| { + BUTTON.borrow(cs).replace(Some(board_btn)); + }); + loop { continue; } @@ -73,6 +65,7 @@ fn EXTI15_10() { let mut btn_ref = BUTTON.borrow(cs).borrow_mut(); if let Some(ref mut btn) = btn_ref.deref_mut() { if btn.check_interrupt() { + println!("button pressed"); // if we don't clear this bit, the ISR would trigger indefinitely btn.clear_interrupt_pending_bit(); } diff --git a/examples/lptim_rtic.rs b/examples/lptim_rtic.rs index 5c2c0f26..a2ec97e1 100644 --- a/examples/lptim_rtic.rs +++ b/examples/lptim_rtic.rs @@ -4,9 +4,10 @@ #![deny(unsafe_code)] #![no_main] #![no_std] -extern crate panic_rtt_target; -use rtt_target::rprintln; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; use stm32l4xx_hal::{ flash::ACR, gpio::{gpiob::PB13, Output, PinState, PushPull}, @@ -36,9 +37,7 @@ const APP: () = { #[init] fn init(ctx: init::Context) -> init::LateResources { - rtt_target::rtt_init_print!(); - - rprintln!("Init start"); + println!("Init start"); let device = ctx.device; // Configure the clock. let mut rcc = device.RCC.constrain(); @@ -53,7 +52,7 @@ const APP: () = { &mut gpiob.otyper, PinState::Low, ); - rprintln!("Clocks = {:#?}", clocks); + println!("Clocks = {:#?}", defmt::Debug2Format(&clocks)); let lptim_config = LowPowerTimerConfig::default() .clock_source(ClockSource::LSE) .prescaler(PreScaler::U1) @@ -74,7 +73,7 @@ const APP: () = { let timer_tick::Resources { lptim, led } = ctx.resources; if lptim.is_event_triggered(Event::AutoReloadMatch) { lptim.clear_event_flag(Event::AutoReloadMatch); - rprintln!("LPTIM1 tick"); + println!("LPTIM1 tick"); led.toggle(); } diff --git a/examples/otg_fs_serial.rs b/examples/otg_fs_serial.rs index 52513420..d6a38841 100644 --- a/examples/otg_fs_serial.rs +++ b/examples/otg_fs_serial.rs @@ -4,16 +4,17 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_rt::entry; -use stm32l4xx_hal::gpio::Speed; -use stm32l4xx_hal::otg_fs::{UsbBus, USB}; -use stm32l4xx_hal::prelude::*; -use stm32l4xx_hal::rcc::{ - ClockSecuritySystem, CrystalBypass, MsiFreq, PllConfig, PllDivider, PllSource, +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; +use stm32l4xx_hal::{ + gpio::Speed, + otg_fs::{UsbBus, USB}, + prelude::*, + rcc::{ClockSecuritySystem, CrystalBypass, MsiFreq, PllConfig, PllDivider, PllSource}, + stm32::{Peripherals, CRS, PWR, RCC}, }; -use stm32l4xx_hal::stm32::{Peripherals, CRS, PWR, RCC}; use usb_device::prelude::*; /// Enable CRS (Clock Recovery System) @@ -114,8 +115,7 @@ unsafe fn main() -> ! { .device_class(usbd_serial::USB_CLASS_CDC) .build(); - #[cfg(feature = "semihosting")] - hprintln!("Polling!").ok(); + println!("Polling!"); loop { if !usb_dev.poll(&mut [&mut usb_serial]) { @@ -127,6 +127,7 @@ unsafe fn main() -> ! { match usb_serial.read(&mut buf) { Ok(count) if count > 0 => { // Echo back in upper case + println!("received {}", defmt::Debug2Format(&buf[0..count])); for c in buf[0..count].iter_mut() { if 0x61 <= *c && *c <= 0x7a { *c &= !0x20; diff --git a/examples/pll_config.rs b/examples/pll_config.rs index 130eb99e..f950fe2f 100644 --- a/examples/pll_config.rs +++ b/examples/pll_config.rs @@ -1,25 +1,18 @@ //! Test the serial interface //! //! This example requires you to short (connect) the TX and RX pins. -#![deny(warnings)] #![no_main] #![no_std] -extern crate cortex_m; -#[macro_use(entry, exception)] -extern crate cortex_m_rt as rt; -#[macro_use(block)] -extern crate nb; -extern crate panic_semihosting; - -extern crate stm32l4xx_hal as hal; -// #[macro_use(block)] -// extern crate nb; - -use crate::hal::prelude::*; -use crate::hal::serial::{Config, Serial}; -use crate::rt::ExceptionFrame; -use cortex_m::asm; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; +use stm32l4xx_hal as hal; +use stm32l4xx_hal::{ + prelude::*, + serial::{Config, Serial}, +}; #[entry] fn main() -> ! { @@ -66,23 +59,13 @@ fn main() -> ! { let sent = b'X'; // The `block!` macro makes an operation block until it finishes - // NOTE the error type is `!` - - block!(tx.write(sent)).ok(); - - let received = block!(rx.read()).unwrap(); + nb::block!(tx.write(sent)).unwrap(); + let received = nb::block!(rx.read()).unwrap(); - assert_eq!(received, sent); - - // if all goes well you should reach this breakpoint - asm::bkpt(); + defmt::assert_eq!(received, sent); + println!("message was received"); loop { continue; } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/pwm.rs b/examples/pwm.rs index e64556ca..560e6be5 100644 --- a/examples/pwm.rs +++ b/examples/pwm.rs @@ -1,14 +1,11 @@ //! Testing PWM output - -#![deny(unsafe_code)] -#![deny(warnings)] #![no_main] #![no_std] -extern crate panic_halt; - -// use cortex_m::asm; use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; use stm32l4xx_hal::{delay, prelude::*, stm32}; #[entry] @@ -53,24 +50,24 @@ fn main() -> ! { // NB: if the pins are LEDs, brightness is not // linear in duty value. loop { + println!("100%"); pwm.set_duty(max); timer.delay_ms(second); - // asm::bkpt(); + println!("91%"); pwm.set_duty(max / 11 * 10); timer.delay_ms(second); - // asm::bkpt(); + println!("75%"); pwm.set_duty(3 * max / 4); timer.delay_ms(second); - // asm::bkpt(); + println!("50%"); pwm.set_duty(max / 2); timer.delay_ms(second); - // asm::bkpt(); + println!("25%"); pwm.set_duty(max / 4); timer.delay_ms(second); - // asm::bkpt(); } } diff --git a/examples/qspi.rs b/examples/qspi.rs index e383b546..4e422c02 100644 --- a/examples/qspi.rs +++ b/examples/qspi.rs @@ -4,21 +4,15 @@ #![no_main] #![no_std] -extern crate cortex_m; -#[macro_use(entry, exception)] -extern crate cortex_m_rt as rt; -// #[macro_use(block)] -extern crate nb; -extern crate panic_semihosting; - -extern crate stm32l4xx_hal as hal; -// #[macro_use(block)] -// extern crate nb; - -use crate::hal::prelude::*; -use crate::hal::qspi::{Qspi, QspiConfig, QspiMode, QspiReadCommand}; -use crate::rt::ExceptionFrame; -use cortex_m::asm; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; +use stm32l4xx_hal as hal; +use stm32l4xx_hal::{ + prelude::*, + qspi::{Qspi, QspiConfig, QspiMode, QspiReadCommand}, +}; #[entry] fn main() -> ! { @@ -77,15 +71,9 @@ fn main() -> ! { qspi.transfer(get_id_command, &mut id_arr).unwrap(); - // if all goes well you should reach this breakpoint - asm::bkpt(); + println!("QSPI transfer complete"); loop { continue; } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/rng.rs b/examples/rng.rs index 1c3b9477..f7c4a754 100644 --- a/examples/rng.rs +++ b/examples/rng.rs @@ -1,32 +1,11 @@ #![no_std] #![no_main] -extern crate panic_halt; -extern crate stm32l4xx_hal as hal; - -use core::fmt; use cortex_m_rt::entry; - -use crate::hal::delay::Delay; -use crate::hal::prelude::*; -use crate::hal::serial::{Config, Serial}; -use crate::hal::stm32; -use hal::hal::blocking::rng::Read; - -macro_rules! uprint { - ($serial:expr, $($arg:tt)*) => { - fmt::write($serial, format_args!($($arg)*)).ok() - }; -} - -macro_rules! uprintln { - ($serial:expr, $fmt:expr) => { - uprint!($serial, concat!($fmt, "\n")) - }; - ($serial:expr, $fmt:expr, $($arg:tt)*) => { - uprint!($serial, concat!($fmt, "\n"), $($arg)*) - }; -} +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; +use stm32l4xx_hal::{delay::Delay, hal::blocking::rng::Read, prelude::*, stm32}; #[entry] fn main() -> ! { @@ -44,32 +23,13 @@ fn main() -> ! { .pclk1(32.MHz()) .freeze(&mut flash.acr, &mut pwr); - // setup usart - let mut gpioa = device.GPIOA.split(&mut rcc.ahb2); - let tx = gpioa - .pa9 - .into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh); - let rx = gpioa - .pa10 - .into_alternate(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrh); - - let baud_rate = 9_600; // 115_200; - let serial = Serial::usart1( - device.USART1, - (tx, rx), - Config::default().baudrate(baud_rate.bps()), - clocks, - &mut rcc.apb2, - ); - let (mut tx, _) = serial.split(); - // get a timer let mut timer = Delay::new(core.SYST, clocks); // setup rng let mut rng = device.RNG.enable(&mut rcc.ahb2, clocks); - uprintln!(&mut tx, "{:?}", clocks); + println!("{:?}", defmt::Debug2Format(&clocks)); let some_time: u32 = 500; loop { @@ -77,7 +37,7 @@ fn main() -> ! { let mut random_bytes = [0u8; N]; rng.read(&mut random_bytes) .expect("missing random data for some reason"); - uprintln!(&mut tx, "{} random u8 values: {:?}", N, random_bytes); + println!("{} random u8 values: {:?}", N, random_bytes); timer.delay_ms(some_time); } diff --git a/examples/rtc.rs b/examples/rtc.rs index d275ab7f..01335adc 100644 --- a/examples/rtc.rs +++ b/examples/rtc.rs @@ -3,30 +3,22 @@ #![no_std] #![no_main] -extern crate cortex_m; -#[macro_use] -extern crate cortex_m_rt as rt; -extern crate cortex_m_semihosting as sh; -extern crate panic_semihosting; -extern crate stm32l4xx_hal as hal; -// #[macro_use(block)] -// extern crate nb; - -use crate::hal::datetime::{Date, Time}; -use crate::hal::delay::Delay; -use crate::hal::prelude::*; -use crate::hal::rcc::{ClockSecuritySystem, CrystalBypass}; -use crate::hal::rtc::{Rtc, RtcClockSource, RtcConfig}; -use crate::rt::ExceptionFrame; - -use crate::sh::hio; -use core::fmt::Write; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; +use stm32l4xx_hal::{ + self as hal, + datetime::{Date, Time}, + delay::Delay, + prelude::*, + rcc::{ClockSecuritySystem, CrystalBypass}, + rtc::{Rtc, RtcClockSource, RtcConfig}, +}; #[entry] fn main() -> ! { - let mut hstdout = hio::hstdout().unwrap(); - - writeln!(hstdout, "Hello, world!").unwrap(); + println!("Hello, world!"); let cp = cortex_m::Peripherals::take().unwrap(); let dp = hal::stm32::Peripherals::take().unwrap(); @@ -62,15 +54,10 @@ fn main() -> ! { let (rtc_date, rtc_time) = rtc.get_date_time(); - writeln!(hstdout, "Time: {:?}", rtc_time).unwrap(); - writeln!(hstdout, "Date: {:?}", rtc_date).unwrap(); - writeln!(hstdout, "Good bye!").unwrap(); + println!("Time: {:?}", defmt::Debug2Format(&rtc_time)); + println!("Date: {:?}", defmt::Debug2Format(&rtc_date)); + println!("Good bye!"); loop { continue; } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/rtc_alarm.rs b/examples/rtc_alarm.rs index 03096d42..519fa1fe 100644 --- a/examples/rtc_alarm.rs +++ b/examples/rtc_alarm.rs @@ -3,33 +3,27 @@ #![no_std] #![no_main] -extern crate cortex_m; -#[macro_use] -extern crate cortex_m_rt as rt; -extern crate cortex_m_semihosting as sh; -extern crate panic_semihosting; -extern crate stm32l4xx_hal as hal; - -use crate::hal::datetime::{Date, Time}; -use crate::hal::prelude::*; -use crate::hal::rcc::{ClockSecuritySystem, CrystalBypass}; -use crate::hal::rtc::{Event, Rtc, RtcClockSource, RtcConfig}; -use crate::rt::ExceptionFrame; +use core::{cell::RefCell, ops::DerefMut}; use cortex_m::interrupt::{free, Mutex}; - -use crate::sh::hio; -use core::{cell::RefCell, fmt::Write, ops::DerefMut}; -use hal::interrupt; -use hal::pac; -use pac::NVIC; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; +use stm32l4xx_hal::{ + self as hal, + datetime::{Date, Time}, + device::NVIC, + interrupt, + prelude::*, + rcc::{ClockSecuritySystem, CrystalBypass}, + rtc::{Event, Rtc, RtcClockSource, RtcConfig}, +}; static RTC: Mutex>> = Mutex::new(RefCell::new(None)); #[entry] fn main() -> ! { - let mut hstdout = hio::hstdout().unwrap(); - - writeln!(hstdout, "Hello, world!").unwrap(); + println!("Hello, world!"); let mut dp = hal::stm32::Peripherals::take().unwrap(); dp.RCC.apb2enr.write(|w| w.syscfgen().set_bit()); @@ -65,7 +59,7 @@ fn main() -> ! { rtc.listen(&mut dp.EXTI, Event::WakeupTimer); unsafe { - NVIC::unmask(pac::Interrupt::RTC_WKUP); + NVIC::unmask(hal::pac::Interrupt::RTC_WKUP); } free(|cs| { @@ -79,18 +73,12 @@ fn main() -> ! { #[interrupt] fn RTC_WKUP() { - let mut hstdout = hio::hstdout().unwrap(); free(|cs| { let mut rtc_ref = RTC.borrow(cs).borrow_mut(); if let Some(ref mut rtc) = rtc_ref.deref_mut() { if rtc.check_interrupt(Event::WakeupTimer, true) { - writeln!(hstdout, "RTC Wakeup!").unwrap(); + println!("RTC Wakeup!"); } } }); } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/rtic_frame_serial_dma.rs b/examples/rtic_frame_serial_dma.rs index 04742658..f8fb9e95 100644 --- a/examples/rtic_frame_serial_dma.rs +++ b/examples/rtic_frame_serial_dma.rs @@ -3,29 +3,26 @@ //! //! This is tested on Nucleo-64 STM32L412 over the debuggers VCP. //! -//! This example only compiles for some targets so it is not part of the CI for now. #![deny(unsafe_code)] -// #![deny(warnings)] #![no_main] #![no_std] -use hal::{ - dma::{self, DMAFrame, FrameReader, FrameSender}, - pac::USART2, - prelude::*, - rcc::{ClockSecuritySystem, CrystalBypass, MsiFreq}, - serial::{self, Config, Serial}, -}; +use defmt::println; +use defmt_rtt as _; use heapless::{ pool, pool::singleton::{Box, Pool}, }; -use panic_halt as _; +use panic_probe as _; use rtic::app; -use stm32l4xx_hal as hal; -use stm32l4xx_hal::dma::{RxDma, TxDma}; -use stm32l4xx_hal::serial::{Rx, Tx}; +use stm32l4xx_hal::{ + dma::{self, DMAFrame, FrameReader, FrameSender, RxDma, TxDma}, + pac::USART2, + prelude::*, + rcc::{ClockSecuritySystem, CrystalBypass, MsiFreq}, + serial::{self, Config, Rx, Serial, Tx}, +}; // The pool gives out `Box`s that can hold 8 bytes pool!( @@ -115,6 +112,7 @@ const APP: () = { fn serial_isr(cx: serial_isr::Context) { // Check for character match if cx.resources.frame_reader.check_character_match(true) { + println!("character match"); if let Some(dma_buf) = SerialDMAPool::alloc() { let dma_buf = dma_buf.init(DMAFrame::new()); let buf = cx.resources.frame_reader.character_match_interrupt(dma_buf); @@ -133,7 +131,9 @@ const APP: () = { if let Some(dma_buf) = SerialDMAPool::alloc() { let dma_buf = dma_buf.init(DMAFrame::new()); - // Erroneous packet as it did not fit in a buffer, throw away the buffer + println!( + "received erroneous packet as it did not fit in a buffer, throw away the buffer" + ); let _buf = cx .resources .frame_reader @@ -147,7 +147,7 @@ const APP: () = { let fs = cx.resources.frame_sender; if let Some(_buf) = fs.transfer_complete_interrupt() { - // Frame sent, drop the buffer to return it too the pool + println!("Frame sent, drop the buffer to return it too the pool"); } // Send a new buffer diff --git a/examples/serial.rs b/examples/serial.rs index fe7949dd..f48ed75c 100644 --- a/examples/serial.rs +++ b/examples/serial.rs @@ -1,25 +1,15 @@ //! Test the serial interface //! //! This example requires you to short (connect) the TX and RX pins. -#![deny(warnings)] #![no_main] #![no_std] -extern crate cortex_m; -#[macro_use(entry, exception)] -extern crate cortex_m_rt as rt; -#[macro_use(block)] -extern crate nb; -extern crate panic_semihosting; - -extern crate stm32l4xx_hal as hal; -// #[macro_use(block)] -// extern crate nb; - -use crate::hal::prelude::*; -use crate::hal::serial::Serial; -use crate::rt::ExceptionFrame; -use cortex_m::asm; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use nb::block; +use panic_probe as _; +use stm32l4xx_hal::{self as hal, prelude::*, serial::Serial}; #[entry] fn main() -> ! { @@ -65,21 +55,13 @@ fn main() -> ! { // The `block!` macro makes an operation block until it finishes // NOTE the error type is `!` - block!(tx.write(sent)).ok(); - + block!(tx.write(sent)).unwrap(); let received = block!(rx.read()).unwrap(); assert_eq!(received, sent); - - // if all goes well you should reach this breakpoint - asm::bkpt(); + println!("example complete"); loop { continue; } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/serial_dma.rs b/examples/serial_dma.rs index ec43b6c7..5064828a 100644 --- a/examples/serial_dma.rs +++ b/examples/serial_dma.rs @@ -4,23 +4,18 @@ #![no_main] #![no_std] -#[macro_use(singleton)] -extern crate cortex_m; -#[macro_use(entry, exception)] -extern crate cortex_m_rt as rt; -#[macro_use(block)] -extern crate nb; -extern crate panic_semihosting; - -extern crate stm32l4xx_hal as hal; -// #[macro_use(block)] -// extern crate nb; - -use crate::hal::dma::CircReadDma; -use crate::hal::prelude::*; -use crate::hal::serial::{Config, Serial}; -use crate::rt::ExceptionFrame; -use cortex_m::asm; +use cortex_m::singleton; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use nb::block; +use panic_probe as _; +use stm32l4xx_hal::{ + self as hal, + dma::CircReadDma, + prelude::*, + serial::{Config, Serial}, +}; #[entry] fn main() -> ! { @@ -122,8 +117,7 @@ fn main() -> ! { assert_eq!(rx_len, 8); assert_eq!(&rx_buf[..8], b"abcdefgh"); - // if all goes well you should reach this breakpoint - asm::bkpt(); + println!("example completed"); loop { continue; @@ -132,18 +126,8 @@ fn main() -> ! { fn send(tx: &mut impl embedded_hal::serial::Write, data: &[u8]) { for byte in data { - if let Err(_) = block!(tx.write(*byte)) { - panic!("serial tx failed"); + if let Err(_e) = block!(tx.write(*byte)) { + panic!("error occurred while sending bytes"); } } - - // waste some time so that the data will be received completely - for _ in 0..10000 { - cortex_m::asm::nop(); - } -} - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); } diff --git a/examples/serial_dma_us2.rs b/examples/serial_dma_us2.rs index 60d5c78f..cfb8b9ba 100644 --- a/examples/serial_dma_us2.rs +++ b/examples/serial_dma_us2.rs @@ -4,23 +4,13 @@ #![no_main] #![no_std] -#[macro_use(singleton)] -extern crate cortex_m; -#[macro_use(entry, exception)] -extern crate cortex_m_rt as rt; -#[macro_use(block)] -extern crate nb; -extern crate panic_semihosting; - -extern crate stm32l4xx_hal as hal; -// #[macro_use(block)] -// extern crate nb; - -use crate::hal::dma::CircReadDma; -use crate::hal::prelude::*; -use crate::hal::serial::Serial; -use crate::rt::ExceptionFrame; -use cortex_m::asm; +use cortex_m::singleton; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use nb::block; +use panic_probe as _; +use stm32l4xx_hal::{self as hal, dma::CircReadDma, prelude::*, serial::Serial}; #[entry] fn main() -> ! { @@ -57,32 +47,18 @@ fn main() -> ! { let sent = b'X'; // The `block!` macro makes an operation block until it finishes - // NOTE the error type is `!` - - block!(tx.write(sent)).ok(); - + block!(tx.write(sent)).unwrap(); let buf = singleton!(: [u8; 8] = [0; 8]).unwrap(); - let mut circ_buffer = rx.with_dma(channels.6).circ_read(buf); - let mut rx_buf = [0; 8]; let rx_len = circ_buffer.read(&mut rx_buf).unwrap(); - let _received = &rx_buf[..rx_len]; - - // let received = block!(rx.read()).unwrap(); - - // assert_eq!(received, sent); + let received = &rx_buf[..rx_len]; + defmt::assert_eq!([sent], received); - // if all goes well you should reach this breakpoint - asm::bkpt(); + println!("echo received"); loop { continue; } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/serial_echo_rtic.rs b/examples/serial_echo_rtic.rs index e2a81027..1dc055d8 100644 --- a/examples/serial_echo_rtic.rs +++ b/examples/serial_echo_rtic.rs @@ -1,11 +1,11 @@ #![no_main] #![no_std] -extern crate panic_rtt_target; - -use heapless::{consts::U8, spsc}; +use defmt::println; +use defmt_rtt as _; +use heapless::spsc; use nb::block; -use rtt_target::{rprint, rprintln}; +use panic_probe as _; use stm32l4xx_hal::{ pac::{self, USART2}, prelude::*, @@ -18,16 +18,15 @@ const APP: () = { rx: serial::Rx, tx: serial::Tx, - rx_prod: spsc::Producer<'static, u8, U8>, - rx_cons: spsc::Consumer<'static, u8, U8>, + rx_prod: spsc::Producer<'static, u8, 8>, + rx_cons: spsc::Consumer<'static, u8, 8>, } #[init] fn init(_: init::Context) -> init::LateResources { - static mut RX_QUEUE: spsc::Queue = spsc::Queue(heapless::i::Queue::new()); + static mut RX_QUEUE: spsc::Queue = spsc::Queue::new(); - rtt_target::rtt_init_print!(); - rprint!("Initializing... "); + println!("Initializing... "); let p = pac::Peripherals::take().unwrap(); @@ -58,7 +57,7 @@ const APP: () = { let (tx, rx) = serial.split(); let (rx_prod, rx_cons) = RX_QUEUE.split(); - rprintln!("done."); + println!("done."); init::LateResources { rx, @@ -76,7 +75,7 @@ const APP: () = { loop { if let Some(b) = rx.dequeue() { - rprintln!("Echoing '{}'", b as char); + println!("Echoing '{}'", b as char); block!(tx.write(b)).unwrap(); } } @@ -90,15 +89,14 @@ const APP: () = { let b = match rx.read() { Ok(b) => b, Err(err) => { - rprintln!("Error reading from USART: {:?}", err); + println!("Error reading from USART: {:?}", defmt::Debug2Format(&err)); return; } }; match queue.enqueue(b) { Ok(()) => (), Err(err) => { - rprintln!("Error adding received byte to queue: {:?}", err); - return; + println!("Error adding received byte to queue: {:?}", err); } } } diff --git a/examples/serial_half_duplex.rs b/examples/serial_half_duplex.rs index dfe959ba..b04d6bee 100644 --- a/examples/serial_half_duplex.rs +++ b/examples/serial_half_duplex.rs @@ -3,25 +3,19 @@ //! This example requires you to hook-up a pullup resistor on the TX pin. RX pin is not used. //! Resistor value depends on the baurate and line caracteristics, 1KOhms works well in most cases. //! Half-Duplex mode internally connect TX to RX, meaning that bytes sent will also be received. -#![deny(warnings)] #![no_main] #![no_std] -extern crate cortex_m; -#[macro_use(entry, exception)] -extern crate cortex_m_rt as rt; -#[macro_use(block)] -extern crate nb; -extern crate panic_semihosting; - -extern crate stm32l4xx_hal as hal; -// #[macro_use(block)] -// extern crate nb; - -use crate::hal::prelude::*; -use crate::hal::serial::{Config, Serial}; -use crate::rt::ExceptionFrame; -use cortex_m::asm; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use nb::block; +use panic_probe as _; +use stm32l4xx_hal::{ + self as hal, + prelude::*, + serial::{Config, Serial}, +}; #[entry] fn main() -> ! { @@ -64,25 +58,14 @@ fn main() -> ! { let (mut tx, mut rx) = serial.split(); let sent = b'X'; - // The `block!` macro makes an operation block until it finishes - // NOTE the error type is `!` - - block!(tx.write(sent)).ok(); - + block!(tx.write(sent)).unwrap(); let received = block!(rx.read()).unwrap(); assert_eq!(received, sent); - - // if all goes well you should reach this breakpoint - asm::bkpt(); + println!("example complete"); loop { continue; } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/serial_hw_flow.rs b/examples/serial_hw_flow.rs index 5a301ff2..c5eeb2d1 100644 --- a/examples/serial_hw_flow.rs +++ b/examples/serial_hw_flow.rs @@ -1,25 +1,19 @@ //! Test the serial interface //! //! This example requires you to short (connect) the TX and RX pins. -#![deny(warnings)] #![no_main] #![no_std] -extern crate cortex_m; -#[macro_use(entry, exception)] -extern crate cortex_m_rt as rt; -#[macro_use(block)] -extern crate nb; -extern crate panic_semihosting; - -extern crate stm32l4xx_hal as hal; -// #[macro_use(block)] -// extern crate nb; - -use crate::hal::prelude::*; -use crate::hal::serial::{Config, Serial}; -use crate::rt::ExceptionFrame; -use cortex_m::asm; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use nb::block; +use panic_probe as _; +use stm32l4xx_hal::{ + self as hal, + prelude::*, + serial::{Config, Serial}, +}; #[entry] fn main() -> ! { @@ -74,25 +68,13 @@ fn main() -> ! { let (mut tx, mut rx) = serial.split(); let sent = b'X'; - - // The `block!` macro makes an operation block until it finishes - // NOTE the error type is `!` - - block!(tx.write(sent)).ok(); - + block!(tx.write(sent)).unwrap(); let received = block!(rx.read()).unwrap(); assert_eq!(received, sent); - - // if all goes well you should reach this breakpoint - asm::bkpt(); + println!("example complete"); loop { continue; } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/serial_vcom.rs b/examples/serial_vcom.rs index 583b0196..fd9a920d 100644 --- a/examples/serial_vcom.rs +++ b/examples/serial_vcom.rs @@ -3,21 +3,16 @@ #![no_main] #![no_std] -extern crate cortex_m; -#[macro_use(entry, exception)] -extern crate cortex_m_rt as rt; -#[macro_use(block)] -extern crate nb; -extern crate panic_semihosting; - -extern crate stm32l4xx_hal as hal; -// #[macro_use(block)] -// extern crate nb; - -use crate::hal::prelude::*; -use crate::hal::serial::{Config, Serial}; -use crate::rt::ExceptionFrame; -use cortex_m::asm; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use nb::block; +use panic_probe as _; +use stm32l4xx_hal::{ + self as hal, + prelude::*, + serial::{Config, Serial}, +}; #[entry] fn main() -> ! { @@ -62,11 +57,11 @@ fn main() -> ! { // The `block!` macro makes an operation block until it finishes // NOTE the error type is `!` - block!(tx.write(sent)).ok(); - block!(tx.write(sent)).ok(); - block!(tx.write(sent)).ok(); - block!(tx.write(sent)).ok(); - block!(tx.write(sent)).ok(); + block!(tx.write(sent)).unwrap(); + block!(tx.write(sent)).unwrap(); + block!(tx.write(sent)).unwrap(); + block!(tx.write(sent)).unwrap(); + block!(tx.write(sent)).unwrap(); // when using virtual com port for recieve can causes a framing error // On the stm32l476 discovery it is working fine at 115200 baud @@ -75,14 +70,9 @@ fn main() -> ! { assert_eq!(received, sent); // if all goes well you should reach this breakpoint - asm::bkpt(); + println!("example complete"); loop { continue; } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/spi_dma_rxtx.rs b/examples/spi_dma_rxtx.rs index af69a3a0..ce8681ab 100644 --- a/examples/spi_dma_rxtx.rs +++ b/examples/spi_dma_rxtx.rs @@ -1,10 +1,10 @@ //! Test the SPI in RX/TX (transfer) DMA mode -#![deny(unsafe_code)] #![no_main] #![no_std] -use panic_rtt_target as _; -use rtt_target::rprintln; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; use stm32l4xx_hal::{ dma::TransferDma, gpio::{PinState, Speed}, @@ -20,8 +20,7 @@ const APP: () = { fn init(cx: init::Context) { static mut DMA_BUF: [u8; 5] = [0xf0, 0xaa, 0x00, 0xff, 0x0f]; - rtt_target::rtt_init_print!(); - rprintln!("Initializing... "); + println!("Initializing... "); let dp = cx.device; @@ -34,7 +33,7 @@ const APP: () = { // // Initialize the clocks to 80 MHz // - rprintln!(" - Clock init"); + println!(" - Clock init"); let clocks = rcc .cfgr .msi(MsiFreq::RANGE4M) @@ -77,7 +76,7 @@ const APP: () = { let dma_spi = spi.with_rxtx_dma(dma1_channels.2, dma1_channels.3); // Check the buffer before using it - rprintln!("buf pre: 0x{:x?}", &DMA_BUF); + println!("buf pre: 0x{:?}", &DMA_BUF); // Perform transfer and wait for it to finish (blocking), this can also be done using // interrupts on the desired DMA channel @@ -88,7 +87,7 @@ const APP: () = { // Inspect the extracted buffer, if the MISO is connected to VCC or GND it will be all 0 or // 1. - rprintln!("buf post: 0x{:x?}", &buf); + println!("buf post: 0x{:?}", &buf); } // Idle function so RTT keeps working diff --git a/examples/spi_slave.rs b/examples/spi_slave.rs index 56e117fc..59625c8d 100644 --- a/examples/spi_slave.rs +++ b/examples/spi_slave.rs @@ -2,18 +2,12 @@ #![no_main] #![no_std] -#[macro_use(entry, exception)] -extern crate cortex_m_rt as rt; -extern crate cortex_m; -extern crate embedded_hal as ehal; -extern crate panic_semihosting; -extern crate stm32l4xx_hal as hal; - -use crate::ehal::spi::{Mode, Phase, Polarity}; -use crate::hal::prelude::*; -use crate::hal::spi::Spi; -use crate::rt::ExceptionFrame; -use cortex_m::asm; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use embedded_hal::spi::{Mode, Phase, Polarity}; +use panic_probe as _; +use stm32l4xx_hal::{self as hal, prelude::*, spi::Spi}; /// SPI mode pub const MODE: Mode = Mode { @@ -62,14 +56,9 @@ fn main() -> ! { // when you reach this breakpoint you'll be able to inspect the variable `data` which contains the // data sent by the master - asm::bkpt(); + println!("example complete"); loop { continue; } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/spi_write.rs b/examples/spi_write.rs index 2598d010..1ef0e5c8 100644 --- a/examples/spi_write.rs +++ b/examples/spi_write.rs @@ -2,18 +2,12 @@ #![no_main] #![no_std] -#[macro_use(entry, exception)] -extern crate cortex_m_rt as rt; -extern crate cortex_m; -extern crate embedded_hal as ehal; -extern crate panic_semihosting; -extern crate stm32l4xx_hal as hal; - -use crate::ehal::spi::{Mode, Phase, Polarity}; -use crate::hal::prelude::*; -use crate::hal::spi::Spi; -use crate::rt::ExceptionFrame; -use cortex_m::asm; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use embedded_hal::spi::{Mode, Phase, Polarity}; +use panic_probe as _; +use stm32l4xx_hal::{self as hal, prelude::*, spi::Spi}; /// SPI mode pub const MODE: Mode = Mode { @@ -84,14 +78,9 @@ fn main() -> ! { // when you reach this breakpoint you'll be able to inspect the variable `_m` which contains the // gyroscope and the temperature sensor readings - asm::bkpt(); + println!("example complete"); loop { continue; } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/timer.rs b/examples/timer.rs index 0fa7002f..9e6832f0 100644 --- a/examples/timer.rs +++ b/examples/timer.rs @@ -3,27 +3,20 @@ #![no_std] #![no_main] -extern crate cortex_m; -#[macro_use] -extern crate cortex_m_rt as rt; -extern crate cortex_m_semihosting as sh; -extern crate panic_semihosting; -extern crate stm32l4xx_hal as hal; - -use crate::hal::interrupt; -use crate::hal::prelude::*; -use crate::hal::timer::{Event, Timer}; -use crate::rt::entry; -use crate::rt::ExceptionFrame; use cortex_m::peripheral::NVIC; - -use crate::sh::hio; -use core::fmt::Write; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; +use stm32l4xx_hal::{ + self as hal, interrupt, + prelude::*, + timer::{Event, Timer}, +}; #[entry] fn main() -> ! { - let mut hstdout = hio::hstdout().unwrap(); - writeln!(hstdout, "Hello, world!").unwrap(); + println!("Hello, world!"); // let cp = cortex_m::Peripherals::take().unwrap(); let dp = hal::stm32::Peripherals::take().unwrap(); @@ -51,11 +44,5 @@ fn main() -> ! { fn TIM7() { static mut COUNT: u32 = 0; *COUNT += 1; - // let mut hstdout = hio::hstdout().unwrap(); - // writeln!(hstdout, "Hello, TIM!").unwrap(); -} - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); + println!("TIM7 interrupt"); } diff --git a/examples/touch.rs b/examples/touch.rs index 2ced0243..0888eb24 100644 --- a/examples/touch.rs +++ b/examples/touch.rs @@ -4,16 +4,11 @@ #![no_main] #![no_std] -extern crate cortex_m; -#[macro_use(entry, exception)] -extern crate cortex_m_rt as rt; -extern crate panic_semihosting; - -extern crate stm32l4xx_hal as hal; - -use crate::hal::prelude::*; -use crate::hal::tsc::Tsc; -use crate::rt::ExceptionFrame; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; +use stm32l4xx_hal::{self as hal, prelude::*, tsc::Tsc}; #[entry] fn main() -> ! { @@ -28,8 +23,6 @@ fn main() -> ! { // clock configuration using the default settings (all clocks run at 8 MHz) let _clocks = rcc.cfgr.freeze(&mut flash.acr, &mut pwr); - // TRY this alternate clock configuration (clocks run at nearly the maximum frequency) - // let clocks = rcc.cfgr.sysclk(64.MHz()).pclk1(32.MHz()).freeze(&mut flash.acr); // let mut delay = Delay::new(cp.SYST, clocks); let mut led = gpiob @@ -60,14 +53,11 @@ fn main() -> ! { // try and pass c1, it will detect an error! let _touched_c2_again = tsc.read(&mut c2).unwrap(); if touched < threshold { + println!("touch channel 1 is below the threshold"); led.set_high(); } else { + println!("touch channel 1 is above the threshold"); led.set_low(); } } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -} diff --git a/examples/usb_serial.rs b/examples/usb_serial.rs index 39c30921..a45e6499 100644 --- a/examples/usb_serial.rs +++ b/examples/usb_serial.rs @@ -3,9 +3,10 @@ #![no_std] #![no_main] -extern crate panic_semihosting; - use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; use stm32l4xx_hal::usb::{Peripheral, UsbBus}; use stm32l4xx_hal::{prelude::*, stm32}; use usb_device::prelude::*; @@ -92,6 +93,7 @@ fn main() -> ! { Ok(count) if count > 0 => { led.set_high(); // Turn on + println!("received {}", &buf[0..count]); // Echo back in upper case for c in buf[0..count].iter_mut() { if 0x61 <= *c && *c <= 0x7a { diff --git a/examples/watchdog.rs b/examples/watchdog.rs index bc425911..42d836d2 100644 --- a/examples/watchdog.rs +++ b/examples/watchdog.rs @@ -2,22 +2,15 @@ #![no_std] #![no_main] -use crate::hal::delay::Delay; -use crate::hal::prelude::*; -use crate::hal::watchdog::IndependentWatchdog; -use cortex_m_rt::{entry, exception, ExceptionFrame}; -use cortex_m_semihosting as sh; -use panic_semihosting as _; -use stm32l4xx_hal as hal; - -use crate::sh::hio; -use core::fmt::Write; +use cortex_m_rt::entry; +use defmt::println; +use defmt_rtt as _; +use panic_probe as _; +use stm32l4xx_hal::{self as hal, delay::Delay, prelude::*, watchdog::IndependentWatchdog}; #[entry] fn main() -> ! { - let mut hstdout = hio::hstdout().unwrap(); - - writeln!(hstdout, "Hello, world!").unwrap(); + println!("Hello, world!"); let cp = cortex_m::Peripherals::take().unwrap(); let dp = hal::stm32::Peripherals::take().unwrap(); @@ -47,13 +40,9 @@ fn main() -> ! { timer.delay_ms(1000_u32); watchdog.feed(); - writeln!(hstdout, "Good bye!").unwrap(); + println!("Good bye!"); + // watchdog will reset after 1020 milliseconds loop { continue; } } - -#[exception] -unsafe fn HardFault(ef: &ExceptionFrame) -> ! { - panic!("{:#?}", ef); -}