FATFS: How to understand basic functionality?

Go To Last Post
19 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Hello,

 

I'm just getting started trying to use FATFS to write to an SD card. I've read all the FATFS posts under "Beginners Start Here", but I haven't been able to get any farther ahead. From everything I've read this is very easy to use, so I'm guessing that I'm missing something simply in my interpretation of how to use the functions. As you can see from the code below, I haven't gotten very far with this when I've run into an issue with the project building without errors. My plan was to check for the presence of the sd card with disk status. Assuming the card was there, initialize disk, and then use f_mount to connect to the card for f_write. Then call f_mount again to close the connection. But, I've got to get through some of these more basic issues before I can proceed to these other steps. Can you help me understand why I am seeing these errors and what I need to do to clear them?

 

/*
 * Test_FATFSLibrary_Rev2.c
 *
 * Created: 1/5/2022 3:38:28 PM
 * Author : Houdini
 */ 

#include <avr/io.h>
#include <avr/interrupt.h>
#include "diskio.h"
#include "ff.h"
#include "uart.h"

DSTATUS	disk_status(2);
DSTATUS	disk_initialize(2);

int main(void)
{

    
	/* Replace with your application code */
    while (1) 
    {
    }
}

 

 

 

 

Tim Watson

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well the errors on disk_status() and disk_initialize() are self explanatory aren't they? The compiler is seeing two different versions of each function. One where the parameters are (void) and one where they are (BYTE). That would usually suggest .h that documents the interface and .c that implements the interface do not agree. Often this can be as a result of conditional compilation where one thing is seeing a -D that the other is not. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


 

The parameters are the same in both the .c and .h files. One thing which is not clear to me is what BYTE pdrv is.?. BYTE is not a data type. Do you know what this is?

 

 

Tim Watson

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

what BYTE pdrv is.?. BYTE is not a data type.

BYTE is defined as "typedef unsigned char    BYTE;" in integer.h, lots of other types are also used there.

 

BYTE pdrv is an "unsigned char" (or uint8_t ) containing the drive number as it shows above.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


in #3, tim.watson@watsonanalytical.com wrote:
The parameters are the same in both the .c and .h files

You show diskio.c and diskio.h - but the errors in #1 are talking about main.c and diskio.h:

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tim.watson@watsonanalytical.com wrote:

DSTATUS	disk_status(2);
DSTATUS	disk_initialize(2);

 

How is that ever going to work?

Letting the smoke out since 1978

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you studied the FatFs documentation: http://elm-chan.org/fsw/ff/00index_e.html ?

 

In particular, see the 'Resources' section at the foot of that page.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Above all have you waded through the thread in Tutorial Forum about FatFs. Sure it is many pages but there's a lot of info. 

 

As you'll discover there and also in more recent threads (ones like this occur once every month to three months) the main issue is what happened to FatFs (for AVR) around the 2015 (was it?) mark. The ffexample.zip file used to have a mega64 example that used the SPI so mmc_avr.c had full working SPI code. Sure it was specific to the m64 layout but was easy to adapt. Then the example was changed (to mega1284p I believe). When this happened the author (not sure if it was Chan himself or a contributor) chose to use the 1284 USART in SPI mode (mmc_uart.c) but for whatever reason they also changed the SPI file to. To make into more of a "template" they cut out great swaths of the existing code and left a "to be filled" comment instead. So it's now much more difficult to use SPI "out of the box". One way (assuming a "modern" AVR) is to use USART in SPI mode, sure that prevents it being used as a UART but it is true that USART/SPI is just marginally more efficient than real SPI because of the additional buffering it provides. If you plan to use the SPI peripheral the way to see a full worked example is to take a trip back in time, get one of the older versions and use the mmc_avr.c that has the full mega64 example code.

 

As to your current issue. If you get really stuck zip up your Studio 7 project and post it as an attachment and I'll tell you what's wrong and how to fix it. (this may have to wait until I finally return to work next Monday in fact) 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Can you be more specific? Can you tell me what you see wrong with this, and how it should be written?  I'm getting up to speed on FATFS, so if you see something that doesn't make any sense, it's a reflection of my nearly zero knowledge of FATFS. It is noted on the Elm Chan website that these have been superseded by F_MOUNT and should not be used. It that the case, I don't know.?.

Tim Watson

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have studied this a bit, but my issue is I don't have a foundational point to start from. For example, if I had a piece of working code that I could copy and paste into the IDE to experiment with to get familiar with the functions and how they are used, that is how I would learn this best...experimentally.

Tim Watson

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have gone through the three tutorial posts, but it doesn't give me a point to start from. I think it would be beneficial to the community if I create a step by step post with sample code that new users can drop into their project. I think a lot of new users learn experimentally, and this would give them the foundation to get started and grow from.

 

Yep...I just took a look at the mmc_avr_spi.c file and indeed everything has been blanked out. Where as, the 2014 has the variables defined. That could be part of the problem, but I think the bigger issue is my unfamiliarity with the functions and the arguments the functions are looking for. I suspect many other new users are running into the same issue, so perhaps another opportunity for a tutorial post.

 

 

Tim Watson

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tim.watson@watsonanalytical.com wrote:
it's a reflection of my nearly zero knowledge of FATFS.

