From 74de03ea3c3042d32eb81af798136ed6ddc2bd45 Mon Sep 17 00:00:00 2001 From: Colin Date: Sun, 15 Sep 2019 18:17:33 -0700 Subject: [PATCH] Reading from external input and setting the LEDs Next: analog input --- Cargo.toml | 14 ++++++ openocd.gdb | 10 +++++ src/bsp.rs | 27 ++++++++++++ src/main.rs | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 175 insertions(+) create mode 100644 Cargo.toml create mode 100644 openocd.gdb create mode 100644 src/bsp.rs create mode 100644 src/main.rs diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..080fd73 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +authors = ["Colin "] +edition = "2018" +name = "colin-ddr" +version = "0.1.0" + +[dependencies] +cortex-m = "0.5.6" +cortex-m-rt = "0.6.3" +panic-itm = "0.4.0" + +[dependencies.f3] +features = ["rt"] +version = "0.6.1" diff --git a/openocd.gdb b/openocd.gdb new file mode 100644 index 0000000..cafe49b --- /dev/null +++ b/openocd.gdb @@ -0,0 +1,10 @@ +target remote :3333 +set print asm-demangle on +set print pretty on +monitor tpiu config internal itm.txt uart off 8000000 +monitor itm port 0 on +load +break DefaultHandler +break UserHardFault +break main +continue diff --git a/src/bsp.rs b/src/bsp.rs new file mode 100644 index 0000000..021384a --- /dev/null +++ b/src/bsp.rs @@ -0,0 +1,27 @@ +//! Initialization code + + +pub use f3::hal::stm32f30x::{gpioa, gpioc, rcc}; + +use f3::hal::stm32f30x::{self, GPIOA, GPIOD, GPIOE, RCC}; + +pub struct Peripherals { + pub gpioa: &'static gpioa::RegisterBlock, + pub gpiod: &'static gpioc::RegisterBlock, + pub gpioe: &'static gpioc::RegisterBlock, + pub rcc: &'static rcc::RegisterBlock, +} + +pub fn init() -> Peripherals { + // restrict access to the other peripherals + (stm32f30x::Peripherals::take().unwrap()); + + unsafe { + Peripherals { + gpioa: &*GPIOA::ptr(), + gpiod: &*GPIOD::ptr(), + gpioe: &*GPIOE::ptr(), + rcc: &*RCC::ptr(), + } + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..3d1390e --- /dev/null +++ b/src/main.rs @@ -0,0 +1,124 @@ +#![no_main] +#![no_std] + +// Which GPIOs are free to use?? +// Rather, which are taken? +// PA0 - AIN_1 with some filtering +// PA2 - STLINK_TX +// PA3 - STLINK_RX +// PA5 - SCL/SPC, T_JTCK +// PA6 - SA0/SDO, T_JTDO +// PA7 - SDA/SDI/SDO, T_JTDI +// PA8 - MCO (with LPF) +// PA9 - maybe tied to PA11 +// PA10 - maybe tied to PA12 +// PA11 - D-, maybe tied to PA9 +// PA12 - D+, maybe tied to PA10 +// PA13 - TMS/SWDIO +// PA14 - TCK/SWCLK +// PA15 - JTDI +// PB0 - T_NRST +// PB2 - GND +// PB3 - T_SWO +// PB4 - JNTRST +// PB5 - SWIM_RST_IN +// PB6 - SWIM_RST, SCL +// PB7 - SWIM_IN, SDA +// PB8 - SWIM +// PB9 - SWIM_IN +// PB10 - SWIM_IN +// PB11 - SWIM +// PB12 - T_SWDIO_IN connected to PB14 +// PB13 - T_JTCK +// PB14 - T_JTMS connected to PB12 +// PC4 - USARTI_RX +// PC5 - USARTI_TX +// PC13 - 10k pulldown +// PC14 - 10k pulldown (not fitted) tied to PC14-OSC32_IN +// PC15 - tied to PC15-OSC32_OUT +// PE0 - INT1 +// PE1 - DRDY/INT2 +// PE2 - DRDY +// PE3 - CS_I2C/SPI +// PE4 - INT1 +// PE5 - INT2 +// PE8 - LD4 blue +// PE9 - LD3 red +// PE10 - LD5 orange +// PE11 - LD7 green +// PE12 - LD9 blue +// PE13 - LD10 red +// PE14 - LD8 orange +// PE15 - LD6 green +// PF0 - tied to PF0-OSC_IN +// PF1 - tied to PF1-OSC_OUT +// +// It looks like Port D is safe to use, most of port c + +extern crate panic_itm; // panic handler + +mod bsp; + +use cortex_m::asm::bkpt; +use cortex_m_rt::entry; + +#[entry] +fn main() -> ! { + let per = bsp::init(); + + // Configure clock gates + per.rcc.ahbenr.modify(|_, w| { + // enable IO Port A (push-button) + w.iopaen().set_bit(); + // enable IO Pord D (piezo) + w.iopden().set_bit(); + // enable IO Port E (LEDs) + w.iopeen().set_bit() + }); + + // All LEDS are outputs + per.gpioe.moder.modify(|_, w| { + // NB colin: there's also a .analog() mode! + w.moder8().output(); + w.moder9().output(); + w.moder10().output(); + w.moder11().output(); + w.moder12().output(); + w.moder13().output(); + w.moder14().output(); + w.moder15().output() + }); + + // Configure push-button as input: + per.gpioa.moder.modify(|_, w| { + w.moder0().input() + }); + + // Configure piezo as digital input + per.gpiod.moder.modify(|_, w| { + w.moder0().input() + }); + + // Turn on all the LEDs in the compass + per.gpioe.odr.write(|w| { + w.odr8().set_bit(); + w.odr9().set_bit(); + w.odr10().set_bit(); + w.odr11().set_bit(); + w.odr12().set_bit(); + w.odr13().set_bit(); + w.odr14().set_bit(); + w.odr15().set_bit() + }); + + bkpt(); + + loop { + //let push_button = per.gpioa.idr.read().idr0().bit(); + let push_button = per.gpiod.idr.read().idr0().bit(); + per.gpioe.odr.modify(|_, w| { + w.odr8().bit(push_button) + }); + } +} +