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

133 lines
3.9 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 22:50:21 +00:00
#[rtic::app(device = stm32f1xx_hal::pac, dispatchers = [SPI1, SPI2, SPI3])]
2024-03-08 16:59:02 +00:00
mod app {
2024-03-22 00:11:17 +00:00
use crate::dmx::DMX;
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-22 00:11:17 +00:00
const DMX_LEN: usize = 512;
2024-03-21 14:29:37 +00:00
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-22 00:11:17 +00:00
buffer: &'static mut [u8],
2024-03-21 21:04:51 +00:00
delay_us: timer::DelayUs<pac::TIM2>,
2024-03-11 19:09:21 +00:00
}
2024-03-08 16:59:02 +00:00
#[local]
struct Local {
2024-03-22 00:11:17 +00:00
dmx: DMX<DMX_LEN>,
2024-03-22 12:45:36 +00:00
led: gpio::gpioc::PC13<gpio::Output<gpio::PushPull>>,
int_led: gpio::gpiob::PB0<gpio::Output<gpio::OpenDrain>>,
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();
2024-03-22 12:45:36 +00:00
let mut gpiob = cx.device.GPIOB.split();
2024-03-08 16:59:02 +00:00
let mut gpioc = cx.device.GPIOC.split();
let mut afio = cx.device.AFIO.constrain();
let dma1 = cx.device.DMA1.split();
2024-03-22 12:45:36 +00:00
cx.device.EXTI.imr.write(|w| w.mr1().set_bit());
let _ = gpiob.pb10.into_pull_up_input(&mut gpiob.crh);
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-21 21:04:51 +00:00
// rtic::pend(pac::Interrupt::DMA1_CHANNEL4); // ???
2024-03-21 19:23:06 +00:00
foo::spawn().unwrap();
2024-03-08 16:59:02 +00:00
(
2024-03-11 19:09:21 +00:00
Shared {
2024-03-22 00:11:17 +00:00
buffer: singleton!(: [u8; DMX_LEN] = [0b01010101; DMX_LEN]).unwrap(),
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-21 21:04:51 +00:00
},
Local {
dmx: DMX::new(serial, dma1.4, &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.
2024-03-21 22:50:21 +00:00
led: gpioc
.pc13
.into_push_pull_output_with_state(&mut gpioc.crh, gpio::PinState::High),
2024-03-22 12:45:36 +00:00
int_led: gpiob
.pb0
.into_open_drain_output_with_state(&mut gpiob.crl, gpio::PinState::Low),
2024-03-08 16:59:02 +00:00
},
init::Monotonics(mono),
)
}
2024-03-21 19:23:06 +00:00
#[idle]
fn idle(_: idle::Context) -> ! {
2024-03-08 16:59:02 +00:00
loop {
2024-03-21 21:04:51 +00:00
rtic::export::wfi();
2024-03-21 19:23:06 +00:00
}
}
2024-03-01 15:18:26 +00:00
2024-03-21 21:04:51 +00:00
#[task(local = [dmx, led], shared = [&buffer, delay_us])]
fn foo(mut cx: foo::Context) {
2024-03-21 19:23:06 +00:00
cx.local.dmx.send(cx.shared.buffer);
2024-03-08 16:59:02 +00:00
2024-03-21 21:04:51 +00:00
cx.local.led.toggle();
cx.shared.delay_us.lock(|d| d.delay(1.secs()));
2024-03-21 19:23:06 +00:00
2024-03-21 22:50:21 +00:00
// cx.local.dmx.wait();
2024-03-21 19:23:06 +00:00
foo::spawn().unwrap();
2024-02-27 22:55:50 +00:00
}
2024-03-22 12:45:36 +00:00
#[task(binds=EXTI0, local = [int_led], shared = [delay_us])]
fn bar(mut cx: bar::Context) {
cx.local.int_led.toggle();
cx.shared.delay_us.lock(|d| d.delay(1.secs()));
}
2024-02-27 22:55:50 +00:00
}