seems also to be some limited experience with C programming?

 

The problem causing the error in the OP is a C programming issue: you seem to have put a prototype for disk_status() in main.c when it already has a prototype in diskio.h - and those prototypes don't match.

 

Another C problem in the code posted in #1:

/*
 * Test_FATFSLibrary_Rev2.c
 *
 * Created: 1/5/2022 3:38:28 PM
 * Author : Houdini
 */ 

#include <avr/io.h>
#include <avr/interrupt.h>
#include "diskio.h"
#include "ff.h"
#include "uart.h"

DSTATUS	disk_status(2);         <<< can't have function calls here
DSTATUS	disk_initialize(2);

int main(void)
{

The highlighted function calls there are outside any function - that's not allowed in C.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Jan 6, 2022 - 06:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the feedback. Yes, some of my issues could be limited real life experience with C. I had three semesters of C/C++ in school with 4.0 marks across the board. The issue I think new users, such as myself, run into, is: in a class environment we're confined to the bounds of the curriculum but once we leave the bounds, we get lost fairly quickly, and rely on communities such as this to get us over that "hump". 

 

In the above example, it was not clear to me where DISKSTATUS_xxxxx needed to be called, and it's also not clear what arguments the function is looking to have passed to it. Opening up the .c file, I see this is a switch statement, but I don't know if the argument should be an int e.g. 0, 1, 2 or a string e.g. mmc, usb, flash. 

 

I'm just trying to get orient/get my bearings, so I can navigate this and future projects with a bit more independence.  

Tim Watson

Last Edited: Thu. Jan 6, 2022 - 07:40 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I suggest downloading the Arduino IDE and looking at the code for the SD card library that included with the IDE download.  

The code is extensive in this library and it will parallel the Chan FatFs code and the diskio.h ff.h libraries.

I adjust the code in the included examples to do tests of SD without having to master how everything works.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

tim.watson@watsonanalytical.com wrote:
it was not clear to me where DISKSTATUS_xxxxx needed to be called

At any point that you need to know the status of the "disk".

 

tim.watson@watsonanalytical.com wrote:
it's also not clear what arguments the function is looking to have passed to it.

The documentation of the function is here: http://elm-chan.org/fsw/ff/doc/dstat.html

 

It tells you what the parameter is:

Physical drive number to identify the target device. Always zero in single drive system

and what the return value is:

The current drive status is returned in combination of status flags described below. FatFs refers only STA_NOINIT and STA_PROTECT.

 

STA_NOINIT

Indicates that the device has not been initialized and not ready to work. This flag is set on system reset, media removal or failure of disk_initialize function. It is cleared on disk_initialize function succeeded. Any media change that occurs asynchronously must be captured and reflect it to the status flags, or auto-mount function will not work correctly. If the system does not support media change detection, application program needs to explicitly re-mount the volume with f_mount function after each media change.

 

STA_NODISK

Indicates that no medium in the drive. This is always cleared when the drive is non-removable class. Note that FatFs does not refer this flag.

 

STA_PROTECT

Indicates that the medium is write protected. This is always cleared when the drive has no write protect function. Not valid if STA_NODISK is set.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:
I suggest downloading the Arduino IDE and looking at the code for the SD card library that included with the IDE download.  

That's probably a good idea - as the whole point of Arduino is to take care of the arcane detail, and let you focus on what you're actually trying to achieve.

 

But it is all open source - so you can still dig into it to see what's going on if you want to.

 

In fact, it seems the Arduino library doesn't use FatFs:

Arduino wrote:
It is built on sdfatlib by William Greiman

https://www.arduino.cc/en/reference/SD  

 

EDIT

 

It seems Arduino's link is out-of-date - the source is now at: https://github.com/greiman/SdFat

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Thu. Jan 6, 2022 - 09:29 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As a rule, a global function should only be declared in a header file and by its definition.

Some global functions, e.g. some system functions,

might need to be declared in more than one header,

but the declarations should still match.

Moderation in all things. -- ancient proverb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

I can't believe I'm actually going to say this but for the very first time in my life I think I actually agree with something Simonetta has said.

 

Given your experience background Arduino probably IS the way to approach SD/MMC. FatFs is not a beginner lib, it is for seasoned professionals who can adapt it to their needs and understand what is provided and what the user's responsibilities are. If you don't have the experience then the very clever thing about Arduino is that it hides many levels of complexity from the end user and provides a top level, simple interface anyone should be able to understand so you can get on and use FAT on SD/MMC without requiring detailed knowledge of the lower layers. FatFs achieves this to some extent but not to the extent that Arduino does. 

 

So if you just want a project with some filing but don't want to get bogged down in the nitty-gritty detail of how to do that Arduino should isolate you perfectly.

 

Another advantage of Arduino is that it's so widely used and supported you should easily find examples trying to achieve pretty much whatever it is you want to do with FAT/SD/MMC. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
for the very first time in my life I think I actually agree with something Simonetta has said.

surprise surprise surprise surprise surprise surprise 

 

Well, I guess if you just keep saying the same thing over & over all the time, you're going to be right eventually just by the law of averages.

 

Given your experience background Arduino probably IS the way to approach SD/MMC. FatFs is not a beginner lib,

Indeed.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...