Skip to content

Commit 12be914

Browse files
authored
feat(uart_xilinx): Remove lifetime & add read write function (#21)
1 parent 9e7b6de commit 12be914

File tree

4 files changed

+80
-44
lines changed

4 files changed

+80
-44
lines changed

Cargo.lock

Lines changed: 17 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

uart_xilinx/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ readme = "README.md"
1616
[dependencies]
1717
# embedded-hal = { version = "=1.0.0-alpha.4", optional = true }
1818
# nb = { version = "1", optional = true }
19-
bitflags = "1"
19+
bitflags = "2"
2020
volatile-register = "0.2"
2121

2222
[features]

uart_xilinx/src/uart_lite/registers.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
use volatile_register::{RO, WO};
22

3-
macro_rules! cast {
4-
($expr:expr) => {
5-
unsafe { &mut *(($expr) as *mut super::registers::Registers) }
6-
};
7-
}
8-
93
/// # UART Registers
104
#[repr(C)]
115
pub struct Registers {

uart_xilinx/src/uart_lite/uart.rs

Lines changed: 62 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
use super::registers::Registers;
12
#[cfg(feature = "fmt")]
23
use core::fmt;
34

4-
use super::registers::Registers;
5-
65
bitflags! {
76
/// Status Register Bit Definitions
87
pub struct Status: u8 {
@@ -29,21 +28,26 @@ bitflags! {
2928
/// # MMIO version of XPS UART Lite
3029
///
3130
/// **Noticed** This hasn't been tested.
32-
pub struct MmioUartXpsLite<'a> {
33-
reg: &'a mut Registers,
31+
pub struct MmioUartXpsLite {
32+
reg_pointer: *mut Registers,
3433
}
3534

36-
impl<'a> MmioUartXpsLite<'a> {
35+
impl MmioUartXpsLite {
3736
/// New a uart
38-
pub fn new(base_address: usize) -> Self {
37+
pub const fn new(base_address: usize) -> Self {
3938
Self {
40-
reg: cast!(base_address),
39+
reg_pointer: base_address as _,
4140
}
4241
}
4342

43+
#[allow(clippy::mut_from_ref)]
44+
fn reg(&self) -> &mut Registers {
45+
unsafe { &mut *self.reg_pointer }
46+
}
47+
4448
/// Set a new base_address
4549
pub fn set_base_address(&mut self, base_address: usize) {
46-
self.reg = cast!(base_address);
50+
self.reg_pointer = base_address as _;
4751
}
4852

4953
/// Read a byte
@@ -63,25 +67,25 @@ impl<'a> MmioUartXpsLite<'a> {
6367
/// Read Rx FIFO
6468
#[inline]
6569
pub fn read_rx(&self) -> u32 {
66-
self.reg.rx.read()
70+
self.reg().rx.read()
6771
}
6872

6973
/// Write Tx FIFO
7074
#[inline]
7175
pub fn write_tx(&self, value: u32) {
72-
unsafe { self.reg.tx.write(value) }
76+
unsafe { self.reg().tx.write(value) }
7377
}
7478

7579
/// Read Uart Lite Status Register
7680
#[inline]
7781
pub fn read_stat(&self) -> u32 {
78-
self.reg.stat.read()
82+
self.reg().stat.read()
7983
}
8084

8185
/// Get Uart Lite Status
8286
#[inline]
8387
pub fn status(&self) -> Status {
84-
Status::from_bits_truncate(self.reg.stat.read().reverse_bits() as u8)
88+
Status::from_bits_truncate(self.reg().stat.read().reverse_bits() as u8)
8589
}
8690

8791
pub fn is_rx_fifo_valid(&self) -> bool {
@@ -119,7 +123,7 @@ impl<'a> MmioUartXpsLite<'a> {
119123
/// Write Uart Lite Control Register
120124
#[inline]
121125
pub fn write_ctrl(&self, value: u32) {
122-
unsafe { self.reg.ctrl.write(value) }
126+
unsafe { self.reg().ctrl.write(value) }
123127
}
124128

125129
pub fn enable_interrupt(&self) {
@@ -161,7 +165,7 @@ impl<'a> MmioUartXpsLite<'a> {
161165
///
162166
/// A simple implementation, may be changed in the future
163167
#[cfg(feature = "fmt")]
164-
impl<'a> fmt::Write for MmioUartXpsLite<'a> {
168+
impl fmt::Write for MmioUartXpsLite {
165169
fn write_str(&mut self, s: &str) -> fmt::Result {
166170
for c in s.as_bytes() {
167171
self.write_byte(*c);
@@ -173,21 +177,26 @@ impl<'a> fmt::Write for MmioUartXpsLite<'a> {
173177
/// # MMIO version of AXI UART Lite
174178
///
175179
/// **Noticed** This hasn't been tested.
176-
pub struct MmioUartAxiLite<'a> {
177-
reg: &'a mut Registers,
180+
pub struct MmioUartAxiLite {
181+
reg_pointer: *mut Registers,
178182
}
179183

180-
impl<'a> MmioUartAxiLite<'a> {
184+
impl MmioUartAxiLite {
181185
/// New a uart
182-
pub fn new(base_address: usize) -> Self {
186+
pub const fn new(base_address: usize) -> Self {
183187
Self {
184-
reg: cast!(base_address),
188+
reg_pointer: base_address as _,
185189
}
186190
}
187191

192+
#[allow(clippy::mut_from_ref)]
193+
fn reg(&self) -> &mut Registers {
194+
unsafe { &mut *self.reg_pointer }
195+
}
196+
188197
/// Set a new base_address
189198
pub fn set_base_address(&mut self, base_address: usize) {
190-
self.reg = cast!(base_address);
199+
self.reg_pointer = base_address as _;
191200
}
192201

193202
/// Read a byte
@@ -204,28 +213,55 @@ impl<'a> MmioUartAxiLite<'a> {
204213
self.write_tx(value as u32)
205214
}
206215

216+
/// Read a slice
217+
pub fn read(&self, buf: &mut [u8]) -> usize {
218+
let mut count = 0;
219+
for current in buf {
220+
if let Some(ch) = self.read_byte() {
221+
count += 1;
222+
*current = ch;
223+
} else {
224+
break;
225+
}
226+
}
227+
count
228+
}
229+
230+
/// Write a slice
231+
pub fn write(&self, buf: &[u8]) -> usize {
232+
let mut count = 0;
233+
for current in buf {
234+
if self.is_tx_fifo_full() {
235+
break;
236+
}
237+
count += 1;
238+
self.write_byte(*current);
239+
}
240+
count
241+
}
242+
207243
/// Read Rx FIFO
208244
#[inline]
209245
pub fn read_rx(&self) -> u32 {
210-
self.reg.rx.read()
246+
self.reg().rx.read()
211247
}
212248

213249
/// Write Tx FIFO
214250
#[inline]
215251
pub fn write_tx(&self, value: u32) {
216-
unsafe { self.reg.tx.write(value) }
252+
unsafe { self.reg().tx.write(value) }
217253
}
218254

219255
/// Read Uart Lite Status Register
220256
#[inline]
221257
pub fn read_stat(&self) -> u32 {
222-
self.reg.stat.read()
258+
self.reg().stat.read()
223259
}
224260

225261
/// Get Uart Lite Status
226262
#[inline]
227263
pub fn status(&self) -> Status {
228-
Status::from_bits_truncate(self.reg.stat.read() as u8)
264+
Status::from_bits_truncate(self.reg().stat.read() as u8)
229265
}
230266

231267
pub fn is_rx_fifo_valid(&self) -> bool {
@@ -263,7 +299,7 @@ impl<'a> MmioUartAxiLite<'a> {
263299
/// Write Uart Lite Control Register
264300
#[inline]
265301
pub fn write_ctrl(&self, value: u32) {
266-
unsafe { self.reg.ctrl.write(value) }
302+
unsafe { self.reg().ctrl.write(value) }
267303
}
268304

269305
pub fn enable_interrupt(&self) {
@@ -304,7 +340,7 @@ impl<'a> MmioUartAxiLite<'a> {
304340
///
305341
/// A simple implementation, may be changed in the future
306342
#[cfg(feature = "fmt")]
307-
impl<'a> fmt::Write for MmioUartAxiLite<'a> {
343+
impl fmt::Write for MmioUartAxiLite {
308344
fn write_str(&mut self, s: &str) -> fmt::Result {
309345
for c in s.as_bytes() {
310346
self.write_byte(*c);

0 commit comments

Comments
 (0)