|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
-
-
- #if defined(__arm__)
- #include "SD_t3.h"
- #ifdef USE_TEENSY3_OPTIMIZED_CODE
-
- #define sector_t SDClass::sector_t
-
- File::File()
- {
- type = FILE_INVALID;
- namestr[0] = 0;
- }
-
- File::~File(void)
- {
- close();
- }
-
- size_t File::write(uint8_t b)
- {
- return write(&b, 1);
- }
-
- size_t File::write(const uint8_t *buf, size_t size)
- {
- if (type != FILE_WRITE) {
- setWriteError();
- return 0;
- }
-
- return 0;
- }
-
- int File::read()
- {
- uint8_t b;
- int ret = read(&b, 1);
- if (ret != 1) return -1;
- return b;
- }
-
- int File::peek()
- {
- uint32_t save_offset = offset;
- uint32_t save_cluster = current_cluster;
- uint8_t b;
- int ret = read(&b, 1);
- if (ret != 1) return -1;
- offset = save_offset;
- current_cluster = save_cluster;
- return b;
- }
-
- int File::available()
- {
- if (type > FILE_WRITE) return 0;
- uint32_t maxsize = length - offset;
- if (maxsize > 0x7FFFFFFF) maxsize = 0x7FFFFFFF;
- return maxsize;
- }
-
- void File::flush()
- {
-
- }
-
- int File::read(void *buf, uint32_t size)
- {
- if (type > FILE_WRITE) return 0;
- uint32_t maxsize = length - offset;
- if (size > maxsize) size = maxsize;
- if (size == 0) return 0;
- uint32_t count = 0;
- uint8_t *dest = (uint8_t *)buf;
- uint32_t lba = custer_to_sector(current_cluster);
- uint32_t sindex = cluster_offset(offset);
-
-
-
- lba += sindex >> 9;
- sindex &= 511;
- if (sindex) {
-
- do {
- SDCache cache;
- sector_t *sector = cache.read(lba);
- if (!sector) {
-
- return 0;
- }
- uint32_t n = 512 - sindex;
- if (size < n) {
-
- memcpy(dest, sector->u8 + sindex, size);
- offset += size;
-
- return size;
- } else {
-
- memcpy(dest, sector->u8 + sindex, n);
- dest += n;
- count = n;
- offset += n;
-
- }
- } while (0);
- if (is_new_cluster(++lba)) {
- if (!next_cluster()) {
-
- return count;
- }
- }
- if (count >= size) return count;
- }
- while (1) {
-
- do {
- SDCache cache;
- uint32_t n = size - count;
- if (n < 512) {
-
- sector_t *sector = cache.read(lba);
- if (!sector) {
-
- return count;
- }
- memcpy(dest, sector->u8, n);
- offset += n;
- count += n;
-
- return count;
- } else {
-
- if (!cache.read(lba, dest)) return count;
- dest += 512;
- offset += 512;
- count += 512;
- }
- } while (0);
- if (is_new_cluster(++lba)) {
- if (!next_cluster()) {
-
- return count;
- }
- }
- if (count >= size) return count;
- }
- }
-
- bool File::seek(uint32_t pos)
- {
- if (type > FILE_WRITE) return false;
- if (pos > length) return false;
-
-
- uint32_t save_cluster = current_cluster;
- uint32_t count;
-
- signed int diff = (int)cluster_number(pos) - (int)cluster_number(offset);
- if (diff >= 0) {
-
- count = diff;
- } else {
-
- current_cluster = start_cluster;
- count = cluster_number(pos);
- }
- while (count > 0) {
- if (!next_cluster()) {
- current_cluster = save_cluster;
- return false;
- }
- count--;
- }
- offset = pos;
- return true;
- }
-
- void File::close()
- {
- type = FILE_INVALID;
- namestr[0] = 0;
- }
-
- #endif
- #endif
|