Updated to use defmt
This commit is contained in:
parent
7b30081320
commit
d9c3a36950
|
@ -1,9 +1,19 @@
|
||||||
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
|
||||||
rustflags = [
|
# TODO(2) replace `$CHIP` with your chip's name (see `probe-run --list-chips` output)
|
||||||
"-C", "linker=rust-lld",
|
|
||||||
"-C", "link-arg=-Tlink.x",
|
|
||||||
]
|
|
||||||
runner = "probe-run --chip STM32F103CB"
|
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]
|
[build]
|
||||||
target = "thumbv7m-none-eabi"
|
target = "thumbv7m-none-eabi" # Cortex-M3
|
||||||
|
|
||||||
|
|
||||||
|
[alias]
|
||||||
|
rb = "run --bin"
|
||||||
|
rrb = "run --release --bin"
|
||||||
|
|
84
Cargo.toml
84
Cargo.toml
|
@ -1,26 +1,76 @@
|
||||||
[package]
|
[package]
|
||||||
authors = ["sebastian"]
|
# TODO(1) fix `authors` and `name` if you didn't use `cargo-generate`
|
||||||
|
authors = ["sebastian <sebastian@sebastians-site.de>"]
|
||||||
|
name = "cheapsdp"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
readme = "README.md"
|
|
||||||
name = "stm32_f1_blinky"
|
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
cortex-m = "0.6"
|
cortex-m = "~0.7.1"
|
||||||
cortex-m-rt = "0.6"
|
cortex-m-rt = "~0.6.13"
|
||||||
stm32f1xx-hal = { version = "0.6.1", features = ["stm32f103", "stm32-usbd", "rt"] }
|
defmt = "~0.2.0"
|
||||||
panic-semihosting = "0.5"
|
defmt-rtt = "~0.2.0"
|
||||||
embedded-hal = "0.2.3"
|
panic-probe = { version = "~0.2.0", features = ["print-defmt"] }
|
||||||
rtt-target = {version = "0.2.2", features = ["cortex-m"]}
|
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`!
|
# do NOT modify these features
|
||||||
[[bin]]
|
defmt-default = []
|
||||||
name = "stm32_f1_blinky"
|
defmt-trace = []
|
||||||
test = false
|
defmt-debug = []
|
||||||
bench = false
|
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]
|
[profile.release]
|
||||||
codegen-units = 1 # better optimizations
|
codegen-units = 1
|
||||||
debug = true # symbols are nice and they don't increase the size on Flash
|
debug = 2
|
||||||
lto = true # better optimizations
|
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`" }
|
||||||
|
|
15
build.rs
15
build.rs
|
@ -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");
|
|
||||||
}
|
|
32
memory.x
32
memory.x
|
@ -1,32 +1,6 @@
|
||||||
|
/* Linker script for the STM32F103C8T6 */
|
||||||
MEMORY
|
MEMORY
|
||||||
{
|
{
|
||||||
/* STM32f1*/
|
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
|
||||||
FLASH : ORIGIN = 0x08000000, LENGTH = 0x00020000
|
RAM : ORIGIN = 0x20000000, LENGTH = 20K
|
||||||
RAM : ORIGIN = 0x20000000, LENGTH = 0x00005000
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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;
|
|
||||||
*/
|
|
||||||
|
|
53
src/main.rs
53
src/main.rs
|
@ -1,12 +1,15 @@
|
||||||
#![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![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 cortex_m_rt::entry;
|
||||||
use embedded_hal::digital::v2::OutputPin;
|
use embedded_hal::digital::v2::OutputPin;
|
||||||
use rtt_target::{rprintln, rtt_init_print};
|
|
||||||
use stm32f1xx_hal::{
|
use stm32f1xx_hal::{
|
||||||
delay::Delay,
|
delay::Delay,
|
||||||
pac,
|
pac,
|
||||||
|
@ -18,12 +21,27 @@ use stm32f1xx_hal::{
|
||||||
timer::{Tim3NoRemap, Timer},
|
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;
|
const target_freq: f64 = 10.0f64;
|
||||||
|
|
||||||
#[entry]
|
#[entry]
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
rtt_init_print!();
|
|
||||||
|
|
||||||
// Get access to the core peripherals from the cortex-m crate
|
// Get access to the core peripherals from the cortex-m crate
|
||||||
let cp = cortex_m::Peripherals::take().unwrap();
|
let cp = cortex_m::Peripherals::take().unwrap();
|
||||||
// Get access to the device specific peripherals from the peripheral access crate
|
// 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 last_ic = 0u32;
|
||||||
let mut avg = 10f64;
|
let mut avg = 10f64;
|
||||||
let max_pwm = pwm.get_max_duty() as u32;
|
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
|
// Skip the first measurement, it will be garbage
|
||||||
while !tim1.sr.read().cc1if().bit_is_set() || !tim2.sr.read().cc1if().bit_is_set() {
|
while !tim1.sr.read().cc1if().bit_is_set() || !tim2.sr.read().cc1if().bit_is_set() {
|
||||||
|
@ -158,24 +176,27 @@ fn main() -> ! {
|
||||||
|
|
||||||
led.toggle().unwrap();
|
led.toggle().unwrap();
|
||||||
|
|
||||||
if diff > 0.000_030 || diff < -0.000_030 {
|
if diff > 0.000_100 || diff < -0.000_100 {
|
||||||
continue;
|
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 };
|
cur_pwm = if cur_pwm > max_pwm { max_pwm } else { cur_pwm };
|
||||||
|
|
||||||
pwm.set_duty(cur_pwm as u16);
|
pwm.set_duty(cur_pwm as u16);
|
||||||
|
|
||||||
rprintln!("Counters:");
|
defmt::info!("ic1:\t{}", ic1);
|
||||||
rprintln!("ic1:\t{}", ic1);
|
defmt::info!("ic2:\t{}", ic2);
|
||||||
rprintln!("ic2:\t{}", ic2);
|
defmt::info!("sum_ic:\t{}", sum_ic);
|
||||||
rprintln!("sum_ic:\t{}", sum_ic);
|
defmt::info!("diff_ic:\t{}", diff_ic);
|
||||||
rprintln!("diff_ic:\t{}", diff_ic);
|
defmt::info!("freq:\t{} MHz", freq);
|
||||||
rprintln!("freq:\t{} MHz", freq);
|
defmt::info!("avg:\t{} MHz", avg);
|
||||||
rprintln!("avg:\t{} MHz", avg);
|
defmt::info!("pwm:\t{}", cur_pwm);
|
||||||
rprintln!("pwm:\t{}", cur_pwm);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue