diff --git a/.cargo/config b/.cargo/config index cab4708..bf8dd40 100644 --- a/.cargo/config +++ b/.cargo/config @@ -1,9 +1,19 @@ [target.'cfg(all(target_arch = "arm", target_os = "none"))'] -rustflags = [ - "-C", "linker=rust-lld", - "-C", "link-arg=-Tlink.x", -] +# TODO(2) replace `$CHIP` with your chip's name (see `probe-run --list-chips` output) runner = "probe-run --chip STM32F103CB" +rustflags = [ + "-C", "linker=flip-link", + "-C", "link-arg=-Tlink.x", + "-C", "link-arg=-Tdefmt.x", + # This is needed if your flash or ram addresses are not aligned to 0x10000 in memory.x + # See https://github.com/rust-embedded/cortex-m-quickstart/pull/95 + "-C", "link-arg=--nmagic", +] [build] -target = "thumbv7m-none-eabi" +target = "thumbv7m-none-eabi" # Cortex-M3 + + +[alias] +rb = "run --bin" +rrb = "run --release --bin" diff --git a/Cargo.toml b/Cargo.toml index 9b211bf..7700bac 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,26 +1,76 @@ [package] -authors = ["sebastian"] +# TODO(1) fix `authors` and `name` if you didn't use `cargo-generate` +authors = ["sebastian "] +name = "cheapsdp" edition = "2018" -readme = "README.md" -name = "stm32_f1_blinky" version = "0.1.0" [dependencies] -cortex-m = "0.6" -cortex-m-rt = "0.6" -stm32f1xx-hal = { version = "0.6.1", features = ["stm32f103", "stm32-usbd", "rt"] } -panic-semihosting = "0.5" -embedded-hal = "0.2.3" -rtt-target = {version = "0.2.2", features = ["cortex-m"]} +cortex-m = "~0.7.1" +cortex-m-rt = "~0.6.13" +defmt = "~0.2.0" +defmt-rtt = "~0.2.0" +panic-probe = { version = "~0.2.0", features = ["print-defmt"] } +stm32f1xx-hal = { version = "~0.6.1", features = ["stm32f103", "rt"] } +embedded-hal = {version = "~0.2.3"} +[features] +# set logging levels here +default = [ + "defmt-default", + # "dependency-a/defmt-trace", +] -# this lets you use `cargo fix`! -[[bin]] -name = "stm32_f1_blinky" -test = false -bench = false +# do NOT modify these features +defmt-default = [] +defmt-trace = [] +defmt-debug = [] +defmt-info = [] +defmt-warn = [] +defmt-error = [] +# cargo build/run +[profile.dev] +codegen-units = 1 +debug = 2 +debug-assertions = true # <- +incremental = false +opt-level = 3 # <- +overflow-checks = true # <- + +# cargo test +[profile.test] +codegen-units = 1 +debug = 2 +debug-assertions = true # <- +incremental = false +opt-level = 3 # <- +overflow-checks = true # <- + +# cargo build/run --release [profile.release] -codegen-units = 1 # better optimizations -debug = true # symbols are nice and they don't increase the size on Flash -lto = true # better optimizations +codegen-units = 1 +debug = 2 +debug-assertions = false # <- +incremental = false +#lto = 'fat' +opt-level = 3 # <- +overflow-checks = false # <- + +# cargo test --release +[profile.bench] +codegen-units = 1 +debug = 2 +debug-assertions = false # <- +incremental = false +lto = 'fat' +opt-level = 3 # <- +overflow-checks = false # <- + +# uncomment this to switch from the crates.io version of defmt to its git version +# check app-template's README for instructions +# [patch.crates-io] +# defmt = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } +# defmt-rtt = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } +# defmt-test = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } +# panic-probe = { git = "https://github.com/knurling-rs/defmt", rev = "use defmt version reported by `probe-run --version`" } diff --git a/build.rs b/build.rs deleted file mode 100644 index 6cba4d6..0000000 --- a/build.rs +++ /dev/null @@ -1,15 +0,0 @@ -use std::env; -use std::fs; -use std::io::Write; -use std::path::PathBuf; - -fn main() { - // Put the linker script somewhere the linker can find it - let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); - fs::File::create(out_dir.join("memory.x")) - .unwrap() - .write_all(include_bytes!("memory.x")) - .unwrap(); - println!("cargo:rustc-link-search={}", out_dir.display()); - println!("cargo:rerun-if-changed=memory.x"); -} diff --git a/memory.x b/memory.x index fbf584e..71f245d 100644 --- a/memory.x +++ b/memory.x @@ -1,32 +1,6 @@ +/* Linker script for the STM32F103C8T6 */ MEMORY { - /* STM32f1*/ - FLASH : ORIGIN = 0x08000000, LENGTH = 0x00020000 - RAM : ORIGIN = 0x20000000, LENGTH = 0x00005000 + FLASH : ORIGIN = 0x08000000, LENGTH = 64K + RAM : ORIGIN = 0x20000000, LENGTH = 20K } - -/* This is where the call stack will be allocated. */ -/* The stack is of the full descending type. */ -/* You may want to use this variable to locate the call stack and static - variables in different memory regions. Below is shown the default value */ -/* _stack_start = ORIGIN(RAM) + LENGTH(RAM); */ - -/* You can use this symbol to customize the location of the .text section */ -/* If omitted the .text section will be placed right after the .vector_table - section */ -/* This is required only on microcontrollers that store some configuration right - after the vector table */ -/* _stext = ORIGIN(FLASH) + 0x400; */ - -/* Example of putting non-initialized variables into custom RAM locations. */ -/* This assumes you have defined a region RAM2 above, and in the Rust - sources added the attribute `#[link_section = ".ram2bss"]` to the data - you want to place there. */ -/* Note that the section will not be zero-initialized by the runtime! */ -/* SECTIONS { - .ram2bss (NOLOAD) : ALIGN(4) { - *(.ram2bss); - . = ALIGN(4); - } > RAM2 - } INSERT AFTER .bss; -*/ diff --git a/src/main.rs b/src/main.rs index 009a000..722c33b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,15 @@ #![deny(unsafe_code)] #![no_std] #![no_main] +use defmt_rtt as _; // global logger -extern crate panic_semihosting; +use panic_probe as _; +use stm32f1xx_hal as _; + +use core::sync::atomic::{AtomicU32, Ordering}; use cortex_m_rt::entry; use embedded_hal::digital::v2::OutputPin; -use rtt_target::{rprintln, rtt_init_print}; use stm32f1xx_hal::{ delay::Delay, pac, @@ -18,12 +21,27 @@ use stm32f1xx_hal::{ timer::{Tim3NoRemap, Timer}, }; +// same panicking *behavior* as `panic-probe` but doesn't print a panic message +// this prevents the panic message being printed *twice* when `defmt::panic` is invoked +#[defmt::panic_handler] +fn panic() -> ! { + cortex_m::asm::udf() +} + +static COUNT: AtomicU32 = AtomicU32::new(0); +defmt::timestamp!("{=u32}", COUNT.fetch_add(1, Ordering::Relaxed)); + +/// Terminates the application and makes `probe-run` exit with exit-code = 0 +pub fn exit() -> ! { + loop { + cortex_m::asm::bkpt(); + } +} + const target_freq: f64 = 10.0f64; #[entry] fn main() -> ! { - rtt_init_print!(); - // Get access to the core peripherals from the cortex-m crate let cp = cortex_m::Peripherals::take().unwrap(); // Get access to the device specific peripherals from the peripheral access crate @@ -124,7 +142,7 @@ fn main() -> ! { let mut last_ic = 0u32; let mut avg = 10f64; let max_pwm = pwm.get_max_duty() as u32; - let mut cur_pwm = 3000; //max_pwm / 2; + let mut cur_pwm = 3000u32; //max_pwm / 2; // Skip the first measurement, it will be garbage while !tim1.sr.read().cc1if().bit_is_set() || !tim2.sr.read().cc1if().bit_is_set() { @@ -158,24 +176,27 @@ fn main() -> ! { led.toggle().unwrap(); - if diff > 0.000_030 || diff < -0.000_030 { + if diff > 0.000_100 || diff < -0.000_100 { continue; } - avg = avg * 0.99 + freq * 0.01; + avg = avg * 0.999 + freq * 0.001; - cur_pwm += 10_000_000 - diff_ic; + cur_pwm = if 10_000_000 >= diff_ic { + cur_pwm + (10_000_000 - diff_ic) + } else { + cur_pwm - (diff_ic - 10_000_000) + }; cur_pwm = if cur_pwm > max_pwm { max_pwm } else { cur_pwm }; pwm.set_duty(cur_pwm as u16); - rprintln!("Counters:"); - rprintln!("ic1:\t{}", ic1); - rprintln!("ic2:\t{}", ic2); - rprintln!("sum_ic:\t{}", sum_ic); - rprintln!("diff_ic:\t{}", diff_ic); - rprintln!("freq:\t{} MHz", freq); - rprintln!("avg:\t{} MHz", avg); - rprintln!("pwm:\t{}", cur_pwm); + defmt::info!("ic1:\t{}", ic1); + defmt::info!("ic2:\t{}", ic2); + defmt::info!("sum_ic:\t{}", sum_ic); + defmt::info!("diff_ic:\t{}", diff_ic); + defmt::info!("freq:\t{} MHz", freq); + defmt::info!("avg:\t{} MHz", avg); + defmt::info!("pwm:\t{}", cur_pwm); } }