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
use core::marker::PhantomData;
use core::fmt::{Write, Result};

use hal;
use hal::prelude::*;
use nb;

use nrf51::UART0;
use gpio::gpio::PIN;
use gpio::{Floating, Input, Output, PushPull};

pub use nrf51::uart0::baudrate::BAUDRATEW;
pub use nrf51::uart0::baudrate::BAUDRATEW::*;

/// Serial abstraction
pub struct Serial<UART> {
    uart: UART,
    txpin: PIN<Output<PushPull>>,
    rxpin: PIN<Input<Floating>>,
}

/// Serial receiver
pub struct Rx<UART> {
    _uart: PhantomData<UART>,
}

/// Serial transmitter
pub struct Tx<UART> {
    _uart: PhantomData<UART>,
}

#[derive(Debug)]
pub enum Error {}

impl Serial<UART0> {
    pub fn uart0(
        uart: UART0,
        txpin: PIN<Output<PushPull>>,
        rxpin: PIN<Input<Floating>>,
        speed: BAUDRATEW,
    ) -> Self {
        // Fill register with dummy data to trigger txd event
        uart.txd.write(|w| unsafe { w.bits(0) });

        // Set output TXD and RXD pins
        uart.pseltxd
            .write(|w| unsafe { w.bits(txpin.get_id().into()) });
        uart.pselrxd
            .write(|w| unsafe { w.bits(rxpin.get_id().into()) });

        // Set baud rate
        uart.baudrate.write(|w| w.baudrate().variant(speed));

        // Enable UART function
        uart.enable.write(|w| w.enable().enabled());

        // Fire up transmitting and receiving task
        uart.tasks_starttx.write(|w| unsafe { w.bits(1) });
        uart.tasks_startrx.write(|w| unsafe { w.bits(1) });

        Serial { uart, txpin, rxpin }
    }

    pub fn release(self) -> (UART0, PIN<Output<PushPull>>, PIN<Input<Floating>>) {
        (self.uart, self.txpin, self.rxpin)
    }

    pub fn split(self) -> (Tx<UART0>, Rx<UART0>) {
        (Tx { _uart: PhantomData }, Rx { _uart: PhantomData })
    }
}

impl hal::serial::Read<u8> for Rx<UART0> {
    type Error = Error;

    fn read(&mut self) -> nb::Result<u8, Self::Error> {
        let uart = unsafe { &*UART0::ptr() };
        match uart.events_rxdrdy.read().bits() {
            0 => Err(nb::Error::WouldBlock),
            _ => {
                // Read one 8bit value
                let byte = uart.rxd.read().bits() as u8;

                // Reset ready for receive event
                uart.events_rxdrdy.reset();

                Ok(byte)
            }
        }
    }
}

impl hal::serial::Write<u8> for Tx<UART0> {
    type Error = !;

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

    fn write(&mut self, byte: u8) -> nb::Result<(), Self::Error> {
        let uart = unsafe { &*UART0::ptr() };
        // Are we ready for sending out next byte?
        if uart.events_txdrdy.read().bits() == 1 {
            // Send byte
            uart.txd.write(|w| unsafe { w.bits(u32::from(byte)) });

            // Reset ready for transmit event
            uart.events_txdrdy.reset();

            Ok(())
        } else {
            // We're not ready, tell application to try again
            Err(nb::Error::WouldBlock)
        }
    }
}

impl<UART> Write for Tx<UART>
where
    Tx<UART>: hal::serial::Write<u8>,
{
    fn write_str(&mut self, s: &str) -> Result {
        let _ = s.as_bytes()
            .into_iter()
            .map(|c| block!(self.write(*c)))
            .last();
        Ok(())
    }
}