/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / Sporadic glitches when writing to .txt file

Username:     
Password:     
             

Forum

# 1   2009-07-26 10:25:51 Sporadic glitches when writing to .txt file

DashboardDave
New member
Registered: 2009-07-02
Posts: 6

Sporadic glitches when writing to .txt file

Hi Guys,

I'm having a go at writing to a .txt file to a SD card in the Primer 2.

For the most part it seems to work, however every now and then i get a little glitch in the file.

Thought i would ask if:

1. Any thoughts on what could be causing the glitch?
2. Any suggestions for improvements to the code to stop the glitch from happening?

Please be gentle I'm a bit of a newb smile

Source (apologies for the parts blatently copied from others projects):

char data[13] = {"Hello World   "};

...

MENU_code Application_Ini ()
{
   data[11] = 0x0d;
   data[12] = 0x0a;

   StartMBR=FS_Mount(MMCSD_SDIO);

   if (StartMBR == 0xFFFFFFFF)
   {
      DRAW_DisplayString(20,100,"No SDCARD",19);
   }

   if (FS_GetVolumeInfo(0, StartMBR, &volume_info))
   {
     DRAW_DisplayString(20,100,"\nErr: GetVolInfo",19);
   }

   if (FS_OpenDirectory(&volume_info, "", &directory_info))
   {
      DRAW_DisplayString(20,100,"\nErr: Open Root",19);
   }

   if(FS_OpenFile(&volume_info, "Data.txt", FS_WRITE, &file_info))
   {
      DRAW_DisplayString(20,100,"No File",19);
   }
}


MENU_code Application_Handler ()
{
   if ( BUTTON_GetState() != BUTTON_PUSHED )
   {   
   
   if(FS_OpenFile(&volume_info, "Data.txt", FS_WRITE, &file_info))
   {
      DRAW_DisplayString(20,100,"No File",19);
   }
           
   FS_Seek(&file_info, (file_info.filelen));
                       
   if(FS_WriteFile(&file_info,data, &successcount,13))
   {
      DRAW_DisplayString(20,100,"Write Fail",19);
   }
       
   FS_Close(&file_info);

   }
}

This produces a file Data.txt with the following content:

"Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
HeHello WorldHello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World"

Last edited by DashboardDave (2009-07-26 10:27:27)

Offline

 

# 2   2009-07-27 02:22:53 Sporadic glitches when writing to .txt file

logictechs
Member
Registered: 2009-05-07
Posts: 68

Re: Sporadic glitches when writing to .txt file

Had similar problem.  Try adding this line of code at the beginning of your app ini section:
TIM_Cmd( TIM2, DISABLE );

Then when your done doing what you want to do add this line of code at the end of your program:  TIM_Cmd( TIM2, ENABLE );

Also make sure the length in your code here:  if(FS_WriteFile(&file_info,data, &successcount,13)) is correct.  I assume you have a line feed and carriage return at the end of Hello World?  I have gotten away with just using the carriage return 0x13 value to the end of the string.

Good luck!

Offline

 

# 3   2009-07-28 12:29:20 Sporadic glitches when writing to .txt file

DashboardDave
New member
Registered: 2009-07-02
Posts: 6

Re: Sporadic glitches when writing to .txt file

Thanks for the reply,

I tried adding in the suggested lines to turn off the timer but it didn't make a difference.

By changing the following line i got something that looks broken in notepad, but looks flawless in other programs like textpad / excel.

"FS_Seek(&file_info, (file_info.filelen -1));"

So i think as you suggested the issue has something to do with the lengths written.

This line:

char data[13] = {"Hello World "};

puts a '/0' in the last character? I wonder if writing or not writing this affects the .txt file?

I could only get it to work if a carriage return and a line feed is used. It did not work with just a carriage return.

Also if i run the application, it will stop at a txt file size of ~ 32k. What is the limiting factor here?

Offline

 

# 4   2009-07-28 21:48:43 Sporadic glitches when writing to .txt file

logictechs
Member
Registered: 2009-05-07
Posts: 68

Re: Sporadic glitches when writing to .txt file

I had the looks bad in notepad and not in another program problem also.  Try reformatting your SD card and put a file on it making sure it has something in it.  Think that cleared my problem.  I have found that FAT or FAT32 works ok.  I've asked about this but no response.

If you tell the Primer2 to write 13 bytes but only have 12, it will add the null (0x00) at the end of the file. 

Had the same problem about the file size limit.  I found a fix from Francis I believe in this forum but you should upgrade the Ride software to the latest version which is now 7.22.09.0203 and 1.20.09.0154 for the RKIT-ARM for Ride.  Believe it has the fix in it because I didn't have to mod the file again after installing the latest version.

I'm currently having a problem where it seems like every 255 or 256 bytes there is corruption for a couple of bytes.  There may be a buffer overflow occurring because I'm writing too fast?  I'm writing a stream of 64 bytes to the file on the card.  Should I put some delay in between the writes?  I've tried but it does not seem to help.  Any help would be greatly appreciated.

Good luck!

Offline

 

