Minor cosmetics and comments

pull/4/head
Andre Richter 6 years ago
parent 298bec39c8
commit bd5827ba0e

@ -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<()> { pub fn call(&mut self, channel: u32) -> Result<()> {
// wait until we can write to the mailbox // wait until we can write to the mailbox
loop { loop {

@ -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<()> { pub fn init(&self, mbox: &mut mbox::Mbox) -> Result<()> {
// turn off UART0 // turn off UART0
unsafe { (*self.registers).CR.write(0) }; unsafe { (*self.registers).CR.write(0) };
@ -81,10 +82,14 @@ impl Uart {
// map UART0 to GPIO pins // map UART0 to GPIO pins
unsafe { unsafe {
let mut ret = (*gpio::GPFSEL1).read(); (*gpio::GPFSEL1).modify(|x| {
ret &= !((7 << 12) | (7 << 15)); // gpio14, gpio15 // Modify with a closure
ret |= (4 << 12) | (4 << 15); // alt0 let mut ret = x;
(*gpio::GPFSEL1).write(ret); ret &= !((7 << 12) | (7 << 15)); // gpio14, gpio15
ret |= (4 << 12) | (4 << 15); // alt0
ret
});
(*gpio::GPPUD).write(0); // enable pins 14 and 15 (*gpio::GPPUD).write(0); // enable pins 14 and 15
for _ in 0..150 { for _ in 0..150 {
@ -107,27 +112,27 @@ impl Uart {
Ok(()) Ok(())
} }
/// Send a character
pub fn send(&self, c: char) { pub fn send(&self, c: char) {
// wait until we can send unsafe {
loop { // wait until we can send
unsafe { loop {
if !(((*self.registers).FR.read() & 0x20) == 0x20) { if !(((*self.registers).FR.read() & 0x20) == 0x20) {
break; break;
} }
asm!("nop" :::: "volatile"); asm!("nop" :::: "volatile");
} }
}
// write the character to the buffer // write the character to the buffer
unsafe {
(*self.registers).DR.write(c as u32); (*self.registers).DR.write(c as u32);
} }
} }
/// Receive a character
pub fn getc(&self) -> char { pub fn getc(&self) -> char {
// wait until something is in the buffer unsafe {
loop { // wait until something is in the buffer
unsafe { loop {
if !(((*self.registers).FR.read() & 0x10) == 0x10) { if !(((*self.registers).FR.read() & 0x10) == 0x10) {
break; break;
} }
@ -146,6 +151,7 @@ impl Uart {
ret ret
} }
/// Display a string
pub fn puts(&self, string: &str) { pub fn puts(&self, string: &str) {
for c in string.chars() { for c in string.chars() {
// convert newline to carrige return + newline // convert newline to carrige return + newline
@ -157,6 +163,7 @@ impl Uart {
} }
} }
/// Display a binary value in hexadecimal
pub fn hex(&self, d: u32) { pub fn hex(&self, d: u32) {
let mut n; let mut n;

Loading…
Cancel
Save