AnsweredAssumed Answered

MFS Non-contigous write

Question asked by Jeff Kadin on May 10, 2011
Latest reply on May 18, 2011 by Petr MacHala

 

MQX 3.6.2

Tower MCF52259

 

I am still trying to use the files from an SPI FLASH for the web server.  Some of this works but my problem lies in the whole file not being sent to the browser.

 

It is not any problem with the webserver since I tried to open and print the file outside of the webserver context and it does the same thing.

 

I can read and write the flash perfectly with low level read and writes but once I install MFS on top and try to write and read files I see the problem.

 

I have added print statements in my low level read and write routines to tell me what page is being written or read.

I have setup MFS as a block device with a 1024 block size which is the page size of my flash.

 

I expect to wrtie and read full pages at a time, and as such the write routine erase the page before it writes it.

 

I install MFS this way

     error_code = _io_mfs_install(webflsh_handle, filesystem_name, (_file_size)0);

I open the file system this way:

     filesystem_handle = fopen(filesystem_name, 0);
     error_code = ferror (filesystem_handle);

    if ( (error_code == MFS_NOT_A_DOS_DISK) )
     {
         printf("\nTrying to format");
         format_data.BYTES_PER_SECTOR = 1024;
         format_data.NUMBER_OF_SECTORS = 8000;
         format_data.HIDDEN_SECTORS = 0;
         format_data.RESERVED_SECTORS = 0;
         error_code = _io_ioctl(filesystem_handle, IO_IOCTL_FORMAT_TEST, &format_param_ptr);
         if (error_code != MFS_NO_ERROR)
         {
             printf("Error formatting MFS: %s\n", MFS_Error_text((uint_32)error_code));
             _task_block();
         }
     }

This is the printout during format

Rea 0
Rea 0
NOT A DOS DISK! You must format to continue.

Trying to format
Wri 1
Wri 2
Wri 3
Wri 4
Wri 5
Wri 6
Wri 7
Wri 8
Wri 9
Wri 10
Wri 11
Wri 12
Wri 13
Wri 14
Wri 15
Wri 16
Wri 17
Wri 18
Wri 19
Wri 20
Wri 21
Wri 22
Wri 23
Wri 24
Wri 25
Wri 26
Wri 27
Wri 28
Wri 29
Wri 30
Wri 31
Wri 32
Wri 33
Wri 34
Wri 35
Wri 36
Wri 37
Wri 0
Wri 0

Which looks good to me.

 

 

Then I check some parameters on the disk


Rea 1
Rea 2
Rea 3
Rea 4
Rea 4
Rea 5
0 Bad Clusters
Rea 0
Rea 1
Rea 1
Rea 2
Rea 3
Rea 4
Rea 4
Rea 5
Free Space is 1222656
The current directory is: \
Cluster Size is 2048

 

Now I am ready to copy

 

My last try was to change the cache mode to off

         ioctl(filesystem_handle,IO_IOCTL_GET_FAT_CACHE_MODE,&mode);
         printf("\nCache Mode=%d",mode);
         mode = MFS_WRITE_THROUGH_CACHE;
         ioctl(filesystem_handle,IO_IOCTL_SET_FAT_CACHE_MODE,&mode);
         ioctl(filesystem_handle,IO_IOCTL_GET_FAT_CACHE_MODE,&mode);
         printf("\nCache Mode=%d",mode);

Now time to open the files and copy

     printf ("Copying %s\n", copy_name1);
     in_fd = fopen(copy_name1, "r");
     if (in_fd == NULL)  {
         printf("Error, unable to open source file\n" );
         _task_block();
         }
   
     out_fd = fopen(copy_to1, "w");
     if (out_fd == NULL)  {
         printf("Error, unable to open destination file\n" );
         _task_block();
         }

     buffer = NULL;
     buffer = _mem_alloc(COPY_BLOCK_SIZE);
     buf_fil = (char *)buffer;
    
     if (buffer == NULL)  {
         printf("Error, unable to alloc memory\n" );
         _task_block();
         }
     copysize = COPY_BLOCK_SIZE;
     copybuffer= buffer;
     do {
        size = read(in_fd, copybuffer, copysize);
        if (size > 0) {
            if(size < COPY_BLOCK_SIZE){
                while(size < COPY_BLOCK_SIZE)
                    buf_fil[size++] = 0;
            }
            //printf("\n%s",copybuffer);
           wsize = write(out_fd, copybuffer, copysize);
        }
        else
           break;
     } while (wsize == size);

Here is the log for the copy

Cache Mode=2
Cache Mode=0
Copying

Rea 6
Rea 7
Rea 8
Rea 9
Rea 10
Rea 11
Rea 12
Rea 13
Rea 14
Rea 15
Rea 16
Rea 17
Rea 18
Rea 19
Rea 6
Rea 7
Rea 8
Rea 9
Rea 10
Rea 11
Rea 12
Rea 13
Rea 14
Rea 15
Rea 16
Rea 17
Rea 18
Rea 19
Rea 6
Rea 7
Rea 8
Rea 9
Rea 10
Rea 11
Rea 12
Rea 13
Rea 14
Rea 15
Rea 16
Rea 17
Rea 18
Rea 19
Rea 0
Rea 1
    Wri 52
Wri 0
Wri 1
    Wri 53
    Wri 56
Wri 0
Wri 1
    Wri 57
    Wri 64
Wri 0
Wri 1
    Wri 65
NOTICE HOW THE PAGES ARE NOT CONTIGOUS.

 

Close the files

   if (buffer) _mem_free(buffer);
   if (in_fd) fclose(in_fd);
   if (out_fd) fclose(out_fd);

'Now re-open and try to read

 

 printf ("reading %s\n", copy_name1);
 in_fd = fopen(copy_to1, "r");
 if (in_fd == NULL)  {
     printf("Error, unable to open source file\n" );
     _task_block();
     }

 buffer = NULL;
 buffer = _mem_alloc(COPY_BLOCK_SIZE);
 if (buffer == NULL)  {
     printf("Error, unable to alloc memory\n" );
     _task_block();
     }
 wsize = COPY_BLOCK_SIZE;
 copysize = COPY_BLOCK_SIZE;
 copybuffer= buffer;
 do {
    size = read(in_fd, copybuffer, copysize);
    if (size > 0) {
       printf("\n%s",copybuffer);
    }
    else
       break;
 } while (size);

The printout for the read is:

Rea 6
Rea 7
Rea 8
Rea 9
Rea 10
Rea 11
Rea 12
Rea 13
Rea 14
Rea 15
Rea 16
Rea 17
Rea 18
Rea 19
    Rea 52
    Rea 53
Rea 0
Rea 1

Notice how the read stops after the first discontinuity from the wrtie, to be specific

pages written were 52, 53, 56, 57, 64, 65  pages read were 52, 53 and then it stops.

after reading page 53 the next read returns "0".

 

Any ideas anyone?

Thanks

Outcomes