serial-dma-circ.rs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //! Serial interface circular DMA RX transfer test
  2. #![deny(unsafe_code)]
  3. #![deny(warnings)]
  4. #![no_std]
  5. #![no_main]
  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::dma::Half;
  13. use hal::prelude::*;
  14. use hal::serial::Serial;
  15. use hal::stm32f103xx;
  16. use rt::{entry, exception, ExceptionFrame};
  17. #[entry]
  18. fn main() -> ! {
  19. let p = stm32f103xx::Peripherals::take().unwrap();
  20. let mut flash = p.FLASH.constrain();
  21. let mut rcc = p.RCC.constrain();
  22. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  23. let mut afio = p.AFIO.constrain(&mut rcc.apb2);
  24. let channels = p.DMA1.split(&mut rcc.ahb);
  25. let mut gpioa = p.GPIOA.split(&mut rcc.apb2);
  26. // let mut gpiob = p.GPIOB.split(&mut rcc.apb2);
  27. // USART1
  28. let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh);
  29. let rx = gpioa.pa10;
  30. // USART1
  31. // let tx = gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl);
  32. // let rx = gpiob.pb7;
  33. // USART2
  34. // let tx = gpioa.pa2.into_alternate_push_pull(&mut gpioa.crl);
  35. // let rx = gpioa.pa3;
  36. // USART3
  37. // let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh);
  38. // let rx = gpiob.pb11;
  39. let serial = Serial::usart1(
  40. p.USART1,
  41. (tx, rx),
  42. &mut afio.mapr,
  43. 9_600.bps(),
  44. clocks,
  45. &mut rcc.apb2,
  46. );
  47. let rx = serial.split().1;
  48. let buf = singleton!(: [[u8; 8]; 2] = [[0; 8]; 2]).unwrap();
  49. let mut circ_buffer = rx.circ_read(channels.5, buf);
  50. while circ_buffer.readable_half().unwrap() != Half::First {}
  51. let _first_half = circ_buffer.peek(|half, _| *half).unwrap();
  52. while circ_buffer.readable_half().unwrap() != Half::Second {}
  53. let _second_half = circ_buffer.peek(|half, _| *half).unwrap();
  54. asm::bkpt();
  55. loop {}
  56. }
  57. #[exception]
  58. fn HardFault(ef: &ExceptionFrame) -> ! {
  59. panic!("{:#?}", ef);
  60. }
  61. #[exception]
  62. fn DefaultHandler(irqn: i16) {
  63. panic!("Unhandled exception (IRQn = {})", irqn);
  64. }