aboutsummaryrefslogtreecommitdiffstats
path: root/sw/demo1/src
diff options
context:
space:
mode:
authorMatthias P. Braendli <matthias.braendli@mpb.li>2020-04-10 21:47:17 +0200
committerMatthias P. Braendli <matthias.braendli@mpb.li>2020-04-10 21:47:17 +0200
commitd547c5c5c9834dbb1dbe000b42e09b22f064f947 (patch)
tree13e7c20a839e6ded8267926c27a067e5c0c39d8e /sw/demo1/src
parent3b543dcfac08aeea9891aa7e279db5fe33b9099b (diff)
downloadpicardy-d547c5c5c9834dbb1dbe000b42e09b22f064f947.tar.gz
picardy-d547c5c5c9834dbb1dbe000b42e09b22f064f947.tar.bz2
picardy-d547c5c5c9834dbb1dbe000b42e09b22f064f947.zip
Add demo1 stm32f103 example with blinky code
Diffstat (limited to 'sw/demo1/src')
-rw-r--r--sw/demo1/src/main.rs72
1 files changed, 72 insertions, 0 deletions
diff --git a/sw/demo1/src/main.rs b/sw/demo1/src/main.rs
new file mode 100644
index 0000000..80e38b9
--- /dev/null
+++ b/sw/demo1/src/main.rs
@@ -0,0 +1,72 @@
+#![no_main]
+#![no_std]
+
+use cortex_m_rt::ExceptionFrame;
+use cortex_m_semihosting::hio;
+use panic_semihosting as _;
+
+use stm32f1xx_hal::{
+ prelude::*,
+ pac,
+ timer::Timer,
+};
+
+use embedded_hal::digital::v2::{OutputPin, ToggleableOutputPin};
+
+use core::fmt::Write;
+use nb::block;
+
+fn print(step: usize) -> Result<(), core::fmt::Error> {
+ let mut stdout = match hio::hstdout() {
+ Ok(fd) => fd,
+ Err(()) => return Err(core::fmt::Error),
+ };
+
+ let language = "Rust";
+ let ranking = 1;
+
+ write!(stdout, "{}: {} on embedded is #{}!\n", step, language, ranking)?;
+
+ Ok(())
+}
+
+#[cortex_m_rt::entry]
+fn main() -> ! {
+ let cp = cortex_m::Peripherals::take().unwrap();
+ let dp = pac::Peripherals::take().unwrap();
+
+ let mut flash = dp.FLASH.constrain();
+ let mut rcc = dp.RCC.constrain();
+ let clocks = rcc.cfgr.freeze(&mut flash.acr);
+
+ let mut gpiob = dp.GPIOB.split(&mut rcc.apb2);
+
+ // Configure PB14 as output. (LED)
+ let mut led = gpiob.pb14.into_push_pull_output(&mut gpiob.crh);
+ led.set_low().unwrap();
+
+ let mut timer = Timer::syst(cp.SYST, &clocks).start_count_down(1.hz());
+
+ let mut step = 0;
+ print(step).unwrap();
+
+ loop {
+ for _ in 0..5 {
+ led.toggle().unwrap();
+ block!(timer.wait()).ok();
+ }
+
+ print(step).unwrap();
+ step += 1;
+ }
+}
+
+#[cortex_m_rt::exception]
+fn HardFault(ef: &ExceptionFrame) -> ! {
+ panic!("HardFault at {:#?}", ef);
+}
+
+#[cortex_m_rt::exception]
+fn DefaultHandler(irqn: i16) {
+ panic!("Unhandled exception (IRQn = {})", irqn);
+}