Skip to content

Commit 0879b5a

Browse files
committed
feat: 更新版本号至 0.6.0,重构 Serial 和 Receiver 结构,优化错误处理
1 parent 00e9471 commit 0879b5a

File tree

3 files changed

+307
-217
lines changed

3 files changed

+307
-217
lines changed

interface/rdif-serial/Cargo.toml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
[package]
2-
name = "rdif-serial"
3-
version = "0.5.4"
4-
edition.workspace = true
5-
repository.workspace = true
62
authors = ["周睿 <[email protected]>"]
7-
license = "MIT"
3+
categories = ["embedded", "no-std"]
84
description = "Driver Interface base definition."
5+
edition.workspace = true
96
keywords = ["os", "driver"]
10-
categories = ["embedded", "no-std"]
7+
license = "MIT"
8+
name = "rdif-serial"
9+
repository.workspace = true
10+
version = "0.6.0"
1111

1212
[dependencies]
13-
rdif-base = { workspace = true }
14-
futures = { version = "0.3", default-features = false, features = ["alloc"] }
15-
spin = "0.10"
16-
thiserror = { version = "2", default-features = false }
1713
bitflags = "2.8"
18-
mbarrier = "0.1"
14+
futures = {version = "0.3", default-features = false, features = ["alloc"]}
15+
rdif-base = {workspace = true}
16+
spin = "0.10"
17+
thiserror = {version = "2", default-features = false}
18+
heapless = "0.9"

interface/rdif-serial/src/lib.rs

Lines changed: 98 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
extern crate alloc;
44

5-
use core::any::Any;
5+
use core::{
6+
any::Any,
7+
fmt::{Debug, Display},
8+
num::NonZeroU32,
9+
};
610

711
use alloc::boxed::Box;
812
use bitflags::bitflags;
9-
use mbarrier::rmb;
1013

1114
pub use rdif_base::{DriverGeneric, KError};
1215

@@ -45,6 +48,22 @@ pub enum ConfigError {
4548
Timeout,
4649
}
4750

51+
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
52+
pub struct TransBytesError {
53+
pub bytes_transferred: usize,
54+
pub kind: TransferError,
55+
}
56+
57+
impl Display for TransBytesError {
58+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
59+
write!(
60+
f,
61+
"Transfer error after transferring {} bytes: {}",
62+
self.bytes_transferred, self.kind
63+
)
64+
}
65+
}
66+
4867
#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
4968
pub enum TransferError {
5069
#[error("Data overrun by `{0:#x}`")]
@@ -107,25 +126,6 @@ impl InterruptMask {
107126
}
108127
}
109128

110-
bitflags! {
111-
/// 线路状态标志
112-
#[derive(Debug, Clone, Copy)]
113-
pub struct LineStatus: u32 {
114-
const DATA_READY = 0x01;
115-
const TX_HOLDING_EMPTY = 0x20;
116-
}
117-
}
118-
119-
impl LineStatus {
120-
pub fn can_read(&self) -> bool {
121-
self.contains(LineStatus::DATA_READY)
122-
}
123-
124-
pub fn can_write(&self) -> bool {
125-
self.contains(LineStatus::TX_HOLDING_EMPTY)
126-
}
127-
}
128-
129129
#[derive(Debug, Clone, Default)]
130130
pub struct Config {
131131
pub baudrate: Option<u32>,
@@ -160,10 +160,12 @@ impl Config {
160160
}
161161
}
162162

163-
pub trait Register: Send + Sync + Any + 'static {
164-
// ==================== 基础数据传输 ====================
165-
fn write_byte(&mut self, byte: u8);
166-
fn read_byte(&mut self) -> Result<u8, TransferError>;
163+
pub trait InterfaceRaw: Send + Any + 'static {
164+
type IrqHandler: TIrqHandler;
165+
type Sender: TSender;
166+
type Reciever: TReciever;
167+
168+
fn base_addr(&self) -> usize;
167169

168170
// ==================== 配置管理 ====================
169171
fn set_config(&mut self, config: &Config) -> Result<(), ConfigError>;
@@ -172,7 +174,7 @@ pub trait Register: Send + Sync + Any + 'static {
172174
fn data_bits(&self) -> DataBits;
173175
fn stop_bits(&self) -> StopBits;
174176
fn parity(&self) -> Parity;
175-
fn clock_freq(&self) -> u32;
177+
fn clock_freq(&self) -> Option<NonZeroU32>;
176178

177179
fn open(&mut self);
178180
fn close(&mut self);
@@ -190,52 +192,48 @@ pub trait Register: Send + Sync + Any + 'static {
190192
fn set_irq_mask(&mut self, mask: InterruptMask);
191193
/// 获取当前中断使能掩码
192194
fn get_irq_mask(&self) -> InterruptMask;
193-
/// 获取并清除所有中断状态
194-
fn clean_interrupt_status(&mut self) -> InterruptMask;
195-
196-
// ==================== 传输状态查询 ====================
197-
198-
/// 获取线路状态
199-
fn line_status(&mut self) -> LineStatus;
200195

