// reading sd card type is slow void sd_read(char *a) { char *cardtype; if (card.init(SPI_HALF_SPEED, 2)) { switch (card.type()) { case SD_CARD_TYPE_SD1: cardtype = "1 "; break; case SD_CARD_TYPE_SD2: cardtype = "2 "; break; case SD_CARD_TYPE_SDHC: cardtype = "HC"; break; default: cardtype = "? "; } sprintf(a, "SD: %s OK", cardtype); } else { sprintf(a, "SD: FAIL "); } }
void sdError_P(const char* str) { cout << pstr("error: "); cout << pgm(str) << endl; if (card.errorCode()) { cout << pstr("SD error: ") << hex << int(card.errorCode()); cout << ',' << int(card.errorData()) << dec << endl; } while (1); }
void setup(void) { Serial.begin(9600); // If your TFT's plastic wrap has a Red Tab, use the following: tft.initR(INITR_REDTAB); // initialize a ST7735R chip, red tab // If your TFT's plastic wrap has a Green Tab, use the following: //tft.initR(INITR_GREENTAB); // initialize a ST7735R chip, green tab // how much memory have we got left at this point? Serial.print("Avail mem (bytes):"); Serial.println(AVAIL_MEM); Serial.print("Initializing SD card..."); if (!SD.begin(SD_CS)) { Serial.println("failed!"); return; } Serial.println("OK!"); // clear to yellow tft.fillScreen(tft.Color565(0xff, 0xff, 0x00)); lcd_image_draw(&map_image, &tft, 0, 0, 0, 0, 128, 128); // how much memory have we got left at this point? Serial.print("Avail mem (bytes):"); Serial.println(AVAIL_MEM); // test out reading blocks from the SD card if (!card.init(SPI_HALF_SPEED, SD_CS)) { Serial.println("Raw SD Initialization has failed"); while (1) {}; // Just wait, stuff exploded. } // how much memory have we got left at this point? Serial.print("Avail mem (bytes):"); Serial.println(AVAIL_MEM); uint32_t block_num = 4000000; uint32_t start = millis(); for (int l=0; l<135; l++) { card.readBlock( block_num, (uint8_t *) block_buf); // Serial.println(block_buf[1].name); } uint32_t stop = millis(); //Serial.println(stop - start); //dump_block((uint8_t *) block_buf, BLOCK_LEN); // Serial.println(block_buf[1].name); for(int i = 0; i < 1066; i++) { printRest(i); } }
int App::run() { if (!card.init(SPI_HALF_SPEED, 4)) { Serial.puts("initialization failed. Things to check:\r\n"); Serial.puts("* is a card is inserted?\r\n"); Serial.puts("* Is your wiring correct?\r\n"); Serial.puts("* did you change the chipSelect pin to match your shield or module?\r\n"); return 0; } else { Serial.puts("Wiring is correct and a card is present.\r\n"); } Serial.puts("\r\nCard type: "); switch(card.type()) { case SD_CARD_TYPE_SD1: Serial.puts("SD1\r\n"); break; case SD_CARD_TYPE_SD2: Serial.puts("SD2\r\n"); break; case SD_CARD_TYPE_SDHC: Serial.puts("SDHC\r\n"); break; default: Serial.puts("Unknown\r\n"); } if (!volume.init(card)) { Serial.puts("Could not find FAT16/FAT32 partition.\r\n"); Serial.puts("Make sure you've formatted the card\r\n"); return 0; } uint32_t volumesize; Serial.printf("\r\nVolume type is FAT%u\r\n", volume.fatType()); volumesize = volume.blocksPerCluster(); volumesize *= volume.clusterCount(); volumesize *= 512; Serial.printf("Volume size (bytes): %u\r\n", volumesize); volumesize /= 1024; Serial.printf("Volume size (Kbytes): %u\r\n"); volumesize /= 1024; Serial.printf("Volume size (Mbytes): %u\r\n"); Serial.puts("\r\nFiles found on the card (name, date and size in bytes): \r\n"); root.openRoot(volume); root.ls(LS_R | LS_DATE | LS_SIZE, 0, Serial); while (true) { } return 0; }
void error_P(const char* str) { PgmPrint("error: "); SerialPrintln_P(str); if (card.errorCode()) { PgmPrint("SD error: "); Serial.print(card.errorCode(), HEX); Serial.print(','); Serial.println(card.errorData(), HEX); } while(1); }
void error(const char* str) { Serial.print("error: "); Serial.println(str); if (card.errorCode()) { Serial.print("SD error: "); Serial.print(card.errorCode(), HEX); Serial.print(','); Serial.println(card.errorData(), HEX); } while(1) { SPARK_WLAN_Loop(); }; }
int initSDCard(void){ if (!card.init(SPI_HALF_SPEED, SS_CS_Pin)) //Set SCK rate to F_CPU/4 (mode 1) { Serial.println("Error Init SD-Card"); return SDCARD_ERROR; } else { // initialize a FAT volume if (!volume.init(&card)) { Serial.println("Error Init Volume in SD-Card"); return SDCARD_ERROR; } else { // Open volume if (!root.openRoot(&volume)) { Serial.println("Error Open Volume in SD-Card"); return SDCARD_ERROR; } else { return SUCCESS; } } } }
//------------------------------------------------------------------------------ // zero FAT and root dir area on SD void clearFatDir(uint32_t bgn, uint32_t count) { clearCache(false); if (!card.writeStart(bgn, count)) { sdError("Clear FAT/DIR writeStart failed"); } for (uint32_t i = 0; i < count; i++) { if ((i & 0XFF) == 0) cout << '.'; if (!card.writeData(cache.data)) { sdError("Clear FAT/DIR writeData failed"); } } if (!card.writeStop()) { sdError("Clear FAT/DIR writeStop failed"); } cout << endl; }
void setup() { Serial.begin(9600); Serial1.begin(9600); tft.initR(INITR_REDTAB); randomSeed(analogRead(4)); joystickXCentre = analogRead(JOYSTICK_MOVE_X_PIN) - 512; joystickYCentre = analogRead(JOYSTICK_MOVE_Y_PIN) - 512; pinMode(JOYSTICK_BUTTON_PIN, INPUT_PULLUP); pinMode(COUNTDOWN_START_RED_1, OUTPUT); pinMode(COUNTDOWN_START_RED_2, OUTPUT); pinMode(COUNTDOWN_MID_RED_1, OUTPUT); pinMode(COUNTDOWN_MID_RED_2, OUTPUT); pinMode(COUNTDOWN_GREEN, OUTPUT); tft.setRotation(1); //because our screen is nonstandard rotation player1.score = 0; player2.score = 0; if (!SD.begin(SD_CS)) { Serial.println("SD Init Failed"); return; } if (!card.init(SPI_HALF_SPEED, SD_CS)) { Serial.println("Raw SD Init failed"); while (1); } tft.fillScreen(ST7735_BLACK); }
void setup(void) { Serial.begin(9600); pinMode(JOYSTICK_BUTTON, INPUT); pinMode(RATING_LED_0, OUTPUT); pinMode(RATING_LED_1, OUTPUT); pinMode(RATING_LED_2, OUTPUT); pinMode(RATING_LED_3, OUTPUT); pinMode(RATING_LED_4, OUTPUT); digitalWrite(JOYSTICK_BUTTON, HIGH); tft.initR(INITR_REDTAB); Serial.print("Initializing SD card..."); if (!SD.begin(SD_CS)) { Serial.println("failed!"); return; } Serial.println("OK!"); drawMap(); // test out reading blocks from the SD card if (!card.init(SPI_HALF_SPEED, SD_CS)) { Serial.println("Raw SD Initialization has failed"); while (1) {}; // Just wait, stuff exploded. } verticalMidpoint = getVertical(); }
void setup() { Serial.begin(BPS_115200); PgmPrintln("Type any character to start"); while (!Serial.available()); // initialize the SD card at SPI_FULL_SPEED for best performance. // try SPI_HALF_SPEED if bus errors occur. if (!card.init(SPI_FULL_SPEED)) error("card.init failed"); // initialize a FAT volume if (!volume.init(&card)) error("volume.init failed"); // open the root directory if (!root.openRoot(&volume)) error("openRoot failed"); // write files to root if FAT32 if (volume.fatType() == 32) { PgmPrintln("Writing files to root"); dirAllocTest(root); } // create sub1 and write files SdFile sub1; if (!sub1.makeDir(&root, "SUB1")) error("makdeDir SUB1 failed"); PgmPrintln("Writing files to SUB1"); dirAllocTest(sub1); // create sub2 and write files SdFile sub2; if (!sub2.makeDir(&sub1, "SUB2")) error("makeDir SUB2 failed"); PgmPrintln("Writing files to SUB2"); dirAllocTest(sub2); PgmPrintln("Done"); }
void get_restaurant(int i, Restaurant *r) { Restaurant buffer[BLOCK_LEN/sizeof(Restaurant)]; uint32_t block = RESTAURANT_START_BLOCK + i/8; uint32_t index = i % 8; card.readBlock(block, (uint8_t *) buffer); (*r) = buffer[index]; }
/** * initialize * Initialize Writter and SD Card behavior * @return {void} */ bool SDWriter::initialize() { Sd2Card card; card.init(SPI_HALF_SPEED, pin); SdVolume volume; volume.init(card); volumesize = volume.blocksPerCluster() * volume.clusterCount() * 512; if (!SD.begin(pin)) state = false; else state = true; return state; }
// ---------------------------------------------------------------------------- // Scan the SD card and open the volume // Set reg[STATUS] to FDC_ST_NOTREADY if no card present // ---------------------------------------------------------------------------- void scanSD() { if (!card.init(SPI_FULL_SPEED, SD_CHIP_SELECT_PIN)) { Serial.print("Init failed, error:"); Serial.println(card.errorCode()); mb8877.reg[STATUS] = FDC_ST_NOTREADY; return; } #ifdef SD_DEBUG Serial.print("\nCard type: "); switch(card.type()) { case SD_CARD_TYPE_SD1: Serial.println("SD1"); break; case SD_CARD_TYPE_SD2: Serial.println("SD2"); break; case SD_CARD_TYPE_SDHC: Serial.println("SDHC"); break; default: Serial.println("Unknown"); } #endif if (!volume.init(card)) { #ifdef SD_DEBUG Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card"); #endif mb8877.reg[STATUS] = FDC_ST_NOTREADY; return; } #ifdef SD_DEBUG // ----- Print the type and size of the first FAT-type volume Serial.print("\nVolume type is FAT"); Serial.println(volume.fatType(), DEC); Serial.println(); long volumesize; volumesize = volume.blocksPerCluster(); // clusters are collections of blocks volumesize *= volume.clusterCount(); // we'll have a lot of clusters volumesize *= 512; // SD card blocks are always 512 bytes Serial.print("Volume size (bytes): "); Serial.println(volumesize); #endif root.openRoot(volume); mb8877.reg[STATUS]=0x00; }
void setup(void) { Serial.begin(BPS_115200); Serial.println(); #if WAIT_TO_START Serial.println("Type any character to start"); while (!Serial.available()); #endif //WAIT_TO_START // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with // breadboards. use SPI_FULL_SPEED for better performance. if (!card.init(SPI_HALF_SPEED)) error("card.init failed"); // initialize a FAT volume if (!volume.init(&card)) error("volume.init failed"); // open root directory if (!root.openRoot(&volume)) error("openRoot failed"); // create a new file char name[] = "LOGGER00.CSV"; for (uint8_t i = 0; i < 100; i++) { name[6] = i/10 + '0'; name[7] = i%10 + '0'; if (file.open(&root, name, O_CREAT | O_EXCL | O_WRITE)) break; } if (!file.isOpen()) error ("file.create"); Serial.print("Logging to: "); Serial.println(name); // write header file.writeError = 0; file.print("millis"); #if ECHO_TO_SERIAL Serial.print("millis"); #endif //ECHO_TO_SERIAL #if SENSOR_COUNT > 6 #error SENSOR_COUNT too large #endif //SENSOR_COUNT for (uint8_t i = 0; i < SENSOR_COUNT; i++) { file.print(",sens");file.print(i, DEC); #if ECHO_TO_SERIAL Serial.print(",sens");Serial.print(i, DEC); #endif //ECHO_TO_SERIAL } file.println(); #if ECHO_TO_SERIAL Serial.println(); #endif //ECHO_TO_SERIAL if (file.writeError || !file.sync()) { error("write header failed"); } }
bool FileSystem::initSDCard() { boolean status; if (root.isOpen()) root.close(); // allows repeated calls // First, detect the card status = card.init(pinCS); // Audio shield has SD card SD on pin 10 if (status) { Serial.println("SD card is connected :-)"); } else { Serial.println("SD card is not connected or unusable :-("); sdCardInitialized = false; return sdCardInitialized; } // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32 if (!volume.init(card)) { Serial.println( "Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card"); sdCardInitialized = false; return sdCardInitialized; } root.openRoot(volume); // print the type and size of the first FAT-type volume Serial.print("\nVolume type is FAT"); Serial.println(volume.fatType(), DEC); Serial.println(); float size = volume.blocksPerCluster() * volume.clusterCount(); size = size * (512.0 / 1e6); // convert blocks to millions of bytes Serial.print("File system space is "); Serial.print(size); Serial.println(" Mbytes."); status = SD.begin(pinCS); // Audio shield has SD card CS on pin 10 if (status) { Serial.println("SD library is able to access the filesystem"); } else { Serial.println("SD library can not access the filesystem!"); Serial.println( "Please report this problem, with the make & model of your SD card."); Serial.println( " http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports"); } sdCardInitialized = true; return sdCardInitialized; }
void WriteSDSector(unsigned long SDS, byte *fileBuffer) { #ifdef HW_DISK_LED_ENABLE digitalWrite(HW_DISK_LED, HIGH); #endif // HW_DISK_LED_ENABLE if (int error = card.writeBlock(SDS, fileBuffer) == 0) { SerialUSB.print("SD Card write error: "); SerialUSB.println(error, HEX); } #ifdef HW_DISK_LED_ENABLE digitalWrite(HW_DISK_LED, LOW); #endif // HW_DISK_LED_ENABLE }
void setup() { //Serial.begin(9600); PgmPrint("Free RAM: "); //Serial.println(FreeRam()); // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with // breadboards. use SPI_FULL_SPEED for better performance. pinMode(10, OUTPUT); // set the SS pin as an output (necessary!) digitalWrite(10, HIGH); // but turn off the W5100 chip! if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!"); // initialize a FAT volume if (!volume.init(&card)) error("vol.init failed!"); PgmPrint("Volume is FAT"); //Serial.println(volume.fatType(),DEC); //Serial.println(); if (!root.openRoot(&volume)) error("openRoot failed"); // list file in root with date and size PgmPrintln("Files found in root:"); root.ls(LS_DATE | LS_SIZE); //Serial.println(); // Recursive list of all directories PgmPrintln("Files found in all dirs:"); root.ls(LS_R); //Serial.println(); PgmPrintln("Done"); // Debugging complete, we start the server! Ethernet.begin(mac, ip); server.begin(); // Start up the temperature library sensorsa.begin(); sensorsb.begin(); sensorsc.begin(); sensorsd.begin(); setTime(0); // start the clock time33mins = time60mins = now(); }
void setup(void) { Serial.begin(BPS_115200); Serial.println(); PgmPrintln("Type any character to start"); while (!Serial.available()); //PgmPrint("FreeRam: "); //Serial.println(FreeRam()); // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with // breadboards. use SPI_FULL_SPEED for better performance. if (!card.init(SPI_HALF_SPEED)) error("card.init failed"); // initialize a FAT volume if (!volume.init(&card)) error("volume.init failed"); // open the root directory if (!root.openRoot(&volume)) error("openRoot failed"); strcpy_P(buf, PSTR("APPEND.TXT")); // open for read if (!from.open(&root, buf, O_READ)) { PgmPrint("Can't open "); Serial.println(buf); PgmPrintln("Run the append example to create the file."); error("from.open failed"); } strcpy_P(buf, PSTR("ACOPY.TXT")); // create if needed, truncate to zero length, open for write if (!copy.open(&root, buf, O_CREAT | O_TRUNC | O_WRITE)) { error("copy.open failed"); } // count for printing periods uint16_t p = 0; int16_t n; while ((n = from.read(buf, sizeof(buf))) > 0) { if (copy.write(buf, n) != n) error("write failed"); // print progress periods if (!(p++ % 25)) Serial.print('.'); if (!(p % 500)) Serial.println(); } Serial.println(); if (n != 0) error ("read"); // force write of directory entry and last data if (!copy.close()) error("copy.close failed"); PgmPrintln("Copy done."); }
void setup() { Serial.begin(BPS_115200); PgmPrintln("Type any character to start"); while (!Serial.available()); // initialize the SD card at SPI_FULL_SPEED for best performance. // try SPI_HALF_SPEED if bus errors occur. if (!card.init(SPI_FULL_SPEED)) error("card.init failed"); // initialize a FAT volume if (!volume.init(&card)) error("volume.init failed"); // open the root directory if (!root.openRoot(&volume)) error("openRoot failed"); // delete files in root if FAT32 if (volume.fatType() == 32) { PgmPrintln("Remove files in root"); deleteFiles(root); } // open SUB1 and delete files SdFile sub1; if (!sub1.open(&root, "SUB1", O_READ)) error("open SUB1 failed"); PgmPrintln("Remove files in SUB1"); deleteFiles(sub1); // open SUB2 and delete files SdFile sub2; if (!sub2.open(&sub1, "SUB2", O_READ)) error("open SUB2 failed"); PgmPrintln("Remove files in SUB2"); deleteFiles(sub2); // remove SUB2 if (!sub2.rmDir()) error("sub2.rmDir failed"); PgmPrintln("SUB2 removed"); // remove SUB1 if (!sub1.rmDir()) error("sub1.rmDir failed"); PgmPrintln("SUB1 removed"); PgmPrintln("Done"); }
void setup(void) { Serial.begin(9600); Serial.println(); Serial.println("type any character to start"); while (!Serial.available()); Serial.println(); // initialize the SD card at SPI_HALF_SPEED to avoid bus errors with // breadboards. use SPI_FULL_SPEED for better performance. if (!card.init(SPI_HALF_SPEED)) error("card.init failed"); // initialize a FAT volume if (!volume.init(&card)) error("volume.init failed"); // open the root directory if (!root.openRoot(&volume)) error("openRoot failed"); // open a file if (file.open(&root, "PRINT00.TXT", O_READ)) { Serial.println("Opened PRINT00.TXT"); } else if (file.open(&root, "WRITE00.TXT", O_READ)) { Serial.println("Opened WRITE00.TXT"); } else { error("file.open failed"); } Serial.println(); // copy file to serial port int16_t n; uint8_t buf[7];// nothing special about 7, just a lucky number. while ((n = file.read(buf, sizeof(buf))) > 0) { for (uint8_t i = 0; i < n; i++) Serial.print(buf[i]); } /* easier way int16_t c; while ((c = file.read()) > 0) Serial.print((char)c); */ Serial.println("\nDone"); }
void setup() { Serial.begin(115200); while (!Serial.available()) SPARK_WLAN_Loop(); while (Serial.available()) Serial.read(); // initialize the SD card at SPI_FULL_SPEED for best performance. // try SPI_HALF_SPEED if bus errors occur. // Initialize HARDWARE SPI with user defined chipSelect if (!card.init(SPI_FULL_SPEED, chipSelect)) error("card.init failed"); // Initialize SOFTWARE SPI //if (!card.init(mosiPin, misoPin, clockPin, chipSelect)) error("card.init failed"); // initialize a FAT volume if (!volume.init(&card)) error("volume.init failed!"); Serial.print("Type is FAT"); Serial.println(volume.fatType(), DEC); if (!root.openRoot(&volume)) error("openRoot failed"); }
void printCardInfo() { Serial.print(F("Data logging is ")); if (disableLogging) { Serial.println(F("DISABLED")); } else { Serial.println(F("ENABLED")); } Serial.println(); Serial.print("\nCard type: "); switch (card.type()) { case SD_CARD_TYPE_SD1: Serial.println("SD1"); break; case SD_CARD_TYPE_SD2: Serial.println("SD2"); break; case SD_CARD_TYPE_SDHC: Serial.println("SDHC"); break; default: Serial.println("Unknown"); } Serial.print("\nVolume type is FAT"); Serial.println(volume.fatType(), DEC); unsigned long volumesize; volumesize = volume.blocksPerCluster(); volumesize *= volume.clusterCount(); volumesize /= 2; volumesize /= 1024; Serial.print("Volume size: "); Serial.print(volumesize, DEC); Serial.println("MB"); Serial.println("name\tdate\tsize"); root.ls(LS_R | LS_DATE | LS_SIZE); Serial.println(); }
void setup() { Serial.begin(9600); pinMode(greenLEDandBEEP, OUTPUT); pinMode(redLEDpin, OUTPUT); PgmPrint("Free RAM: "); Serial.println(FreeRam()); pinMode(10, OUTPUT); digitalWrite(10, HIGH); if (!card.init(SPI_HALF_SPEED, 4)) error("card.init failed!"); if (!volume.init(&card)) error("vol.init failed!"); PgmPrint("Volume is FAT"); Serial.println(volume.fatType(),DEC); Serial.println(); if (!root.openRoot(&volume)) error("openRoot failed"); PgmPrintln("Files found in root:"); root.ls(LS_DATE | LS_SIZE); Serial.println(); PgmPrintln("Files found in all dirs:"); root.ls(LS_R); Serial.println(); PgmPrintln("Done"); Ethernet.begin(mac, ip); server.begin(); }
void setupLog() { if (!card.init(SPI_HALF_SPEED, SD_CS)) { Serial.println(F("No SD card inserted, disabling data logger.")); disableLogging = true; } if (!disableLogging && !volume.init(card)) { Serial.println(F("Unable to initialize SD volume")); disableLogging = true; } if (!disableLogging && !root.openRoot(volume)) { Serial.println(F("Unable to open volume root")); disableLogging = true; } #ifdef DEBUG printCardInfo(); #endif if (!disableLogging) { openLog(); } }
bool DLSD::setRate(uint8_t rate) { Sd2Card *c = _card.card(); return c->setSckRate(rate); }
//------------------------------------------------------------------------------ // write cached block to the card uint8_t writeCache(uint32_t lbn) { return card.writeBlock(lbn, cache.data); }
void setup() { Serial.begin(BPS_115200); PgmPrintln("Type any character to start"); while (!Serial.available()); // PgmPrint("Free RAM: "); /// Serial.println(FreeRam()); // initialize the SD card at SPI_FULL_SPEED for best performance. // try SPI_HALF_SPEED if bus errors occur. if (!card.init(SPI_FULL_SPEED)) error("card.init failed"); // initialize a FAT volume if (!volume.init(&card)) error("volume.init failed!"); PgmPrint("Type is FAT"); Serial.println(volume.fatType(), DEC); if (!root.openRoot(&volume)) error("openRoot failed"); // open or create file - truncate existing file. if (!file.open(&root, "BENCH.DAT", O_CREAT | O_TRUNC | O_RDWR)) { error("open failed"); } // fill buf with known data for (uint16_t i = 0; i < (BUF_SIZE-2); i++) { buf[i] = 'A' + (i % 26); } buf[BUF_SIZE-2] = '\r'; buf[BUF_SIZE-1] = '\n'; PgmPrint("File size "); Serial.print(FILE_SIZE_MB); PgmPrintln(" MB"); PgmPrintln("Starting write test. Please wait up to a minute"); // do write test uint32_t n = FILE_SIZE/sizeof(buf); uint32_t t = millis(); for (uint32_t i = 0; i < n; i++) { if (file.write(buf, sizeof(buf)) != sizeof(buf)) { error("write failed"); } } t = millis() - t; file.sync(); double r = (double)file.fileSize()/t; PgmPrint("Write "); Serial.print(r); PgmPrintln(" KB/sec"); Serial.println(); PgmPrintln("Starting read test. Please wait up to a minute"); // do read test file.rewind(); t = millis(); for (uint32_t i = 0; i < n; i++) { if (file.read(buf, sizeof(buf)) != sizeof(buf)) { error("read failed"); } } t = millis() - t; r = (double)file.fileSize()/t; PgmPrint("Read "); Serial.print(r); PgmPrintln(" KB/sec"); PgmPrintln("Done"); }
void setup() { Serial.begin(BPS_115200); PgmPrintln("Type any character to start"); while (!Serial.available()); Serial.print("\nInitializing SD card..."); // On the Ethernet Shield, CS is pin 4. It's set as an output by default. // Note that even if it's not used as the CS pin, the hardware SS pin // (10 on most Arduino boards, 53 on the Mega) must be left as an output // or the SD library functions will not work. //pinMode(10, OUTPUT); // change this to 53 on a mega // we'll use the initialization code from the utility libraries // since we're just testing if the card is working! if (!card.init(SPI_HALF_SPEED, chipSelect)) { Serial.println("initialization failed. Things to check:"); Serial.println("* is a card is inserted?"); Serial.println("* Is your wiring correct?"); Serial.println("* did you change the chipSelect pin to match your shield or module?"); return; } else { Serial.println("Wiring is correct and a card is present."); } // print the type of card Serial.print("\nCard type: "); switch(card.type()) { case SD_CARD_TYPE_SD1: Serial.println("SD1"); break; case SD_CARD_TYPE_SD2: Serial.println("SD2"); break; case SD_CARD_TYPE_SDHC: Serial.println("SDHC"); break; default: Serial.println("Unknown"); } // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32 if (!volume.init(card)) { Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card"); return; } // print the type and size of the first FAT-type volume uint32_t volumesize; Serial.print("\nVolume type is FAT"); Serial.println(volume.fatType(), DEC); Serial.println(); volumesize = volume.blocksPerCluster(); // clusters are collections of blocks volumesize *= volume.clusterCount(); // we'll have a lot of clusters volumesize *= 512; // SD card blocks are always 512 bytes Serial.print("Volume size (bytes): "); Serial.println(volumesize); Serial.print("Volume size (Kbytes): "); volumesize /= 1024; Serial.println(volumesize); Serial.print("Volume size (Mbytes): "); volumesize /= 1024; Serial.println(volumesize); Serial.println("\nFiles found on the card (name, date and size in bytes): "); root.openRoot(volume); // list all files in the card with date and size root.ls(LS_R | LS_DATE | LS_SIZE); }
void setup() { int logNo; char configLineBuffer[LINE_BUFFER_MAX]; spi.begin(SPI_281_250KHZ, MSBFIRST, 0); pinMode(GRN_LED,OUTPUT); pinMode(ORN_LED,OUTPUT); pinMode(RED_LED,OUTPUT); digitalWrite(GRN_LED,HIGH); digitalWrite(ORN_LED,LOW); digitalWrite(RED_LED,LOW); iwdg_init(IWDG_PRE_256, WATCHDOG_TIMEOUT); Watchdog_Reset(); if (!card.init(&spi)) { //if (!card.init()) { console.printf("FTL: card.init failed"); } delay(100); // initialize a FAT volume if (!volume.init(&card)) { console.printf("FTL: volume.init failed"); } // open the root directory if (!root.openRoot(&volume)) ;//SerialUSB.println("FTL: openRoot failed"); for (logNo=0; (!logOpened) && logNo<512; logNo++) { Watchdog_Reset(); //int snprintf(char *str, size_t size, const char *format, ...); snprintf(logFileName,15,"LOG%03d.TXT",logNo); if (file.open(&root, logFileName, O_READ)) { //SerialUSB.print("DBG: Exists :"); SerialUSB.println(logFileName); file.close(); } else if (file.open(&root, logFileName, O_CREAT|O_READ|O_WRITE)) { //SerialUSB.print("DBG: New File:"); SerialUSB.println(logFileName); logOpened=true; file.sync(); file.close(); file.open(&root,logFileName,O_WRITE|O_READ); while (file.read(configLineBuffer,LINE_BUFFER_MAX)) { } file.sync(); } } //if (!logOpened) SerialUSB.println("FTL: openRoot failed"); digitalWrite(GRN_LED,LOW); digitalWrite(RED_LED,HIGH); readSettings(); console.printf("LSV:" BOM_VERSION "\r\n"); console.printf("NST: %s\r\n",networkStatus()?"CONNECTED":"NOT CONNECTED"); digitalWrite(ORN_LED,HIGH); digitalWrite(RED_LED,networkStatus()?HIGH:LOW); }