Corrupted files after use "fwrite"

Designing hardware and software for systems that use the VS1010 MP3 Audio DSP Microcontroller.
Post Reply
Arek
Senior User
Posts: 111
Joined: Thu 2016-09-01 10:58

Corrupted files after use "fwrite"

Post by Arek »

Hi,

I use SPI memory for programm data and SD for mp3 files.
For setting data I place a small file of two bytes in SPI memory.
My problem: using procedure "WriteSPIbyte" to changing content of this file will corrupt other files. Data in SetFile is correct written.
(Changing two bytes in "SetFile.txt" corrupted data in more as 50 places.)

What is wrong?

Code: Select all

void WriteSPIbyte(u_int16 n){
FILE *f;

	f = fopen("s:SetFile.txt", "wb");
	if (f != NULL) {
		fwrite(&n, 1, 1, f);
		fclose(f);
	}
	else 	{ Err = 3; }
}
Many thanks for tips
am
User avatar
Panu
VSDSP Expert
Posts: 2829
Joined: Tue 2010-06-22 13:43

Re: Corrupted files after use "fwrite"

Post by Panu »

Hi!

Let's see. Your function works in my VS1010D board, which is, to be honest, a little bit surprising, because I thought that SPI flash file writing was forbidden by default, because there are severe memory limitations when you write to the SPI flash.

But let's see if we can find a common ground to agree upon. I converted your function into a small test program:

Code: Select all

#include <vo_stdio.h>
void WriteSPIbyte(u_int16 n){
	FILE *f;	
	f = fopen("s:SetFile.txt", "wb");
	if (f != NULL) {
		fwrite(&n, 1, 1, f);
		fclose(f);
	}	
}
	
ioresult main (char *params) {	
	WriteSPIbyte(0x4142);
}
This program works. It creates the file and writes its content:
spifilewrite.png
spifilewrite.png (32.83 KiB) Viewed 8077 times
I have two possible ideas of why it might be failing at your site:

1) Writing to SPI flash requires a large memory buffer for handling the read-erase-modfy-write cycle for the SPI flash. This buffer is allocated at a fixed address X:0x3000 in the VS1010 RAM. If this memory area is in use by your program, then it will write garbage. You can find out which memory areas are in use by calling RunLib("LIBLIST",NULL);. Also the MP3 decoder uses this memory.

2) The last block to be written to the SPI flash might not get committed to the SPI flash and might still only be in the RAM cache at x:0x3000. The way to force writing of this data to the SPI flash is to read some other area of the SPI flash.

Because of these two reasons, I thought that the kernel would immediately give an error when attempting to write to the SPI flash, and that you would need to enable writing separately to acknowledge that you know this issue. But it doesn't, it allows the write to occur, possiby failing if the memory is not free or the ram block is not committed.

3) The disk is already corrupted somehow before the write happens and writing makes this corruption worse. Try formatting your SPI flash and try again.

Does any of this give you an idea of what's going on at your site? Which VS1010 version you are using?

-Panu
Arek
Senior User
Posts: 111
Joined: Thu 2016-09-01 10:58

Re: Corrupted files after use "fwrite"

Post by Arek »

Hi Panu,

many thanks!

I use VS1010D in my application.
The SPI memory also contain driver for ogg etc. sound format.

Due to memory limitation i have divided my program into a few smaller ones.
Programs are run only once. For example:
Start with "usercode.dlx" then RunLib("s:A.dlx", "d")
or
Start with "usercode.dlx" then RunLib("s:B.dlx", "d")
or
Start with "usercode.dlx" then RunLib("s:C.dlx", "d")


Ad 1
Message from LIBLIST looks to be OK:
C#0d1fcd
VS1010D VSOS 4.20
Files:6. Buffers:3.
Runlevel 7
SPIF:c214, 2048K

SD:120 MiB
SpiCacheDrv v7
S: SPI Flash c214


Reset virtual memory
BootFromX(BOOT.X)
Loaded 830 bytes.
0: 0x1880:USERCODE.DL I[0x4000..0x414f] X[0x1880..0x18e3] Y[0x450..0x44f]
1: 0x18e4:usbChange.d I[0x4150..0x421b] X[0x18e4..0x1911] Y[0x450..0x44f]
2: 0x1912:LIBLIST.DLX I[0x421c..0x428d] X[0x1912..0x1995] Y[0x450..0x44f]

