[驱动范例]RAM DISK

发布于 2013-03-06 10:17:00
测试时经常用到ram disk,虽然写了wiki,但每次都是重新创建一个文件再粘贴代码,
所以把完整文件备份在这里以方便使用。
#include 
#include

#define SECTOR_SIZE (512)
#define RAM_DISK_SIZE (1024*32)

static char ram_disk_buffer[RAM_DISK_SIZE];

/* 设备控制块 */
static struct rt_device ram_disk_device;

static rt_err_t ram_disk_init(rt_device_t dev)
{
return RT_EOK;
}

static rt_err_t ram_disk_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}

static rt_err_t ram_disk_close(rt_device_t dev)
{
return RT_EOK;
}

static rt_size_t ram_disk_read(rt_device_t dev,
rt_off_t pos,
void* buffer,
rt_size_t size)
{
memcpy(buffer, &ram_disk_buffer[SECTOR_SIZE * pos], SECTOR_SIZE * size);

return size;
}

static rt_size_t ram_disk_write(rt_device_t dev,
rt_off_t pos,
const void* buffer,
rt_size_t size)
{
memcpy(&ram_disk_buffer[SECTOR_SIZE * pos], buffer, SECTOR_SIZE * size);

return size;
}

static rt_err_t ram_disk_control(rt_device_t dev,
rt_uint8_t cmd,
void *args)
{
if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
{
struct rt_device_blk_geometry *geometry;

geometry = (struct rt_device_blk_geometry *)args;
if (geometry == RT_NULL) return -RT_ERROR;

geometry->bytes_per_sector = SECTOR_SIZE;
geometry->sector_count = RAM_DISK_SIZE / SECTOR_SIZE;
geometry->block_size = SECTOR_SIZE;
}

return RT_EOK;
}

void rt_hw_ram_disk_init(void)
{
/* 清零设备控制块 */
memset(&ram_disk_device, 0, sizeof(ram_disk_device));

/* 初始化设备控制块 */
ram_disk_device.type = RT_Device_Class_Block;
ram_disk_device.init = ram_disk_init;
ram_disk_device.open = ram_disk_open;
ram_disk_device.close = ram_disk_close;
ram_disk_device.read = ram_disk_read;
ram_disk_device.write = ram_disk_write;
ram_disk_device.control = ram_disk_control;

/* 向系统注册设备 */
rt_device_register(&ram_disk_device, "ram_disk",
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
}

查看更多

关注者
0
被浏览
3k
7 个回答
bernard
bernard 2013-03-06
512kB的RAMDISK不知道效果会如何,128kB呢?是否就只能是FAT12了
nongxiaoming
nongxiaoming 2013-04-15
RAMDISK?用在哪里多啊?我发现M3的ram好多都不够用的呢,这个能解决?
aozima
aozima 2013-04-15
拒绝白嫖,拒绝键盘侠!
楼上的回复好萌 [s:175]
RAMDISK 是专门把你所有RAM都吃掉的 [s:193]
nongxiaoming
nongxiaoming 2013-04-16
楼上的回复好萌 [s:175]
RAMDISK 是专门把你所有RAM都吃掉的 [s:193]

好吧,就这么点,还要吃掉啊~
luoyuncong
luoyuncong 2013-04-19
不错,刚好要用到
iwillbeback008
iwillbeback008 2013-05-10
标记一下,以后说不准有用

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览