Skip to content

Instantly share code, notes, and snippets.

@DanielO
Created May 26, 2020 01:22
Show Gist options
  • Save DanielO/95313b2c2f793455399dcc978ecc9b7a to your computer and use it in GitHub Desktop.
Save DanielO/95313b2c2f793455399dcc978ecc9b7a to your computer and use it in GitHub Desktop.
// std and main are not available for bare metal software
#![no_std]
#![no_main]
//extern crate panic_halt;
extern crate panic_semihosting;
extern crate stm32f1xx_hal;
extern crate cortex_m_semihosting;
use nb::block;
use cortex_m_semihosting::hprintln;
use stm32f1xx_hal::{
prelude::*,
pac,
timer::Timer,
serial::Serial,
};
use cortex_m_rt::entry;
// use `main` as the entry point of this application
#[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 GPIOA peripheral
let mut gpioa = dp.GPIOA.split(&mut rcc.apb2);
// Acquire the GPIOC peripheral
let mut gpioc = dp.GPIOC.split(&mut rcc.apb2);
// Prepare the alternate function I/O registers
let mut afio = dp.AFIO.constrain(&mut rcc.apb2);
// USART1
let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
let rx = gpioa.pa10;
// Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of
// the registers are used to enable and configure the device.
let serial = Serial::usart1(
dp.USART1,
(tx, rx),
&mut afio.mapr,
9_600.bps(),
clocks,
&mut rcc.apb2,
);
hprintln!("Hello, world!").unwrap();
// Pins PA9 and PA10 have been routed to e.g. an UART <-> USB adapter so they
// can only be used for serial communication
// `Serial1` represents that serial interface
//pub type Serial1 = Serial<USART1, PA9<AF7>, PA10<AF7>>;
// Configure gpio C pin 6 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 led1 = gpioc.pc6.into_push_pull_output(&mut gpioc.crl);
let mut led2 = gpioc.pc7.into_push_pull_output(&mut gpioc.crl);
// Configure the syst timer to trigger an update every second
let mut timer = Timer::syst(cp.SYST, 1.hz(), clocks);
// Split the serial struct into a receiving and a transmitting part
let (mut tx, mut rx) = serial.split();
// Wait for the timer to trigger an update and change the state of the LED
loop {
block!(timer.wait()).unwrap();
led1.set_low();
led2.set_high();
let sent = b'X';
block!(tx.write(sent)).ok();
block!(timer.wait()).unwrap();
led1.set_high();
led2.set_low();
let sent = b'Y';
block!(tx.write(sent)).ok();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment