Skip to content

Commit

Permalink
uart write seems to work
Browse files Browse the repository at this point in the history
  • Loading branch information
sajattack committed Feb 18, 2019
1 parent 88f4e19 commit e08b393
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ authors = ["Paul Sajna <sajattack@gmail.com>"]
edition = "2018"
description = "Implements embedded-hal traits by bitbanging"
license = "MIT"
repository = "https://github.com/sajattack/bitbang-hal"
readme = "README.md"

[dependencies]
nb = "~0.1"
Expand Down
Empty file added README.md
Empty file.
71 changes: 71 additions & 0 deletions src/serial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use embedded_hal::digital::{OutputPin, InputPin};
use embedded_hal::blocking::delay::DelayUs;
use embedded_hal::serial;
use embedded_hal::serial::{Write, Read};
use crate::time::Hertz;

pub struct Serial<TX, /*RX,*/ Delay>
where
TX: OutputPin,
RX: InputPin,
Delay: DelayUs<u32>,
{
delay_time: u32,
tx: TX,
rx: RX,
delay: Delay,
}

impl <TX, RX, Delay> Serial <TX, RX, Delay>
where
TX: OutputPin,
RX: InputPin,
Delay: DelayUs<u32>
{
pub fn new<F: Into<Hertz>>(
baud: F,
tx: TX,
rx: RX,
delay: Delay
) -> Self {
let delay_time = 1_000_000 / (baud.into().0);
Serial {
delay_time: delay_time,
tx: tx,
rx: rx,
delay: delay
}
}
}

impl <TX, RX, Delay> serial::Write<u8> for Serial <TX, RX, Delay>
where
TX: OutputPin,
RX: InputPin,
Delay: DelayUs<u32>
{

type Error = ();

fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
let mut out_byte = byte;
self.tx.set_low(); // start bit
self.delay.delay_us(self.delay_time);
for _bit in 0..8 {
if out_byte & 1 == 1 {
self.tx.set_high();
} else {
self.tx.set_low();
}
out_byte >>= 1;
self.delay.delay_us(self.delay_time);
}
self.tx.set_high(); // stop bit
self.delay.delay_us(self.delay_time);
Ok(())
}

fn flush(&mut self) -> nb::Result<(), Self::Error> {
Ok(())
}
}

0 comments on commit e08b393

Please sign in to comment.