From 8fff1281f206f28111123cd65b4d6f367476e977 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Mon, 23 Nov 2020 16:01:43 -0600 Subject: [PATCH] Bluepill template init --- LICENSE | 3 +++ README.md | 2 ++ template.toml | 9 +++++++ template/.cargo/config | 7 ++++++ template/Cargo.toml | 4 ++++ template/memory.x | 6 +++++ template/openocd.cfg | 2 ++ template/openocd.gdb | 10 ++++++++ template/src/main.rs | 54 ++++++++++++++++++++++++++++++++++++++++-- 9 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 template.toml create mode 100644 template/.cargo/config create mode 100644 template/memory.x create mode 100644 template/openocd.cfg create mode 100644 template/openocd.gdb diff --git a/LICENSE b/LICENSE index 8deb767..ac892a3 100644 --- a/LICENSE +++ b/LICENSE @@ -1,3 +1,6 @@ +MIT License + +Copyright (c) 2018 Guillaume P. Copyright (c) 2020 Joey Hines Permission is hereby granted, free of charge, to any person obtaining a copy diff --git a/README.md b/README.md index adfdc0f..abf6bb9 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ A template for my Rust projects. Uses [tmpl](https://git.jojodev.com/jolheiser/tmpl). +Inspired from [blue-pill-quickstart](https://github.com/TeXitoi/blue-pill-quickstart) + ## License [MIT](LICENSE) diff --git a/template.toml b/template.toml new file mode 100644 index 0000000..4d71893 --- /dev/null +++ b/template.toml @@ -0,0 +1,9 @@ +# template.toml +# Write any template args here to prompt the user for, giving any defaults/options as applicable + +project_name = "project" +version = "0.1.0" +author = "Joey Hines" +author_email = "joey@ahines.net" +runner = "gdb-multiarch -q -x openocd.gdb" + diff --git a/template/.cargo/config b/template/.cargo/config new file mode 100644 index 0000000..bce511c --- /dev/null +++ b/template/.cargo/config @@ -0,0 +1,7 @@ +[target.thumbv7m-none-eabi] +runner = "{{runner}}" + +rustflags = ["-C", "link-arg=-Tlink.x"] + +[build] +target = "thumbv7m-none-eabi" diff --git a/template/Cargo.toml b/template/Cargo.toml index 923763e..56f03d4 100644 --- a/template/Cargo.toml +++ b/template/Cargo.toml @@ -7,3 +7,7 @@ edition = "{{rust_edition}}" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +stm32f1xx-hal = { version = "0.7", features = ["rt", "stm32f103" ] } +cortex-m = "0.7" +cortex-m-rt = { version = "0.6", features = ["device"] } +panic-semihosting = "0.5" diff --git a/template/memory.x b/template/memory.x new file mode 100644 index 0000000..71f245d --- /dev/null +++ b/template/memory.x @@ -0,0 +1,6 @@ +/* Linker script for the STM32F103C8T6 */ +MEMORY +{ + FLASH : ORIGIN = 0x08000000, LENGTH = 64K + RAM : ORIGIN = 0x20000000, LENGTH = 20K +} diff --git a/template/openocd.cfg b/template/openocd.cfg new file mode 100644 index 0000000..b00e2bf --- /dev/null +++ b/template/openocd.cfg @@ -0,0 +1,2 @@ +source [find interface/stlink-v2.cfg] +source [find target/stm32f1x.cfg] diff --git a/template/openocd.gdb b/template/openocd.gdb new file mode 100644 index 0000000..316149d --- /dev/null +++ b/template/openocd.gdb @@ -0,0 +1,10 @@ +target remote :3333 +set print asm-demangle on +monitor arm semihosting enable + +# detect unhandled exceptions, hard faults and panics +break DefaultHandler +break HardFault +break rust_begin_unwind + +load diff --git a/template/src/main.rs b/template/src/main.rs index e7a11a9..b17f951 100644 --- a/template/src/main.rs +++ b/template/src/main.rs @@ -1,3 +1,53 @@ -fn main() { - println!("Hello, world!"); +//! Blinks an LED +//! +//! This assumes that a LED is connected to pc13 as is the case on the blue pill board. +//! +//! Note: Without additional hardware, PC13 should not be used to drive an LED, see page 5.1.2 of +//! the reference manual for an explanation. This is not an issue on the blue pill. +//! Taken from https://github.com/stm32-rs/stm32f1xx-hal/blob/master/examples/blinky.rs + +#![deny(unsafe_code)] +#![no_std] +#![no_main] + +use panic_halt as _; + +use nb::block; + +use cortex_m_rt::entry; +use embedded_hal::digital::v2::OutputPin; +use stm32f1xx_hal::{pac, prelude::*, timer::Timer}; + +#[entry] +fn main() -> ! { + // 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); + // Configure the syst timer to trigger an update every second + let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.hz()); + + // Wait for the timer to trigger an update and change the state of the LED + loop { + block!(timer.wait()).unwrap(); + led.set_high().unwrap(); + block!(timer.wait()).unwrap(); + led.set_low().unwrap(); + } }