Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

file_t3.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* Optimized SD Library for Teensy 3.X
  2. * Copyright (c) 2015, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this SD library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing genuine Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #if defined(__arm__)
  27. #include "SD_t3.h"
  28. #ifdef USE_TEENSY3_OPTIMIZED_CODE
  29. #define sector_t SDClass::sector_t
  30. File::File()
  31. {
  32. type = FILE_INVALID;
  33. }
  34. File::~File(void)
  35. {
  36. close();
  37. }
  38. size_t File::write(uint8_t b)
  39. {
  40. return write(&b, 1);
  41. }
  42. size_t File::write(const uint8_t *buf, size_t size)
  43. {
  44. if (type != FILE_WRITE) {
  45. setWriteError();
  46. return 0;
  47. }
  48. // TODO: a lot of work....
  49. return 0;
  50. }
  51. int File::read()
  52. {
  53. uint8_t b;
  54. int ret = read(&b, 1);
  55. if (ret != 1) return -1;
  56. return b;
  57. }
  58. int File::peek()
  59. {
  60. uint32_t save_offset = offset;
  61. uint32_t save_cluster = current_cluster;
  62. uint8_t b;
  63. int ret = read(&b, 1);
  64. if (ret != 1) return -1;
  65. offset = save_offset;
  66. current_cluster = save_cluster;
  67. return b;
  68. }
  69. int File::available()
  70. {
  71. if (type > FILE_WRITE) return 0;
  72. uint32_t maxsize = length - offset;
  73. if (maxsize > 0x7FFFFFFF) maxsize = 0x7FFFFFFF;
  74. return maxsize;
  75. }
  76. void File::flush()
  77. {
  78. }
  79. int File::read(void *buf, uint32_t size)
  80. {
  81. if (type > FILE_WRITE) return 0;
  82. uint32_t maxsize = length - offset;
  83. if (size > maxsize) size = maxsize;
  84. if (size == 0) return 0;
  85. uint32_t count = 0;
  86. uint8_t *dest = (uint8_t *)buf;
  87. uint32_t lba = custer_to_sector(current_cluster);
  88. uint32_t sindex = cluster_offset(offset);
  89. lba += sindex >> 9;
  90. sindex &= 511;
  91. if (sindex) {
  92. // first read starts in the middle of a sector
  93. do {
  94. SDCache cache;
  95. sector_t *sector = cache.read(lba);
  96. if (!sector) return 0;
  97. uint32_t n = 512 - sindex;
  98. if (size < n) {
  99. // read does not consume all of the sector
  100. memcpy(dest, sector->u8 + sindex, size);
  101. offset += size;
  102. cache.priority(+1);
  103. return size;
  104. } else {
  105. // read fully consumes this sector
  106. memcpy(dest, sector->u8 + sindex, n);
  107. dest += n;
  108. count = n;
  109. offset += n;
  110. cache.priority(-1);
  111. }
  112. } while (0);
  113. if (is_new_cluster(++lba)) {
  114. if (!next_cluster()) return count;
  115. }
  116. if (count >= size) return count;
  117. }
  118. while (1) {
  119. // every read starts from the beginning of a sector
  120. do {
  121. SDCache cache;
  122. uint32_t n = size - count;
  123. if (n < 512) {
  124. // only part of a sector is needed
  125. sector_t *sector = cache.read(lba);
  126. if (!sector) return count;
  127. memcpy(dest, sector->u8, n);
  128. offset += n;
  129. count += n;
  130. cache.priority(+1);
  131. return count;
  132. } else {
  133. // a full sector is required
  134. if (!cache.read(lba, dest)) return count;
  135. dest += 512;
  136. offset += 512;
  137. count += 512;
  138. }
  139. } while (0);
  140. if (is_new_cluster(++lba)) {
  141. if (!next_cluster()) return count;
  142. }
  143. if (count >= size) return count;
  144. }
  145. }
  146. bool File::seek(uint32_t pos)
  147. {
  148. if (type > FILE_WRITE) return false;
  149. if (pos > length) return false;
  150. uint32_t save_cluster = current_cluster;
  151. uint32_t count;
  152. // TODO: if moving to a new lba, lower cache priority
  153. signed int diff = (int)cluster_number(pos) - (int)cluster_number(offset);
  154. if (diff >= 0) {
  155. // seek fowards, 0 or more clusters from current position
  156. count = diff;
  157. } else {
  158. // seek backwards, need to start from beginning of file
  159. current_cluster = start_cluster;
  160. count = cluster_number(pos);
  161. }
  162. while (count > 0) {
  163. if (!next_cluster()) {
  164. current_cluster = save_cluster;
  165. return false;
  166. }
  167. count--;
  168. }
  169. offset = pos;
  170. return true;
  171. }
  172. void File::close()
  173. {
  174. }
  175. #endif
  176. #endif