Skip to content
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

#294 rtt logging #297

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion .cargo/config → .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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]
Expand Down
17 changes: 5 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -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
```
13 changes: 6 additions & 7 deletions examples/adc.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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);
}
}
14 changes: 6 additions & 8 deletions examples/adc_dma.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#![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,
dma::{dma1, RxDma, Transfer, W},
prelude::*,
};

use rtic::app;

const SEQUENCE_LEN: usize = 3;

#[app(device = stm32l4xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)]
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
39 changes: 12 additions & 27 deletions examples/blinky.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}

Expand Down
19 changes: 9 additions & 10 deletions examples/can-loopback.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
//! Run the bxCAN peripheral in loopback mode.

#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]

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();
Expand All @@ -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 =
Expand Down Expand Up @@ -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);
}
Expand Down
68 changes: 27 additions & 41 deletions examples/i2c_write.rs
Original file line number Diff line number Diff line change
@@ -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();
Expand Down Expand Up @@ -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);
}
Loading