serial-dma-rx.rs 1.7 KB

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