MMC FAT file close?

General discussion on mikroPascal PRO for PIC32.
Post Reply
Author
Message
LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

MMC FAT file close?

#1 Post by LGR » 01 Jul 2011 00:39

To write to an MMC FAT file, you:

1. Initialize : Mmc_Fat_Init();
2. Open for writing: Mmc_Fat_Rewrite();
3. Write: Mmc_Fat_Write(file_contents, 42);

Shouldn't there be a procedure to close the file when you're done, or is the file ok after the last call to Mmc_Fat_Write?

I have some code that writes a few lines to a file, and when I tried to open it in notepad, it gave me an error:

Code: Select all

Mmc_Fat_Assign('RTC_____.TXT',0x00);
Mmc_Fat_Rewrite(); // rewind the file, open for writing
LongWordToStr(RTC1, RTCS);  // make string
writebuf := RTCS;
Mmc_Fat_Write(writebuf, 30);
LongWordToStr(RTC2, RTCS);  // make string
writebuf := RTCS;
Mmc_Fat_Write(writebuf, 30);
LongWordToStr(RTC3, RTCS);  // make string
writebuf := RTCS;
Mmc_Fat_Write(writebuf, 30);
If you know what you're doing, you're not learning anything.

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: MMC FAT file close?

#2 Post by janko.kaljevic » 01 Jul 2011 16:56

Hello LGR,

There is no need to close the file, when You finished with writing You should see it when opened in notepad.

The code You provided is OK, and it should work fine.
But if You are having problems it must be because of the rest of the code.

Just a reminder

SD card must be formatted in FAT16, and file name must be in 8.3 format (8 letters).txt

These are the steps needed to get it working

1. Define global variables Mmc_Chip_Select and Mmc_Chip_Select_Direction
for example
var Mmc_Chip_Select : sbit at PORTF_OUT.B5;
var Mmc_Chip_Select_Direction : sbit at PORTF_DIR.B5;

2. Initialize SPI

SPID_Init_Advanced(_SPI_MASTER, _SPI_FCY_DIV128, _SPI_CLK_LO_LEADING);

3. You can start writing on SD card

if Mmc_Fat_Init() = 0 then
begin
Mmc_Fat_Assign('filename.TXT',0xA0);
Mmc_Fat_Rewrite(); // rewind the file, open for writing
byteToStr(RTC1, RTCS); // make string
writebuf := RTCS;
.....

This is how I got Your example working on mikroPascal Pro and mikroMMB for XMEGA
If You are using different system check SPI port and Chip select and direction pins.

Hope You'll get your project done.

Regards.

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

Re: MMC FAT file close?

#3 Post by LGR » 01 Jul 2011 18:10

I'm using MikroPascal for Pic32 on LV32MXv6.

I know the SPI and initialization are ok, because prior to this writing, I can successfully read a file. The problem is that when it tries to write, the file is corrupt. I can still read the other files from Windows (XP), but when I attempt to read the RTC_____.txt file, notepad opens, and a box pops up and says:
Cannot open D:\RTC_____.TXT file.
Make sure a disk is in the drive you specified.
I also get a balloon that says:
Untitled - Notepad: NOTEPAD.EXE - Corrupt file
The file or directory \RTC_____.TXT is corrupt and unreadable. Please run the Chkdsk utility.
So clearly, the library is finding SPI2 correctly, and finding the file correctly.

I also changed the relevant section per the example to:

Code: Select all

if Mmc_Fat_Init() = 0 then
            begin
              Mmc_Fat_Set_File_Date(2011,1,12,11,9,0);
              Mmc_Fat_Assign('RTC_____.TXT',0xA0);
              Mmc_Fat_Rewrite(); // rewind the file, open for writing
              LongWordToStr(RTC1, RTCS);  // make string
              writebuf := RTCS;
              Mmc_Fat_Write(writebuf, 30);
              LongWordToStr(RTC2, RTCS);  // make string
              writebuf := RTCS;
              Mmc_Fat_Write(writebuf, 30);
              LongWordToStr(RTC3, RTCS);  // make string
              writebuf := RTCS;
              Mmc_Fat_Write(writebuf, 30);
              SixMinCtr := 0;
            end;
Just to be sure that I'm doing it just like the example. It still produces a corrupt file. :x
If you know what you're doing, you're not learning anything.

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

Re: MMC FAT file close?

#4 Post by LGR » 01 Jul 2011 19:39

One other very odd thing -

When I hover the mouse over the file icon in Windows Explorer, it says 90 bytes, which is what it should be. The library is apparently writing the data correctly, but the file overhead is corrupted for some strange reason. It seems to be there, I just can't open the file and read it. :?

Also, when I open it with Notepad++ text editor, it opens without an error message, but comes up blank. The same thing happens when I open it on a Linux machine.
If you know what you're doing, you're not learning anything.

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

Re: MMC FAT file close?

#5 Post by LGR » 01 Jul 2011 20:10

PROBLEM SOLVED - sort of. :shock:

The source of the problem was that I was starting out with an empty file created in Notepad/XP. When I deleted the file entirely, and let the library create a new file, it worked. 8)

Please make a note in the documentation that you can't write to a file that was created in windows and notepad; it has to be created by the library. It's a problem with XP, I don't know about other OSes.
If you know what you're doing, you're not learning anything.

Post Reply

Return to “mikroPascal PRO for PIC32 General”