|
| 1 | +//! Serial interface loopback test |
| 2 | +//! |
| 3 | +//! You have to short the TX and RX pins to make this program work |
| 4 | +
|
| 5 | +#![no_main] |
| 6 | +#![no_std] |
| 7 | + |
| 8 | +use panic_halt as _; |
| 9 | + |
| 10 | +use cortex_m_rt::entry; |
| 11 | +use stm32f1xx_hal::{ |
| 12 | + pac, |
| 13 | + pac::interrupt, |
| 14 | + pac::USART1, |
| 15 | + prelude::*, |
| 16 | + serial::{Config, Rx, Serial, Tx}, |
| 17 | +}; |
| 18 | + |
| 19 | +static mut RX: Option<Rx<USART1>> = None; |
| 20 | +static mut TX: Option<Tx<USART1>> = None; |
| 21 | +#[entry] |
| 22 | +fn main() -> ! { |
| 23 | + // Get access to the device specific peripherals from the peripheral access crate |
| 24 | + let p = pac::Peripherals::take().unwrap(); |
| 25 | + |
| 26 | + // Take ownership over the raw flash and rcc devices and convert them into the corresponding |
| 27 | + // HAL structs |
| 28 | + let mut flash = p.FLASH.constrain(); |
| 29 | + let rcc = p.RCC.constrain(); |
| 30 | + |
| 31 | + // Freeze the configuration of all the clocks in the system and store the frozen frequencies in |
| 32 | + // `clocks` |
| 33 | + let clocks = rcc.cfgr.freeze(&mut flash.acr); |
| 34 | + |
| 35 | + // Prepare the alternate function I/O registers |
| 36 | + let mut afio = p.AFIO.constrain(); |
| 37 | + |
| 38 | + // Prepare the GPIOB peripheral |
| 39 | + let mut gpiob = p.GPIOB.split(); |
| 40 | + |
| 41 | + // USART1 |
| 42 | + let tx = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl); |
| 43 | + let rx = gpiob.pb7; |
| 44 | + |
| 45 | + // Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of |
| 46 | + // the registers are used to enable and configure the device. |
| 47 | + let (mut tx, mut rx) = Serial::usart1( |
| 48 | + p.USART1, |
| 49 | + (tx, rx), |
| 50 | + &mut afio.mapr, |
| 51 | + Config::default().baudrate(115200.bps()), |
| 52 | + clocks, |
| 53 | + ) |
| 54 | + .split(); |
| 55 | + tx.listen(); |
| 56 | + rx.listen(); |
| 57 | + rx.listen_idle(); |
| 58 | + |
| 59 | + cortex_m::interrupt::free(|_| unsafe { |
| 60 | + TX.replace(tx); |
| 61 | + RX.replace(rx); |
| 62 | + }); |
| 63 | + unsafe { |
| 64 | + cortex_m::peripheral::NVIC::unmask(pac::Interrupt::USART1); |
| 65 | + } |
| 66 | + |
| 67 | + loop { |
| 68 | + cortex_m::asm::wfi() |
| 69 | + } |
| 70 | +} |
| 71 | +const BUFFER_LEN: usize = 4096; |
| 72 | +static mut BUFFER: &mut [u8; BUFFER_LEN] = &mut [0; BUFFER_LEN]; |
| 73 | +static mut WIDX: usize = 0; |
| 74 | + |
| 75 | +unsafe fn write(buf: &[u8]) { |
| 76 | + if let Some(tx) = TX.as_mut() { |
| 77 | + buf.iter() |
| 78 | + .for_each(|w| if let Err(_err) = nb::block!(tx.write(*w)) {}) |
| 79 | + } |
| 80 | +} |
| 81 | +#[interrupt] |
| 82 | +unsafe fn USART1() { |
| 83 | + cortex_m::interrupt::free(|_| { |
| 84 | + if let Some(rx) = RX.as_mut() { |
| 85 | + if rx.is_rxne() { |
| 86 | + if let Ok(w) = nb::block!(rx.read()) { |
| 87 | + BUFFER[WIDX] = w; |
| 88 | + WIDX += 1; |
| 89 | + if WIDX >= BUFFER_LEN - 1 { |
| 90 | + write(&BUFFER[..]); |
| 91 | + WIDX = 0; |
| 92 | + } |
| 93 | + } |
| 94 | + rx.listen_idle(); |
| 95 | + } else if rx.is_idle() { |
| 96 | + rx.unlisten_idle(); |
| 97 | + write(&BUFFER[0..WIDX]); |
| 98 | + WIDX = 0; |
| 99 | + } |
| 100 | + } |
| 101 | + }) |
| 102 | +} |
0 commit comments