|
-
-
- #if defined(__arm__)
- #include "SD_t3.h"
- #ifdef USE_TEENSY3_OPTIMIZED_CODE
-
- #define sector_t SDClass::sector_t
- #define fatdir_t SDClass::fatdir_t
-
- File SDClass::open(const char *path, uint8_t mode)
- {
- File ret, parent = rootDir;
-
-
-
- while (1) {
- while (*path == '/') path++;
- if (*path == 0) {
-
- ret = parent;
- break;
- }
- File next;
- bool found = parent.find(path, &next);
- const char *p = path;
- do p++; while (*p != '/' && *p != 0);
- if (found) {
-
- if (*p == 0) {
-
- ret = next;
- break;
- }
-
- parent = next;
- path = p;
- } else {
-
-
- if (*p == '/') break;
-
- if (mode == FILE_READ) break;
-
- break;
- }
- }
- return ret;
- }
-
- bool SDClass::exists(const char *path)
- {
- File f = open(path);
- return (bool)f;
- }
-
- File File::openNextFile(uint8_t mode)
- {
- File f;
- uint32_t lba, sector_offset, sector_index, sector_count;
-
-
-
- if (mode > FILE_READ) return f;
- if (type == FILE_DIR_ROOT16) {
-
- sector_offset = offset >> 9;
- lba = start_cluster + sector_offset;
- sector_count = length >> 9;
- } else if (type == FILE_DIR) {
-
- sector_offset = cluster_offset(offset) >> 9;
- lba = custer_to_sector(current_cluster) + sector_offset;
- sector_count = (1 << SDClass::sector2cluster);
- } else {
- return f;
- }
-
-
-
-
-
-
- sector_index = (offset >> 5) & 15;
-
-
- while (1) {
- SDCache sector;
- sector_t *s = sector.read(lba);
- if (!s) return f;
- fatdir_t *dirent = s->dir + sector_index;
- while (sector_index < 16) {
-
-
- if (dirent->attrib != ATTR_LONG_NAME) {
- uint8_t b0 = dirent->name[0];
-
-
- if (b0 == 0) return f;
- if (b0 != 0xE5 && memcmp(dirent->name, ". ", 11) != 0
- && memcmp(dirent->name, ".. ", 11) != 0) {
- f.init(dirent);
- sector.release();
- offset += 32;
- if (cluster_offset(offset) == 0) {
- next_cluster();
- }
- return f;
- }
- }
- offset += 32;
- dirent++;
- sector_index++;
- }
- sector.release();
- sector_index = 0;
- if (++sector_offset >= sector_count) {
- if (type == FILE_DIR_ROOT16) {
- break;
- } else {
- if (!next_cluster()) break;
- lba = custer_to_sector(current_cluster);
- sector_offset = 0;
- }
- }
- }
- return f;
- }
-
-
- bool File::find(const char *filename, File *found)
- {
- bool find_unused = true;
- char name83[11];
- uint32_t lba, sector_count;
-
-
-
- const char *f = filename;
- if (*f == 0) return false;
- char *p = name83;
- while (p < name83 + 11) {
- char c = *f++;
- if (c == 0 || c == '/') {
- while (p < name83 + 11) *p++ = ' ';
- break;
- }
- if (c == '.') {
- while (p < name83 + 8) *p++ = ' ';
- continue;
- }
- if (c > 126) continue;
- if (c >= 'a' && c <= 'z') c -= 32;
- *p++ = c;
- }
-
-
-
-
-
-
- if (type == FILE_DIR_ROOT16) {
- lba = start_cluster;
- sector_count = length >> 9;
- } else if (type == FILE_DIR) {
- current_cluster = start_cluster;
- lba = custer_to_sector(start_cluster);
- sector_count = (1 << SDClass::sector2cluster);
- } else {
- return false;
- }
- while (1) {
- for (uint32_t i=0; i < sector_count; i++) {
- SDCache sector;
- sector_t *s = sector.read(lba);
- if (!s) return false;
- fatdir_t *dirent = s->dir;
- for (uint32_t j=0; j < 16; j++) {
- if (dirent->attrib == ATTR_LONG_NAME) {
-
- }
- if (memcmp(dirent->name, name83, 11) == 0) {
-
- found->init(dirent);
- return true;
- }
- uint8_t b0 = dirent->name[0];
- if (find_unused && (b0 == 0 || b0 == 0xE5)) {
- found->dirent_lba = lba;
- found->dirent_index = j;
- find_unused = false;
- }
- if (b0 == 0) return false;
- offset += 32;
- dirent++;
- }
- lba++;
- }
- if (type == FILE_DIR_ROOT16) break;
- if (!next_cluster()) break;
- lba = custer_to_sector(current_cluster);
-
- }
- return false;
- }
-
- void File::init(fatdir_t *dirent)
- {
- offset = 0;
- length = dirent->size;
- start_cluster = (dirent->cluster_high << 16) | dirent->cluster_low;
- current_cluster = start_cluster;
- type = (dirent->attrib & ATTR_DIRECTORY) ? FILE_DIR : FILE_READ;
- char *p = namestr;
- const char *s = dirent->name;
- for (int i=0; i < 8; i++) {
- if (*s == ' ') break;
- *p++ = *s++;
- }
- s = dirent->name + 8;
- if (*s != ' ') {
- *p++ = '.';
- *p++ = *s++;
- if (*s != ' ') *p++ = *s++;
- if (*s != ' ') *p++ = *s++;
- }
- *p = 0;
-
- }
-
- #endif
- #endif
|