Looking for Example and/or help with FatFs

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

Hello. 
I've spend 2 days trying to get the FatFs module working on my ATMega32U4 and i am slowly giving up.
I thought i was golden when i found the kubark42 tutorial until i've tried 2 times to configure it and given up because the tutorial is +10 years old and a lot of stuff have happened since.
I've teared down the ffsample to the minimum but still no success. 
Tried other peoples code with no success 
To clear out some of my possible woopsies
Internal pullup on MISO is enabled
I have a 3V3 level converter on the PCB
I am running 5V on MCU so it can do 16MHz

I have sat down and (tried) to configure the libraries but it is not easy specially with all the outdated tutorials and the much existing confusion around the 100 threads or so.
It could surely be something related to all these st***d macros there is in the library but im not really sure.. 

I have attached the Atmel studio project to the post for those who have the balls to check it. 
It is a mess and im sorry but at this point im not even sure about what I need or not 
 

Attachment(s): 

This topic has a solution.
Last Edited: Wed. Jul 21, 2021 - 08:12 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

https://www.avrfreaks.net/forum/im-going-use-fatfs-first-time-there-anything-particular-i-should-be-aware

 

Do you have an oscilloscope or logic analyser to see what's happening on the interface lines between your AVR and your SD-Card?

 

 

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

Thank you, i'll take a read.
Yeah right now i have a LA connected to the SPI. It is doing exactly.. NOTHING
CS high
MISO HIGH ( Pullup )
Mosi LOW
CLK HIGH 

Requirement is SPI_MODE0 so..
IDK why clock is IDLE low, configured as the following:
SPCR &= ~(1<<CPHA);    // Clock phase: Rising edge sample
SPCR &= ~(1<<CPOL);    // Clock Polarity: IDLE LOW
https://ww1.microchip.com/downlo...

Not even the CS pin is being operated. I did another strip down of the ffSample which i am playing around with and getting these results with.

And after I kind of gave up on that and tried out the minimalFatFS.zip and came to the same conclusion as OP. It gets stuck in disk_initialize(0);
I did go in and reconfigure the IOs 
Weirdly enough I can see that enable acts like a clock.. Runs 1t 1.414KHz and then the clock pin got some weird "spikes" 
MOSI is LOW, MISO HIGH

 

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

Decee1 wrote:
It is doing exactly.. NOTHING

is that before or after your level shifter?

 

what hardware are you using? if it's custom, can you post a schematic - see Tip #1 in my signature, below, for instructions on how to post pictures.

 

if it's custom, have you tested the SPI on its own?

 

Do you have a debugger to see what the code's doing

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

It is actually measured after the level shifter... ISP header is surely the most easy thing to get access to..

I'll just.. For good measures put on another piece of Software that utilises another part of my SPI bus so i can see if the MOSI, SCK and CS is getting shifted properly.
And yes, i have pulled those pins HIGH so I'm sure it is not that part of the SPI bus causing issues.
Update: Pins checked, signals gets shifted correctly. 
It would also surprise me if this was a HW issue, as if there are no activity on the SPI bus right now but there is with my other Software, that surely doesn't make sense.

I do own a ICE box, tho I haven't added TP's for JTAG, i always use ISP.. 
Lucky me uses the whole PF bus which got JTAG on it for inputs so it shouldn't be a problem soldering a few jumper wires to it. 
 

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

Could all the issue be caused by me AND others being just copying everything into Atmel Studio without referring to the MakeFile file as external makefile? 
I've never used something like that it but I was wondering how the heck the editor should refer to the file and started researching just to realise you are supposed to create a project using the makefile.. Right? 

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

The project would need to supply all the same options & settings as the makefile.

 

Surely, there must be ready-to-go Atmel Studio projects?

 

 

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

Decee1 wrote:
Could all the issue be caused by me AND others being just copying everything into Atmel Studio without referring to the MakeFile file as external makefile? 
Very much so. If you are suing ffsample.zip for example then for AVR there is quite a large selection of files but they are actually all the files to build about 4 different variants. Then in the directory are a number of Makefiles:

D:\ffsamp_apr2021\avr>dir mak*
 Volume in drive D is DATA
 Volume Serial Number is 1CF4-86D6

 Directory of D:\ffsamp_apr2021\avr

