嵌入式linux MTD么的读写
时间:2007-05-10
来源:互联网
哪位大哥写过嵌入式的MTD编程,也就是linux下对FLASH 的读写,看了一下驱动源代码
其中写函数如下:
static ssize_t mtd_write(struct file *file, const char *buf, size_t count,loff_t *ppos)
{
struct mtd_info *mtd = (struct mtd_info *)file->private_data;
char *kbuf;
size_t retlen;
size_t total_retlen=0;
int ret=0;
int len;
DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
if (*ppos == mtd->size)
return -ENOSPC;
if (*ppos + count > mtd->size)
count = mtd->size - *ppos;
if (!count)
return 0;
while (count) {
if (count > MAX_KMALLOC_SIZE)
len = MAX_KMALLOC_SIZE;
else
len = count;
kbuf=kmalloc(len,GFP_KERNEL);
if (!kbuf) {
printk("kmalloc is null\n");
return -ENOMEM;
}
if (copy_from_user(kbuf, buf, len)) {
kfree(kbuf);
return -EFAULT;
}
ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
if (!ret) {
*ppos += retlen;
total_retlen += retlen;
count -= retlen;
buf += retlen;
}
else {
kfree(kbuf);
return ret;
}
kfree(kbuf);
}
return total_retlen;
} /* mtd_write */
可是我在应用程序里该怎么调用呀?
int mtfbuf[10]={1,2,3,4};
write(mtdfd,mtdbuf,5);
然后再读出来
read(mtdfd,mtdbuf,5);
读出来的mtdbuf[0]怎么不等于1呀?
其中写函数如下:
static ssize_t mtd_write(struct file *file, const char *buf, size_t count,loff_t *ppos)
{
struct mtd_info *mtd = (struct mtd_info *)file->private_data;
char *kbuf;
size_t retlen;
size_t total_retlen=0;
int ret=0;
int len;
DEBUG(MTD_DEBUG_LEVEL0,"MTD_write\n");
if (*ppos == mtd->size)
return -ENOSPC;
if (*ppos + count > mtd->size)
count = mtd->size - *ppos;
if (!count)
return 0;
while (count) {
if (count > MAX_KMALLOC_SIZE)
len = MAX_KMALLOC_SIZE;
else
len = count;
kbuf=kmalloc(len,GFP_KERNEL);
if (!kbuf) {
printk("kmalloc is null\n");
return -ENOMEM;
}
if (copy_from_user(kbuf, buf, len)) {
kfree(kbuf);
return -EFAULT;
}
ret = (*(mtd->write))(mtd, *ppos, len, &retlen, kbuf);
if (!ret) {
*ppos += retlen;
total_retlen += retlen;
count -= retlen;
buf += retlen;
}
else {
kfree(kbuf);
return ret;
}
kfree(kbuf);
}
return total_retlen;
} /* mtd_write */
可是我在应用程序里该怎么调用呀?
int mtfbuf[10]={1,2,3,4};
write(mtdfd,mtdbuf,5);
然后再读出来
read(mtdfd,mtdbuf,5);
读出来的mtdbuf[0]怎么不等于1呀?
作者: zhouliang55 发布时间: 2007-05-10
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28