//! Serial interface echo server
//!
//! In this example every received byte will be sent back to the sender. You can test this example
//! with serial terminal emulator like `minicom`.
#![deny(unsafe_code)]
//#![deny(warnings)]
#![no_std]

#[macro_use(singleton)]
extern crate cortex_m;
extern crate stm32f4x_hal as f4;

use cortex_m::asm;
use f4::prelude::*;
use f4::serial::Serial;
use f4::stm32f4x;

fn main() {
    let p = stm32f4x::Peripherals::take().unwrap();

    let mut flash = p.FLASH.constrain();
    let mut rcc = p.RCC.constrain();
    let mut gpioa = p.GPIOA.split(&mut rcc.ahb1);
    let streams = p.DMA1.split(&mut rcc.ahb1);
    let rx_stream = streams.0.into_channel4(); // S5<C4>

    let clocks = rcc.cfgr.freeze(&mut flash.acr);

    let tx = gpioa.pa2.into_af7(&mut gpioa.moder, &mut gpioa.afrl);
    let rx = gpioa.pa3.into_af7(&mut gpioa.moder, &mut gpioa.afrl);

    let serial = Serial::usart2(p.USART2, (tx, rx), 115_200.bps(), clocks, &mut rcc.apb1);
    let (mut tx, mut rx) = serial.split();

    let buf = singleton!(: [u8; 8] = [0; 8]).unwrap();

    let (_buf, _c, _rx) = rx.read_exact(rx_stream, buf).wait();

    asm::bkpt();
}