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

108 lines
3.1 KiB
Rust
Raw Normal View History

2024-03-09 01:17:29 +00:00
#![deny(unsafe_code)]
2024-02-27 22:55:50 +00:00
#![no_std]
#![no_main]
2024-03-21 14:29:37 +00:00
// mod i2c_reg_slave;
// mod i2c_slave;
mod dmx;
2024-03-03 15:49:38 +00:00
2024-03-12 17:03:28 +00:00
// extern crate panic_halt;
extern crate panic_semihosting;
2024-03-11 19:09:21 +00:00
2024-03-21 14:29:37 +00:00
#[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [DMA1_CHANNEL4])]
2024-03-08 16:59:02 +00:00
mod app {
2024-03-12 01:45:11 +00:00
use cortex_m::singleton;
2024-03-21 18:52:04 +00:00
use stm32f1xx_hal::{gpio, pac, prelude::*, serial, timer};
2024-03-09 01:17:29 +00:00
use systick_monotonic::Systick;
2024-03-08 16:59:02 +00:00
2024-03-21 14:29:37 +00:00
use crate::dmx::DMX;
2024-03-08 16:59:02 +00:00
// A monotonic timer to enable scheduling in RTIC
#[monotonic(binds = SysTick, default = true)]
type MyMono = Systick<100>; // 100 Hz / 10 ms granularity
#[shared]
2024-03-11 19:09:21 +00:00
struct Shared {
2024-03-15 12:37:55 +00:00
buffer: &'static mut [u8; 512],
2024-03-11 19:09:21 +00:00
}
2024-03-08 16:59:02 +00:00
#[local]
struct Local {
2024-03-21 14:29:37 +00:00
dmx: DMX,
2024-03-11 23:03:30 +00:00
delay_us: timer::DelayUs<pac::TIM2>,
2024-03-09 01:17:29 +00:00
led: gpio::gpioc::PC13<gpio::Output>,
2024-03-08 16:59:02 +00:00
}
#[init]
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
// Take ownership over the raw flash and rcc devices and convert them into the corresponding
// HAL structs
let mut flash = cx.device.FLASH.constrain();
let rcc = cx.device.RCC.constrain();
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in
// `clocks`
2024-03-09 01:17:29 +00:00
let clocks = rcc
.cfgr
// hf external quartz frequency
.use_hse(8.MHz())
// system clock frequency
.sysclk(72.MHz())
.freeze(&mut flash.acr);
2024-03-08 16:59:02 +00:00
// Initialize the monotonic
let mono = Systick::new(cx.core.SYST, clocks.sysclk().to_Hz());
// Acquire the peripherals
let mut gpioa = cx.device.GPIOA.split();
let mut gpioc = cx.device.GPIOC.split();
let mut afio = cx.device.AFIO.constrain();
let dma1 = cx.device.DMA1.split();
2024-03-09 01:17:29 +00:00
// Serial config
2024-03-21 18:52:04 +00:00
let serial = serial::Serial::new(
2024-03-08 16:59:02 +00:00
cx.device.USART1,
2024-03-11 19:09:21 +00:00
(
2024-03-21 14:29:37 +00:00
gpioa.pa9.into_alternate_open_drain(&mut gpioa.crh),
2024-03-11 19:09:21 +00:00
gpioa.pa10, //.into_pull_up_input(&mut gpioa.crh),
),
2024-03-08 16:59:02 +00:00
&mut afio.mapr,
2024-03-21 18:52:04 +00:00
serial::Config::default(),
2024-03-08 16:59:02 +00:00
&clocks,
);
(
2024-03-11 19:09:21 +00:00
Shared {
2024-03-15 12:37:55 +00:00
buffer: singleton!(: [u8; 512] = [0b01010101; 512]).unwrap(),
2024-03-11 19:09:21 +00:00
},
2024-03-08 16:59:02 +00:00
Local {
2024-03-21 18:52:04 +00:00
dmx: DMX::new(serial, dma1.4, &clocks),
2024-03-08 16:59:02 +00:00
// Configure timer
2024-03-11 23:03:30 +00:00
delay_us: cx.device.TIM2.delay_us(&clocks),
2024-03-08 16:59:02 +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.
led: gpioc.pc13.into_push_pull_output(&mut gpioc.crh),
},
init::Monotonics(mono),
)
}
2024-03-21 14:29:37 +00:00
#[idle(local = [dmx, delay_us, led], shared = [&buffer])]
2024-03-15 12:37:55 +00:00
fn idle(cx: idle::Context) -> ! {
2024-03-08 16:59:02 +00:00
loop {
2024-03-21 14:29:37 +00:00
cx.local.dmx.send(cx.shared.buffer);
2024-03-01 15:18:26 +00:00
2024-03-15 12:37:55 +00:00
cx.local.delay_us.delay(1.secs());
cx.local.led.set_high();
2024-03-08 16:59:02 +00:00
2024-03-15 12:37:55 +00:00
cx.local.delay_us.delay(1.secs());
cx.local.led.set_low();
2024-03-21 17:58:02 +00:00
cx.local.dmx.wait();
2024-03-08 16:59:02 +00:00
}
2024-02-27 22:55:50 +00:00
}
}