serial_config.rs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 nb::block;
  9. use cortex_m_rt::entry;
  10. use stm32f1xx_hal::{
  11. pac,
  12. prelude::*,
  13. serial::{self, Serial},
  14. };
  15. #[entry]
  16. fn main() -> ! {
  17. // Get access to the device specific peripherals from the peripheral access crate
  18. let p = pac::Peripherals::take().unwrap();
  19. // Take ownership over the raw flash and rcc devices and convert them into the corresponding
  20. // HAL structs
  21. let mut flash = p.FLASH.constrain();
  22. let mut rcc = p.RCC.constrain();
  23. // Freeze the configuration of all the clocks in the system and store the frozen frequencies in
  24. // `clocks`
  25. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  26. // Prepare the alternate function I/O registers
  27. let mut afio = p.AFIO.constrain(&mut rcc.apb2);
  28. // Prepare the GPIOB peripheral
  29. let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
  30. // USART1
  31. // let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
  32. // let rx = gpioa.pa10;
  33. // USART1
  34. // let tx = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
  35. // let rx = gpiob.pb7;
  36. // USART2
  37. // let tx = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl);
  38. // let rx = gpioa.pa3;
  39. // USART3
  40. // Configure pb10 as a push_pull output, this will be the tx pin
  41. let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
  42. // Take ownership over pb11
  43. let rx = gpiob.pb11;
  44. // Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of
  45. // the registers are used to enable and configure the device.
  46. let serial = Serial::usart3(
  47. p.USART3,
  48. (tx, rx),
  49. &mut afio.mapr,
  50. serial::Config::default()
  51. .baudrate(9600.bps())
  52. .stopbits(serial::StopBits::STOP2)
  53. .parity_odd(),
  54. clocks,
  55. &mut rcc.apb1,
  56. );
  57. // Split the serial struct into a receiving and a transmitting part
  58. let (mut tx, _rx) = serial.split();
  59. let sent = b'U';
  60. block!(tx.write(sent)).ok();
  61. block!(tx.write(sent)).ok();
  62. loop {}
  63. }