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

149 lines
4.5 KiB
Rust
Raw Normal View History

2024-03-25 00:41:57 +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;
2024-03-25 00:37:47 +00:00
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-25 00:37:47 +00:00
use crate::dmx::DMX;
2024-03-23 15:24:11 +00:00
use stm32f1xx_hal::{
gpio::{self, ExtiPin},
pac,
prelude::*,
2024-03-24 23:51:08 +00:00
timer,
2024-03-23 15:24:11 +00:00
};
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-23 23:38:30 +00:00
dmx: DMX<DMX_LEN>,
2024-03-24 23:51:08 +00:00
delay_us: timer::DelayUs<pac::TIM2>,
2024-03-23 23:38:30 +00:00
int_led: gpio::gpiob::PB0<gpio::Output<gpio::OpenDrain>>,
2024-03-11 19:09:21 +00:00
}
2024-03-08 16:59:02 +00:00
#[local]
struct Local {
2024-03-22 12:45:36 +00:00
led: gpio::gpioc::PC13<gpio::Output<gpio::PushPull>>,
2024-03-23 15:24:11 +00:00
int_pin: gpio::gpiob::PB10<gpio::Input<gpio::PullUp>>,
2024-03-08 16:59:02 +00:00
}
2024-03-25 00:41:57 +00:00
#[allow(unsafe_code)]
2024-03-25 00:36:02 +00:00
#[init(local = [buffer: [u8; DMX_LEN * 2] = [0b0101_0101; DMX_LEN * 2]])]
2024-03-23 14:55:03 +00:00
fn init(mut cx: init::Context) -> (Shared, Local, init::Monotonics) {
2024-03-08 16:59:02 +00:00
// 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();
2024-03-24 23:51:08 +00:00
let dma1 = cx.device.DMA1.split();
2024-03-08 16:59:02 +00:00
2024-03-23 15:24:11 +00:00
// setup EXTI10 for Pin B10
let mut int_pin = gpiob.pb10.into_pull_up_input(&mut gpiob.crh);
int_pin.make_interrupt_source(&mut afio);
int_pin.enable_interrupt(&mut cx.device.EXTI);
int_pin.trigger_on_edge(&mut cx.device.EXTI, gpio::Edge::Falling);
unsafe { cx.core.NVIC.set_priority(pac::Interrupt::EXTI15_10, 1) }; // EXTI10 priority
2024-03-23 14:55:03 +00:00
2024-03-23 23:38:30 +00:00
sender::spawn().unwrap();
2024-03-21 19:23:06 +00:00
2024-03-08 16:59:02 +00:00
(
2024-03-11 19:09:21 +00:00
Shared {
2024-03-24 23:51:08 +00:00
dmx: DMX::new(
cx.local.buffer,
dma1.4,
2024-03-25 14:49:27 +00:00
gpioa.pa8,
2024-03-25 00:28:37 +00:00
gpioa.pa9,
gpioa.pa10,
&mut gpioa.crh,
2024-03-24 23:51:08 +00:00
&mut afio.mapr,
&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-23 23:38:30 +00:00
int_led: gpiob
.pb0
.into_open_drain_output_with_state(&mut gpiob.crl, gpio::PinState::Low),
},
Local {
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
2024-03-25 00:36:02 +00:00
int_pin,
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-24 23:51:08 +00:00
#[task(local = [led], shared = [delay_us, dmx])]
2024-03-23 23:38:30 +00:00
fn sender(mut cx: sender::Context) {
2024-03-25 00:36:02 +00:00
cx.shared.dmx.lock(DMX::start_tx);
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-23 23:38:30 +00:00
sender::spawn().unwrap();
}
#[task(binds = DMA1_CHANNEL4, shared = [dmx, int_led])]
fn waiter(cx: waiter::Context) {
(cx.shared.dmx, cx.shared.int_led).lock(|dmx, int_led| {
2024-03-24 23:51:08 +00:00
assert!(dmx.tx_is_idle());
2024-03-23 23:38:30 +00:00
int_led.toggle();
});
2024-02-27 22:55:50 +00:00
}
2024-03-22 12:45:36 +00:00
2024-03-23 23:38:30 +00:00
#[task(binds = EXTI15_10, local = [int_pin], shared = [delay_us, int_led])]
fn toggler(cx: toggler::Context) {
(cx.shared.delay_us, cx.shared.int_led).lock(|delay_us, int_led| {
int_led.toggle();
delay_us.delay(100.millis());
});
2024-03-23 14:55:03 +00:00
2024-03-23 15:24:11 +00:00
// clear EXTI10 pending status
cx.local.int_pin.clear_interrupt_pending_bit()
2024-03-22 12:45:36 +00:00
}
2024-02-27 22:55:50 +00:00
}