Nand flash read isuue

Thread Starter

oddparity

Joined Oct 16, 2018
2
Hi,

I have a custom made board based on Atmel SAME70Q20 microcontroller. I am having trouble figuring out a strange bug related to external Flash. I have integrated ELM Chan's FATFS in the code and sometimes if I add some code or modify anything even if it doesn't have anything to do with the file system. It gives me FR_NO_FILESYSTEM error. I traced the problem to flash read. Actually during the initialization when it is looking for the FATFS boot record the read on that sector returns all bytes as 0xFF and doesn't give any error either. The cal to f_mkfs also completes successfully and I know that the data was written to the flash because if I read the boot sector just after formatting the boot record is there, but later when it initializes further the same sector's read returns 0xFF in the whole sector.

This issue is driving me nuts and I seem to have exhausted all my knowledge and hit a roadblock. If anyone could point to where could the problem lie, or where to investigate, it will be very helpful.

Thanks
 

Thread Starter

oddparity

Joined Oct 16, 2018
2
Update:- I fixed my issue but I still don't understand what was the exact problem. If anyone could help understand it (just to satisfy the curiosity) would great. Actually it was the gcc optimization that was causing the problem. We were using the O1 optimization and I got a hunch that it might have something to do with the optimization. When I disable the optimization it works fine, furthermore if I disable the optimization specifically for files containing the raw nand access api the code works fine.

Although I think I am wrong but what lead me to look in this direction is that I had read that compilers align or not align the data and code at word boundary depending upon some flag because it is faster to access a word at a time for CPU than say something which starts at fraction of word which causes more CPU cycles. Since my issue appeared or disappeared randomly after some code change, I thought maybe some code or data was getting aligned or dis-aligned which somehow affected the execution but that doesn't seem to be the case.

So if anyone can explain what flags in O1 level optimization could affect the code in such way.

Following are the flags in O1 optimization in gcc for reference:

-fauto-inc-dec

-fbranch-count-reg

-fcombine-stack-adjustments

-fcompare-elim

-fcprop-registers

-fdce

-fdefer-pop

-fdelayed-branch

-fdse

-fforward-propagate

-fguess-branch-probability

-fif-conversion2

-fif-conversion

-finline-functions-called-once

-fipa-pure-const

-fipa-profile

-fipa-reference

-fmerge-constants

-fmove-loop-invariants

-fomit-frame-pointer

-freorder-blocks

-fshrink-wrap

-fshrink-wrap-separate

-fsplit-wide-types

-fssa-backprop

-fssa-phiopt

-ftree-bit-ccp

-ftree-ccp

-ftree-ch

-ftree-coalesce-vars

-ftree-copy-prop

-ftree-dce

-ftree-dominator-opts

-ftree-dse

-ftree-forwprop

-ftree-fre

-ftree-phiprop

-ftree-scev-cprop

-ftree-sink

-ftree-slsr

-ftree-sra

-ftree-pta

-ftree-ter

-funit-at-a-time
 

BobaMosfet

Joined Jul 1, 2009
2,113
Update:- I fixed my issue but I still don't understand what was the exact problem. If anyone could help understand it (just to satisfy the curiosity) would great. Actually it was the gcc optimization that was causing the problem. We were using the O1 optimization and I got a hunch that it might have something to do with the optimization. When I disable the optimization it works fine, furthermore if I disable the optimization specifically for files containing the raw nand access api the code works fine.

Although I think I am wrong but what lead me to look in this direction is that I had read that compilers align or not align the data and code at word boundary depending upon some flag because it is faster to access a word at a time for CPU than say something which starts at fraction of word which causes more CPU cycles. Since my issue appeared or disappeared randomly after some code change, I thought maybe some code or data was getting aligned or dis-aligned which somehow affected the execution but that doesn't seem to be the case.

So if anyone can explain what flags in O1 level optimization could affect the code in such way.

Following are the flags in O1 optimization in gcc for reference:

-fauto-inc-dec

-fbranch-count-reg

-fcombine-stack-adjustments

-fcompare-elim

-fcprop-registers

-fdce

-fdefer-pop

-fdelayed-branch

-fdse

-fforward-propagate

-fguess-branch-probability

-fif-conversion2

-fif-conversion

-finline-functions-called-once

-fipa-pure-const

-fipa-profile

-fipa-reference

-fmerge-constants

-fmove-loop-invariants

-fomit-frame-pointer

-freorder-blocks

-fshrink-wrap

-fshrink-wrap-separate

-fsplit-wide-types

-fssa-backprop

-fssa-phiopt

-ftree-bit-ccp

-ftree-ccp

-ftree-ch

-ftree-coalesce-vars

-ftree-copy-prop

-ftree-dce

-ftree-dominator-opts

-ftree-dse

-ftree-forwprop

-ftree-fre

-ftree-phiprop

-ftree-scev-cprop

-ftree-sink

-ftree-slsr

-ftree-sra

-ftree-pta

-ftree-ter

-funit-at-a-time
You're not going to find an 'this is it' answer. Compiler optimization has been this way since compilers were created (I know, I helped many original compiler makers debug their products). You have to understand that the optimizer does not understand the logic in the code. It looks at things and makes a best guess for how it can substitute some code piece with a piece of it's own- which may alter register usage, code timing, may eliminate pieces of your code that are essential, but it doesn't see it that way.

There are usually several levels of optimization available, simply try a level that optimizes less, until you find an optimization level that works reliably.
 
Top