From 8249c4c3b6fcf54b2b8aa2d5798585e7ee48b936 Mon Sep 17 00:00:00 2001 From: LongHairedHacker Date: Wed, 9 Sep 2020 19:25:41 +0200 Subject: [PATCH] Initial commit --- .cargo/config | 9 +++++++++ .gitignore | 5 +++++ Cargo.toml | 26 ++++++++++++++++++++++++++ build.rs | 15 +++++++++++++++ memory.x | 32 ++++++++++++++++++++++++++++++++ src/main.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 135 insertions(+) create mode 100644 .cargo/config create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100644 build.rs create mode 100644 memory.x create mode 100644 src/main.rs diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..cab4708 --- /dev/null +++ b/.cargo/config @@ -0,0 +1,9 @@ +[target.'cfg(all(target_arch = "arm", target_os = "none"))'] +rustflags = [ + "-C", "linker=rust-lld", + "-C", "link-arg=-Tlink.x", +] +runner = "probe-run --chip STM32F103CB" + +[build] +target = "thumbv7m-none-eabi" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..59a4524 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +**/*.rs.bk +.#* +.gdb_history +Cargo.lock +target/ diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..ae67c13 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,26 @@ +[package] +authors = ["sebastian"] +edition = "2018" +readme = "README.md" +name = "reflow-firmware" +version = "0.1.0" + +[dependencies] +cortex-m = "0.6" +cortex-m-rt = "0.6" +stm32f1xx-hal = { version = "0.5.3", features = ["stm32f103", "rt"] } +panic-semihosting = "0.5" +embedded-hal = "0.2.3" +rtt-target = {version = "0.2.2", features = ["cortex-m"]} + + +# this lets you use `cargo fix`! +[[bin]] +name = "reflow-firmware" +test = false +bench = false + +[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 diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..6cba4d6 --- /dev/null +++ b/build.rs @@ -0,0 +1,15 @@ +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 new file mode 100644 index 0000000..fbf584e --- /dev/null +++ b/memory.x @@ -0,0 +1,32 @@ +MEMORY +{ + /* STM32f1*/ + FLASH : ORIGIN = 0x08000000, LENGTH = 0x00020000 + 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; +*/ diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..055a1e7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,48 @@ +#![deny(unsafe_code)] +#![no_std] +#![no_main] + +extern crate panic_semihosting; + +use cortex_m_rt::entry; +use embedded_hal::digital::v2::OutputPin; +use rtt_target::{rprintln, rtt_init_print}; +use stm32f1xx_hal::{delay::Delay, pac, prelude::*}; + +#[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 + let dp = pac::Peripherals::take().unwrap(); + + // Take ownership over the raw flash and rcc devices and convert them into the corresponding + // HAL structs + let mut flash = dp.FLASH.constrain(); + let mut rcc = dp.RCC.constrain(); + + // Freeze the configuration of all the clocks in the system and store the frozen frequencies in + // `clocks` + let clocks = rcc.cfgr.freeze(&mut flash.acr); + + // Acquire the GPIOC peripheral + let mut gpioc = dp.GPIOC.split(&mut rcc.apb2); + + // Configure gpio C pin 13 as a push-pull output. The `crh` register is passed to the function + // in order to configure the port. For pins 0-7, crl should be passed instead. + let mut led = gpioc.pc13.into_push_pull_output(&mut gpioc.crh); + + let mut delay = Delay::new(cp.SYST, clocks); + + // Blink using the delay function + loop { + rprintln!("blink"); + led.set_high().unwrap(); + delay.delay_ms(1000u16); + rprintln!("blonk"); + led.set_low().unwrap(); + delay.delay_ms(1000u16); + } +}