|
| 1 | +//! Simple (and unsafe) wrappers around USB devices. |
| 2 | +
|
| 3 | +use crate::raw; |
| 4 | +use crate::error::{Error, Result, to_result_void, to_result}; |
| 5 | + |
| 6 | +use core::ffi::{c_uchar, c_int}; |
| 7 | + |
| 8 | +use super::Unique; |
| 9 | + |
| 10 | +/// A wrapper around a UART device on Zephyr. |
| 11 | +pub struct Uart { |
| 12 | + /// The underlying device itself. |
| 13 | + #[allow(dead_code)] |
| 14 | + pub(crate) device: *const raw::device, |
| 15 | +} |
| 16 | + |
| 17 | +/// Uart control values. |
| 18 | +/// |
| 19 | +/// This mirrors these definitions from C, but as an enum. |
| 20 | +#[repr(u32)] |
| 21 | +pub enum LineControl { |
| 22 | + /// Baud rate |
| 23 | + BaudRate = raw::uart_line_ctrl_UART_LINE_CTRL_BAUD_RATE, |
| 24 | + /// Request To Send (RTS) |
| 25 | + RTS = raw::uart_line_ctrl_UART_LINE_CTRL_RTS, |
| 26 | + /// Data Terminal Ready (DTR) |
| 27 | + DTR = raw::uart_line_ctrl_UART_LINE_CTRL_DTR, |
| 28 | + /// Data Carrier Detect (DCD) |
| 29 | + DCD = raw::uart_line_ctrl_UART_LINE_CTRL_DCD, |
| 30 | + /// Data Set Ready (DSR) |
| 31 | + DSR = raw::uart_line_ctrl_UART_LINE_CTRL_DSR, |
| 32 | +} |
| 33 | + |
| 34 | +impl Uart { |
| 35 | + // Note that the `poll_in` and `poll_out` are terrible. |
| 36 | + |
| 37 | + /// Constructor, used by the devicetree generated code. |
| 38 | + #[allow(dead_code)] |
| 39 | + pub(crate) unsafe fn new(unique: &Unique, device: *const raw::device) -> Option<Uart> { |
| 40 | + if !unique.once() { |
| 41 | + return None; |
| 42 | + } |
| 43 | + |
| 44 | + Some(Uart { device }) |
| 45 | + } |
| 46 | + |
| 47 | + /// Attempt to read a character from the UART fifo. |
| 48 | + /// |
| 49 | + /// Will return Ok(Some(ch)) if there is a character available, `Ok(None)` if no character |
| 50 | + /// is available, or `Err(e)` if there was an error. |
| 51 | + pub unsafe fn poll_in(&mut self) -> Result<Option<u8>> { |
| 52 | + let mut ch: c_uchar = 0; |
| 53 | + |
| 54 | + match to_result_void(unsafe { raw::uart_poll_in(self.device, &mut ch) }) { |
| 55 | + Ok(()) => Ok(Some(ch as u8)), |
| 56 | + Err(Error(1)) => Ok(None), |
| 57 | + Err(e) => Err(e), |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Attempt to write to the outgoing FIFO. |
| 62 | + /// |
| 63 | + /// This writes to the outgoing UART fifo. This will block if the outgoing fifo is full. |
| 64 | + pub unsafe fn poll_out(&mut self, out_char: u8) { |
| 65 | + unsafe { raw::uart_poll_out(self.device, out_char as c_uchar) } |
| 66 | + } |
| 67 | + |
| 68 | + /// Fill FIFO with data. |
| 69 | + /// |
| 70 | + /// This is unspecified what happens if this is not called from IRQ context. |
| 71 | + /// Returns Ok(n) for the number of bytes sent. |
| 72 | + pub unsafe fn fifo_fill(&mut self, data: &[u8]) -> Result<usize> { |
| 73 | + to_result(unsafe { |
| 74 | + raw::uart_fifo_fill(self.device, data.as_ptr(), data.len() as c_int) |
| 75 | + }).map(|count| count as usize) |
| 76 | + } |
| 77 | + |
| 78 | + /// Drain FIFO. |
| 79 | + /// |
| 80 | + /// This is unspecified as to what happens if not called from IRQ context. |
| 81 | + pub unsafe fn fifo_read(&mut self, data: &mut [u8]) -> Result<usize> { |
| 82 | + to_result(unsafe { |
| 83 | + raw::uart_fifo_read(self.device, data.as_mut_ptr(), data.len() as c_int) |
| 84 | + }).map(|count| count as usize) |
| 85 | + } |
| 86 | + |
| 87 | + /// Read one of the UART line control values. |
| 88 | + pub unsafe fn line_ctrl_get(&self, item: LineControl) -> Result<u32> { |
| 89 | + let mut result: u32 = 0; |
| 90 | + to_result_void(unsafe { |
| 91 | + raw::uart_line_ctrl_get(self.device, item as u32, &mut result) |
| 92 | + }).map(|()| result) |
| 93 | + } |
| 94 | +} |
0 commit comments