17/04/2021  12:52             3,278 Makefile_cfc
17/04/2021  12:52             3,297 Makefile_cfmm
17/04/2021  12:52             3,267 Makefile_mmc
17/04/2021  12:52             3,250 Makefile_mmcbb
               4 File(s)         13,092 bytes

Each of these "cherry picks" different sub-sets of all the files:

D:\ffsamp_apr2021\avr>grep main.c Make*
Makefile_cfc:CSRC    = main.c uart.c diskio.c cfc_avr.c rtc_ds1338.c ff.c ffunicode_avr.c
Makefile_cfmm:CSRC    = main.c uart.c diskio.c cfc_avr.c mmc_avr_usart.c rtc_ds1338.c ff.c ffunicode_avr.c sound.c
Makefile_mmc:CSRC    = main.c uart.c diskio.c mmc_avr_usart.c rtc_ds1338.c ff.c ffunicode_avr.c
Makefile_mmcbb:CSRC    = main.c uart.c ff.c mmc_bb.c rtc_ds1338.c sound.c ffunicode_avr.c

So, no, you can't just create an AS7 project and dump ALL the files into it and just say "build all that". Similarly the Makefiles each define various things:

D:\ffsamp_apr2021\avr>grep F_CPU Make*
Makefile_cfc:DEFS       = F_CPU=20000000 DRV_CFC=0
Makefile_cfmm:DEFS      = F_CPU=20000000 DRV_CFC=0 DRV_MMC=1
Makefile_mmc:DEFS       = F_CPU=20000000 DRV_MMC=0
Makefile_mmcbb:DEFS     = F_CPU=20000000

So things like this need to be made as -D's (that is "Symbols" in AS7) so the right parts of the code are built in each case.

 

There may be other things done in the Makefiles that need to be "lifted" and set as AS7 project options too.

 

