-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from YdrMaster/dev-uart
Merge
- Loading branch information
Showing
10 changed files
with
180 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod fbdev; | ||
mod input; | ||
mod random; | ||
mod uartdev; | ||
|
||
pub use fbdev::FbDev; | ||
pub use input::{EventDev, MiceDev}; | ||
pub use random::RandomINode; | ||
pub use uartdev::UartDev; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use alloc::sync::Arc; | ||
use core::any::Any; | ||
use rcore_fs::vfs::{make_rdev, FileType, FsError, INode, Metadata, PollStatus, Result, Timespec}; | ||
use rcore_fs_devfs::DevFS; | ||
use zcore_drivers::{scheme::UartScheme, DeviceError}; | ||
|
||
/// Uart device. | ||
pub struct UartDev { | ||
index: usize, | ||
port: Arc<dyn UartScheme>, | ||
inode_id: usize, | ||
} | ||
|
||
impl UartDev { | ||
pub fn new(index: usize, port: Arc<dyn UartScheme>) -> Self { | ||
Self { | ||
index, | ||
port, | ||
inode_id: DevFS::new_inode_id(), | ||
} | ||
} | ||
} | ||
|
||
impl INode for UartDev { | ||
fn read_at(&self, offset: usize, buf: &mut [u8]) -> Result<usize> { | ||
info!( | ||
"uart read_at: offset={:#x} buf_len={:#x}", | ||
offset, | ||
buf.len() | ||
); | ||
|
||
let mut len = 0; | ||
for b in buf.iter_mut() { | ||
match self.port.try_recv() { | ||
Ok(Some(b_)) => { | ||
*b = b_; | ||
len += 1; | ||
} | ||
Ok(None) => break, | ||
Err(e) => return Err(convert_error(e)), | ||
} | ||
} | ||
Ok(len) | ||
} | ||
|
||
fn write_at(&self, offset: usize, buf: &[u8]) -> Result<usize> { | ||
info!( | ||
"uart write_at: offset={:#x} buf_len={:#x}", | ||
offset, | ||
buf.len() | ||
); | ||
|
||
for b in buf { | ||
self.port.send(*b).map_err(convert_error)?; | ||
} | ||
Ok(buf.len()) | ||
} | ||
|
||
fn poll(&self) -> Result<PollStatus> { | ||
Ok(PollStatus { | ||
// TOKNOW and TODO | ||
read: true, | ||
write: false, | ||
error: false, | ||
}) | ||
} | ||
|
||
fn metadata(&self) -> Result<Metadata> { | ||
Ok(Metadata { | ||
dev: 1, | ||
inode: self.inode_id, | ||
size: 0, | ||
blk_size: 0, | ||
blocks: 0, | ||
atime: Timespec { sec: 0, nsec: 0 }, | ||
mtime: Timespec { sec: 0, nsec: 0 }, | ||
ctime: Timespec { sec: 0, nsec: 0 }, | ||
type_: FileType::CharDevice, | ||
mode: 0o600, // owner read & write | ||
nlinks: 1, | ||
uid: 0, | ||
gid: 0, | ||
rdev: make_rdev(4, self.index), | ||
}) | ||
} | ||
|
||
#[allow(unsafe_code)] | ||
fn io_control(&self, _cmd: u32, _data: usize) -> Result<usize> { | ||
warn!("uart ioctl unimplemented"); | ||
Ok(0) | ||
} | ||
|
||
fn as_any_ref(&self) -> &dyn Any { | ||
self | ||
} | ||
} | ||
|
||
fn convert_error(e: DeviceError) -> FsError { | ||
match e { | ||
DeviceError::NotSupported => FsError::NotSupported, | ||
DeviceError::NotReady => FsError::Busy, | ||
DeviceError::InvalidParam => FsError::InvalidParam, | ||
DeviceError::BufferTooSmall | ||
| DeviceError::DmaError | ||
| DeviceError::IoError | ||
| DeviceError::AlreadyExists | ||
| DeviceError::NoResources => FsError::DeviceError, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters