Skip to content

Commit 2a118b3

Browse files
authored
feat(uart_sifive): add uart_sifive (#23)
1 parent 12be914 commit 2a118b3

File tree

8 files changed

+321
-2
lines changed

8 files changed

+321
-2
lines changed

Cargo.lock

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[workspace]
2-
members = ["uart8250", "uart_xilinx"]
2+
members = ["uart8250", "uart_xilinx", "uart_sifive"]

uart_sifive/Cargo.lock

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

uart_sifive/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "uart_sifive"
3+
version = "0.0.0"
4+
edition = "2024"
5+
authors = ["Woshiluo Luo"]
6+
license = "MIT"
7+
keywords = ["uart"]
8+
categories = ["embedded"]
9+
description = "This crate provide a struct with many methods to operate uarts in Sifive UART"
10+
homepage = "https://github.com/duskmoon314/uart-rs"
11+
repository = "https://github.com/duskmoon314/uart-rs"
12+
readme = "README.md"
13+
14+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
15+
16+
[dependencies]
17+
bitflags = "2"
18+
volatile-register = "0.2"
19+
20+
[features]
21+
default = []

uart_sifive/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# uart-sifive
2+
3+
**Work In Progress**
4+
5+
A simple struct and helper function for Sifive UART (`sifive,uart0`).
6+
7+
## REF
8+
9+
- <https://static.dev.sifive.com/SiFive-E300-platform-reference-manual-v1.0.1.pdf>
10+
11+
## Intro
12+
13+
**Noticed:** This crate may have problems. Any help would be welcomed, even if your help will bring about **breaking change**. Please feel free to start an Issue or a PR.
14+
15+
Currently I **cannot guarantee** the stability of this crate, and it is likely to introduce destructive updates (including but not limited to renaming of structs, renaming of functions and methods, code restructuring). So fixing the dependency version should be a good way to go.
16+
17+
Besides, this crate currently is not following [Rust API Guidelines](https://rust-lang.github.io/api-guidelines/). Please feel free to start an Issue or a PR to help me fix this.

uart_sifive/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*!
2+
# uart_sifive
3+
4+
A simple struct and helper function for Sifive UART (`sifive,uart0`).
5+
6+
## REF
7+
8+
- <https://static.dev.sifive.com/SiFive-E300-platform-reference-manual-v1.0.1.pdf>
9+
*/
10+
11+
#![no_std]
12+
13+
#[macro_use]
14+
extern crate bitflags;
15+
16+
pub mod registers;
17+
pub mod uart;
18+
19+
pub use uart::MmioUartSifive;

uart_sifive/src/registers.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use volatile_register::{RO, RW, WO};
2+
3+
/// # UART Registers
4+
#[repr(C)]
5+
pub struct Registers {
6+
pub tx: RW<u32>,
7+
pub rx: RO<u32>,
8+
pub txctrl: RW<u32>,
9+
pub rxctrl: RW<u32>,
10+
pub ie: RW<u32>,
11+
pub ip: RO<u32>,
12+
pub div: RW<u32>,
13+
}

uart_sifive/src/uart.rs

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
use super::registers::Registers;
2+
3+
bitflags! {
4+
/// TxData Register
5+
pub struct TxData: u32 {
6+
const FULL = 1 << 31;
7+
// const DATA = 0b1111_1111;
8+
}
9+
10+
/// RxData Register
11+
pub struct RxData: u32 {
12+
const EMPTY = 1 << 31;
13+
// const DATA = 0b1111_1111;
14+
}
15+
16+
/// TxControl Register
17+
pub struct TxControl: u32 {
18+
const ENABLE = 0b01;
19+
const NSTOP = 0b10;
20+
// const COUNT = 0b111 << 15;
21+
}
22+
23+
/// RxControl Register
24+
pub struct RxControl: u32 {
25+
const ENABLE = 0b01;
26+
const NSTOP = 0b10;
27+
// const COUNT = 0b111 << 15;
28+
}
29+
30+
/// This sturct use for `ie` and `ip` register
31+
pub struct InterruptRegister: u32 {
32+
const RXWM = 0b10;
33+
const TXWM = 0b01;
34+
}
35+
36+
// struct DivRegister: u32 {
37+
// const div = 1 << 16 - 1;
38+
// }
39+
}
40+
41+
/// # MMIO version of Sifive UART
42+
///
43+
/// **Noticed** This hasn't been tested.
44+
pub struct MmioUartSifive {
45+
reg_pointer: *mut Registers,
46+
}
47+
48+
impl MmioUartSifive {
49+
/// New a uart
50+
pub const fn new(base_address: usize) -> Self {
51+
Self {
52+
reg_pointer: base_address as _,
53+
}
54+
}
55+
56+
#[allow(clippy::mut_from_ref)]
57+
fn reg(&self) -> &mut Registers {
58+
unsafe { &mut *self.reg_pointer }
59+
}
60+
61+
/// Set a new base_address
62+
pub fn set_base_address(&mut self, base_address: usize) {
63+
self.reg_pointer = base_address as _;
64+
}
65+
66+
/// Read a byte
67+
pub fn read_byte(&self) -> Option<u8> {
68+
let rx = self.read_rx();
69+
let rx_empty = RxData::from_bits_truncate(rx).contains(RxData::EMPTY);
70+
if !rx_empty {
71+
Some(self.read_rx() as u8)
72+
} else {
73+
None
74+
}
75+
}
76+
77+
/// Write a byte
78+
pub fn write_byte(&self, value: u8) {
79+
self.write_tx(value as u32)
80+
}
81+
82+
/// Read Rx FIFO
83+
#[inline]
84+
pub fn read_rx(&self) -> u32 {
85+
self.reg().rx.read()
86+
}
87+
88+
#[inline]
89+
pub fn read_tx(&self) -> u32 {
90+
self.reg().tx.read()
91+
}
92+
93+
#[inline]
94+
pub fn write_tx(&self, value: u32) {
95+
unsafe { self.reg().tx.write(value) }
96+
}
97+
98+
#[inline]
99+
pub fn read_rxctrl(&self) -> u32 {
100+
self.reg().rxctrl.read()
101+
}
102+
103+
#[inline]
104+
pub fn write_rxctrl(&self, value: u32) {
105+
unsafe { self.reg().rxctrl.write(value) }
106+
}
107+
108+
#[inline]
109+
pub fn read_txctrl(&self) -> u32 {
110+
self.reg().txctrl.read()
111+
}
112+
113+
#[inline]
114+
pub fn write_txctrl(&self, value: u32) {
115+
unsafe { self.reg().txctrl.write(value) }
116+
}
117+
118+
#[inline]
119+
pub fn read_ip(&self) -> InterruptRegister {
120+
InterruptRegister::from_bits_truncate(self.reg().ip.read())
121+
}
122+
123+
#[inline]
124+
pub fn read_ie(&self) -> InterruptRegister {
125+
InterruptRegister::from_bits_truncate(self.reg().ie.read())
126+
}
127+
128+
#[inline]
129+
pub fn write_ie(&self, value: u32) {
130+
unsafe { self.reg().ie.write(value) }
131+
}
132+
133+
#[inline]
134+
pub fn read_div(&self) -> u32 {
135+
self.reg().div.read()
136+
}
137+
138+
#[inline]
139+
pub fn write_div(&self, value: u32) {
140+
unsafe { self.reg().div.write(value) }
141+
}
142+
143+
pub fn is_tx_fifo_full(&self) -> bool {
144+
TxData::from_bits_truncate(self.read_tx()).contains(TxData::FULL)
145+
}
146+
147+
pub fn is_read_interrupt_enabled(&self) -> bool {
148+
self.read_ie().contains(InterruptRegister::RXWM)
149+
}
150+
151+
pub fn is_write_interrupt_enabled(&self) -> bool {
152+
self.read_ie().contains(InterruptRegister::TXWM)
153+
}
154+
155+
pub fn enable_write(&self) {
156+
self.write_txctrl(self.read_txctrl() | TxControl::ENABLE.bits())
157+
}
158+
159+
pub fn enable_read(&self) {
160+
self.write_rxctrl(self.read_rxctrl() | RxControl::ENABLE.bits())
161+
}
162+
163+
pub fn disable_write(&self) {
164+
self.write_txctrl(self.read_txctrl() & !TxControl::ENABLE.bits())
165+
}
166+
167+
pub fn disable_read(&self) {
168+
self.write_rxctrl(self.read_rxctrl() & !RxControl::ENABLE.bits())
169+
}
170+
171+
pub fn disable_interrupt(&self) {
172+
self.write_ie(0)
173+
}
174+
175+
pub fn enable_read_interrupt(&self) {
176+
self.write_ie((self.read_ie() | InterruptRegister::RXWM).bits() as u32)
177+
}
178+
179+
pub fn enable_write_interrupt(&self) {
180+
self.write_ie((self.read_ie() | InterruptRegister::TXWM).bits() as u32)
181+
}
182+
183+
/// Read a slice
184+
pub fn read(&self, buf: &mut [u8]) -> usize {
185+
let mut count = 0;
186+
for current in buf {
187+
if let Some(ch) = self.read_byte() {
188+
count += 1;
189+
*current = ch;
190+
} else {
191+
break;
192+
}
193+
}
194+
count
195+
}
196+
197+
/// Write a slice
198+
pub fn write(&self, buf: &[u8]) -> usize {
199+
let mut count = 0;
200+
for current in buf {
201+
if self.is_tx_fifo_full() {
202+
break;
203+
}
204+
count += 1;
205+
self.write_byte(*current);
206+
}
207+
count
208+
}
209+
}

0 commit comments

Comments
 (0)