serial.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 cortex_m_rt::entry;
  11. use stm32f1xx_hal::{
  12. pac,
  13. prelude::*,
  14. serial::{Config, Serial},
  15. };
  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 mut 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. // Loopback test. Write `X` and wait until the write is successful.
  56. let sent = b'X';
  57. block!(serial.write(sent)).ok();
  58. // Read the byte that was just sent. Blocks until the read is complete
  59. let received = block!(serial.read()).unwrap();
  60. // Since we have connected tx and rx, the byte we sent should be the one we received
  61. assert_eq!(received, sent);
  62. // Trigger a breakpoint to allow us to inspect the values
  63. asm::bkpt();
  64. // You can also split the serial struct into a receiving and a transmitting part
  65. let (mut tx, mut rx) = serial.split();
  66. let sent = b'Y';
  67. block!(tx.write(sent)).ok();
  68. let received = block!(rx.read()).unwrap();
  69. assert_eq!(received, sent);
  70. asm::bkpt();
  71. loop {}
  72. }