bluepill-rust-blinky/bluepill-rs/src/main.rs

102 lines
3.1 KiB
Rust
Raw Normal View History

2024-02-27 22:55:50 +00:00
//! 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.
#![deny(unsafe_code)]
#![no_std]
#![no_main]
2024-02-29 20:29:54 +00:00
use cortex_m::singleton;
2024-02-27 22:55:50 +00:00
use panic_halt as _;
use nb::block;
use cortex_m_rt::entry;
2024-02-29 20:29:54 +00:00
use stm32f1xx_hal::{pac, prelude::*, rcc, serial, timer::Timer};
2024-02-27 22:55:50 +00:00
2024-03-03 15:49:38 +00:00
mod i2c_reg_slave;
mod i2c_slave;
2024-02-27 22:55:50 +00:00
#[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 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_with_config(
2024-02-29 20:29:54 +00:00
rcc::Config {
// HSE frequency
hse: Some(8_000_000),
// PLLMUL represented by an integer -2
pllmul: Some(9 - 2),
// PCLK1 freq must be 36 MHz or less
ppre1: stm32f1xx_hal::rcc::PPre::Div2,
// ADCCLK freq must be 14 MHz or less
adcpre: pac::rcc::cfgr::ADCPRE_A::Div6,
..Default::default()
},
&mut flash.acr,
);
2024-02-27 22:55:50 +00:00
2024-03-01 15:18:26 +00:00
// Acquire the peripherals
2024-02-29 20:29:54 +00:00
let mut gpioa = dp.GPIOA.split();
2024-02-27 22:55:50 +00:00
let mut gpioc = dp.GPIOC.split();
2024-02-29 20:29:54 +00:00
let mut afio = dp.AFIO.constrain();
let dma1 = dp.DMA1.split();
// USART1
let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
let rx = gpioa.pa10; //.into_pull_up_input(&mut gpioa.crh);
let serial = serial::Serial::new(
dp.USART1,
(tx, rx),
&mut afio.mapr,
serial::Config {
2024-03-01 15:18:26 +00:00
baudrate: 250_000.bps(),
2024-02-29 20:29:54 +00:00
..Default::default()
},
&clocks,
);
2024-03-01 15:18:26 +00:00
let mut tx = serial.tx.with_dma(dma1.4);
let mut buf = singleton!(: [u8; 255] = [0b01010101; 255]).unwrap();
2024-02-29 20:29:54 +00:00
2024-02-27 22:55:50 +00:00
// 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);
2024-02-27 22:55:50 +00:00
// Configure the syst timer to trigger an update every second
let mut timer = Timer::syst(cp.SYST, &clocks).counter_hz();
// at 72 MHz, timer of 1 Hz overflows, use 10 Hz instead (8 Hz experimental minimum)
timer.start(10.Hz()).unwrap();
2024-02-27 22:55:50 +00:00
// Wait for the timer to trigger an update and change the state of the LED
loop {
2024-03-01 15:18:26 +00:00
let xfer = tx.write(buf);
for _ in 0..10 {
block!(timer.wait()).unwrap();
}
2024-02-27 22:55:50 +00:00
led.set_high();
for _ in 0..10 {
block!(timer.wait()).unwrap();
}
2024-02-27 22:55:50 +00:00
led.set_low();
2024-03-01 15:18:26 +00:00
(buf, tx) = xfer.wait();
2024-02-27 22:55:50 +00:00
}
}