This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SPI Flash

Hi,

I want to read and write spi flash from user space.

Is there any API's ?

Thanks

  • Hi,

    Please check the AM335x McSPI Driver's Guide.

    The SPI flash device should be accessed via MTD-Utils from user space: http://processors.wiki.ti.com/index.php/MTD_Utilities#MTD-Utils_User-space_tools

    Best regards,
    Miroslav

  • Hi Miroslav,

    Thanks a lot for your reply.

    But in MTD_utilities i didn't find any command to read and write to spi flash.

    How can i directly access SPI flash memory from user space.

    Thanks

  • OK, here is an example:

    On my AM335x EVM's SPI flash there are 5 partitions (this is just an example):

    [    1.061096] Creating 5 MTD partitions on "spi_flash":
    [    1.066375] 0x000000000000-0x000000020000 : "SPL"
    [    1.072784] 0x000000020000-0x00000007f000 : "U-Boot"
    [    1.079284] 0x00000007f000-0x000000080000 : "U-Boot Env"
    [    1.086059] 0x000000080000-0x0000003e2000 : "Kernel"
    [    1.092468] 0x0000003e2000-0x000000800000 : "File System"

    When you cat /proc/mtd in user space you get:

    root@am335x-evm:~# cat /proc/mtd
    dev:    size   erasesize  name
    mtd0: 00020000 00001000 "SPL"
    mtd1: 0005f000 00001000 "U-Boot"
    mtd2: 00001000 00001000 "U-Boot Env"
    mtd3: 00362000 00001000 "Kernel"
    mtd4: 0041e000 00001000 "File System"

    There are the corresponding character devices /dev/mtd{0-4), which you can use to write/read the SPI flash partitions.

    This is a hexdump from an empty partition:

    root@am335x-evm:~# hexdump /dev/mtd1
    0000000 ffff ffff ffff ffff ffff ffff ffff ffff
    *
    005f000

    Then use flashcp from the MTD Utils package to copy something to the partition:

    root@am335x-evm:~# flashcp -v /media/mmcblk0p1/MLO /dev/mtd1
    Erasing blocks: 25/25 (100%)
    Writing data: 98k/0k (100%)
    Verifying data: 98k/0k (100%)

    Then do a hexdump again:

    root@am335x-evm:~# hexdump /dev/mtd1
    0000000 0040 0000 000c 0000 0000 0000 0000 0000
    0000010 0000 0000 4843 4553 5454 4e49 5347 0000
    0000020 ffff ffff ffff ffff ffff ffff ffff ffff
    *
    0000040 c0c1 c0c0 0100 0000 0000 0000 0000 0000
    0000050 0000 0000 0000 0000 0000 0000 0000 0000
    *
    0000200 86bd 0001 0400 402f 0014 ea00 f014 e59f
    0000210 f014 e59f f014 e59f f014 e59f f014 e59f
    0000220 f014 e59f f014 e59f 0420 402f 0424 402f

    ............................................................

    Then use flash_erase to erase this partition:

    root@am335x-evm:~# flash_erase /dev/mtd1 0 0
    Erasing 4 Kibyte @ 5e000 -- 100 % complete

    and do a hexdump again:

    root@am335x-evm:~# hexdump /dev/mtd1
    0000000 ffff ffff ffff ffff ffff ffff ffff ffff
    *
    005f000

    As you can see the partition is empty again.

    Please note that you can't just "read a file" from the partition as there is no file system on it, so it's just a contiguous memory space.

    Best regards,
    Miroslav

  • Hi Miroslav,

    Thank you so much for your fast reply.
    Usually in board file  MAC id is reading from eeprom using some read command.
    So is it possible to read from and write to a specific memory address of spi flash using the that method.
    Thanks
  • Hi,

    The EEPROM is connected to the I2C interface, so the MAC id is read via I2C inside the board file at boot time. Any changes to the board file will result in the necessity for recompiling the kernel. What I understood earlier was that you wish to read/write to the SPI flash from user space. As you can see here the SPI flash in the AM335x EVM passes through the MTD abstraction layer, so it can only be accessed by the user space MTD Utils and I'm not sure they support the type of operations you wish to perform.

    Perhaps you can fetch the source code from http://www.linux-mtd.infradead.org/ and modify or write your own tools. Please note that writing to NOR flashes requires the block you are wishing to write to, to first be erased.

    Best regards,
    Miroslav

  • Hi Miroslav,

          Thank you for each reply It helped me. I am also playing around SPI flash and tried what ever you have explained up here;  I am able to see data in mtd0 as  I have made only one drive mtd0 and also able to retrieve data from mtd0

    i.e cp from X location to /dev/mtd0 and from /dev/mtd0 to X location.

    But When I try to access the same after restart I am not able to find what ever the data I have written over there.

    It got lost. Pls. tell me how to save the data in SPI Flash. Is there any command or any keyword to sae the data.

    Thank You;

    Regards

    Praveen

  • Hi Praveen,

    Create a folder in /mnt

    mkdir /mnt/spi

    Mount the partition

    mount -t jffs2 /dev/mtdblock0 /mnt/spi

    Then you can read/write files to spi folder.

    Thanks

  • Hi ,

    I have tried the as u mention above making directory and then mounting but i am not able to mount it show

    " no device found "

    I have gone through McSPI Driver guide --

    In which it is written with media/card;

    it shows the same error

    " no device found " 

    Is something is missing??

    When I hexdump /dev/mtdblock0

    I get the same output and have checked spi directory and media/card directory nothing is found

    Regards

    Praveen Kumar

  • Hi Miroslav,

          I am tired to mount but it is not mounted ---

    One thing i noted that while booting it shows as follows:

    /etc/init.d/rc: line 176: /usr/bin/psplash-write: cannot execute binary file
    umount: can't umount /mnt/.psplash: Device or resource busy
     
     _____                    _____           _         _    
    |  _  |___ ___ ___ ___   |  _  |___ ___  |_|___ ___| |_  
    |     |  _| .'| . | . |  |   __|  _| . | | | -_|  _|  _|
    |__|__|_| |__,|_  |___|  |__|  |_| |___|_| |___|___|_|   
                  |___|                    |___|           

    Using the following command i came to know the one process is running:

    #lsof | grep 'mnt'              ----------------------------------------------(1)

    then

    #umount -l mnt/.psplash  after that it is unmounted after applying same command --(1) it didn't show any process

    follow the same process:

    flash_eraseall /dev/mtd0

    mount -t jffs2 /dev/mtdblock0  mnt/spi

    it is shows :

     mounting /dev/mtdblock0  on  mnt/spi failed : No Such Device;

    Pls give some Suggestion i will be very thank ful to you:

    Regards,

    Praveen

  • Hi Praveen,

    I was testing a specific SPI Flash memory part and I wrote some code to help me test it from User space.

    I hope the attached code can help you.  It's only some test code I previously used. It may do some unusual things, but I think you can see how to access memory from user space.

    inputs would be:

    • /dev/mtdX  where X would be 0,1,2,...
    • r,w,e for read, write or erase
    • address
    • size
  • hi  all,

    i am using spi flash for my AM335x unit

    the spi flash is detecting as MTD partition ( i have both spi and NAND connected)


    /dev/mtd0 and /dev/mtd1 are spi  partitions and mtd2, mtd3 .. are NAND partitions

    i am facing following problems


    1. flash_erase /dev/mtd1 0 0 is success but it is not erasing spi actually

    2. hexdump /dev/mtd1 (spi partition) is result in hanging the system

    3. flashcp -v /img/armboot_AM.img /dev/mtd1 is crashing the system

    can you please help me ?

    regards

    Nagendra

  • Hi Nagendra,

    I'm not sure what is wrong for you, but you may want to try to post this to a new thread if you have not already.  A new thread will be seen by a much wider audience.  This thread may only be seen by those people who have posted here.  Also you may want to include information like the following:

    1. What board are you using (specific TI EVM or custom board)

    2. If custom, what type SPI mem. are you using.

    3 Check the boot logs and dmesg for any related error information.

    4. If none of the above help to find the issue, try to enable debug messages in the SPI driver.