A brief intro before the programming question:
The standart SD library uses more than half of Arduino's RAM. Only "reward" for this is possibility to use filesystem and create "real files". I don't think it is much useful. I would rather keep the RAM free, save raw data without any structure and when the time comes to retrive collected data I will read them and send to computer by Serial. It will be the computer's work to transform them into something eligible.
As far as I know there is a problem with SD card writing - you have to write whole 512 byte blocks, you cannot write single bytes as EEPROMs usually enable. So if you want to use the whole capacity of the card you need to have a buffer 512 bytes large. So I expected 600 bytes will be more then enough to implement the SD card of which I can use the 512 bytes buffer to store collected data before I send them to card at once.
I started to dissect the SD.h library and quickly found it is build over Sd2Card.h library. Just adding either of those two libraries to a blank sketch consumes nearly 800B of RAM despite there is no large buffer included in the Sd2Card. Morover it uses 5k program space without ever calling any function.
Next the Sd2Card #includes two header files. SdInfo.h and Sd2PinMap.h. Adding either of those (or both) to blank sketch yields the same result: 5k program space and 800 RAM consumed. I have tried comment most of the SdInfo.h including defines. Omiting the commented code SdInfo.h looks this way:
/** GO_IDLE_STATE - init card in spi mode if CS low */
uint8_t const CMD0 = 0X00;
/** SEND_IF_COND - verify SD Memory Card interface operating condition.*/
uint8_t const CMD8 = 0X08;
/** SEND_CSD - read the Card Specific Data (CSD register) */
uint8_t const CMD9 = 0X09;
/** SEND_CID - read the card identification information (CID register) */
uint8_t const CMD10 = 0X0A;
/** SEND_STATUS - read the card status register */
uint8_t const CMD13 = 0X0D;
/** READ_BLOCK - read a single data block from the card */
uint8_t const CMD17 = 0X11;
/** WRITE_BLOCK - write a single data block to the card */
uint8_t const CMD24 = 0X18;
/** WRITE_MULTIPLE_BLOCK - write blocks of data until a STOP_TRANSMISSION */
uint8_t const CMD25 = 0X19;
/** ERASE_WR_BLK_START - sets the address of the first block to be erased */
uint8_t const CMD32 = 0X20;
/** ERASE_WR_BLK_END - sets the address of the last block of the continuous
range to be erased*/
uint8_t const CMD33 = 0X21;
/** ERASE - erase all previously selected blocks */
uint8_t const CMD38 = 0X26;
/** APP_CMD - escape for application specific command */
uint8_t const CMD55 = 0X37;
/** READ_OCR - read the OCR register of a card */
uint8_t const CMD58 = 0X3A;
/** SET_WR_BLK_ERASE_COUNT - Set the number of write blocks to be
pre-erased before writing */
uint8_t const ACMD23 = 0X17;
/** SD_SEND_OP_COMD - Sends host capacity support information and
activates the card's initialization process */
uint8_t const ACMD41 = 0X29;
//------------------------------------------------------------------------------
/** status for card in the ready state */
uint8_t const R1_READY_STATE = 0X00;
/** status for card in the idle state */
uint8_t const R1_IDLE_STATE = 0X01;
/** status bit for illegal command */
uint8_t const R1_ILLEGAL_COMMAND = 0X04;
/** start data token for read or write single block*/
uint8_t const DATA_START_BLOCK = 0XFE;
/** stop token for write multiple blocks*/
uint8_t const STOP_TRAN_TOKEN = 0XFD;
/** start data token for write multiple blocks*/
uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
/** mask for data response tokens after a write block operation */
uint8_t const DATA_RES_MASK = 0X1F;
/** write data accepted token */
uint8_t const DATA_RES_ACCEPTED = 0X05;
This Arduino code
#include <SdInfo.h>
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
compiles succesfully but still needs 5k program space and 800B RAM. Why this happens? Does Arduino include something because it thinks I may need it? Or is it some sort of bug? I tried to restart it repeatedly but still got the same result.
P.S. I just tried to delete all "advanced libraries" leaving only Sd2Card.h & .cpp, Sd2PinMap.h and SdInfo.h. Now it compiles using 498 bytes program space and 11 bytes RAM (48 program space and 2 RAM more than sketch without the include). Why?? If it is some kind of "clever feature" how can I disable linking unwanted libraries?
Thanks