Ad 2
Read other file after write change nothing. Files sill will be corrupted.
Attached "test5.txt" file is damaged between address 0x600 and 0x7ff.


Ad 3
I have many boards and always get same error.
Optically (hex comparator), the destroyed files looks to be corrupted at the same address with same data.
Attachments
test5.txt
(48.15 KiB) Downloaded 300 times
User avatar
Panu
VSDSP Expert
Posts: 2829
Joined: Tue 2010-06-22 13:43

Re: Corrupted files after use "fwrite"

Post by Panu »

Hi!

Ok, based on your liblist output, the buffer memory is not allocated for your programs, it's only allocated for codecs.

Could it be that you have a file open for writing at the same time you have a music decoder loaded? That must not happen, ever.

Can you write your code so that you open the file, then write the data and then immediately close the file before playing music? Or do you already do that?

Furthermore, writing to the same flash from where you are running programs is always dangerous, due to possible power failures etc, even when the software is working 100%. So you should only write to the flash very rarely and never automatically. For example, it may be ok to write some user settings to the flash a few times during the unit's lifetime, when the user changes some setting. But it is not advisable to write to the flash during normal system operation, you should use a separate flash for that. You can also use the 32 bits of battery back up RAM in the RTC to save up to 32 bits of data.

-Panu
Arek
Senior User
Posts: 111
Joined: Thu 2016-09-01 10:58

Re: Corrupted files after use "fwrite"

Post by Arek »

Hi Panu,

thanks for checking my message.
as written earlier, I use a few small programs to manage the program needs.
The first one is a “Starter”. It checks: playing music or work in “usb mode” (runlevel 2) or ...
a very special case: “Writing procedure”. The last one will happen as a last step in our factory and take place one time only.

I write two byte and, of course, I check if the written value is correct. Between “Write” and “Read” is a gap (approx. 400ms).

Code: Select all

WriteSPIbyte(v1);  
DelayL(M04);
if ((ReadSPIbyte() == v1) && (Err == 0))
...
Contens of this procedures - see my first message in this thread.


Many thanks
am
User avatar
Panu
VSDSP Expert
Posts: 2829
Joined: Tue 2010-06-22 13:43

Re: Corrupted files after use "fwrite"

Post by Panu »

Hi...

Actually you never gave the source code of ReadSPIByte, at least at a short glance I cannot find it. But no worries, first try to put these lines of code after the fclose() call in your WriteSPIByte function:

Code: Select all

		if (currentBlockLoaded) {
			printf("Flush\n");
			SaveBlock(&spiBus);
		}
This is to save a possibly remaining unwritten SPI flash block in the internal cache RAM after writing and closing the file. I think that might solve the problem. This code is taken from this posting: viewtopic.php?f=15&t=2261 (main.c file in the .zip file) so you can check what .h files etc you might need to get it compiled. Please tell me if it helps ...

-Panu



Code: Select all

#include <vs1010dRom.h>

void WriteSPIbyte(u_int16 n){
	FILE *f;	
	f = fopen("s:SetFile.txt", "wb");
	if (f != NULL) {
		fwrite(&n, 1, 1, f);
		fclose(f);
		if (currentBlockLoaded) {
			printf("Flush\n");
			SaveBlock(&spiBus);
		}
	}	
}
Arek
Senior User
Posts: 111
Joined: Thu 2016-09-01 10:58

Re: Corrupted files after use "fwrite"

Post by Arek »

Hi,

I make a few test . Unfortunately your tip did not bring the expected results.
After a few "fwrite" I try to play the sound files again and for ogg get:
TheRobotsO.ogg E'Bad libfile VR2'
Maybe important. As I've seen in terminal window, "currentBlockLoaded" was newer true.

many thanks
am
User avatar
Panu
VSDSP Expert
Posts: 2829
Joined: Tue 2010-06-22 13:43

Re: Corrupted files after use "fwrite"

Post by Panu »

Maybe important. As I've seen in terminal window, "currentBlockLoaded" was newer true.
Yes, very important. It shows that this is not the case.

We must look further. If you format the flash again, and start from the beginning, which step shows the first error?
Arek
Senior User
Posts: 111
Joined: Thu 2016-09-01 10:58

Re: Corrupted files after use "fwrite"

Post by Arek »

I suspect that the problem described in another thread ("Problem with automation") may have a common cause.
Post Reply