AN222 - Interfacing an SD Flash Drive

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

AN222 - Interfacing an SD Flash Drive

Post by dkinzer »

A new application note, AN-222, has been published describing how to interface an SD Flash drive to a native mode ZX or AVR generic target device. The example code utilizes the open source FatFS filesystem code (written in C) to facilitate file operations. The application note and example code also demonstrate how to use the recently introduced ability to import identifier information from C/C++ header files and discusses some type compatibility issues that arise when importing identifiers
Last edited by dkinzer on 28 January 2013, 7:27 AM, edited 1 time in total.
- Don Kinzer
DocJC
Posts: 112
Joined: 16 March 2006, 6:23 AM
Location: Cleveland, OH
Contact:

Post by DocJC »

Wow! Nice job, Don!

I've used MMC cards before, but always as a linear memory array, thus avoiding the entire file structure issue.

Now I'll have to rethink that strategy!

JC
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Post by FFMan »

Could be useful to me for too.

Do you have any performance figures i.e. what can one achieve in sequential writes in terms of throughput ?

does it work with fat 16 & fat 32 and with the new large cards ?
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

FFMan wrote:Do you have any performance figures i.e. what can one achieve in sequential writes in terms of throughput ?
I only measured read speed - it came in at 220KB/sec for a large block. I'll see if I can measure write throughput.
FFMan wrote:does it work with fat 16 & fat 32 and with the new large cards ?
FatFS supports FAT12, FAT16 and FAT32 formats. The 4GB SD card that I used was FAT32 (FAT16 has a 2GB partition size limit). The FatFS documentation says that volumes up to 2TB are supported (using FAT32, of course).
- Don Kinzer
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Post by FFMan »

Don

just looking at this in a little more detail. I get some errors if i try and compile the example as it stands - is that to be expected or is it going to need some work for a 128a1 ? I see some notes in the comments that I need to read in detail.

Also, can you give some details of the hardware required to use this with an sd card. I know sparkfun etc have some very simple sd break out boards ? Are these ok - Is the SPI interface used for this ?

thanks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

FFMan wrote:I get some errors if i try and compile the example as it stands - is that to be expected or is it going to need some work for a 128a1 ?
The errors aren't intended. I'll have to look into it to see what is going on.
FFMan wrote:Also, can you give some details of the hardware required to use this with an sd card.
The SD/MMC cards have an SPI mode - that is what I used. Some of the sites I looked at had pictures of pins soldered directly to the terminals on the card. I chose to use the SparkFun breakout board to make changing the SD card simpler. Another site suggested getting a micro SD card with an adapter that has the form factor of a standard SD card. Then, you could solder leads directly to the adapter and still have the ability to change out the micro SD card.
- Don Kinzer
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

FFMan wrote:just looking at this in a little more detail. I get some errors if i try and compile the example as it stands
I just compiled it for the ZX-128a1 with no errors. If you don't have the v4.0.2 compiler (which the app note indicates is required) you probably will get errors. If you are using that version and still getting errors I'll need more information about the error messages.
- Don Kinzer
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Post by FFMan »

i upgraded to the latest compiler which must have helped.

however something is intolerant of a dash in the folder name.

all my projects are stored in folder shared-work which is usually fine, however something in this project i think see everything after the dash as switches ? not sure about that, here is the error.

if i move the files to c:\temp they compile ok

avr-gcc: warning: '-x c++' after last input file has no effect
avr-gcc: no input files
FatFS.bas:58: Error: file "C:\My Dropbox\Shared-Work\Zbasic\Examples\AN222_SD card_interface\FatFS.bas", line 58, column 39: use of undefined constant "AM_DIR"
FatFS.bas:60: Error: "FATFS" is not a known type, structure or enumeration
Fa
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

FFMan wrote:however something is intolerant of a dash in the folder name.
The issue arises due to there being spaces in the pathname and there are two aspects to the problem. I've placed an updated version of the import helper program avr-xml on the server. Extract the executable from the .zip file to the ZBasic installation directory.

With the updated executable in place, you'll get messages indicating that pathnames with spaces are incompatible with the default back-end build process (which uses the make utility). You can avoid this by adding the command line option --use-batch-file to the project file.

It may be possible to modify the compiler to automatically select building with a batchfile if it detects external files with spaces in their pathnames. That is on the "to do" list.
- Don Kinzer
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Post by FFMan »

ok i'll give that a try.

is this something to do with the mixture of c used in this project, or the new compiler as i've not seen this problem before.

thanks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

FFMan wrote:is this something to do with the mixture of c used in this project, or the new compiler as i've not seen this problem before.
The issue of needing the option --use-batch-file when a project contains external files whose pathnames contain spaces has always been present. Unless you included such external (C, C++, assembly language or object library) files in your project you wouldn't have encountered it.

The other aspect of the problem related to the special utility (avr-xml) that we use to import identifiers from C/C++ header files. Since that utility was just introduced with v4.0.1, you wouldn't have seen it before either.
- Don Kinzer
FFMan
Posts: 502
Joined: 09 January 2010, 12:52 PM

Post by FFMan »

don

do you have any guide as to how much code space and variable space is required to include these routines.

Is the hardware requirement just a free SPI channel ?

thanks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

FFMan wrote:do you have any guide as to how much code space and variable space is required to include these routines.
The code size depends, of course, on which of the FatFS routines are used in the application. The example code compiles to about 20KB with only the initialization and TestWrite() functions used. Adding the "directory listing" output adds another 3.5K of code space.

There is some data on the FatFS site about how RAM use varies with number of volumes, open files, etc. There are a lot of configuration options for FatFS (see ffconf.h) that affect code size and RAM use.
FFMan wrote:Is the hardware requirement just a free SPI channel?
One SPI channel is used for both the DS3234 and the SD card. This is possible because the DS3234 is only accessed at startup (to initialize the ZBasic RTC). Also, the second compare match register of the RTC timer is used for detecting timeout conditions.
- Don Kinzer
dlh
Posts: 395
Joined: 15 December 2006, 12:12 PM
Location: ~Cincinnati

Post by dlh »

How much current is needed for a typical MicroSD card?

I'm adding a MicroSD slot and a 3.3V F-RAM chip to a project where all other chips are 3V3 or 5V. The F-RAM only needs a few µA. I'm planning on using an LP3990MF-3.3CT LDO which can supply 150mA which I'm sure will be ample but would like to know the current requirements of a typical MicroSD card for my documentation.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

dlh wrote:How much current is needed for a typical MicroSD card?
The SD Specification says 200mA maximum but each card can specify it current at 45mA or 80mA. Also, see the Wikipedia entry.

The card will draw a lot of current upon insertion - I've seen recommendations to power the card by its own regulator because of this.
- Don Kinzer
Post Reply