Author Topic: Write to/ read from file  (Read 7917 times)

Offline evgenik

  • Newbie
  • *
  • Posts: 18
    • View Profile
Write to/ read from file
« on: July 13, 2008, 07:19:00 AM »
Hi Mark.
At last at me began work SPI memory. But now I have collided with a following problem: at record in a file the first 5 bytes of the kept information are ignored and at file reading I receive '-1' in "uGetFileLength" function and no received bytes. If I to the initial address add 'FILE_HEADER' the information is read out, but with wrong long (more short, than have been requested).

This is my small code example:
    MEMORY_RANGE_POINTER ucFile;
    MAX_FILE_LENGTH byte_retrieved = 0;
    MAX_FILE_LENGTH file_offset = 0;
    MAX_FILE_SYSTEM_OFFSET FileLength = 0;
    u32 lAddress = 0;
    unsigned char ucBlockErase[] = {M95XXX_BLOCK_ERASE_4K,0,0,0};
    static const unsigned char ucWriteEnable[] = {M95XXX_WRITE_ENABLE};
    // test_buffer - array to be copied to test_buffer2
    for(i = 0; i < PageLen; i++)
    {
        if(i >= FILE_HEADER - 1)
        {
            test_buffer = i - FILE_HEADER;
        }
    }
    uMemset(test_buffer2, 0x0, PageLen);

     ucFile = uOpenFile("0File.txt");                          // open the file to be displayed
    lAddress = (u32)ucFile;

    ucBlockErase[3] = (unsigned char)(lAddress);
    ucBlockErase[2] = (unsigned char)((lAddress) >> 8);
    ucBlockErase[1] = (unsigned char)((lAddress) >> 16);
 
    fnSendSPIMessage((unsigned char *)ucWriteEnable, sizeof(ucWriteEnable)); // prepare write
    fnSendSPIMessage((unsigned char *)ucBlockErase, PageLen);                // prepare write
    // !!! for write file to EEPROM to file first 5 bytes are ignored for HEADER information
   uFileWrite((unsigned char*)ucFile, test_buffer, PageLen);                // save the received data to file. Existing files will automatically be deleted   
    if(uFileCloseMime((unsigned char*)ucFile,(unsigned char*)ucMimeType) != PageLen)
    {
        // error
        return;
     }
    ucFile = uOpenFile("0File.txt");                          // open the file to be displayed
    FileLength = uGetFileLength(ucFile);                  // Get opened file length
    do
    {
        byte_retrieved = uGetFileData(ucFile + FILE_HEADER, file_offset, test_buffer2, FileLength );
        if(byte_retrieved == 0)
        {
            return; // error
        }
        file_offset += byte_retrieved;
    }while(byte_retrieved < PageLen);

Where is my problem?
I have use with SP6.
Thanks. Evgeni.

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3236
    • View Profile
    • uTasker
Re: Write to/ read from file
« Reply #1 on: July 14, 2008, 02:10:54 PM »
Evgeni

It looks as though you are using an SPI EEPROM. I would suggest moving to an SPI FLASH (ATMEL for example) since they are larger and cheaper, and more suitable for files than the SPI EEPROMs. I have to admit not having tested the SPI EEPROM code for a long time since generally this is obsolete due to the (newer) SPI FLASH support (although I would expect to to still work...).

I see that you are sending some HW commands over the low level fnSendSPIMessage() interface. I don't think that these commands are normally needed and the standard file system interface should be possible.

I would suggest that you take a look at the FTP interface to the file system since this is a good reference for writing.
1.                     ptrFile = uOpenFile((CHAR *)(ucIp_Data+5));          // get file pointer (to new file or file to overwrite)
This opens the file when the "STOR" command is received
2.            uFileWrite(ptrFile, ucIp_Data, usPortLen SUBFILE_WRITE);      // save the received data to file. Existing files will automatically be deleted
This saves each frame received.
3.            uFileCloseMime(ptrFile, &ucMimeType);
This closes the file after all data has been received.

There is no need to know about the details of the header since this is handled in the file routines.

I don't understand how uGetFileLength() returns -1. It returns MAX_FILE_LENGTH, which is an unsigned value. If the length read from the memory is 0xffff (or 0xffffffff depending on MAX_FILE_LENGTH typedef) it will interpret it a blank memory and return 0. Therefore I would check this again to see whether you maybe misinterpreted it.

Regards

Mark