cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
vishnumotghare
Observer
Observer
18,216 Views
Registered: ‎07-02-2014

MTD flash read/write operation problem

Hi,

 

I am trying to read/write to mtd flash device. I am using following command.

 
 
dd if=/dev/urandom of=./new.bin bs=1 count=1024       /*it will create a file of 1 kb */
 
cat /dev/mtdchar0 < new.bin                                                /* write data into mtd flash device*/
 
cmp /dev/mtdchar0 new.bin                                                /* Compair data */
 
But  after compairing i am getting following error.
 
/dev/mtd0 new.bin differ: char 1, line 1
 
What could be the problem?  Is it problem with SPI driver?
 
Please help.
 
Thanks ,
Vishnu 
 
 
 
Tags (2)
0 Kudos
4 Replies
milosoftware
Scholar
Scholar
18,198 Views
Registered: ‎10-26-2012

Two errors:

- Your "cat" syntax is wrong, you meant: "cat new.bin > /dev/mtdchar0"

- You must use "flashcp" to write to the mtd device. Using "cat" will not ERASE it first.

 

0 Kudos
jpl@xiphos.ca
Adventurer
Adventurer
18,186 Views
Registered: ‎10-28-2007

If you have block device support for MTD, then you should also have /dev/mtdblock0 that you can use.  This will perform erases on blocks as required.

0 Kudos
vishnumotghare
Observer
Observer
18,179 Views
Registered: ‎07-02-2014

Hi All,

 

I have written a C apllication for erase/read/write . Now i am able to read the correct data from mtd flash.

 

#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <mtd/mtd-user.h>

int main()
{
    mtd_info_t mtd_info;           // the MTD structure
    erase_info_t ei;               // the erase block structure
    int i;

    unsigned char data[20] = { 0xDE, 0xAD, 0xBE, 0xEF,  // our data to write
                               0xDE, 0xAD, 0xBE, 0xEF,
                               0xDE, 0xAD, 0xBE, 0xEF,
                               0xDE, 0xAD, 0xBE, 0xEF,
                               0xDE, 0xAD, 0xBE, 0xEF};
    unsigned char read_buf[20] = {0x00};                // empty array for reading

    int fd = open("/dev/mtd0", O_RDWR); // open the mtd device for reading and writing
                                        
    ioctl(fd, MEMGETINFO, &mtd_info);   // get the device info

    // dump it for a sanity check, should match what's in /proc/mtd

    printf("MTD Type: %x\nMTD total size: %x bytes\nMTD erase size: %x bytes\n",
         mtd_info.type, mtd_info.size, mtd_info.erasesize);

    ei.length = mtd_info.erasesize;   //set the erase block size
    for(ei.start = 0; ei.start < mtd_info.size; ei.start += ei.length)
    {
        ioctl(fd, MEMUNLOCK, &ei);
        // printf("Eraseing Block %#x\n", ei.start); // show the blocks erasing
                                                  // warning, this prints a lot!
        ioctl(fd, MEMERASE, &ei);
    }    

    lseek(fd, 0, SEEK_SET);               // go to the first block
    read(fd, read_buf, sizeof(read_buf)); // read 20 bytes

    // sanity check, should be all 0xFF if erase worked

    for(i = 0; i<20; i++)
        printf("buf[%d] = 0x%02x\n", i, (unsigned int)read_buf[i]);

    lseek(fd, 0, SEEK_SET);        // go back to first block's start
    write(fd, data, sizeof(data)); // write our message

    lseek(fd, 0, SEEK_SET);              // go back to first block's start
    read(fd, read_buf, sizeof(read_buf));// read the data

    // sanity check, now you see the message we wrote!    

    for(i = 0; i<20; i++)
         printf("buf[%d] = 0x%02x\n", i, (unsigned int)read_buf[i]);

    close(fd);
    return 0;
}

 

Thnaks,

Vishnu

 

0 Kudos
milosoftware
Scholar
Scholar
18,170 Views
Registered: ‎10-26-2012

After installing a nice OS on your system, it's a bit of a waste to not use it... If you want to use flash for storage, mount a filesystem on it. Linux offers two especially for r/w access to flash memory (and a bunch of readonly ones too). Simplest is jffs2, it accepts empty (erased) flash. Just run: mkdir /media/flash mount -t jffs2 /dev/mtd2 /media/flash Now you can put files into your flash as if it were an ordinary USB stick. Linux will take care of wear leveling, erasing, allocating sectors, handling power-outs, bad sectors, and much more. If you want to store more than just a bit of configuration, or if your flash is large (say, over 8MB), you can better migrate to UBIFS. It's slightly more complicated to mount, but performs much better than jffs2. Note that you can also have your root filesystem in flash as ubi or jffs2.
0 Kudos