Skip to main content

avr/addons/
uart.rs

1use Core;
2use Addon;
3use io;
4
5pub struct Uart
6{
7    /// The baud rate (bits/second)
8    pub baud: u64,
9    /// The number of CPU ticks in a single second (ticks/second)
10    pub cpu_frequency: u64,
11    /// Number of ticks between each bit.
12    ticks_between_bits: u64,
13
14    ticks_until_next_bit: u64,
15
16    _tx: io::Port,
17    _rx: io::Port,
18
19    _processed_bits: Vec<u8>,
20}
21
22impl Uart
23{
24    pub fn new(cpu_frequency: u64, baud: u64, tx: io::Port, rx: io::Port) -> Self {
25        let ticks_between_bits = cpu_frequency / baud;
26
27        Uart {
28            cpu_frequency: cpu_frequency,
29            baud: baud,
30            _tx: tx,
31            _rx: rx,
32
33            ticks_between_bits: ticks_between_bits, // TODO: set this variable
34            ticks_until_next_bit: ticks_between_bits,
35
36            _processed_bits: Vec::new(),
37        }
38    }
39
40    fn process_bit(&mut self, _core: &mut Core) {
41        println!("tick");
42    }
43}
44
45impl Addon for Uart
46{
47    fn tick(&mut self, core: &mut Core) {
48        self.ticks_until_next_bit -= 1;
49
50        if self.ticks_until_next_bit == 0 {
51            self.process_bit(core);
52            self.ticks_until_next_bit = self.ticks_between_bits;
53        }
54    }
55}
56