SD Cards anyone?

Anything not specifically related to NBTV, but at least of some technical nature that might be of interest to NBTV members. Items for sale and links to retailers do not belong here.

Moderators: Dave Moll, Steve Anderson

SD Cards anyone?

Postby Steve Anderson » Wed Dec 28, 2011 6:49 pm

Has anyone tried interfacing some form of electronics to SD Cards? Shown below is my stab at doing so. However, I'm having one or two teething problems with it.

The idea behind it is to use the cards in one of two manners. Firstly as a huge great EEPROM, which in effect is all it is. This would make it non-removable as there would be no real distinct file system.

Second was then to add the FAT32 file-system such that files could be read/copied by any PC with a card-reader. FAT32 allows cards from 512MB up to 'really huge' to be used.

I'm frustrated by the fact I can read any sector of a 1GB card (I have two, different manufacturers) but any other card just fails, 16MB, 32MB, 256MB, 2GB and 4GB. Formatting in either FAT16 or FAT32 makes no difference...as one would expect when just reading raw sectors.

Now the 4GB is a SDHC card, so that does need handling differently, basically block-addressing rather than byte-addressing, so I'm not fretting over that.

So why the smaller cards refuse to work is confounding me! The cards work in a PC, camera...whatever, so there's nothing wrong with them.

I've trawled the 'net and only come across mostly conflicting 'solutions'. Even the SD Associations own data is vague in certain areas.

I have yet to address the next issue of writing to them. That will be fun for sure!!

Hardware is a PIC18F25K20, a MAX3232 and the SD card itself, all running at 3.3V so no 'fudging' of different voltage levels. Xtal is 12.288MHz using the x4 PLL so the instruction frequency is also 12.288MHz. (These PICs can run at 64MHz if need be, here after the PLL Fosc = 49.152MHz). The cards are clocked at just over 100kHz as they need to be in the initialization process. I have tried ramping up the speed for the cards to 12.288MHz after initialization and the 1GB cards are quite OK with that. But for now I have kept the SD card clock speed at 100kHz or so until I get this problem fixed.

Decoupling/bypassing is (as usual with me) quite thorough, in addition to what you can see in the photo, there's another 47uF cap right under the SD Card 'board' and another 100nF disc-ceramic cap right on the pins of the card connector (also out of view on the underside).

The TO220 package is a LM317 regulator for the 3.3V supply. It's all a bit rough and ready...a dummy SD card was fitted at the time the photo was taken. The only reason I used a 40-pin chip is that my supplier here was out of stock of the 28-pin variant which for this exercise is functionally identical (PIC18F25K20). My plan is eventually to squeeze this into a 20-pin package, something like a PIC18F14K50 perhaps?

This has many potential uses from data-logging (i.e. slow) to NBTV video recording and playback with the file being saved as either a 44.1kHz or 48kHz .wav file, hence my other posting regarding multiples of 44.1kHz crystals a few weeks back.

