1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
//! A serial.
//!
//! *WARNING* The current implementation of this will only work on ATmega328.

use core::prelude::v1::*;
use core::ptr::{read_volatile, write_volatile};

pub enum CharacterSize {
    FiveBits,
    SixBits,
    SevenBits,
    EightBits,
    NineBits,
}

impl CharacterSize {
    /// Returns bits for UCSR0B, UCSR0C
    #[inline]
    fn bits(&self) -> (u8, u8) {
        use self::CharacterSize::*;

        match *self {
            FiveBits => (0, 0 | 0),
            SixBits => (0, 0 | UCSZ00),
            SevenBits => (0, UCSZ01 | 0),
            EightBits => (0, UCSZ01 | UCSZ00),
            // Reserved => (UCSZ02, 0      | 0     ),
            // Reserved => (UCSZ02, 0      | UCSZ00),
            // Reserved => (UCSZ02, UCSZ01 | 0     ),
            NineBits => (UCSZ02, UCSZ01 | UCSZ00),
        }
    }

    #[inline]
    fn mask() -> (u8, u8) {
        (!(UCSZ01 | UCSZ00), !(UCSZ02))
    }
}

pub enum Mode {
    Asynchronous,
    Synchronous,
    MasterSpi,
}

impl Mode {
    #[inline]
    fn bits(&self) -> u8 {
        use self::Mode::*;

        match *self {
            Asynchronous => 0 | 0,
            Synchronous => 0 | UMSEL00,
            // Reserved  => UMSEL01 | 0,
            MasterSpi => UMSEL01 | UMSEL00,
        }
    }

    #[inline]
    fn mask() -> u8 {
        !(UMSEL01 | UMSEL00)
    }
}

pub enum Parity {
    Disabled,
    Even,
    Odd,
}

impl Parity {
    #[inline]
    fn bits(&self) -> u8 {
        use self::Parity::*;

        match *self {
            Disabled => 0 | 0,
            // Reserved => 0     | UPM00,
            Even => UPM01 | 0,
            Odd => UPM01 | UPM00,
        }
    }

    #[inline]
    fn mask() -> u8 {
        !(UPM01 | UPM00)
    }
}

pub enum StopBits {
    OneBit,
    TwoBits,
}

impl StopBits {
    #[inline]
    fn bits(&self) -> u8 {
        use self::StopBits::*;

        match *self {
            OneBit => 0,
            TwoBits => USBS0,
        }
    }

    #[inline]
    fn mask() -> u8 {
        !USBS0
    }
}

/// A serial connection.
/// *WARNING* The current implementation of this will only work on ATmega328.
pub struct Serial {
    ubrr: u16,
    a: u8,
    b: u8,
    c: u8,
}

impl Serial {
    #[inline]
    pub fn new(ubrr: u16) -> Self {
        Serial {
            ubrr: ubrr,
            a: 0,
            b: 0,
            c: 0,
        }
    }

    #[inline]
    pub fn character_size(mut self, character_size: CharacterSize) -> Self {
        let (b, c) = CharacterSize::mask();
        self.b &= b;
        self.c &= c;

        let (b, c) = character_size.bits();
        self.b |= b;
        self.c |= c;

        self
    }

    #[inline]
    pub fn mode(mut self, mode: Mode) -> Self {
        self.c &= Mode::mask();
        self.c |= mode.bits();
        self
    }

    #[inline]
    pub fn parity(mut self, parity: Parity) -> Self {
        self.c &= Parity::mask();
        self.c |= parity.bits();
        self
    }

    #[inline]
    pub fn stop_bits(mut self, stop_bits: StopBits) -> Self {
        self.c &= StopBits::mask();
        self.c |= stop_bits.bits();
        self
    }

    #[inline]
    pub fn configure(self) {
        unsafe {
            // Set Baud rate
            write_volatile(UBRR0, self.ubrr);

            write_volatile(UCSR0A, self.a);
            write_volatile(UCSR0B, self.b | RXEN0 | TXEN0);
            write_volatile(UCSR0C, self.c);
        }
    }
}

#[inline]
pub fn ready_to_transmit() -> bool {
    unsafe { (read_volatile(UCSR0A) & UDRE0) != 0 }
}

#[inline]
fn do_write(byte: u8) {
    unsafe {
        write_volatile(UDR0, byte);
    }
}

/// Does a blocking transfer of one byte
#[inline]
pub fn transmit(byte: u8) {
    while !ready_to_transmit() {}
    do_write(byte);
}

#[inline]
pub fn try_transmit(byte: u8) -> Result<(), ()> {
    if ready_to_transmit() {
        do_write(byte);
        Ok(())
    } else {
        Err(())
    }
}

#[inline]
pub fn ready_to_receive() -> bool {
    unsafe { (read_volatile(UCSR0A) & RXC0) != 0 }
}

#[inline]
fn do_read() -> u8 {
    unsafe { read_volatile(UDR0) }
}

/// Does a blocking read of one byte
#[inline]
pub fn receive() -> u8 {
    while !ready_to_receive() {}
    do_read()
}

#[inline]
pub fn try_receive() -> Option<u8> {
    if ready_to_receive() {
        Some(do_read())
    } else {
        None
    }
}

// Dirty hack.
// We should write this out and use the neat build-script method instead.
use self::hack::*;
mod hack {
    macro_rules! bit {
        (-, $pos:expr) => {};
        ($name:ident, $pos:expr) => {
            pub const $name: u8 = 1 << $pos;
        };
    }

    macro_rules! register {
        ($address:expr, $name:ident, [$b7:tt, $b6:tt, $b5:tt, $b4:tt, $b3:tt, $b2:tt, $b1:tt, $b0:tt]) => {
            register!($address, $name);
            bit!($b7, 7);
            bit!($b6, 6);
            bit!($b5, 5);
            bit!($b4, 4);
            bit!($b3, 3);
            bit!($b2, 2);
            bit!($b1, 1);
            bit!($b0, 0);
        };
        ($address:expr, $name:ident) => {
            pub const $name: *mut u8 = $address as *mut u8;
        };
    }

    register!(0xC6, UDR0);
    register!(0xC4, UBRR0L);
    register!(0xC2, UCSR0C, [UMSEL01, UMSEL00, UPM01, UPM00, USBS0, UCSZ01, UCSZ00, - ]);
    register!(0xC1, UCSR0B, [-, -, -, RXEN0, TXEN0, UCSZ02, -, - ]);
    register!(0xC0, UCSR0A, [RXC0, -, UDRE0, -, -, -, -, - ]);

    // 16-bit register pairs
    pub const UBRR0: *mut u16 = UBRR0L as *mut u16;
}