Add a UART example

It uses the legacy module, but some more work will be required to have a
high-level UART interface based on the ruduino::Register trait.
This commit is contained in:
Dylan McKay 2020-07-25 02:45:22 +12:00
parent 65c363509c
commit 871e7a6573
1 changed files with 31 additions and 0 deletions

31
examples/uart.rs Normal file
View File

@ -0,0 +1,31 @@
#![no_std]
#![no_main]
//! Serial port example.
//!
//! The output is viewable with simavr
//!
//! ```
//! cargo build -Z build-std=core --target avr-atmega328p.json --examples --release
//! simavr -m atmega328p target/avr-atmega328p/release/examples/uart.elf
//! ```
use ruduino::legacy::serial;
#[no_mangle]
fn main() {
const CPU_FREQUENCY_HZ: u64 = 16_000_000;
const BAUD: u64 = 9600;
const UBRR: u16 = (CPU_FREQUENCY_HZ / 16 / BAUD - 1) as u16;
serial::Serial::new(UBRR)
.character_size(serial::CharacterSize::EightBits)
.mode(serial::Mode::Asynchronous)
.parity(serial::Parity::Disabled)
.stop_bits(serial::StopBits::OneBit)
.configure();
for &b in b"Hello, from Rust!\n" {
serial::transmit(b);
}
}