Przeglądaj źródła

Add CopyFromSD example

main
PaulStoffregen 9 lat temu
rodzic
commit
40f774721c
2 zmienionych plików z 116 dodań i 0 usunięć
  1. +41
    -0
      SerialFlashDirectory.cpp
  2. +75
    -0
      examples/CopyFromSD/CopyFromSD.ino

+ 41
- 0
SerialFlashDirectory.cpp Wyświetl plik

@@ -71,6 +71,7 @@ static uint32_t check_signature(void)
uint32_t sig[2];

SerialFlash.read(0, sig, 8);
//Serial.printf("sig: %08X %08X\n", sig[0], sig[1]);
if (sig[0] == 0xFA96554C) return sig[1];
if (sig[0] == 0xFFFFFFFF) {
sig[0] = 0xFA96554C;
@@ -114,6 +115,15 @@ static bool filename_compare(const char *filename, uint32_t straddr)
}
}

void pbuf(const void *buf, uint32_t len)
{
const uint8_t *p = (const uint8_t *)buf;
do {
//Serial.printf("%02X ", *p++);
} while (--len > 0);
//Serial.println();
}

SerialFlashFile SerialFlashChip::open(const char *filename)
{
uint32_t maxfiles, straddr;
@@ -123,6 +133,7 @@ SerialFlashFile SerialFlashChip::open(const char *filename)
SerialFlashFile file;

maxfiles = check_signature();
//Serial.printf("sig: %08X\n", maxfiles);
if (!maxfiles) return file;
maxfiles &= 0xFFFF;
hash = filename_hash(filename);
@@ -130,12 +141,23 @@ SerialFlashFile SerialFlashChip::open(const char *filename)
n = 8;
if (n > maxfiles - index) n = maxfiles - index;
SerialFlash.read(8 + index * 2, hashtable, n * 2);
//Serial.printf(" read %u: ", 8 + index * 2);
//pbuf(hashtable, n * 2);
for (i=0; i < n; i++) {
if (hashtable[i] == hash) {
//Serial.printf(" hash match at index %u\n", index+i);
buf[2] = 0;
SerialFlash.read(8 + maxfiles * 2 + (index+i) * 10, buf, 10);

//Serial.printf(" maxf=%d, index=%d, i=%d\n", maxfiles, index, i);
//Serial.printf(" read %u: ", 8 + maxfiles * 2 + (index+i) * 10);
//pbuf(buf, 10);
straddr = 8 + maxfiles * 12 + buf[2] * 4;
//Serial.printf(" straddr = %u\n", straddr);
if (filename_compare(filename, straddr)) {
//Serial.printf(" match!\n");
//Serial.printf(" addr = %u\n", buf[0]);
//Serial.printf(" len = %u\n", buf[1]);
file.address = buf[0];
file.length = buf[1];
file.offset = 0;
@@ -207,9 +229,12 @@ bool SerialFlashChip::create(const char *filename, uint32_t length, uint32_t ali
stringsize = (maxfiles & 0xFFFF0000) >> 14;
maxfiles &= 0xFFFF;
// TODO: should we check if the file already exists? Then what?


// find the first unused slot for this file
index = find_first_unallocated_file_index(maxfiles);
if (index >= maxfiles) return false;
//Serial.printf("index = %u\n", index);
// compute where to store the filename and actual data
straddr = 8 + maxfiles * 12;
if (index == 0) {
@@ -222,14 +247,19 @@ bool SerialFlashChip::create(const char *filename, uint32_t length, uint32_t ali
straddr += string_length(straddr);
straddr = (straddr + 3) & 0x0003FFFC;
}
//Serial.printf("straddr = %u\n", straddr);
//Serial.printf("address = %u\n", address);
//Serial.printf("length = %u\n", length);
if (align > 0) {
// for files aligned to sectors, adjust addr & len
address += align - 1;
address /= align;
address *= align;
//Serial.printf("align address = %u\n", address);
length += align - 1;
length /= align;
length *= align;
//Serial.printf("align length = %u\n", length);
} else {
// always align every file to a page boundary
// for predictable write latency and to guarantee
@@ -238,6 +268,7 @@ bool SerialFlashChip::create(const char *filename, uint32_t length, uint32_t ali
// a write page).
address = (address + 255) & 0xFFFFFF00;
}
//Serial.printf("address = %u\n", address);
// last check, if enough space exists...
len = strlen(filename);
// TODO: check for enough string space for filename
@@ -250,7 +281,12 @@ bool SerialFlashChip::create(const char *filename, uint32_t length, uint32_t ali
buf[1] = length;
buf[2] = (straddr - (8 + maxfiles * 12)) / 4;
SerialFlash.write(8 + maxfiles * 2 + index * 10, buf, 10);
//Serial.printf(" write %u: ", 8 + maxfiles * 2 + index * 10);
//pbuf(buf, 10);
while (!SerialFlash.ready()) ; // TODO: timeout
buf[0] = filename_hash(filename);
//Serial.printf("hash = %04X\n", buf[0]);
SerialFlash.write(8 + index * 2, buf, 2);
while (!SerialFlash.ready()) ; // TODO: timeout
return true;
@@ -270,12 +306,15 @@ bool SerialFlashChip::readdir(char *filename, uint32_t strsize, uint32_t &filesi
index = dirindex;
if (index >= maxfiles) return false;
dirindex = index + 1;
//Serial.printf("readdir, index = %u\n", index);

buf[1] = 0;
SerialFlash.read(8 + 4 + maxfiles * 2 + index * 10, buf, 6);
if (buf[0] == 0xFFFFFFFF) return false;
filesize = buf[0];
straddr = 8 + maxfiles * 12 + buf[1] * 4;
//Serial.printf(" length = %u\n", buf[0]);
//Serial.printf(" straddr = %u\n", straddr);

while (strsize) {
n = strsize;
@@ -284,12 +323,14 @@ bool SerialFlashChip::readdir(char *filename, uint32_t strsize, uint32_t &filesi
for (i=0; i < n; i++) {
*p++ = str[i];
if (str[i] == 0) {
//Serial.printf(" name = %s\n", filename);
return true;
}
}
strsize -= n;
}
*(p - 1) = 0;
//Serial.printf(" name(overflow) = %s\n", filename);
return true;
}


+ 75
- 0
examples/CopyFromSD/CopyFromSD.ino Wyświetl plik

@@ -0,0 +1,75 @@
#include <SerialFlash.h>
#include <SD.h>
#include <SPI.h>

const int SDchipSelect = 4;
const int FlashChipSelect = 6;

void setup() {
//uncomment these if using Teensy audio shield
SPI.setSCK(14); // Audio shield has SCK on pin 14
SPI.setMOSI(7); // Audio shield has MOSI on pin 7

// wait up to 10 seconds for Arduino Serial Monitor
unsigned long startMillis = millis();
while (!Serial && (millis() - startMillis < 10000)) ;
delay(100);
Serial.println("Copy all files from SD Card to SPI Flash");

if (!SD.begin(SDchipSelect)) {
error("Unable to access SD card");
}
if (!SerialFlash.begin()) {
error("Unable to access SPI Flash chip");
}

int count = 0;
File rootdir = SD.open("/");
while (1) {
Serial.println();
File f = rootdir.openNextFile();
if (!f) break;
const char *filename = f.name();
Serial.print(filename);
Serial.print(" ");
unsigned long length = f.size();
Serial.println(length);
if (SerialFlash.create(filename, length)) {
SerialFlashFile ff = SerialFlash.open(filename);
if (ff) {
Serial.print(" copying");
// copy data.
unsigned long count = 0;
while (count < length) {
char buf[256];
unsigned int n;
n = f.read(buf, 256);
ff.write(buf, n);
count = count + n;
Serial.print(".");
}
ff.close();
Serial.println();
} else {
Serial.println(" error opening freshly created file!");
}
} else {
Serial.println(" unable to create file");
}
if (++count > 12) break; // testing, only do first 12 files
f.close();
}
rootdir.close();


}

void loop() {
}

void error(const char *message) {
while (1) {
Serial.println(message);
delay(2500);
}
}

Ładowanie…
Anuluj
Zapisz