#![no_main]
#![no_std]
use cortex_m_rt::entry;
use core::fmt::Write;
use heapless::{Vec, consts};
use rtt_target::rtt_init_print;
use panic_rtt_target as _;
#[cfg(feature = "v1")]
use microbit::{
hal::prelude::*,
hal::uart,
hal::uart::{Baudrate, Parity},
};
#[cfg(feature = "v2")]
use microbit::{
hal::prelude::*,
hal::uarte,
hal::uarte::{Baudrate, Parity},
};
#[cfg(feature = "v2")]
mod serial_setup;
#[cfg(feature = "v2")]
use serial_setup::UartePort;
#[entry]
fn main() -> ! {
rtt_init_print!();
let board = microbit::Board::take().unwrap();
#[cfg(feature = "v1")]
let mut serial = {
uart::Uart::new(
board.UART0,
board.uart.into(),
Parity::EXCLUDED,
Baudrate::BAUD115200,
)
};
#[cfg(feature = "v2")]
let mut serial = {
let serial = uarte::Uarte::new(
board.UARTE0,
board.uart.into(),
Parity::EXCLUDED,
Baudrate::BAUD115200,
);
UartePort::new(serial)
};
// 32バイト容量のバッファ
let mut buffer: Vec<u8, consts::U32> = Vec::new();
loop {
buffer.clear();
loop {
// 受信は失敗しないものと想定します。
let byte = nb::block!(serial.read()).unwrap();
if buffer.push(byte).is_err() {
writeln!(serial, "error: buffer full").unwrap();
break;
}
if byte == 13 {
for byte in buffer.iter().rev().chain(&[b'\n', b'\r']) {
nb::block!(serial.write(*byte)).unwrap();
}
break;
}
}
nb::block!(serial.flush()).unwrap()
}
}