diff --git a/.gitignore b/.gitignore index af83db9c987bf66de048234c56fba4b6a1597d46..7130f7edbb4baf9fff5c9eba940ce803005e1747 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ target -.gdb_history \ No newline at end of file +.gdb_history +itm.fifo diff --git a/blinky-itm/.cargo/config b/blinky-itm/.cargo/config new file mode 100644 index 0000000000000000000000000000000000000000..d62cf99daa78b9815bc34747230bfdfcc569a263 --- /dev/null +++ b/blinky-itm/.cargo/config @@ -0,0 +1,10 @@ +[target.thumbv7em-none-eabi] +runner = 'arm-none-eabi-gdb' +rustflags = [ +# "-C", "linker=arm-none-eabi-gcc", +# "-C", "link-arg=-Wl,-Tlink.x", +# "-C", "link-arg=-nostartfiles" + "-C", "link-arg=-Tlink.x" +] +[build] +target = "thumbv7em-none-eabi" diff --git a/blinky-itm/.gdbinit b/blinky-itm/.gdbinit new file mode 100644 index 0000000000000000000000000000000000000000..0c4f8c87e3ae6ac7a9c138ce7bb262382ea1c124 --- /dev/null +++ b/blinky-itm/.gdbinit @@ -0,0 +1,21 @@ +target remote :3333 + +# print demangled symbols by default +set print asm-demangle on + +#monitor arm semihosting enable + +# # send captured ITM to the file itm.fifo +# # (the microcontroller SWO pin must be connected to the programmer SWO pin) +# # 8000000 must match the core clock frequency +monitor tpiu config internal itm.fifo uart off 168000000 + +# # OR: make the microcontroller SWO pin output compatible with UART (8N1) +# # 2000000 is the frequency of the SWO pin +# monitor tpiu config external uart off 8000000 2000000 + +# # enable ITM port 0 +monitor itm port 0 on + +load +step diff --git a/blinky-itm/Cargo.lock b/blinky-itm/Cargo.lock new file mode 100644 index 0000000000000000000000000000000000000000..b388ab1a55d8ceb4c5697aeb35f36c33d80a55a1 --- /dev/null +++ b/blinky-itm/Cargo.lock @@ -0,0 +1,202 @@ +[[package]] +name = "aligned" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "bare-metal" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "blinky-itm" +version = "0.0.1" +dependencies = [ + "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "cortex-m 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "cortex-m-rt 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "embedded-hal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "panic-abort 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "stm32f407g-disc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cast" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cortex-m" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aligned 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bare-metal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "volatile-register 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cortex-m-rt" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cortex-m-rt-macros 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "r0 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cortex-m-rt-macros" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "embedded-hal" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "nb 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "nb" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "panic-abort" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "proc-macro2" +version = "0.4.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "r0" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "stm32f4" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bare-metal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "cortex-m 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "cortex-m-rt 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "stm32f407g-disc" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cortex-m 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "cortex-m-rt 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "embedded-hal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "stm32f4xx-hal 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "stm32f4xx-hal" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bare-metal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", + "cortex-m 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)", + "cortex-m-rt 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)", + "embedded-hal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "nb 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "stm32f4 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "0.15.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "vcell" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "void" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "volatile-register" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[metadata] +"checksum aligned 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d39da9b88ae1a81c03c9c082b8db83f1d0e93914126041962af61034ab44c4a5" +"checksum bare-metal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "1bdcf9294ed648c7cd29b11db06ea244005aeef50ae8f605b1a3af2940bf8f92" +"checksum cast 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "926013f2860c46252efceabb19f4a6b308197505082c609025aa6706c011d427" +"checksum cortex-m 0.5.7 (registry+https://github.com/rust-lang/crates.io-index)" = "4573199c5b1e9b0eeae418b46f7c0af5fdf11b3057f83880810dfef68dd1dcb5" +"checksum cortex-m-rt 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)" = "d86cfa89fa220d3cb7a41133693e2d302d18e9a632298ffb3738f175c8c12325" +"checksum cortex-m-rt-macros 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3139fdadccaa0db6fa96637678ced9b0b97e4f10047c9ab603d125048e107d1a" +"checksum embedded-hal 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "26944677e4934eb5fb4025501dc0d6cdbcf6bfabd6200fcfee2e7e8eef8c0362" +"checksum nb 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "69f380b5fe9fab8c0d7a6a99cda23e2cc0463bedb2cbc3aada0813b98496ecdc" +"checksum panic-abort 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2c14a66511ed17b6a8b4256b868d7fd207836d891db15eea5195dbcaf87e630f" +"checksum proc-macro2 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)" = "ffe022fb8c8bd254524b0b3305906c1921fa37a84a644e29079a9e62200c3901" +"checksum quote 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "dd636425967c33af890042c483632d33fa7a18f19ad1d7ea72e8998c6ef8dea5" +"checksum r0 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f" +"checksum rand 0.5.5 (registry+https://github.com/rust-lang/crates.io-index)" = "e464cd887e869cddcae8792a4ee31d23c7edd516700695608f5b98c67ee0131c" +"checksum rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "edecf0f94da5551fc9b492093e30b041a891657db7940ee221f9d2f66e82eef2" +"checksum stm32f4 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9dd897c48dc34c0483a190715a0d4e0e805308fbefabcf941e4e2948d6f622" +"checksum stm32f407g-disc 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "57197f804593fc9fd73af204996b347d5279946d9e3d555ff19f0b0090a680cc" +"checksum stm32f4xx-hal 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d4293bcaabb6a6a0993f7013f8bb17f9a115257c8e5d89e843e2af81d336855" +"checksum syn 0.15.8 (registry+https://github.com/rust-lang/crates.io-index)" = "356d1c5043597c40489e9af2d2498c7fefc33e99b7d75b43be336c8a59b3e45e" +"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" +"checksum vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "45c297f0afb6928cd08ab1ff9d95e99392595ea25ae1b5ecf822ff8764e57a0d" +"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" +"checksum volatile-register 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0d67cb4616d99b940db1d6bd28844ff97108b498a6ca850e5b6191a532063286" diff --git a/blinky-itm/Cargo.toml b/blinky-itm/Cargo.toml new file mode 100644 index 0000000000000000000000000000000000000000..7fe4e68e2d9a7112e42cb0b270519024d06abd63 --- /dev/null +++ b/blinky-itm/Cargo.toml @@ -0,0 +1,40 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g. crates.io) dependencies +# +# If you believe there's an error in this file please file an +# issue against the rust-lang/cargo repository. If you're +# editing this file be aware that the upstream Cargo.toml +# will likely look very different (and much more reasonable) + +[package] +name = "blinky-itm" +version = "0.0.1" +authors = ["Tim Docker<tim@dockerz.net>"] +description = "A flashing light" +keywords = ["arm", "cortex-m"] +categories = ["embedded", "no-std"] +license = "MIT OR Apache-2.0" +repository = "https://github.com/timbod7/..." + +[dependencies.cast] +version = "0.2.2" +default-features = false + +[dependencies.stm32f407g-disc] +version = "0.1.0" + +[dependencies.embedded-hal] +version = "0.2.1" + +[dependencies.cortex-m] +version = "0.5.7" + +[dependencies.cortex-m-rt] +version = "0.6.4" + +[dependencies.panic-abort] +version = "0.3.1" diff --git a/blinky-itm/README.md b/blinky-itm/README.md new file mode 100644 index 0000000000000000000000000000000000000000..abd47cab276d7db16915373583a5ebf7ea24e6bb --- /dev/null +++ b/blinky-itm/README.md @@ -0,0 +1,11 @@ +Run the binary on the stm32f407 discovery board as per normal. With +the gdb configuration as in this directory, an itm.fifo file will be +written. + +Run this command: + +``` +itmdump -F -f itm.fifo +``` + +to follow it. diff --git a/blinky-itm/build.rs b/blinky-itm/build.rs new file mode 100644 index 0000000000000000000000000000000000000000..98f603edca220793729c92ff608e25d6bf0e8e7e --- /dev/null +++ b/blinky-itm/build.rs @@ -0,0 +1,18 @@ +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + // Put the linker script somewhere the linker can find it + let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); + File::create(out.join("memory.x")) + .unwrap() + .write_all(include_bytes!("memory.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + + // Only re-run the build script when memory.x is changed, + // instead of when any part of the source code changes. + println!("cargo:rerun-if-changed=memory.x"); +} diff --git a/blinky-itm/memory.x b/blinky-itm/memory.x new file mode 100644 index 0000000000000000000000000000000000000000..f4df782cdd2865f751f303e2994738324b0b254b --- /dev/null +++ b/blinky-itm/memory.x @@ -0,0 +1,22 @@ +MEMORY +{ + /* NOTE K = KiBi = 1024 bytes */ + FLASH : ORIGIN = 0x08000000, LENGTH = 256K + RAM : ORIGIN = 0x20000000, LENGTH = 40K +} + +/* 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; */ + +/* Size of the heap (in bytes) */ +/* _heap_size = 1024; */ diff --git a/blinky-itm/src/main.rs b/blinky-itm/src/main.rs new file mode 100644 index 0000000000000000000000000000000000000000..b58e8c24d2c746857d404611b1b6394261b60acd --- /dev/null +++ b/blinky-itm/src/main.rs @@ -0,0 +1,76 @@ +#![no_main] +#![no_std] + +extern crate cortex_m; +extern crate cortex_m_rt; +extern crate panic_abort; + +extern crate stm32f407g_disc as board; +extern crate embedded_hal as hal; + +use cortex_m_rt::entry; + +use board::hal::delay::Delay; +use board::hal::prelude::*; +use board::hal::stm32; +use board::gpio; +use board::gpio::gpiod::{PD12, PD13, PD14, PD15}; + +use hal::digital::OutputPin; + +use cortex_m::iprintln; +use cortex_m::peripheral::Peripherals; + +struct Leds { + green: PD12<gpio::Output<gpio::PushPull>>, + orange: PD13<gpio::Output<gpio::PushPull>>, + red: PD14<gpio::Output<gpio::PushPull>>, + blue: PD15<gpio::Output<gpio::PushPull>>, +} + +#[entry] +fn main() -> ! { + if let (Some(p), Some(cp)) = (stm32::Peripherals::take(), Peripherals::take()) { + let gpiod = p.GPIOD.split(); + let mut itm = cp.ITM; + + // Configure LED outputs + let mut leds = Leds { + green: gpiod.pd12.into_push_pull_output(), + orange: gpiod.pd13.into_push_pull_output(), + red: gpiod.pd14.into_push_pull_output(), + blue: gpiod.pd15.into_push_pull_output(), + }; + + // Constrain clock registers + let mut rcc = p.RCC.constrain(); + + // Configure clock to 168 MHz (i.e. the maximum) and freeze it + let clocks = rcc.cfgr.sysclk(168.mhz()).freeze(); + + // Get delay provider + let mut delay = Delay::new(cp.SYST, clocks); + + iprintln!(&mut itm.stim[0], "start" ); + + loop { + // Turn LED on + leds.red.set_high(); + iprintln!(&mut itm.stim[0], "on" ); + + // Delay twice for half a second due to limited timer resolution + delay.delay_ms(500_u16); + delay.delay_ms(500_u16); + + // Turn LED off + leds.red.set_low(); + iprintln!(&mut itm.stim[0], "off" ); + + // Delay twice for half a second due to limited timer resolution + delay.delay_ms(500_u16); + delay.delay_ms(500_u16); + } + } + + loop {} +} \ No newline at end of file