Page 1 of 4

Write to start of sd card file

Posted: Thu Jul 21, 2022 9:47 pm
by WingNut
One for Ben again. I've been struggling for quite a while with trying to write a string to the start of an open file on an sd card. The string is the first 9 characters (currently). Attached is an image of the macro that does the writing (with help from Mnfisher) It simulates effectively (writes the first sector with the string at the start). However, it only works in simulation and only if slowed to 5hz. Can you tell me if I'm wasting mine and Martins time? If it cant be done with the existing sd macros I would rather know and use an alternative solution. But this idea is the start of bigger ambitions of writing to specific locations on a file on an sd card.

Re: Write to start of sd card file

Posted: Fri Jul 22, 2022 7:01 pm
by mnfisher
This is the 'offending' program.

It seems to work in SIM ok (at least on my machine) at any speed. Time to pull out a SD card reader and see if I can get it to work in hardware.
sd config.fcfx
(18.75 KiB) Downloaded 55 times
Martin

Re: Write to start of sd card file

Posted: Fri Jul 22, 2022 8:06 pm
by mnfisher
Not much joy - I can't find an SD card that works and can be formatted as fat16 - I don't know what I do to them - two SD and 2 micro SD fails so far (on Arduino and in Windows)

Some EEPROM might be more reliable?

Martin

Re: Write to start of sd card file

Posted: Fri Jul 22, 2022 8:11 pm
by WingNut
Martin I cant get yours to get past initializing the sd card tonight and now my own version wont simulate at any speed

Re: Write to start of sd card file

Posted: Sat Jul 23, 2022 12:19 pm
by WingNut
I've tried on another machine and it works fine again at 5hz but not faster. Overwriting the old cfg characters with the new

Re: Write to start of sd card file

Posted: Sun Jul 24, 2022 10:59 am
by mnfisher
I've just got it to work on Arduino - using software SPI (as per viewtopic.php?f=3&p=7578#p7578)

It works - with the proviso that having changed the amount of data written - to 10 x 'SOME DATA' - there is a block of crud in the middle of the file - looks like the WriteSector extends the file size to 512 bytes.

It works on first run (create file) but not if the file exists.. Hmm, back to the drawing board..

Changing back to HW and no dice...

Martin

Re: Write to start of sd card file

Posted: Sun Jul 24, 2022 11:21 am
by mnfisher
Removing the WriteFileSector from ChangeConfig seems to fix it (software SPI)

Note there is a 10s delay at the start of code (allow me to fire up 'putty')
sd config.fcfx
(21.33 KiB) Downloaded 49 times
Allows change of config and appends data to file correctly (although thinking about it - I haven't checked beyond a sector boundary)

Martin

Re: Write to start of sd card file

Posted: Sun Jul 24, 2022 11:39 am
by mnfisher
Still seem to be some issues - if writing more data (change to 100 x lines of data) then it grinds to a halt on the change config.

Looks like the ReadByteFromIndex(0) - attempting to read the first sector of the file is causing the stall?

Re: Write to start of sd card file

Posted: Mon Jul 25, 2022 10:57 am
by BenR
Hello

The WriteFileSctor function is intended to write the current buffer and then add a new sector onto the file and that's why the file size is growing and corruption could be creeping in. With a load of null bytes in the file anything you then add to the end of the file likely won't ever be seen in a text editor etc.

I've added a new macro called UpdateFileSector which should allow you to modify existing data without doing extras like editing the size of the file etc.

Please note for this to work you would first have to create a file and append a string to get the file up to the correct size to then be able to overwrite with new data using the new UpdateFileSector macro. Hope this makes sense and helps.

Re: Write to start of sd card file

Posted: Tue Jul 26, 2022 8:34 pm
by WingNut
Thanks Ben. I still cant get it to simulate writing the data above 5hz. Not tried on hardware