serial-dma-tx.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //! Serial interface DMA TX transfer test
  2. #![deny(unsafe_code)]
  3. #![deny(warnings)]
  4. #![no_main]
  5. #![no_std]
  6. extern crate cortex_m;
  7. extern crate cortex_m_rt as rt;
  8. extern crate panic_semihosting;
  9. extern crate stm32f1xx_hal as hal;
  10. use cortex_m::asm;
  11. use hal::prelude::*;
  12. use hal::serial::Serial;
  13. use hal::stm32f103xx;
  14. use rt::{entry, exception, ExceptionFrame};
  15. #[entry]
  16. fn main() -> ! {
  17. let p = stm32f103xx::Peripherals::take().unwrap();
  18. let mut flash = p.FLASH.constrain();
  19. let mut rcc = p.RCC.constrain();
  20. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  21. let mut afio = p.AFIO.constrain(&mut rcc.apb2);
  22. let channels = p.DMA1.split(&mut rcc.ahb);
  23. let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
  24. // let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
  25. // USART1
  26. let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
  27. let rx = gpioa.pa10;
  28. // USART1
  29. // let tx = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
  30. // let rx = gpiob.pb7;
  31. // USART2
  32. // let tx = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl);
  33. // let rx = gpioa.pa3;
  34. // USART3
  35. // let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
  36. // let rx = gpiob.pb11;
  37. let serial = Serial::usart1(
  38. p.USART1,
  39. (tx, rx),
  40. &mut afio.mapr,
  41. 9_600.bps(),
  42. clocks,
  43. &mut rcc.apb2,
  44. );
  45. let tx = serial.split().0;
  46. let (_, c, tx) = tx.write_all(channels.4, b"The quick brown fox").wait();
  47. asm::bkpt();
  48. let (_, c, tx) = tx.write_all(c, b" jumps").wait();
  49. asm::bkpt();
  50. tx.write_all(c, b" over the lazy dog.").wait();
  51. asm::bkpt();
  52. loop {}
  53. }
  54. #[exception]
  55. fn HardFault(ef: &ExceptionFrame) -> ! {
  56. panic!("{:#?}", ef);
  57. }
  58. #[exception]
  59. fn DefaultHandler(irqn: i16) {
  60. panic!("Unhandled exception (IRQn = {})", irqn);
  61. }