serial-fmt.rs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //! Serial interface write formatted strings test
  2. //!
  3. //! You need to connect the Tx pin to the Rx pin of a serial-usb converter
  4. //! so you can see the message in a serial console (e.g. Arduino console).
  5. #![deny(unsafe_code)]
  6. #![no_main]
  7. #![no_std]
  8. use panic_halt as _;
  9. use cortex_m_rt::entry;
  10. use stm32f1xx_hal::{
  11. pac,
  12. prelude::*,
  13. serial::{Config, Serial},
  14. };
  15. use core::fmt::Write;
  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, _rx) = serial.split();
  57. let number = 103;
  58. writeln!(tx, "Hello formatted string {}", number).unwrap();
  59. // for windows
  60. // write!(tx, "Hello formatted string {}\r\n", number).unwrap();
  61. loop {}
  62. }