#include #include "assert.h" #include #include #include #include #define SD_CS 5 // Chip select line for SD card #define CS 6 #define DC 7 #define RST 8 Adafruit_ST7735 tft = Adafruit_ST7735(CS, DC, RST); Sd2Card card; /* The 1.8" LCD has a uSD (micro SD) card slot, which can be used to store data, images, or graphics programs. A SD card is organized as blocks of 512 bytes. You cannot read or write individual bytes, an entire block must be moved in and out. So what this means is that you need a 512 byte buffer for I/O. Even though there are byte address commands available, they still cause blocks to move, and so are very expensive, and cause needless writes to the card. Also, writes are much more expensive than reads, so if you do not need to write anything, don't. Some displays recognize that the uSD card might have a FAT file system on them, and so reads and writes the data in a form that allows you to also read and write to the card as if it was a memory stick. Not for our screen, so you will not be able to mount this card in your own machine. But, you can still access the raw data using the dd command in OSX and Linux. To address a block on the card you need to provide a sector address, which is 3 bytes long. This means that a sector addres ranges 0 .. 16777215, or 0x0 .. 0xffffff first. The Sd2card.h header file defines various useful types for accessing the memory card: unsigned char - sector address and these basic operations to read and write a block uint8_t readBlock(uint32_t block, uint8_t* dst); uint8_t writeBlock(uint32_t blockNumber, const uint8_t* src); */ #define BLOCK_SIZE 512 unsigned char out_buf[BLOCK_SIZE]; unsigned char in_buf[BLOCK_SIZE]; uint32_t start_time; uint32_t stop_time; void start_clock() { start_time = millis(); } uint32_t stop_clock() { unsigned long elapsed_time; stop_time = millis(); elapsed_time = stop_time - start_time; Serial.print("Elapsed time "); Serial.println(elapsed_time); return elapsed_time; } void setup() { /* write at the 1G mark so we skip the useful data on the beginning of the card */ uint32_t start_sector; uint32_t end_sector; uint32_t cur_sector; Serial.begin( 9600 ); // wait for button press to start button_pause("Press button to start"); tft.initR(INITR_REDTAB); // initialize a ST7735R chip, red tab // initialize memory card if (!card.init(SPI_HALF_SPEED, SD_CS)) { Serial.println("initialization failed. Things to check:"); Serial.println("* is a card is inserted?"); Serial.println("* Is your wiring correct?"); return; } else { Serial.println("Wiring is correct and a card is present."); } // Select starting and end sector start_sector = 2097152; end_sector = start_sector + 32; cur_sector = start_sector; for (cur_sector = start_sector; cur_sector < end_sector; cur_sector++) { // write a block of data to the SD card for (int i=0; i < BLOCK_SIZE; i++) { out_buf[i] = random(0, 256); } Serial.print("Writing block "); Serial.println(cur_sector); start_clock(); card.writeBlock(cur_sector, out_buf); stop_clock(); // read it back for comparison Serial.print("Reading block "); Serial.println(cur_sector); start_clock(); card.readBlock(cur_sector, in_buf); stop_clock(); // compare what we wrote to the SD card and what we just read for (int i=0; i < BLOCK_SIZE; i++) { if ( in_buf[i] != out_buf[i] ) { Serial.print(" Byte mismatch "); Serial.print( i ); Serial.print(" in:"); Serial.print( in_buf[i], HEX ); Serial.print(" out:"); Serial.println( out_buf[i], HEX ); button_pause("Continue?"); break; } } } } void loop() { }