If the PIC internal A-D can be used all that's needed in addition would be some filtering. Advantage over mp3/wav recorders? It's DC coupled. (I'm not in any way suggesting using mp3 at all).

If any of you can help or know of others who might be able to, great! If not, never mind, I'll get there one way or another, I'm just trying to short-circuit a lot of iteration.

Steve A.

P.S. I have no intention of handling MMC (Multi-Media-Cards) which are physically and to a large degree electrically compatible...I've actually never seen one!
Attachments
PIC-SD 1.jpg
PIC-SD 1.jpg (102.03 KiB) Viewed 18773 times
SD Card SPI Interface 2.gif
SD Card SPI Interface 2.gif (17.57 KiB) Viewed 18773 times
User avatar
Steve Anderson
"Fester! Don't do that to 'Thing'"
 
Posts: 5360
Joined: Fri Mar 30, 2007 10:54 pm
Location: Bangkok, Thailand

Postby Steve Anderson » Sun Jan 01, 2012 3:21 pm

Progress! Further trawling of the 'net revealed this site...

http://www.vb-pic-sd-spi.com/home/main.htm

Provided by a certain W.Molesworth in the UK.

Within it is a very useful utility for diagnosing correct SD behaviour, or the lack thereof. It's not the most intuitive of software but it does work, and I make no apologies for the rather putrid colours.

It does require you to build an interface to your Windoze based machine which also has to have a LPT port, that took a bit of tracking down.

Now, I do not like his hardware interface as it puts 3.0V Zeners across the outputs of three of the LPT port lines. If the LPT output is a NPN transistor or MOSFET driving a resistive load, all well and good. But is every PC like that? I didn't want to take the risk of frying an LPT port. OK, they should have some form of current limiting, but that's for protection, not everyday use.

So my version is shown below, it's more complex, but that's inevitable if you want to save your LPT port and/or your SD cards.

The data back to the PC which is just one serial line is also a bit risky. The spec for an SD card is a sink/source current of 100uA maximum. A typical LPT port input sink current will most likely be over 1mA. Now all of the above may have 'worked' for him, but why throw more unknowns into the equation when you're trying to diagnose a problem?

So I opted for a simple MOSFET and trannie buffer in the circuit below. A photo of said Test-Bed is also shown below, it's also rough and ready, but it only took a morning to build.

The upshot is that I have been able to find (I hope) what the problem is. Some cards require an extra 'dummy' byte to follow on from their response to the command. (I have shown outlined in red in the screen-scrape below) It appears that the 1GB cards I have don't require this and simply ignore it. The others do. I have seen reference to this somewhere, but it was a passing reference, not explicitly stated as a requirement. As I recall it's supposed to flush buffers and so forth. I have been unable to find again said reference in all the SD card data I have, which is quite a bit!

The Internet is a wonderful resource, but there's a lot of mis-information too. Many of the sites are not strictly erroneous, but omit certain information that they presumably assume you know. Here's a prime example of where I didn't.

I have yet to include this into my PIC software, when I do I'll report back here.

I have downloaded all the data and software on the site including the web-pages complete. If the site vanishes as they tend to do, send me a PM and I'll forward all on.

Steve A.
Attachments
SD Card SPI Test-Bed 2.gif
SD Card SPI Test-Bed 2.gif (13.57 KiB) Viewed 18754 times
SD Card SPI Test-Bed 2.jpg
SD Card SPI Test-Bed 2.jpg (354.91 KiB) Viewed 18754 times
SD Card SPI Test-Bed 4.gif
SD Card SPI Test-Bed 4.gif (43.88 KiB) Viewed 18754 times
User avatar
Steve Anderson
"Fester! Don't do that to 'Thing'"
 
Posts: 5360
Joined: Fri Mar 30, 2007 10:54 pm
Location: Bangkok, Thailand

Postby Viewmaster » Sun Jan 01, 2012 7:00 pm

Steve , I cannot help you at all with this as it is so far above my head that I can barely see it ! :-)

But as you sent your last letter at beginning of 2012 I wish you all success with your new project and a Prosperous and Happy New year to you and yours.
“One small step for a man,"......because he has Arthritis.
Albert.
User avatar
Viewmaster
Frankenstein was my uncle.
 
Posts: 1306
Joined: Fri Apr 06, 2007 4:50 am
Location: UK Midlands

Postby kareno » Mon Jan 02, 2012 10:52 am

Hi Steve,

