serial.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #![deny(warnings)]
  6. #![no_main]
  7. #![no_std]
  8. extern crate panic_halt;
  9. use cortex_m::asm;
  10. use nb::block;
  11. use stm32f1xx_hal::{
  12. prelude::*,
  13. pac,
  14. serial::Serial,
  15. };
  16. use cortex_m_rt::entry;
  17. #[entry]
  18. fn main() -> ! {
  19. let p = pac::Peripherals::take().unwrap();
  20. let mut flash = p.FLASH.constrain();
  21. let mut rcc = p.RCC.constrain();
  22. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  23. let mut afio = p.AFIO.constrain(&mut rcc.apb2);
  24. // let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
  25. let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
  26. // USART1
  27. // let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
  28. // let rx = gpioa.pa10;
  29. // USART1
  30. // let tx = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
  31. // let rx = gpiob.pb7;
  32. // USART2
  33. // let tx = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl);
  34. // let rx = gpioa.pa3;
  35. // USART3
  36. let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
  37. let rx = gpiob.pb11;
  38. let serial = Serial::usart3(
  39. p.USART3,
  40. (tx, rx),
  41. &mut afio.mapr,
  42. 9_600.bps(),
  43. clocks,
  44. &mut rcc.apb1,
  45. );
  46. let (mut tx, mut rx) = serial.split();
  47. let sent = b'X';
  48. block!(tx.write(sent)).ok();
  49. let received = block!(rx.read()).unwrap();
  50. assert_eq!(received, sent);
  51. asm::bkpt();
  52. loop {}
  53. }