Oh and you have to be very conscious of ffconf.h too - there is a STRONG chance that the supplied file will not be configuring FatFs the way you actually want to use it (you almost certainly don't want Japanese filename support for example!)

 

In the thread linked in #2 I detailed two approaches to using the FF files. One is to take the core/generic files and "build up" from there. The other is to take the ffsample files and "strip back" from there. In that thread I think OP found the "build up" approach to be the better one.

 

(to be honest I am half tempted, as moderator, to merge these two threads as this one has all the signs of being a major re-run of the other!).

 

REALLY, read the thread from #2 and then if, after that, you have any remaining questions either post to that thread or here.

Last Edited: Wed. Jul 21, 2021 - 10:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

Yeah I did see your huge post, and I actually also went though the process of configuring following your steps but without luck. 
BUT I did find something else..  the foolproof example from web.archive AND after a half hour of playing guessing game.. IT WORKS! I now got a file on my SD card with text in it!!

I've attached the Example

To configure the example do the following:
Go under sdmmc.c and reconfigure "Platform dependent macros" which are the pins used for bitbang
Remove the 2 lines for pulling CS high on 2 other SPi devices. I kept the lines there to remind the user that they may need to do so.
Also be aware that you cannot run 16MHz F_CPU clock so you have to reduce it. In my case i just enabled 8CLK divider in fuses.
Open makefile and reconfigure the microcontroller from ATMEGA32U4 to what you are using, reconfigure the DEFS    = F_CPU=2000000  to what you are using. In my case 16MHz clk / 8(fusebit)
Go under project configuration tap in Atmel Studio and change the device to whatever device you are using!

And remember to format your SD card correctly I have heard about people having issues with that. Download SD card formatter and use that tool to format.

Attachment(s): 

Last Edited: Wed. Jul 21, 2021 - 11:24 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So.
Now I have been working on building a working FatFs example which Utilises Hardware SPI. 

Currently stuck at "undefined reference to `mmc_disk_write'" in the diskio.c file.
I am really getting tired of having weird issues with something that SHOULD work and properly linked together.

I've done the "expected" changes in mmc_avr_spi and also added a "static inline void" as it was reporting a "xmit_spi_multi' defined but not used [-Werror=unused-function]" error

I've attached the Atmel Studio project, hopefully somebody can help me figuring out the issue.

Attachment(s): 

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


Why are you surprised by the error? 

 

 

Line 112 of diskio.c does indeed call a function called mmc_disk_write() but mmc_disk_write() at line 440 of mmc_avr_spi.c is protected behind a check for "USE_WRITE":

 

 

but when I search for that:

 

 

so two places in the project test for it but nothing in the entire solution ever sets it.

 

Oh wait a minute - you have set "Use external makefile" - why would you want to hamper yourself that way. Make this a proper AS7 solution - let it auto-create the Makefile for you.

 

Anyway that Makefile does not define _USE_WRITE either.

 

Oh and why "_USE_WRITE" anyway (which sounds like a name you made up)? ffconf.h has:

 

 

so FF_FS_READONLY = 1 is "read only" and by implication FF_FS_READONLY = 0 is "write functions are available". 

 

Read the entire ffconf.h and understand the purpose of eveything there. If you want to "build out" code that is not needed then change the relevant ffconf.h entry and wrap your code if tests of that symbol.

 

HOWEVER if you build this as an AS7 project it will be using -ffunction-sections and -gc-sections by default anyway so you do not really need to conditionally wrap the build of the code anyway - anything that is not called will be removed from the final image anyway so the old style conditional blocks with macros can largely be avoided.

 

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

Ask the programmer of the fatfs library why they went with  _USE_WRITE
I haven't coded that but surely makes sense why it doesn't work now that _USE_WRITE does not exist. 
It comes from the ffsample file from http://elm-chan.org/fsw/ff/bd/
I'll add it to my "things i've found while building up FatFs" list
I just checked mmc_avr_usart, in there it uses #if !FF_FS_READONLY so i guess this is what you call not validating Software before releasing ;P 
I'll post it on creators forum when I've gotten my library to work

YEP. set it to use external makefile, i am still not used to all the "what to do and what not to do" in Atmel Studio, I am still a n00bie and what I am educated in is Hardware not Software as that I am self-taught so i follow my own do's and dont's 

So yeah, I have been though the ffconf multiple times, and as the code is the modified example code that is why it confuses me so much!
I am also not a fan at all of all this macro-stuff. I really rather not touch it but first thing here is to get everything working and then do things 'correctly' 

And surely replacing the 2 lines including _USE_WRITE with #if !FF_FS_READONLY fixed it.

 

Now I am just facing a "recipe for target 'size' failed" when it compiles MakeFile_mmc
"# Display size of file.
size:
    @echo
    $(SIZE) -G $(PROJECT).elf"
Console: 
"make: *** [size] Error 1" 

UPDATE: The recipe for target 'size' error is caused by the makefile, i disabled the makefile and defined F_CPU and DRV_MMC=0 so now it programs.. 
BUT the SPI bus is quiet.. Really quiet and all pins are HIGH.. Interesting. 
It might be time for attaching Jtag, but I'll start with Serial.

Thank you for your huge help! 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I GOT IT TO WORK

 

What a war I've been though.
The last issue is caused by me disabling/removing MakeFile which defined DRV_MMC 0
Suspiciously enough even tho i have defined DRV_MMC 0 in top of my main.c it does not see it as a general define, I guess that is caused by the compiler compiling files separately? I'm actually really unsure BUT if i went into my mmc_avr_spi and defined DRV_MMC 0 it worked!

 

I bet 20-40% of people having issue with this FatFs s**t is that define causing it not to work. 
Am I guessing correct about it being the compiler just compiling separately, so if it compile main.c last it would define the macros unknown value? I'm really curious about this

 

Can someone help me solve the MakeFile issue so I can create a example?
I might make a new tutorial for configuring this, it is actually NOT hard configuring this.. It is just stupid issues which all are thrown into 100 different threads.

Last Edited: Wed. Jul 21, 2021 - 08:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I got it.
https://bugzilla.redhat.com/show...
" The options are: -A|-B|-C|-G --format={sysv|berkeley|avr|gnu} Select output style (default is berkeley) "

In my compiler there are (only) the following:
" -A|-B|-C  --format={sysv|berkeley|avr}  Select output style (default is berkeley)"
Which is GNU make 4.1 that Atmel Studio 7 uses.
I guess the problem is that the programmer of the library uses a very old version of make and therefor not aware of the issue.