// src/bin/main.rs
#![no_std]
#![no_main]

use defmt::*;
use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use embassy_stm32::usart::{Config, Uart};
use embassy_stm32::bind_interrupts;
use embassy_stm32::mode::Async;
use {defmt_rtt as _, panic_probe as _};

bind_interrupts!(
    struct Irqs {
        USART1 => embassy_stm32::usart::InterruptHandler<embassy_stm32::peripherals::USART1>;
    }
);

#[embassy_executor::task]
async fn receive_uart(mut uart: Uart<'static, Async>) {
    let mut buf = [0u8; 1];
    loop {
        unwrap!(uart.read(&mut buf).await);
        unwrap!(uart.write(&buf).await);
        unwrap!(uart.write(b"a\r\n").await);
        info!("uart received {}", buf[0]);
    }
}

#[embassy_executor::main]
async fn main(spawner: Spawner) {
    info!("tititititi");
    let p = embassy_stm32::init(Default::default());

    let config = Config::default();
    let usart = Uart::new(p.USART1, p.PA10, p.PA9, Irqs, p.GPDMA1_CH1, p.GPDMA1_CH0, config).unwrap();

    info!("starting echo");
    spawner.spawn(receive_uart(usart)).unwrap();

    let mut counter: u8 = 230;
    loop {
        counter = counter.wrapping_add(1);
        info!("CPU doing other work: {}", counter);
        Timer::after(Duration::from_millis(300)).await;

    }

}
