Issue with SD card and the IDE library.

Combining the power of the ATmega2560 MCU with onboard Ethernet, a microSD card slot, an efficient switchmode power supply, and a small prototyping area. [Product page]
Post Reply
stejep
Posts: 4
Joined: Mon Apr 09, 2012 11:46 am

Issue with SD card and the IDE library.

Post by stejep » Mon Apr 09, 2012 11:58 am

Hi,
I have developed my project on an EtherTen, but I have run out of static ram, so have moved to a EtherMega. I was using a Sensor shield to get an I2C output, (also some digital inputs, with power), the I2C did not work with the Mega and then I moved it to the on board one and bingo that worked. My digital inputs are working on digital pins 2,3,5,6,7,8 and this is working fine on the Mega.

My problem is the SD card, this was working fine on the EtherTen, but I get errors on the Mega.
I notice on the schematic the data pins are different between the ten and the mega. Anyone know if the SD library knows this ? Or am I doing something wrong.

Steven

marc
Freetronics Staff
Freetronics Staff
Posts: 16
Joined: Sat Oct 15, 2011 12:54 pm

Re: Issue with SD card and the IDE library.

Post by marc » Tue Apr 10, 2012 2:12 am

Hi Steven,
What kind of errors with the SD card do you get on the Mega?
Looking at the schematic of the EtherTen and EtherMega, at first look the functionality from the library point of view should be just the same.
The EtherMega SPI is D51, D52, D50, and EtherTen is D11, D13, D12, but that is expected because the SPI port function is moved to those pins for Mega type boards.

That said though, the SPI CS pin (D53 on the Mega) must be made an output for the SPI to work. the same as D10 is handled on a standard sized board.
The CS pin for the SD card is D4 on both, the pin that is used to access the micro SD card.

Do you have anything else using SPI? (on-board Ethernet, shields etc). SPI will only talk to one device at a time, so sometimes you need to keep an eye on SPI chip select conflicts.
What IDE version, 1.0?

I'll check some more here too and test some examples in the meantime.

Cheers,
Marc

stejep
Posts: 4
Joined: Mon Apr 09, 2012 11:46 am

Re: Issue with SD card and the IDE library.

Post by stejep » Tue Apr 10, 2012 3:11 am

Marc,

The card is failing the Initialization phase, the code is exactly the same for both boards.
At this stage I am not using the ethernet port, in the future I will be and take note of your comments.

I am using IDE V1.0.

I don't think I am doing anything special with port D10, should I have been, and thus should I for the Mega. I will try setting D53 to output tonight.

Currently I am only interfacing with external devices via I2C and Digital pins to one-wire devices.

Steven

marc
Freetronics Staff
Freetronics Staff
Posts: 16
Joined: Sat Oct 15, 2011 12:54 pm

Re: Issue with SD card and the IDE library.

Post by marc » Tue Apr 10, 2012 9:31 am

I've run a few tests, and it works ok with a micro SD card. There are a few things to look at in the setup:

The example I have run is from the 1.0 IDE and is File>Examples>SD>listfiles

- In setup() the SPI CS output pin needs to be 53 (10 on most boards, 53 on all Mega type boards)
so line 32 becomes: pinMode(53, OUTPUT);

- The init of the SD library needs to know which chip select pin the SD card lives on, in this case D4, so line 34 becomes: if (!SD.begin(4)) {

That should be it for any Mega board type SPI and this SD card setup.

Keep in mind the SD library only reads FAT16 or FAT32 formatted cards so if the card isn't preformatted you'll also get an initialization fail even if the library is setup ok. I just tested this with an unformatted card and it does fail with the same message as if the pins aren't setup.

Hope this helps!
Cheers,
Marc

---
Here's an output I got from a FAT32 formatted 2GB micro SD card:

Initializing SD card...initialization done.
ATMELA~1.CSV 15855
ATMELA~2.CSV 11857
FULL_C~1.JPG 4959
TEST.JPG 4959
done!

stejep
Posts: 4
Joined: Mon Apr 09, 2012 11:46 am

Re: Issue with SD card and the IDE library.

Post by stejep » Tue Apr 10, 2012 11:48 am

So far so good,

The card initialized OK, I just have to wait 30 min until my write routine runs.

look like it was the pin 10 to pin 53 as the problem.

Thanks for you help.

stejep
Posts: 4
Joined: Mon Apr 09, 2012 11:46 am

Re: Issue with SD card and the IDE library.

Post by stejep » Wed Apr 11, 2012 10:03 am

My project is now happily writing data to the SD card.

It is very happy.

Its beyond me why they did not keep the same pins.

Steven

User avatar
jonoxer
Freetronics Staff
Freetronics Staff
Posts: 298
Joined: Sat Oct 15, 2011 11:31 am
Location: Melbourne, Australia
Contact:

Re: Issue with SD card and the IDE library.

Post by jonoxer » Wed Apr 11, 2012 1:12 pm

Thanks for the update @stejep, I'm glad you and Marc got it sorted out!

The reason the pins changed is that the MCU used on "Mega" type boards is in the same *family* as the MCU used on "classic" Arduino-type boards, but not the same actual MCU. The MCU itself has internal hardware support for SPI and the Arduino software environment takes advantage of that, offloading the work of managing the timing etc of SPI comms to the dedicated portion of the MCU designed for the purpose. It's not up to the software to determine which pins are used for MISO, MOSI, and SS, because they're set by the silicon.

Trying to design an Arduino-compatible board with a different MCU is remarkably tricky, because you have to try to map a combination of digital, PWM-capable, analog in, I2C, SPI, and UART functionality in a way that matches existing boards as closely as possible. It's not always possible to make it identical, so ATmega2560-based boards have to put their SPI pins in a different location to ATmega328-based boards.

It's a pain, but it's something that board designers just have to live with.
--
Jon

Post Reply