rtic blinky + dma tx

This commit is contained in:
Jörn-Michael Miehe 2024-03-09 01:17:29 +00:00
parent 6bc62383bc
commit fe2425fca2

View file

@ -1,27 +1,16 @@
//! Blinks an LED #![deny(unsafe_code)]
//!
//! 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_std]
#![no_main] #![no_main]
// use cortex_m::singleton;
use panic_halt as _;
use stm32f1xx_hal::{dma, gpio, pac, prelude::*, rcc, serial, timer};
use systick_monotonic::Systick;
mod i2c_reg_slave; mod i2c_reg_slave;
mod i2c_slave; mod i2c_slave;
// static mut BUFFER: Option<&mut [u8; 256]> = None;
#[rtic::app(device = stm32f1::stm32f103, peripherals = true)] #[rtic::app(device = stm32f1::stm32f103, peripherals = true)]
mod app { mod app {
use super::*; use cortex_m::singleton;
use panic_halt as _;
use stm32f1xx_hal::{dma, gpio, pac, prelude::*, serial, timer};
use systick_monotonic::Systick;
// A monotonic timer to enable scheduling in RTIC // A monotonic timer to enable scheduling in RTIC
#[monotonic(binds = SysTick, default = true)] #[monotonic(binds = SysTick, default = true)]
@ -32,9 +21,9 @@ mod app {
#[local] #[local]
struct Local { struct Local {
tx: dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>, tx: Option<dma::TxDma<serial::Tx<pac::USART1>, dma::dma1::C4>>,
delay: timer::Delay<pac::TIM1, 1_000_000>, delay: timer::Delay<pac::TIM1, 1_000_000>,
led: gpio::Pin<'C', 13, gpio::Output>, led: gpio::gpioc::PC13<gpio::Output>,
} }
#[init] #[init]
@ -46,20 +35,13 @@ mod app {
// Freeze the configuration of all the clocks in the system and store the frozen frequencies in // Freeze the configuration of all the clocks in the system and store the frozen frequencies in
// `clocks` // `clocks`
let clocks = rcc.cfgr.freeze_with_config( let clocks = rcc
rcc::Config { .cfgr
// HSE frequency // hf external quartz frequency
hse: Some(8_000_000), .use_hse(8.MHz())
// PLLMUL represented by an integer -2 // system clock frequency
pllmul: Some(9 - 2), .sysclk(72.MHz())
// PCLK1 freq must be 36 MHz or less .freeze(&mut flash.acr);
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,
);
// Initialize the monotonic // Initialize the monotonic
let mono = Systick::new(cx.core.SYST, clocks.sysclk().to_Hz()); let mono = Systick::new(cx.core.SYST, clocks.sysclk().to_Hz());
@ -71,7 +53,7 @@ mod app {
let mut afio = cx.device.AFIO.constrain(); let mut afio = cx.device.AFIO.constrain();
let dma1 = cx.device.DMA1.split(); let dma1 = cx.device.DMA1.split();
// USART1 // Serial config
let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh); let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
let rx = gpioa.pa10; //.into_pull_up_input(&mut gpioa.crh); let rx = gpioa.pa10; //.into_pull_up_input(&mut gpioa.crh);
@ -86,14 +68,10 @@ mod app {
&clocks, &clocks,
); );
// unsafe {
// BUFFER = Some(singleton!(: [u8; 256] = [0b01010101; 256]).unwrap());
// }
( (
Shared {}, Shared {},
Local { Local {
tx: serial.tx.with_dma(dma1.4), tx: Some(serial.tx.with_dma(dma1.4)),
// Configure timer // Configure timer
delay: cx.device.TIM1.delay_us(&clocks), delay: cx.device.TIM1.delay_us(&clocks),
@ -108,17 +86,19 @@ mod app {
#[idle(local = [tx, delay, led])] #[idle(local = [tx, delay, led])]
fn idle(cx: idle::Context) -> ! { fn idle(cx: idle::Context) -> ! {
// Wait for the timer to trigger an update and change the state of the LED let mut buf = singleton!(: [u8; 256] = [0b01010101; 256]).unwrap();
loop { let mut tx = cx.local.tx.take().unwrap();
// let xfer = cx.shared.tx.lock(|tx| tx.write(BUFFER.unwrap()));
cx.local.delay.delay(1u32.secs()); loop {
let xfer = tx.write(buf);
cx.local.delay.delay(1.secs());
cx.local.led.set_high(); cx.local.led.set_high();
cx.local.delay.delay(1u32.secs()); cx.local.delay.delay(1.secs());
cx.local.led.set_low(); cx.local.led.set_low();
// xfer.wait(); (buf, tx) = xfer.wait();
} }
} }
} }