My solution

  1. #![deny(unsafe_code)]
  2. #![no_main]
  3. #![no_std]
  4. #[allow(unused_imports)]
  5. use aux11::{entry, iprint, iprintln};
  6. use heapless::{consts, Vec};
  7. #[entry]
  8. fn main() -> ! {
  9. let (usart1, mono_timer, itm) = aux11::init();
  10. // A buffer with 32 bytes of capacity
  11. let mut buffer: Vec<u8, consts::U32> = Vec::new();
  12. loop {
  13. buffer.clear();
  14. loop {
  15. while usart1.isr.read().rxne().bit_is_clear() {}
  16. let byte = usart1.rdr.read().rdr().bits() as u8;
  17. if buffer.push(byte).is_err() {
  18. // buffer full
  19. for byte in b"error: buffer full\n\r" {
  20. while usart1.isr.read().txe().bit_is_clear() {}
  21. usart1.tdr.write(|w| w.tdr().bits(u16::from(*byte)));
  22. }
  23. break;
  24. }
  25. // Carriage return
  26. if byte == 13 {
  27. // Respond
  28. for byte in buffer.iter().rev().chain(&[b'\n', b'\r']) {
  29. while usart1.isr.read().txe().bit_is_clear() {}
  30. usart1.tdr.write(|w| w.tdr().bits(u16::from(*byte)));
  31. }
  32. break;
  33. }
  34. }
  35. }
  36. }