serial.rs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //! Serial interface loopback test
  2. //!
  3. //! You have to short the TX and RX pins to make this program work
  4. #![deny(unsafe_code)]
  5. #![no_main]
  6. #![no_std]
  7. use panic_halt as _;
  8. use cortex_m::asm;
  9. use nb::block;
  10. use stm32f1xx_hal::{
  11. prelude::*,
  12. pac,
  13. serial::{Config, Serial},
  14. };
  15. use cortex_m_rt::entry;
  16. #[entry]
  17. fn main() -> ! {
  18. // Get access to the device specific peripherals from the peripheral access crate
  19. let p = pac::Peripherals::take().unwrap();
  20. // Take ownership over the raw flash and rcc devices and convert them into the corresponding
  21. // HAL structs
  22. let mut flash = p.FLASH.constrain();
  23. let mut rcc = p.RCC.constrain();
  24. // Freeze the configuration of all the clocks in the system and store the frozen frequencies in
  25. // `clocks`
  26. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  27. // Prepare the alternate function I/O registers
  28. let mut afio = p.AFIO.constrain(&mut rcc.apb2);
  29. // Prepare the GPIOB peripheral
  30. let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
  31. // USART1
  32. // let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
  33. // let rx = gpioa.pa10;
  34. // USART1
  35. // let tx = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
  36. // let rx = gpiob.pb7;
  37. // USART2
  38. // let tx = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl);
  39. // let rx = gpioa.pa3;
  40. // USART3
  41. // Configure pb10 as a push_pull output, this will be the tx pin
  42. let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
  43. // Take ownership over pb11
  44. let rx = gpiob.pb11;
  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 serial = Serial::usart3(
  48. p.USART3,
  49. (tx, rx),
  50. &mut afio.mapr,
  51. Config::default().baudrate(9600.bps()),
  52. clocks,
  53. &mut rcc.apb1,
  54. );
  55. // Split the serial struct into a receiving and a transmitting part
  56. let (mut tx, mut rx) = serial.split();
  57. let sent = b'X';
  58. // Write `X` and wait until the write is successful
  59. block!(tx.write(sent)).ok();
  60. // Read the byte that was just sent. Blocks until the read is complete
  61. let received = block!(rx.read()).unwrap();
  62. // Since we have connected tx and rx, the byte we sent should be the one we received
  63. assert_eq!(received, sent);
  64. // Trigger a breakpoint to allow us to inspect the values
  65. asm::bkpt();
  66. loop {}
  67. }