You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

преди 9 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // This Teensy 3.x optimized version is a work-in-progress.
  27. // Uncomment this line to use the Teensy version. Otherwise,
  28. // the normal SD library is used.
  29. //#define USE_TEENSY3_OPTIMIZED_CODE
  30. /* Why reinvent the SD library wheel...
  31. * 1: Allow reading files from within interrupts
  32. * 2: Cache more than one sector for improved performance
  33. * 3: General optimization for 32 bit ARM on Teensy 3.x & Teensy-LC
  34. * 4: Permissive MIT license
  35. */
  36. #if !defined(__SD_t3_H__) && defined(__arm__) && defined(USE_TEENSY3_OPTIMIZED_CODE)
  37. #define __SD_t3_H__
  38. #define __SD_H__
  39. #include <Arduino.h>
  40. #include <SPI.h>
  41. #include "utility/ioreg.h"
  42. #define SD_CACHE_SIZE 7 // each cache entry uses 520 bytes of RAM
  43. #define SD_SPI_SPEED SPISettings(25000000, MSBFIRST, SPI_MODE0)
  44. #define FILE_READ 0
  45. #define FILE_WRITE 1
  46. #define FILE_DIR 2
  47. #define FILE_DIR_ROOT16 3
  48. #define FILE_INVALID 4
  49. class File;
  50. class SDClass
  51. {
  52. public:
  53. static bool begin(uint8_t csPin = SS);
  54. static File open(const char *path, uint8_t mode = FILE_READ);
  55. static bool exists(const char *path);
  56. static bool mkdir(const char *path);
  57. static bool remove(const char *path);
  58. static bool rmdir(const char *path);
  59. private:
  60. static uint8_t sd_cmd0();
  61. static uint32_t sd_cmd8();
  62. static uint8_t sd_acmd41(uint32_t hcs);
  63. static uint32_t sd_cmd58();
  64. static bool sd_read(uint32_t addr, void * data);
  65. static void send_cmd(uint16_t cmd, uint32_t arg);
  66. static uint8_t recv_r1();
  67. static uint32_t recv_r3_or_r7();
  68. static void end_cmd();
  69. static volatile IO_REG_TYPE * csreg;
  70. static IO_REG_TYPE csmask;
  71. static uint8_t card_type; // 1=SDv1, 2=SDv2, 3=SDHC
  72. static File rootDir;
  73. static uint32_t fat1_begin_lba;
  74. static uint32_t fat2_begin_lba;
  75. static uint32_t data_begin_lba;
  76. static uint32_t max_cluster;
  77. static uint8_t sector2cluster;
  78. static uint8_t fat_type;
  79. friend class SDCache;
  80. friend class File;
  81. typedef struct {
  82. union {
  83. struct { // short 8.3 filename info
  84. char name[11];
  85. uint8_t attrib;
  86. uint8_t reserved;
  87. uint8_t ctime_tenth;
  88. uint16_t ctime;
  89. uint16_t cdate;
  90. uint16_t adate;
  91. uint16_t cluster_high;
  92. uint16_t wtime;
  93. uint16_t wdate;
  94. uint16_t cluster_low;
  95. uint32_t size;
  96. };
  97. struct { // long filename info
  98. uint8_t ord;
  99. uint8_t lname1[10];
  100. uint8_t lattrib;
  101. uint8_t type;
  102. uint8_t cksum;
  103. uint8_t lname2[12];
  104. uint16_t lcluster_low;
  105. uint8_t lname3[4];
  106. };
  107. };
  108. } fatdir_t;
  109. typedef union {
  110. uint8_t u8[512];
  111. uint16_t u16[256];
  112. uint32_t u32[128];
  113. fatdir_t dir[16];
  114. } sector_t;
  115. };
  116. #define ATTR_READ_ONLY 0x01
  117. #define ATTR_HIDDEN 0x02
  118. #define ATTR_SYSTEM 0x04
  119. #define ATTR_VOLUME_ID 0x08
  120. #define ATTR_DIRECTORY 0x10
  121. #define ATTR_ARCHIVE 0x20
  122. #define ATTR_LONG_NAME 0x0F
  123. class File : public Stream
  124. {
  125. public:
  126. File();
  127. ~File();
  128. // TODO: copy constructors, needs to be ISR safe
  129. virtual size_t write(uint8_t b);
  130. virtual size_t write(const uint8_t *buf, size_t size);
  131. virtual int read();
  132. virtual int peek();
  133. virtual int available();
  134. virtual void flush();
  135. int read(void *buf, uint32_t size);
  136. bool seek(uint32_t pos);
  137. uint32_t position() {
  138. if (type <= FILE_WRITE) return offset;
  139. return 0;
  140. }
  141. uint32_t size() {
  142. if (type <= FILE_WRITE) return length;
  143. return 0;
  144. }
  145. void close();
  146. operator bool() {
  147. return (type < FILE_INVALID);
  148. }
  149. char * name();
  150. bool isDirectory() {
  151. return (type == FILE_DIR) || (type == FILE_DIR_ROOT16);
  152. }
  153. File openNextFile(uint8_t mode = FILE_READ);
  154. void rewindDirectory() {
  155. rewind();
  156. }
  157. using Print::write;
  158. void rewind() {
  159. offset = 0;
  160. current_cluster = start_cluster;
  161. };
  162. private:
  163. bool find(const char *filename, File *found);
  164. void init(SDClass::fatdir_t *dirent);
  165. bool next_cluster();
  166. uint32_t offset; // position within file (EOF = length)
  167. uint32_t length; // total size of file
  168. uint32_t start_cluster; // first cluster for the file
  169. uint32_t current_cluster; // position (must agree w/ offset)
  170. uint32_t dirent_lba; // dir sector for this file
  171. uint8_t dirent_index; // dir index within sector (0 to 15)
  172. uint8_t type; // file vs dir
  173. char namestr[12];
  174. friend class SDClass;
  175. static inline uint32_t cluster_number(uint32_t n) {
  176. return n >> (SDClass::sector2cluster + 9);
  177. }
  178. static inline uint32_t cluster_offset(uint32_t n) {
  179. return n & ((1 << (SDClass::sector2cluster + 9)) - 1);
  180. }
  181. static inline uint32_t custer_to_sector(uint32_t n) {
  182. return (n - 2) * (1 << SDClass::sector2cluster)
  183. + SDClass::data_begin_lba;
  184. }
  185. static inline bool is_new_cluster(uint32_t lba) {
  186. return (lba & ((1 << SDClass::sector2cluster) - 1)) == 0;
  187. }
  188. };
  189. class SDCache
  190. {
  191. private:
  192. // SDCache objects should be created with local scope.
  193. // read(), get(), alloc() acquire temporary locks on
  194. // cache buffers, which are automatically released
  195. // by the destructor when the object goes out of scope.
  196. // Pointers returned by those functions must NEVER be
  197. // used after the SDCache object which returned them
  198. // no longer exists.
  199. SDCache(void) { item = NULL; }
  200. ~SDCache(void) { release(); }
  201. typedef struct {
  202. SDClass::sector_t data;
  203. uint32_t lba;
  204. uint8_t usagecount;
  205. uint8_t priority;
  206. uint8_t flags;
  207. } cache_t;
  208. SDClass::sector_t * read(uint32_t lba, bool is_fat=false);
  209. bool read(uint32_t lba, void *buffer);
  210. SDClass::sector_t * alloc(uint32_t lba);
  211. void priority(signed int n);
  212. void priority(uint32_t lba, signed int n);
  213. void dirty(void);
  214. void flush(void);
  215. void release(void);
  216. cache_t * find(uint32_t lba);
  217. cache_t * empty();
  218. cache_t * item;
  219. static cache_t cache[SD_CACHE_SIZE];
  220. friend class SDClass;
  221. friend class File;
  222. };
  223. extern SDClass SD;
  224. #endif