博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux下擦除、写入flash的简单编程参考
阅读量:4055 次
发布时间:2019-05-25

本文共 3387 字,大约阅读时间需要 11 分钟。

 linux下对dataflash设备操作(擦除、写入文件)的应用程序,注意,所有的操作必须要使用mtdx的字符设备。

    mtdchar的字符设备驱动编程参考!

来自:


#include <unistd.h>

#include <stdlib.h>

#include <stdio.h>

#include <fcntl.h>

#include <time.h>

#include <sys/ioctl.h>

#include <sys/mount.h>

#include <linux/compiler.h>

#include <mtd/mtd-user.h>


int non_region_erase(int Fd, int start, int count, int unlock)

{

    mtd_info_t meminfo;


    if (ioctl(Fd,MEMGETINFO,&meminfo) == 0)

    {

        erase_info_t erase;


        erase.start = start;


        erase.length = meminfo.erasesize;


        for (; count > 0; count--) {

            printf("/rPerforming Flash Erase of length %u at offset 0x%x",

                    erase.length, erase.start);

            fflush(stdout);


            if(unlock != 0)

            {

                //Unlock the sector first.

                printf("/rPerforming Flash unlock at offset 0x%x",erase.start);

                if(ioctl(Fd, MEMUNLOCK, &erase) != 0)

                {

                    perror("/nMTD Unlock failure");

                    close(Fd);

                    return 8;

                }

            }


            if (ioctl(Fd,MEMERASE,&erase) != 0)

            {

                perror("/nMTD Erase failure");

                close(Fd);

                return 8;

            }

            erase.start += meminfo.erasesize;

        }

        printf(" done/n");

    }

    return 0;

}


int main(int argc, char *argv[])

{

    int fd,ifd;

    int i,j;

    char *cmd;

    char *img;

    struct mtd_info_user info;    

    int regcount;

    int res = 0,cnt,write_cnt;

    int imglen,imglen1;

    int readlen=0;

    char *write_buf;

    if(3>argc)

    {

        printf("You must specify a device!/n");

        return 16;

    }

    cmd=argv[1];


    if (argc > 3)

    {

        img=argv[3];

    }



    // Open and size the device

    if ((fd = open(argv[2],O_RDWR)) < 0)

    {

        fprintf(stderr,"File open error/n");

        return 8;

    }

    else

    {

        ioctl(fd,MEMGETINFO,&info);

        printf("info.size=%d/n info.erasesize=%d/n info.writesize=%d/n info.oobsize=%d                /n",info.size,info.erasesize,info.writesize,info.oobsize);

    }

    

    if (ioctl(fd,MEMGETREGIONCOUNT,&regcount) == 0)

    {

        printf("regcount=%d/n",regcount);

    }



    /*erase the device*/

    if (strcmp(cmd, "erase") == 0)

    {

        if(regcount == 0)

        {        

            res = non_region_erase(fd,0,(info.size/info.erasesize),0);        

        }        

        printf("erase!/n");

    }


    /*write file to this device*/

    if (strcmp(cmd, "write") == 0)

    {

        printf("write!/n");

        if((ifd=open(img,O_RDONLY))==-1)

        {

            printf("open input file error!/n");            

            return 9;

        }

        else

        {        

            imglen=lseek(ifd,0,SEEK_END);

            lseek(ifd,0,SEEK_SET);

            printf("The input file image len is %d!/n",imglen);

        }

        

        readlen=info.writesize;

        write_buf=malloc(readlen);

        if(write_buf==NULL)

        {

            printf("can't allocate memory!/n");

            return 10;

        }        

        else

        printf("Write buf is ok!/n");

        

        imglen1=imglen;

        while(imglen)

        {

                memset(write_buf,0xff,readlen);                

                if((cnt=read(ifd,write_buf,readlen))!=readlen)                        

                {

                    if(cnt<readlen)                    

                    printf("the file is end!/n");

                    else

                    {

                        printf("File I/O error on input file");

                        return 11;

                    }

                }

                if((write_cnt=write(fd,write_buf,readlen))!=readlen)

                {

                    printf("write mtd device error! startaddr=%d/n",(imglen1-imglen));

                    return 12;

                }

                else

                printf("/rwrite mtd device ok!startaddr=%d",(imglen1-imglen));


                imglen-=cnt;

        }

        printf("/n");

        return 0;

    }







    if (strcmp(cmd, "read") == 0)

    {

        printf("read!/n");

    }



    return 1;

}



命令格式:


命令:

dataflash_ioctl erase /dev/mtd6 


info.size=1968384

 info.erasesize=528

 info.writesize=528

 info.oobsize=0                         

regcount=0

Performing Flash Erase of length 528 at offset 0x1e06f0 done

erase!


写入文件命令:

dataflash_ioctl write /dev/mtd6 /modules/uImage2.6.21 


info.size=1968384

 info.erasesize=528

 info.writesize=528

 info.oobsize=0                         

regcount=0

write!

The input file image len is 1406740!

Write buf is ok!

write mtd device ok!startaddr=1406064the file is write ok!

write mtd device ok!startaddr=1406592



修改u-boot的启动参数

cp.b d023f700 20400000 158000

其中d023f700是mtd6在中,u-boot识别的地址,20400000是内存地址,0x158000是长度!

转载地址:http://xtoci.baihongyu.com/

你可能感兴趣的文章
解决python2.7中UnicodeEncodeError
查看>>
小谈python 输出
查看>>
Django objects.all()、objects.get()与objects.filter()之间的区别介绍
查看>>
python:如何将excel文件转化成CSV格式
查看>>
机器学习实战之决策树(一)
查看>>
机器学习实战之决策树二
查看>>
[LeetCode By Python]7 Reverse Integer
查看>>
[LeetCode By Python]121. Best Time to Buy and Sell Stock
查看>>
[LeetCode By Python]136. Single Number
查看>>
Android/Linux 内存监视
查看>>
Android2.1消息应用(Messaging)源码学习笔记
查看>>
iphone开发之SDK研究(待续)
查看>>
计算机网络复习要点
查看>>
Variable property attributes or Modifiers in iOS
查看>>
NSNotificationCenter 用法总结
查看>>
C primer plus 基础总结(一)
查看>>
剑指offer算法题分析与整理(三)
查看>>
mint/ubuntu安装搜狗输入法
查看>>
C++动态申请数组和参数传递问题
查看>>
opencv学习——在MFC中读取和显示图像
查看>>