Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] How to append data to file using f_lseek function in FATfs lib with PIC32?

Status
Not open for further replies.

xpress_embedo

Advanced Member level 4
Advanced Member level 4
Joined
Jul 5, 2011
Messages
1,154
Helped
161
Reputation
396
Reaction score
189
Trophy points
1,353
Location
India
embeddedlaboratory.blogspot.in
Activity points
10,591
Hello!! i am using PIC32 and FATfs library to create text file in memory card.
Creating a file is successful and works perfectly.
But i am not able to append anything to the file.
Whenever i tried to do so, text in the file gets corrupted with junk characters.

Here is my code, please help me.

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int main (void)
{
    FATFS fs;
    FIL fdst;
    FRESULT res;
    UINT numread;
    DWORD size;
 
    unsigned char buffer[] = "This is a text file.";
    // Enable optimal performance
    INTEnableSystemMultiVectoredInt();
    SYSTEMConfigPerformance(GetSystemClock());
    mOSCSetPBDIV(OSC_PB_DIV_1);    // Use 1:1 CPU Core:Peripheral clocks
 
    //Initialize the disk
    disk_initialize(0);
 
    f_mount(0,&fs);
    res = f_open(&fdst,"file.txt",FA_WRITE | FA_OPEN_ALWAYS | FA_READ);
 
    size = (&fdst)->fsize;
 
    res = f_lseek(&fdst,size+1);
 
    res = f_write(&fdst,buffer,21,&numread);
 
    f_close(&fdst);
    f_mount(0,NULL);
    while(1);
}

 

This is Petit FAT library i am using elm chans library.
Not much difference but still some difference is der.

- - - Updated - - -

For the first time my code works fine.
After running first time, when i see memory card everything is okay.
But for next time file gets corrupted and i am getting junk characters in file.
 

There are pullups on the bus?

Try this.


Code C - [expand]
1
res = f_write(&fdst,&buffer,21,&numread);



instead of this


Code C - [expand]
1
res = f_write(&fdst,buffer,21,&numread);

 
Last edited:

Hello!!! Sir, No effect with this.

And i found a very new problem, write this code.

Code:
int main (void)
{
    FATFS fs;
    FIL fdst;
    FRESULT res;
    UINT numread;
    DWORD size;
    unsigned char i = 0;;
    
    unsigned char buffer[] = "This is a text file.";
    // Enable optimal performance
    INTEnableSystemMultiVectoredInt();
    SYSTEMConfigPerformance(GetSystemClock());
    mOSCSetPBDIV(OSC_PB_DIV_1);				// Use 1:1 CPU Core:Peripheral clocks

    //Initialize the disk
    disk_initialize(0);

    f_mount(0,&fs);

    for(i=0;i<3;i++)
    {
        res = f_open(&fdst,"james.txt",FA_WRITE | FA_OPEN_ALWAYS);

        size = (&fdst)->fsize;

        res = f_lseek(&fdst,size);

        res = f_write(&fdst,&buffer,21,&numread);

        f_close(&fdst);
    }

    res = f_open(&fdst,"essel.txt",FA_WRITE | FA_OPEN_ALWAYS | FA_READ);
    res = f_write(&fdst,&buffer,21,&numread);

    f_close(&fdst);
    f_mount(0,NULL);
    while(1);
}

I append code same text three times, in a single run and it works.
In file james.txt the buffer text was written three times.

But when i restart the unit, and then again see the memory card data. all the things are corrupted.

It means that restarting of unit is creating some problem.

If the controller is operating then appending is okay, but after power-off appending is not working.

- - - Updated - - -

Hello!!! Sir, No effect with this.

And i found a very new problem, write this code.

Code:
int main (void)
{
    FATFS fs;
    FIL fdst;
    FRESULT res;
    UINT numread;
    DWORD size;
    unsigned char i = 0;;
    
    unsigned char buffer[] = "This is a text file.";
    // Enable optimal performance
    INTEnableSystemMultiVectoredInt();
    SYSTEMConfigPerformance(GetSystemClock());
    mOSCSetPBDIV(OSC_PB_DIV_1);				// Use 1:1 CPU Core:Peripheral clocks

    //Initialize the disk
    disk_initialize(0);

    f_mount(0,&fs);

    for(i=0;i<3;i++)
    {
        res = f_open(&fdst,"james.txt",FA_WRITE | FA_OPEN_ALWAYS);

        size = (&fdst)->fsize;

        res = f_lseek(&fdst,size);

        res = f_write(&fdst,&buffer,21,&numread);

        f_close(&fdst);
    }

    res = f_open(&fdst,"essel.txt",FA_WRITE | FA_OPEN_ALWAYS | FA_READ);
    res = f_write(&fdst,&buffer,21,&numread);

    f_close(&fdst);
    f_mount(0,NULL);
    while(1);
}

I append code same text three times, in a single run and it works.
In file james.txt the buffer text was written three times.

But when i restart the unit, and then again see the memory card data. all the things are corrupted.

It means that restarting of unit is creating some problem.

If the controller is operating then appending is okay, but after power-off appending is not working.
 

Yes Sir, I had formatted the memory card properly and the Chip Select pin is configured as output pin.

Sir the Strange thing is that, on Power On Reset the appending of file is not working, file when i reset the device using PICKIT3 it appends properly.

Creating a new file is okay, appending is also fine, if done without restarting the device while when device is powered up all the data gets corrupted.

I am using the code from this forum for PIC32MX795F512L
https://www.microchip.com/forums/m563218.aspx

Had just changed my main function whose code is as follow:-


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
int main (void)
{
    FATFS fs;
    FIL fdst;
    FRESULT res;
    UINT numread;
    DWORD size;
    unsigned char i = 0;;
    
    unsigned char buffer[] = "This is a text file.";
    // Enable optimal performance
    INTEnableSystemMultiVectoredInt();
    SYSTEMConfigPerformance(GetSystemClock());
    mOSCSetPBDIV(OSC_PB_DIV_1);             // Use 1:1 CPU Core:Peripheral clocks
 
    //Initialize the disk
    disk_initialize(0);
 
    f_mount(0,&fs);
 
    for(i=0;i<3;i++)
    {
        res = f_open(&fdst,"james.txt",FA_WRITE | FA_OPEN_ALWAYS);
 
        size = (&fdst)->fsize;
 
        res = f_lseek(&fdst,size);
 
        res = f_write(&fdst,&buffer,21,&numread);
 
        f_close(&fdst);
    }
 
    res = f_open(&fdst,"file.txt",FA_WRITE | FA_OPEN_ALWAYS | FA_READ);
    res = f_write(&fdst,&buffer,21,&numread);
 
    f_close(&fdst);
    f_mount(0,NULL);
    while(1);
}

 

Please suggest me something.
Someone has faced this problem or i am the only one who is facing this.

There are many users who had implemented Fatfs in there code for Micro-Controller.
Do they encounter this thing or not.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top