|
| 1 | +//! Serial interface reconfiguration test |
| 2 | +//! |
| 3 | +//! You have to short the TX and RX pins to make this program work |
| 4 | +
|
| 5 | +#![deny(unsafe_code)] |
| 6 | +#![no_main] |
| 7 | +#![no_std] |
| 8 | + |
| 9 | +use panic_halt as _; |
| 10 | + |
| 11 | +use cortex_m::asm; |
| 12 | + |
| 13 | +use nb::block; |
| 14 | + |
| 15 | +use cortex_m_rt::entry; |
| 16 | +use stm32f1xx_hal::{ |
| 17 | + pac, |
| 18 | + prelude::*, |
| 19 | + serial::{Config, Serial}, |
| 20 | +}; |
| 21 | + |
| 22 | +#[entry] |
| 23 | +fn main() -> ! { |
| 24 | + // Get access to the device specific peripherals from the peripheral access crate |
| 25 | + let p = pac::Peripherals::take().unwrap(); |
| 26 | + |
| 27 | + // Take ownership over the raw flash and rcc devices and convert them into the corresponding |
| 28 | + // HAL structs |
| 29 | + let mut flash = p.FLASH.constrain(); |
| 30 | + let rcc = p.RCC.constrain(); |
| 31 | + |
| 32 | + // Freeze the configuration of all the clocks in the system and store the frozen frequencies in |
| 33 | + // `clocks` |
| 34 | + let clocks = rcc.cfgr.freeze(&mut flash.acr); |
| 35 | + |
| 36 | + // Prepare the alternate function I/O registers |
| 37 | + let mut afio = p.AFIO.constrain(); |
| 38 | + |
| 39 | + // Prepare the GPIOB peripheral |
| 40 | + let mut gpiob = p.GPIOB.split(); |
| 41 | + |
| 42 | + // USART1 |
| 43 | + // let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh); |
| 44 | + // let rx = gpioa.pa10; |
| 45 | + |
| 46 | + // USART1 |
| 47 | + // let tx = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl); |
| 48 | + // let rx = gpiob.pb7; |
| 49 | + |
| 50 | + // USART2 |
| 51 | + // let tx = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl); |
| 52 | + // let rx = gpioa.pa3; |
| 53 | + |
| 54 | + // USART3 |
| 55 | + // Configure pb10 as a push_pull output, this will be the tx pin |
| 56 | + let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh); |
| 57 | + // Take ownership over pb11 |
| 58 | + let rx = gpiob.pb11; |
| 59 | + |
| 60 | + // Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of |
| 61 | + // the registers are used to enable and configure the device. |
| 62 | + let mut serial = Serial::usart3( |
| 63 | + p.USART3, |
| 64 | + (tx, rx), |
| 65 | + &mut afio.mapr, |
| 66 | + Config::default().baudrate(9600.bps()), |
| 67 | + clocks, |
| 68 | + ); |
| 69 | + |
| 70 | + // Loopback test. Write `X` and wait until the write is successful. |
| 71 | + let sent = b'X'; |
| 72 | + block!(serial.write(sent)).ok(); |
| 73 | + |
| 74 | + // Read the byte that was just sent. Blocks until the read is complete |
| 75 | + let received = block!(serial.read()).unwrap(); |
| 76 | + |
| 77 | + // Since we have connected tx and rx, the byte we sent should be the one we received |
| 78 | + assert_eq!(received, sent); |
| 79 | + |
| 80 | + // Trigger a breakpoint to allow us to inspect the values |
| 81 | + asm::bkpt(); |
| 82 | + |
| 83 | + // You can reconfigure the serial port to use a different baud rate at runtime. |
| 84 | + // This may block for a while if the transmission is still in progress. |
| 85 | + block!(serial.reconfigure(Config::default().baudrate(115_200.bps()), clocks)).unwrap(); |
| 86 | + |
| 87 | + // Let's see if it works.' |
| 88 | + let sent = b'Y'; |
| 89 | + block!(serial.write(sent)).ok(); |
| 90 | + let received = block!(serial.read()).unwrap(); |
| 91 | + assert_eq!(received, sent); |
| 92 | + asm::bkpt(); |
| 93 | + |
| 94 | + loop {} |
| 95 | +} |
0 commit comments