Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

247 lines
7.2KB

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