aboutsummaryrefslogtreecommitdiffstats
path: root/sw/demo1/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'sw/demo1/src/main.rs')
-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);
+}