Skip to content

Commit

Permalink
fix(device): read
Browse files Browse the repository at this point in the history
Signed-off-by: Zone.N <Zone.NiuZH@hotmail.com>
  • Loading branch information
MRNIU committed May 27, 2023
1 parent f4f4216 commit ce2a1b0
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 20 deletions.
2 changes: 2 additions & 0 deletions src/arch/riscv64/intr/plic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ static int32_t external_intr(int, char**) {
auto no = PLIC::get_instance().get();
// 根据中断号判断设备
printf("external_intr: 0x%X.\n", no);
PLIC::get_instance().do_externel_interrupt(no);
info("external_intr done: 0x%X.\n", no);
return 0;
}

Expand Down
17 changes: 14 additions & 3 deletions src/device/device_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,34 @@ device_base_t::~device_base_t(void) {
return;
}

int device_base_t::read(void) {
auto res = drv->read(buf);
int device_base_t::read(buf_t& _buf) {
buf.sector = _buf.sector;
auto res = drv->read(buf);

// 等待中断完成
while (buf.valid == false) {
;
}

memcpy(_buf.data, buf.data, COMMON::BUFFFER_SIZE);

buf.valid = false;

return res;
}

int device_base_t::write(void) {
int device_base_t::write(buf_t& _buf) {
auto res = drv->write(buf);

// 等待中断完成
while (buf.valid == false) {
;
}

memcpy(buf.data, _buf.data, COMMON::BUFFFER_SIZE);

buf.valid = false;

return res;
}

Expand Down
6 changes: 4 additions & 2 deletions src/device/include/device_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ class device_base_t {

/**
* @brief 从设备读
* @param _buf 缓冲区
*/
virtual int read(void);
virtual int read(buf_t& _buf);

/**
* @brief 向设备写
* @param _buf 缓冲区
*/
virtual int write(void);
virtual int write(buf_t& _buf);

/**
* @brief ioctl 控制
Expand Down
2 changes: 0 additions & 2 deletions src/drv/virtio/virtio_mmio_drv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ int virtio_mmio_drv_t::read(buf_t& _buf) {
req->sector = _buf.sector;
req->data = _buf.data;
auto ret = blk_rw(*req);
_buf.valid = true;
return ret;
}

Expand All @@ -288,7 +287,6 @@ int virtio_mmio_drv_t::write(buf_t& _buf) {
req->sector = _buf.sector;
req->data = _buf.data;
auto ret = blk_rw(*req);
_buf.valid = true;
return ret;
}

Expand Down
4 changes: 2 additions & 2 deletions src/drv/virtio/virtio_mmio_intr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ static void virtio_blk_handle_used(device_base_t* _dev, uint32_t _usedidx) {

delete req;

_dev->buf.valid = true;

drv->queue.free_desc(desc1);
drv->queue.free_desc(desc2);
drv->queue.free_desc(desc3);
Expand All @@ -106,5 +104,7 @@ void virtio_mmio_intr(uint8_t _no) {

drv->queue.virtq->seen_used = drv->queue.virtq->used->idx % len;

dev->buf.valid = true;

return;
}
5 changes: 0 additions & 5 deletions src/kernel/dev_drv_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
#include "virtio_dev.h"
#include "virtio_mmio_drv.h"

void virtio_intr_handler(void) {
warn("virtio irq handler.\n");
return;
}

void DEV_DRV_MANAGER::show(void) const {
info("bus count: 0x%X\n", buss.size());
for (auto i : buss) {
Expand Down
5 changes: 3 additions & 2 deletions src/kernel/kernel_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,13 @@ void kernel_main(void) {
TIMER::get_instance().init();
// 初始化设备
DEV_DRV_MANAGER::get_instance().init();
// 测试设备
test_device();

// 允许中断
CPU::ENABLE_INTR();

// 测试设备
test_device();

// 显示基本信息
show_info();
// 进入死循环
Expand Down
10 changes: 6 additions & 4 deletions src/kernel/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,14 @@ int test_device(void) {
device_base_t* dev
= (device_base_t*)DEV_DRV_MANAGER::get_instance().get_dev_via_intr_no(1);

dev->buf.sector = 0;
dev->read();
buf_t buf;
buf.sector = 0;

dev->read(buf);

// fat 第一个扇区的最后两字节
assert(dev->buf.data[COMMON::BUFFFER_SIZE - 1] == 0xAA);
assert(dev->buf.data[COMMON::BUFFFER_SIZE - 2] == 0x55);
assert(buf.data[COMMON::BUFFFER_SIZE - 1] == 0xAA);
assert(buf.data[COMMON::BUFFFER_SIZE - 2] == 0x55);

info("device test done.\n");
return 0;
Expand Down

0 comments on commit ce2a1b0

Please sign in to comment.