serial.rs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 cortex_m;
  9. extern crate cortex_m_rt as rt;
  10. #[macro_use(block)]
  11. extern crate nb;
  12. extern crate panic_semihosting;
  13. extern crate stm32f1xx_hal as hal;
  14. use cortex_m::asm;
  15. use hal::prelude::*;
  16. use hal::serial::Serial;
  17. use hal::stm32f103xx;
  18. use rt::{entry, exception, ExceptionFrame};
  19. #[entry]
  20. fn main() -> ! {
  21. let p = stm32f103xx::Peripherals::take().unwrap();
  22. let mut flash = p.FLASH.constrain();
  23. let mut rcc = p.RCC.constrain();
  24. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  25. let mut afio = p.AFIO.constrain(&mut rcc.apb2);
  26. // let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
  27. let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
  28. // USART1
  29. // let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
  30. // let rx = gpioa.pa10;
  31. // USART1
  32. // let tx = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
  33. // let rx = gpiob.pb7;
  34. // USART2
  35. // let tx = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl);
  36. // let rx = gpioa.pa3;
  37. // USART3
  38. let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
  39. let rx = gpiob.pb11;
  40. let serial = Serial::usart3(
  41. p.USART3,
  42. (tx, rx),
  43. &mut afio.mapr,
  44. 9_600.bps(),
  45. clocks,
  46. &mut rcc.apb1,
  47. );
  48. let (mut tx, mut rx) = serial.split();
  49. let sent = b'X';
  50. block!(tx.write(sent)).ok();
  51. let received = block!(rx.read()).unwrap();
  52. assert_eq!(received, sent);
  53. asm::bkpt();
  54. loop {}
  55. }
  56. #[exception]
  57. fn HardFault(ef: &ExceptionFrame) -> ! {
  58. panic!("{:#?}", ef);
  59. }
  60. #[exception]
  61. fn DefaultHandler(irqn: i16) {
  62. panic!("Unhandled exception (IRQn = {})", irqn);
  63. }