I found this text in a document I found on the web (which I'll happily send to you if you want):

SD Specifications and Compatibility

The SDA is continuously working on enhancing the physical layer specifications as the card capacity is increasing. The code was initially designed using version 1.10 specifications. Version 1.10 starts by
sending CMD0, which is a software reset, and results in the card initialization. It then enters in the SPI mode, if CS is pulled low at this stage. Version 2.00 of the SD Card Physical Layer Specifications
describes a modified Card initialization and identification flow. After powering, it also starts by sending CMD0; however, it is then mandatory to send CMD8 for the version 2.00 cards. CMD8 is used to verify SD
Memory Card operating conditions. Cards with both of these specifications are available in the market. Those having less than 2 GB capacity normally follow version 1.10 or earlier specifications. SDHC are the newer SD memory cards, based on the new SDA 2.00 specifications. Since, SDHC memory accessing works differently than standard SD
cards, the host software must account for this. The associated driver software accounts for this, thus it supports SDHC (version 2.00) cards as well as older (version 1.10).
kareno
 

Postby Steve Anderson » Tue Jan 03, 2012 5:01 pm

Albert, Thanks for the seasonal tidings, it's something that one tends to forget about here. Christmas basically doesn't happen, December 25th is just another day. When it falls during the week, everyone goes off to work as normal, and the kids go off to school.

A few years ago I was writing a document and I dated it 25/12/2005 and thought, "There's something about that date that's familiar." Then it dawned on me..."Must remember to call the family back in the UK later..."

Some of the tourist-orientated areas do make an effort, as do some shopping malls and hotels, otherwise it's easy to miss entirely. But tending to avoid those areas I completely forget about it. New year, although a public holiday, is somewhat of a non-event too. I went to bed at around 11pm on the 31st and woke up some hours later in 2012. I might add that it's the year 2555 here and I was born in the previous century in 2499!

It's apt to add a "Happy New Year" salutation to all here.

Karen, thanks too. There have been several revisions to the SDA's spec over the years and it's quite often hard to spot the sometimes subtle revisions/additions/changes.

It's a good thing that all of you are not within earshot of my place as when I tried the above 'dummy byte' all cards worked! The air turned blue! I'm not usually one for profanities, but yesterday I found words that I never even knew I knew! After about a 30 minute cooling-off period the phone rang and it was a friend inviting me out for a few beers. There was no decision to be made! I suspect that a few of my neigbours learned some Anglo-Saxon words not usually found in a phrase-book!

Anyway, in the cool light of a new day here I can report some progress indeed.

I'm still getting a few error reports but all cards with the exception of the SDHC versions can now be read. Below are two screen-scrapes of what the PIC returns from the first 512-byte sector.

In the first version (which starts at 0x0100 PIC RAM address on the left) shows the response to each command and for the type of card fitted in this case all is proper and correct. From there is the byte-by-byte data in the first half of that Sector 0. Although this is not a bootable disc I have no idea what all that junk is, and don't really care.

What is vital though is in the second half of Sector 0, outlined in red. This is the partition data for the disc/card. There's only one partition on this card as is usually the case. From this we can derive where the Volume ID is located (outlined in green) at Sector 0x00 00 00 81. A sanity check is made on the 0xAA 55 signature such we know this card has been formatted and doesn't just contain random data. All data here is little-endian, i.e. reverse order to how a human would write it.

From the Volume ID we can find where the FATs are located and further where the root directory/folder is located....I hope.

More to come after I've seen my therapist.

Steve A.
Attachments
SD Card SPI Test-Bed 5.gif
SD Card SPI Test-Bed 5.gif (58.12 KiB) Viewed 18721 times
SD Card SPI Test-Bed 7.gif
SD Card SPI Test-Bed 7.gif (75.64 KiB) Viewed 18721 times
User avatar
Steve Anderson
"Fester! Don't do that to 'Thing'"
 
Posts: 5360
Joined: Fri Mar 30, 2007 10:54 pm
Location: Bangkok, Thailand

Postby kareno » Wed Jan 04, 2012 10:39 am

Sometimes I swear chip and protocol designers get a commission for every EE they get sectioned :( You can tell that huge committees are behind a lot of these designs, can't you?
kareno
 

Postby Steve Anderson » Wed Jan 04, 2012 1:27 pm

kareno wrote:...You can tell that huge committees are behind a lot of these designs, can't you?


Yea, and why? In the sense that why on the card above is the Volume ID located where it is? It's not fixed and between two otherwise identical cards the location is different! Huh? And although only a small percentage of the cards capacity, why is it located so far away?

In the case above 0x81 = 129d. At 512 bytes/sector = 66kB+. So there's basically 66kB wasted, unused, inaccessible. Then this repeats itself with the FATs, also located somewhere well over the horizon.*

When you actually grasp how this all works (and I'm not quite there yet) it's no wonder why hard-drives fail when they have to do all this seeking back-and-forth across vast tracts of unused disc-space.

Did someone mention clusters?

Where's my Prozac? Ah! Here it is, Sector 0x2F 51 44 00...heck, it's fragmented. Where are the nurses when you need them?

Steve A.

*It's in these normally unused and inaccessible areas of discs that many viruses often reside which makes them invisible to most Operating Systems.
Last edited by Steve Anderson on Wed Jan 04, 2012 1:37 pm, edited 1 time in total.
User avatar
Steve Anderson
"Fester! Don't do that to 'Thing'"
 
Posts: 5360
Joined: Fri Mar 30, 2007 10:54 pm
Location: Bangkok, Thailand

Postby kareno » Wed Jan 04, 2012 1:36 pm

Oh, I don't bother with all that partition table and file system nonsense! To me they're just compact cassettes that don't need motors and capstans! A 1GByte SD card is a C60 when replaying stereo at 96ksamples/sec and 12 bit samples!

I've been kinda put off SD cards by tales of protracted busy periods when the internal microcontroller is doing its hair or something. Have you noticed any long read access times?
kareno
 

Postby Steve Anderson » Wed Jan 04, 2012 3:10 pm

kareno wrote:I've been kinda put off SD cards by tales of protracted busy periods when the internal microcontroller is doing its hair or something. Have you noticed any long read access times?


Not as yet. Firstly I'm still at the slow initialization clock-speed of 100kHz or so. I have tried over 10MHz which worked quite happily on the 1GB cards, but have yet to re-try it on the rest of them. Basically 1MB/sec which should be plenty fast enough, roughly the same as USB 1.00.

What does concern me will be the write speed. Here things could grind virtually to a halt. These cards have improved over time and their use in HD camcorders is testament to that. So it can be done. They do of course have to be of the highest speed class though in that application.

This 'project' started out as a solution to an as yet undefined problem. There was no defined purpose identified, but in one way or another storage of data is often an issue that needs dealing with. In essence this was to be a general purpose data storage and retrieval system. One could say that a PC is just such a device.

Often I have to do some form of long-term data-logging, battery discharge curves that may take days. One set of readings every 10 minutes being more than enough data, but hardly of any speed. But to be able to use it also at 48kHz sampling would be of use too. NBTV seemed like one application for this.

Long-term data logging for me has meant leaving a PC running for 24 hours a day for quite some time without any interruption. That's a vulgar waste of resources and power, then there's the issue of software stability and here, power glitches.

So I wanted to develop some for of self-supporting logger/recorder that could be independent of a 220V supply yet store an awful lot of data if need be. Although I have a number of spare hard-drives which I did consider using, their power demands and size ruled them out. I did consider 2.5" laptop drives but I rejected them too. Some time ago I also had a 1GB IBM MicroDrive which is the same form-factor as a fat CF card. But that camera it was used in was stolen, it's a rather specialized device and today its storage/price ratio is a no-go. I don't know if IBM still make them.

So that's the genesis of this. Any number of alternative storage media were considered. EEPROMS (limited capacity), Flash memory (not as limited, but still far smaller capacity than SD cards and still may have the write-speed issue), USB Flash Drives too, but if anyone has studied how USB works at grass-roots level you'll understand why I chose FAT32! One day I'll have to get to grips with it but right now I'm trying to put that off.

So wearing my 'Commercial Hat' it seemed a sane(?) extension that if using SD cards why not make them removable and PC-friendly. So here we are. Should a client come along and require an SD card based product, I've already done the donkey-work. Having done this it can be retrospectively used for good old-fashioned hard drives too.

"Thank you Doctor, I feel much better now."

Steve A.
User avatar
Steve Anderson
"Fester! Don't do that to 'Thing'"
 
Posts: 5360
Joined: Fri Mar 30, 2007 10:54 pm
Location: Bangkok, Thailand

Postby Viewmaster » Wed Jan 04, 2012 7:16 pm

Steve Anderson wrote:
kareno wrote:...You can tell that huge committees are behind a lot of these designs, can't you?


When you actually grasp how this all works (and I'm not quite there yet) it's no wonder why hard-drives fail when they have to do all this seeking back-and-forth across vast tracts of unused disc-space.



I can never understand if there is a large amount of memory in a PC why the hard drive has to be used at all just to download web pages and many other items as well. Have often wondered if Microsoft is in cahoots with HD manufacturers to wear out our hard drives prematurely.
“One small step for a man,"......because he has Arthritis.
Albert.
User avatar
Viewmaster
Frankenstein was my uncle.
 
Posts: 1306
Joined: Fri Apr 06, 2007 4:50 am
Location: UK Midlands

Postby Steve Anderson » Wed Jan 04, 2012 8:04 pm

Viewmaster wrote:...Have often wondered if Microsoft is in cahoots with HD manufacturers to wear out our hard drives prematurely.


I did think similar some time back. As the size of disks grew so did M$'s Bloatware. But in the near future that apparent cozy relationship looks destined for the rocks with the introduction of the SSD. (Solid-State-Disc).

It makes one wonder what Seagate, Western Digital and others are going to do? It's either join them or sink.

I don't know what technology SSD's use (I should do some research) but I guess the main issues have been size (capacity) and write speed. Today a Terra-byte sized hard drive is cheaper than a 32GB SD card (or any other format). But that is likely to change.

With the introduction of the SSD here was a chance to tidy-up the awful file systems we're used to. However the horse has bolted and the gate is still open. Too late.

Steve A.

Wikipedia SSD info...they've been around for longer than I thought!

http://en.wikipedia.org/wiki/Solid-state_drive
User avatar
Steve Anderson
"Fester! Don't do that to 'Thing'"
 
Posts: 5360
Joined: Fri Mar 30, 2007 10:54 pm
Location: Bangkok, Thailand

Postby Viewmaster » Wed Jan 04, 2012 10:13 pm

Steve Anderson wrote:It makes one wonder what Seagate, Western Digital and others are going to do? It's either join them or sink.


I don't think Seagate will ever sink.

http://www.seagate.com/ww/v/index.jsp?v ... 48090aRCRD

See links on the left too.

BTW, I have only ever used Seagate HDDs ever since I started a PC up many years ago and never had a failure yet...........
Crash, bang, wallop. Famous last words. :-)
“One small step for a man,"......because he has Arthritis.
Albert.
User avatar
Viewmaster
Frankenstein was my uncle.
 
Posts: 1306
Joined: Fri Apr 06, 2007 4:50 am
Location: UK Midlands

Postby Steve Anderson » Wed Jan 04, 2012 11:11 pm

Viewmaster wrote:I don't think Seagate will ever sink.


Obviously a sane board of directors that saw the light some time back and willing investors. The fate of the magnetic hard drive is sealed and at our consumer level it'll be hard to buy a hard drive as we know them today in a few short years time.

There may be applications where they may reign supreme still, right now I can't think of one though.

So here I sit staring at one of a number of perfectly good (but now unused) 160GB Seagate hard drives that are still in their 5-year warranty period...until August anyway. Will they even spin-up again? Somehow I don't think so.

When I think back to my first 'IBM Compatible'...with a 40MB (yes, 40MB) hard drive...before that I had an Amstrad PCW9512...stop laughing!

I must get back to these SD cards having taken a day out and done nothing further on them.

Steve A.
User avatar
Steve Anderson
"Fester! Don't do that to 'Thing'"
 
Posts: 5360
Joined: Fri Mar 30, 2007 10:54 pm
Location: Bangkok, Thailand

Postby M3DVQ » Thu Jan 05, 2012 4:56 am

Steve Anderson wrote:Some time ago I also had a 1GB IBM MicroDrive which is the same form-factor as a fat CF card. But that camera it was used in was stolen, it's a rather specialized device and today its storage/price ratio is a no-go. I don't know if IBM still make them.


I think the MicroDrive was made more or less obsolete by flash getting faster and cheaper. You can get a 4GB CF card easily. They're a damn sight easier to drive than SD too if you have access to the CPU's address and data bus (I know this is an issue when we're talking embedded micro-controllers rather than a traditional CPU as suddenly you eat through all your I/O pins)
M3DVQ
Just nod and pretend you understand me
 
Posts: 338
Joined: Sat Feb 10, 2007 10:52 am
Location: Lincolnshire

Postby M3DVQ » Thu Jan 05, 2012 5:02 am

Steve Anderson wrote:When I think back to my first 'IBM Compatible'...with a 40MB (yes, 40MB) hard drive...before that I had an Amstrad PCW9512...stop laughing!


I have a Sinclair ZX Spectrum +3 (Amstrad built) on the desk in front of me here... It has a 20GB Maxtor drive hanging off the back by a ribbon cable :D
M3DVQ
Just nod and pretend you understand me
 
Posts: 338
Joined: Sat Feb 10, 2007 10:52 am
Location: Lincolnshire

Next

Return to Off Topic

Who is online

Users browsing this forum: No registered users and 19 guests