# 5   2009-07-29 07:36:49 Sporadic glitches when writing to .txt file

repzak
Member
Registered: 2008-03-05
Posts: 170

Re: Sporadic glitches when writing to .txt file

Hello,

I am not sure it is related, but are you sure the 64byte buffer is aligned to "word" in the memory?? I had problem reading to a buffer where the pointer to start point moves and that causes problem because the DMA function in SDcard file moves 4 bytes at a time and they must be alligned to words in memory...

I think the DMA write could have same issue?

Kasper

Offline

 

# 6   2009-07-29 19:58:14 Sporadic glitches when writing to .txt file

logictechs
Member
Registered: 2009-05-07
Posts: 68

Re: Sporadic glitches when writing to .txt file

Hi Kasper,

Thanks for the response.  I am collecting serial characters in a 64 byte char array.
like this:

char rxbuf[64];

/* Wait the byte is entirtly received by USART2 */ 
    while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
    {         
    }
    /* Store the received byte in the RxBuffer2 */
    rxb = USART_ReceiveData(USART2);
     
        while(NbrOfDataToRead--)
        {           
            rxbuf[RxCounter++] = rxb;
           
            /* Wait the byte is entirtly received by USART2 */ 
            while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
            {         
            }
            /* Store the received byte in the RxBuffer2 */
            rxb = USART_ReceiveData(USART2);       
        }

Then I have code that writes the array to the file like this:


char* rxbadr = rxbuf;

cr = FS_WriteFile(&file_info, rxbadr, &successcount, 64);

I've also tried bumping up the length to 256 but same result of corruption every 256 bytes and the corruption goes on for I think 5 or 6 bytes then works again till 256 bytes later.  I've formatted the SD card in FAT16 and FAT32.  It is a 2GB card.

Will try this next:
1. Open the file and seek to the end.
2. Write 64 bytes.
3. Close the file.
repeating these steps till all is collected.

Offline

 

# 7   2009-07-30 06:14:29 Sporadic glitches when writing to .txt file

repzak
Member
Registered: 2008-03-05
Posts: 170

Re: Sporadic glitches when writing to .txt file

Hello,

I am not sure it helps you, but you could put the char rxbuf[64] in a union with a u64 rxbuf[16].

Then you are sure it is alligned to word size...

But as i said i am not sure that is your problem

Kasper

Offline

 

# 8   2009-08-15 11:37:59 Sporadic glitches when writing to .txt file

DashboardDave
New member
Registered: 2009-07-02
Posts: 6

Re: Sporadic glitches when writing to .txt file

OK so i have had some success with my problems...

Putting "File Open" and "File Close" into Init and shutdown functions solved the file size limit issue. I can make 500kb+ files which is fantastic.

Using  FS_WriteFile(...

But only writing one byte at a time got rid of all glitches in my text files.

So i made a function that takes in my string but calls FS_WriteFile(... once to put out each byte and all my problems are solved!

I don't understand why this works, I assume some sort of buffer over flow in the FS library?

Last edited by DashboardDave (2009-08-15 11:38:34)

Offline

 

# 9   2009-09-17 13:06:45 Sporadic glitches when writing to .txt file

DashboardDave
New member
Registered: 2009-07-02
Posts: 6

Re: Sporadic glitches when writing to .txt file

Turns out my solution is not the end of the problem.

Writing the data one byte at a time is very slow.

I have the following lines in my application init:

        UTIL_SetPll(SPEED_VERY_HIGH);     
        MENU_SetAppliDivider(1);   

My application is simply sampling the MEMS X, Y, Z and converting to strings and writing to a .txt file on the SD card, the best "rate" writing the MEMS data to the .txt file i can achieve is a few lines a second.

By writing 4 bytes at a time the speed increases to about 10 lines a second, but the regular glitch in the text file returns!

I have started going through the Circle OS and FS library to try and understand how it works but its all abit over my head.

From what i understand the SD card runs off an SPI channel of the STM32. Can the SPI bus clock speed be increased to investigate speeding up of SD card writes?

Offline

 

# 10   2009-09-17 14:32:31 Sporadic glitches when writing to .txt file

Francis
Administrator
From: France-Grenoble
Registered: 2007-07-09
Posts: 890

Re: Sporadic glitches when writing to .txt file

There are several recent posts on the CircleOS section about similar issues. Did you read them ? I believe that the main issue was the need of writing packets.. you could try to bufferize your data.

You could also try to disable the timer2 interrupt while writing the data.

Offline

 

# 11   2010-12-22 10:26:15 Sporadic glitches when writing to .txt file

xuwa0800
Member
From: Sweden
Registered: 2010-05-20
Posts: 27

Re: Sporadic glitches when writing to .txt file

DashboardDave :

Putting "File Open" and "File Close" into Init and shutdown functions solved the file size limit issue. I can make 500kb+ files which is fantastic.

i have the same problem with the size problem, and try the way you suggested here, but failed again, I'm using OS3.7

what do you mean put  "File Open" and "File Close" into Init and shutdown functions, which shutdown funtion, the shutdown funtion for the kit?


/Wang

Offline

 

Board footer