From 871e7a65735dab591ea8c60482a966b8ff68ea6e Mon Sep 17 00:00:00 2001 From: Dylan McKay Date: Sat, 25 Jul 2020 02:45:22 +1200 Subject: [PATCH] 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. --- examples/uart.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 examples/uart.rs diff --git a/examples/uart.rs b/examples/uart.rs new file mode 100644 index 0000000..ca9e02f --- /dev/null +++ b/examples/uart.rs @@ -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); + } +}