+ -
当前位置:首页 → 问答吧 → 嵌入式linux MTD么的读写

嵌入式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呀?      

作者: zhouliang55   发布时间: 2007-05-10