201-
// ==================== 底层寄存器访问 ====================
202-
/// 直接读取寄存器
203-
fn read_reg(&self, offset: usize) -> u32;
204-
/// 直接写入寄存器
205-
fn write_reg(&mut self, offset: usize, value: u32);
196+
fn irq_handler(&mut self) -> Option<Self::IrqHandler>;
197+
fn take_tx(&mut self) -> Option<Self::Sender>;
198+
fn take_rx(&mut self) -> Option<Self::Reciever>;
206199

207-
fn get_base(&self) -> usize;
208-
fn set_base(&mut self, base: usize);
200+
fn set_tx(&mut self, tx: Self::Sender) -> Result<(), SetBackError>;
201+
fn set_rx(&mut self, rx: Self::Reciever) -> Result<(), SetBackError>;
202+
}
209203

210-
fn read_buf(&mut self, buf: &mut [u8]) -> Result<usize, TransferError> {
211-
let mut read_count = 0;
212-
for byte in buf.iter_mut() {
213-
if !self.line_status().can_read() {
214-
break;
215-
}
216-
rmb();
217-
match self.read_byte() {
218-
Ok(b) => *byte = b,
219-
Err(e) => return Err(e),
220-
}
204+
#[derive(Clone, Copy)]
205+
pub struct SetBackError {
206+
want: usize,
207+
actual: usize,
208+
}
221209

222-
read_count += 1;
210+
impl SetBackError {
211+
/// Create a new SetBackError
212+
/// # Safety
213+
///
214+
pub fn new(want: usize, actual: usize) -> Self {
215+
Self {
216+
want: want as _,
217+
actual: actual as _,
223218
}
219+
}
220+
}
224221

225-
Ok(read_count)
222+
impl core::error::Error for SetBackError {}
223+
224+
impl Debug for SetBackError {
225+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
226+
write!(
227+
f,
228+
"Failed to set back, base address not eq {:#x} != {:#x}",
229+
self.want, self.actual
230+
)
226231
}
232+
}
227233

228-
fn write_buf(&mut self, buf: &[u8]) -> usize {
229-
let mut write_count = 0;
230-
for &byte in buf.iter() {
231-
if !self.line_status().can_write() {
232-
break;
233-
}
234-
rmb();
235-
self.write_byte(byte);
236-
write_count += 1;
237-
}
238-
write_count
234+
impl Display for SetBackError {
235+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
236+
Debug::fmt(self, f)
239237
}
240238
}
241239

@@ -244,17 +242,15 @@ pub trait Interface: DriverGeneric {
244242
fn take_tx(&mut self) -> Option<Box<dyn TSender>>;
245243
fn take_rx(&mut self) -> Option<Box<dyn TReciever>>;
246244
/// Base address of the serial port
247-
fn base(&self) -> usize;
248-
/// Set base address of the serial port
249-
fn set_base(&mut self, base: usize);
245+
fn base_addr(&self) -> usize;
250246

251247
fn set_config(&mut self, config: &Config) -> Result<(), ConfigError>;
252248

253249
fn baudrate(&self) -> u32;
254250
fn data_bits(&self) -> DataBits;
255251
fn stop_bits(&self) -> StopBits;
256252
fn parity(&self) -> Parity;
257-
fn clock_freq(&self) -> u32;
253+
fn clock_freq(&self) -> Option<NonZeroU32>;
258254

259255
fn enable_loopback(&mut self);
260256
fn disable_loopback(&mut self);
@@ -270,13 +266,43 @@ pub trait TIrqHandler: Send + Sync + 'static {
270266
}
271267

272268
pub trait TSender: Send + 'static {
273-
/// Send data from buf, return sent bytes. If return bytes is less than buf.len(), it means no more space, need to retry later.
274-
fn send(&mut self, buf: &[u8]) -> Result<usize, TransferError>;
269+
fn write_byte(&mut self, byte: u8) -> bool;
270+
271+
fn write_bytes(&mut self, bytes: &[u8]) -> usize {
272+
let mut written = 0;
273+
for &byte in bytes.iter() {
274+
if !self.write_byte(byte) {
275+
break;
276+
}
277+
written += 1;
278+
}
279+
written
280+
}
275281
}
276282

277283
pub trait TReciever: Send + 'static {
284+
fn read_byte(&mut self) -> Option<Result<u8, TransferError>>;
285+
278286
/// Recv data into buf, return recv bytes. If return bytes is less than buf.len(), it means no more data.
279-
fn recive(&mut self, buf: &mut [u8]) -> Result<usize, TransferError>;
287+
fn read_bytes(&mut self, bytes: &mut [u8]) -> Result<usize, TransBytesError> {
288+
let mut read_count = 0;
289+
for byte in bytes.iter_mut() {
290+
match self.read_byte() {
291+
Some(Ok(b)) => {
292+
*byte = b;
293+
}
294+
Some(Err(e)) => {
295+
return Err(TransBytesError {
296+
bytes_transferred: read_count,
297+
kind: e,
298+
});
299+
}
300+
None => break,
301+
}
302+
303+
read_count += 1;
304+
}
280305

281-
fn clean_fifo(&mut self);
306+
Ok(read_count)
307+
}
282308
}

0 commit comments

Comments
 (0)