SD_soft_atmega328.patch - ska-la/arduino_patches GitHub Wiki
PREREQUISITE. I tried to use bundle of SD and Ethernet cards modules on Arduino Nano v.3 (Atmega328 chip). First of all i tested EtherShield library.. without of satisfaction. Then i tested EtherEncLib library with a same result. When i tested bundle of SD and Ethernet libraries on Arduino Mega (Atmega2560 chip) i understood that main problem is that both libraries use own SPI protocol implementation. Once, when i looked through a SD library's header files, my attention was attracted to Sd2Card.h file. It contains definition of software support of SPI protocol for SD/TF cards.
/**
* Define MEGA_SOFT_SPI non-zero to use software SPI on Mega Arduinos.
* Pins used are SS 10, MOSI 11, MISO 12, and SCK 13.
*
* MEGA_SOFT_SPI allows an unmodified Adafruit GPS Shield to be used
* on Mega Arduinos. Software SPI works well with GPS Shield V1.1
* but many SD cards will fail with GPS Shield V1.0.
*/
#define MEGA_SOFT_SPI 0
I thought that software support of SD/TF card can resolve a problem of SD and "ethernet" libraries collisions. But it's implemented only for Mega boards.
ESSENTIAL. The patch contains an amendment to Sd2Card.h file, it allows you to use SD card module with another SPI modules, like ethernet. Pin assignments are: 4-CS, 7-SCK, 8-MISO, 9-MOSI. My advise: don't change MISO and MOSI pins assignments if you don't like my choice. This selection based on definitions in SD/utility/Sd2PinMap.h file. Pin 4 very often is used as ChipSelect in many sketches of SD library and no sense to change it. Safest change is a pin 7 assignment. But i may mistake and you'll find out another pin assignments combinations which are applicable in the patch. If you have a time, go ahead!
THE PATCH IS APPLICABLE SOLELY TO ARDUINO MINI, NANO & UNO BOARDS (BASED ON Atmega328 CHIP).
HOWTO. Download the patch, apply it and leave him there for reverse patch in the future:
cd ~/arduino-x.x.x/libraries/
cp ~/Downloads/SD_soft_atmega328.patch .
cd SD/
patch -p1 < ../SD_soft_atmega328.patch
(if you can't do it, download the patched file, rename (--!!!--DON'T DELETE--!!!--) genuine file SD/utility/Sd2Card.h and copy patched file on his place)
After that simply test one of the SD library's examples.
IMPORTANT. Don't forget to roll back the patch when you intend to use SD library without an ethernet library.
cd ~/arduino-x.x.x/libraries/SD/
patch -p1 -R < ../SD_soft_atmega328.patch