From bd5827ba0e6d2f6edfbf719092935285ebf2352c Mon Sep 17 00:00:00 2001 From: Andre Richter Date: Sun, 1 Apr 2018 20:39:57 +0200 Subject: [PATCH] Minor cosmetics and comments --- 05_uart0/src/mbox.rs | 1 + 05_uart0/src/uart.rs | 33 ++++++++++++++++++++------------- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/05_uart0/src/mbox.rs b/05_uart0/src/mbox.rs index d289afc4..537bafbd 100644 --- a/05_uart0/src/mbox.rs +++ b/05_uart0/src/mbox.rs @@ -93,6 +93,7 @@ impl Mbox { } } + /// Make a mailbox call. Returns Err(MboxError) on failure, Ok(()) success pub fn call(&mut self, channel: u32) -> Result<()> { // wait until we can write to the mailbox loop { diff --git a/05_uart0/src/uart.rs b/05_uart0/src/uart.rs index cfdb9978..3267baf3 100644 --- a/05_uart0/src/uart.rs +++ b/05_uart0/src/uart.rs @@ -61,6 +61,7 @@ impl Uart { } } + ///Set baud rate and characteristics (115200 8N1) and map to GPIO pub fn init(&self, mbox: &mut mbox::Mbox) -> Result<()> { // turn off UART0 unsafe { (*self.registers).CR.write(0) }; @@ -81,10 +82,14 @@ impl Uart { // map UART0 to GPIO pins unsafe { - let mut ret = (*gpio::GPFSEL1).read(); - ret &= !((7 << 12) | (7 << 15)); // gpio14, gpio15 - ret |= (4 << 12) | (4 << 15); // alt0 - (*gpio::GPFSEL1).write(ret); + (*gpio::GPFSEL1).modify(|x| { + // Modify with a closure + let mut ret = x; + ret &= !((7 << 12) | (7 << 15)); // gpio14, gpio15 + ret |= (4 << 12) | (4 << 15); // alt0 + + ret + }); (*gpio::GPPUD).write(0); // enable pins 14 and 15 for _ in 0..150 { @@ -107,27 +112,27 @@ impl Uart { Ok(()) } + /// Send a character pub fn send(&self, c: char) { - // wait until we can send - loop { - unsafe { + unsafe { + // wait until we can send + loop { if !(((*self.registers).FR.read() & 0x20) == 0x20) { break; } asm!("nop" :::: "volatile"); } - } - // write the character to the buffer - unsafe { + // write the character to the buffer (*self.registers).DR.write(c as u32); } } + /// Receive a character pub fn getc(&self) -> char { - // wait until something is in the buffer - loop { - unsafe { + unsafe { + // wait until something is in the buffer + loop { if !(((*self.registers).FR.read() & 0x10) == 0x10) { break; } @@ -146,6 +151,7 @@ impl Uart { ret } + /// Display a string pub fn puts(&self, string: &str) { for c in string.chars() { // convert newline to carrige return + newline @@ -157,6 +163,7 @@ impl Uart { } } + /// Display a binary value in hexadecimal pub fn hex(&self, d: u32) { let mut n;