How to use FATFs in multiple functions of a program?

cancel
Showing results for 
Search instead for 
Did you mean: 

How to use FATFs in multiple functions of a program?

702 Views
Amit_Kumar1
Senior Contributor II

Hi

I am using K60 with cw10.6. I have created a project with multiple files .i.e for each functions there is a separate file. I wanted to implement FATFs in main.c and Events.c but it didn't work (I tried this many times earlier this year) but it didn't worked so I made a separate file for this.

I tried using the functions present in this file from main as well as events.c but my program hanged. on init function. kindly look into this where I am doing wrong.

If I tried to use extern FATFs fs; or extern FIL fp1; in events.c i got errors so i made these files to overcome this issues. I am using FAT SD card library of Erich Styger. using SPI.

 

Kind Regards

Amit Kumar

Original Attachment has been moved to: SD_Card.h.zip

Original Attachment has been moved to: SD_Card.c.zip

6 Replies

199 Views
BlackNight
NXP Employee
NXP Employee

Hello,

If you are using the TWR-K60, I recommend that you verify proper operation with one of the demo examples:

mcuoneclipse/Examples/TWR-K60F120M/TWR-K60F120M_FatFS at master · ErichStyger/mcuoneclipse · GitHub

it looks like you are using it bare metal (without RTOS), so have a look here too:

mcuoneclipse/Examples/TRW-K60N512/TWR-K60N512_FatFS_BM at master · ErichStyger/mcuoneclipse · GitHub

Apart of that, I do not see the proper includes in your example code (e.g. include to WAIT1.h and FAT1.h is missing), and FAT1_Init() is not used.

The other thing to keep in mind: mount the file system only once (just in case this might be your problem).

Additionally, if you are using the FAT module,you need to feed the timeout timer so the SPI operation can timeout. See the implementation of this in the Events.c of both above examples.

I hope this helps,

Erich

199 Views
Amit_Kumar1
Senior Contributor II

Hi Erich

Thanks for the quick reply. I looked at the links and checked the code and updated my code according to your suggestions but still it didn't worked. If I write all the code in main it works fine but my application requires it to work on interrupt based. Attached is the modified code generated in CW10.6. I am using custom made board.

Kind Regards

Amit Kumar

0 Kudos

199 Views
BlackNight
NXP Employee
NXP Employee

It is a very, very bad idea to read/write files in an interrupt service routine. This is not how you should do things, and it cannot work that way.

If you are in an interrupt context, then interrupts are disabled (or at least most of them). But you need interrupts (e.g. timer for timeout, interrupts for SPI) active while writing/reading files.

If you need to trigger file I/O from an interrupt, then simply set a flag from that interrupt.

And do the writing/reading from your main application.

I hope this helps to understand the issue you are facing,

Erich

199 Views
Amit_Kumar1
Senior Contributor II

Thanks for the feedback. what if in the above program I generate a flag in an ISR as u suggested and in main functions call the SD card functions ? will it work in that case ?

Kind Regards

Amit Kumar

0 Kudos

199 Views
BlackNight
NXP Employee
NXP Employee

Hi Amit,

yes, this is how things are supposed to work.

Erich

199 Views
Amit_Kumar1
Senior Contributor II

Thanks Erich for your precious time I will make it work that way and will update you.

Kind Regards

Amit Kumar

0 Kudos