1#![no_std]
2
3extern crate alloc;
4
5use core::{
6 any::Any,
7 fmt::{Debug, Display},
8 num::NonZeroU32,
9};
10
11use alloc::boxed::Box;
12use bitflags::bitflags;
13
14pub use rdif_base::{DriverGeneric, KError};
15
16pub type BIrqHandler = Box<dyn TIrqHandler>;
17pub type BSender = Box<dyn TSender>;
18pub type BReciever = Box<dyn TReciever>;
19pub type BSerial = Box<dyn Interface>;
20
21impl DriverGeneric for Box<dyn Interface> {
22 fn open(&mut self) -> Result<(), KError> {
23 self.as_mut().open()
24 }
25
26 fn close(&mut self) -> Result<(), KError> {
27 self.as_mut().close()
28 }
29}
30
31mod serial;
32
33pub use serial::*;
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq)]
36pub enum ConfigError {
37 InvalidBaudrate,
39 UnsupportedDataBits,
41 UnsupportedStopBits,
43 UnsupportedParity,
45 RegisterError,
47 Timeout,
49}
50
51#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
52pub struct TransBytesError {
53 pub bytes_transferred: usize,
54 pub kind: TransferError,
55}
56
57impl 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
67#[derive(thiserror::Error, Debug, Clone, Copy, PartialEq, Eq)]
68pub enum TransferError {
69 #[error("Data overrun by `{0:#x}`")]
70 Overrun(u8),
71 #[error("Parity error")]
72 Parity,
73 #[error("Framing error")]
74 Framing,
75 #[error("Break condition")]
76 Break,
77 #[error("Serial closed")]
78 Closed,
79}
80
81#[derive(Debug, Clone, Copy, PartialEq, Eq)]
83#[repr(u8)]
84pub enum DataBits {
85 Five = 5,
86 Six = 6,
87 Seven = 7,
88 Eight = 8,
89}
90
91#[derive(Debug, Clone, Copy, PartialEq, Eq)]
93#[repr(u8)]
94pub enum StopBits {
95 One = 1,
96 Two = 2,
97}
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq)]
101pub enum Parity {
102 None,
103 Even,
104 Odd,
105 Mark,
106 Space,
107}
108
109bitflags! {
110 #[derive(Debug, Clone, Copy)]
112 pub struct InterruptMask: u32 {
113 const RX_AVAILABLE = 0x01;
115 const TX_EMPTY = 0x02;
116 }
117}
118
119impl InterruptMask {
120 pub fn rx_available(&self) -> bool {
121 self.contains(InterruptMask::RX_AVAILABLE)
122 }
123
124 pub fn tx_empty(&self) -> bool {
125 self.contains(InterruptMask::TX_EMPTY)
126 }
127}
128
129#[derive(Debug, Clone, Default)]
130pub struct Config {
131 pub baudrate: Option<u32>,
132 pub data_bits: Option<DataBits>,
133 pub stop_bits: Option<StopBits>,
134 pub parity: Option<Parity>,
135}
136
137impl Config {
138 pub fn new() -> Self {
139 Self::default()
140 }
141
142 pub fn baudrate(mut self, baudrate: u32) -> Self {
143 self.baudrate = Some(baudrate);
144 self
145 }
146
147 pub fn data_bits(mut self, data_bits: DataBits) -> Self {
148 self.data_bits = Some(data_bits);
149 self
150 }
151
152 pub fn stop_bits(mut self, stop_bits: StopBits) -> Self {
153 self.stop_bits = Some(stop_bits);
154 self
155 }
156
157 pub fn parity(mut self, parity: Parity) -> Self {
158 self.parity = Some(parity);
159 self
160 }
161}
162
163pub trait InterfaceRaw: Send + Any + 'static {
164 type IrqHandler: TIrqHandler;
165 type Sender: TSender;
166 type Reciever: TReciever;
167
168 fn base_addr(&self) -> usize;
169
170 fn set_config(&mut self, config: &Config) -> Result<(), ConfigError>;
172
173 fn baudrate(&self) -> u32;
174 fn data_bits(&self) -> DataBits;
175 fn stop_bits(&self) -> StopBits;
176 fn parity(&self) -> Parity;
177 fn clock_freq(&self) -> Option<NonZeroU32>;
178
179 fn open(&mut self);
180 fn close(&mut self);
181
182 fn enable_loopback(&mut self);
185 fn disable_loopback(&mut self);
187 fn is_loopback_enabled(&self) -> bool;
189
190 fn set_irq_mask(&mut self, mask: InterruptMask);
193 fn get_irq_mask(&self) -> InterruptMask;
195
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>;
199
200 fn set_tx(&mut self, tx: Self::Sender) -> Result<(), SetBackError>;
201 fn set_rx(&mut self, rx: Self::Reciever) -> Result<(), SetBackError>;
202}
203
204#[derive(Clone, Copy)]
205pub struct SetBackError {
206 want: usize,
207 actual: usize,
208}
209
210impl SetBackError {
211 pub fn new(want: usize, actual: usize) -> Self {
215 Self {
216 want: want as _,
217 actual: actual as _,
218 }
219 }
220}
221
222impl core::error::Error for SetBackError {}
223
224impl 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 )
231 }
232}
233
234impl Display for SetBackError {
235 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
236 Debug::fmt(self, f)
237 }
238}
239
240pub trait Interface: DriverGeneric {
241 fn irq_handler(&mut self) -> Option<Box<dyn TIrqHandler>>;
242 fn take_tx(&mut self) -> Option<Box<dyn TSender>>;
243 fn take_rx(&mut self) -> Option<Box<dyn TReciever>>;
244 fn base_addr(&self) -> usize;
246
247 fn set_config(&mut self, config: &Config) -> Result<(), ConfigError>;
248
249 fn baudrate(&self) -> u32;
250 fn data_bits(&self) -> DataBits;
251 fn stop_bits(&self) -> StopBits;
252 fn parity(&self) -> Parity;
253 fn clock_freq(&self) -> Option<NonZeroU32>;
254
255 fn enable_loopback(&mut self);
256 fn disable_loopback(&mut self);
257 fn is_loopback_enabled(&self) -> bool;
258
259 fn enable_interrupts(&mut self, mask: InterruptMask);
260 fn disable_interrupts(&mut self, mask: InterruptMask);
261 fn get_enabled_interrupts(&self) -> InterruptMask;
262}
263
264pub trait TIrqHandler: Send + Sync + 'static {
265 fn clean_interrupt_status(&self) -> InterruptMask;
266}
267
268pub trait TSender: Send + 'static {
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 }
281}
282
283pub trait TReciever: Send + 'static {
284 fn read_byte(&mut self) -> Option<Result<u8, TransferError>>;
285
286 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 }
305
306 Ok(read_count)
307 }
308}