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

242 lines
6.9KB

  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. return namestr;
  151. }
  152. bool isDirectory() {
  153. return (type == FILE_DIR) || (type == FILE_DIR_ROOT16);
  154. }
  155. File openNextFile(uint8_t mode = FILE_READ);
  156. void rewindDirectory() {
  157. rewind();
  158. }
  159. using Print::write;
  160. void rewind() {
  161. offset = 0;
  162. current_cluster = start_cluster;
  163. };
  164. private:
  165. bool find(const char *filename, File *found);
  166. void init(SDClass::fatdir_t *dirent);
  167. bool next_cluster();
  168. uint32_t offset; // position within file (EOF = length)
  169. uint32_t length; // total size of file
  170. uint32_t start_cluster; // first cluster for the file
  171. uint32_t current_cluster; // position (must agree w/ offset)
  172. uint32_t dirent_lba; // dir sector for this file
  173. uint8_t dirent_index; // dir index within sector (0 to 15)
  174. uint8_t type; // file vs dir
  175. char namestr[13];
  176. friend class SDClass;
  177. static inline uint32_t cluster_number(uint32_t n) {
  178. return n >> (SDClass::sector2cluster + 9);
  179. }
  180. static inline uint32_t cluster_offset(uint32_t n) {
  181. return n & ((1 << (SDClass::sector2cluster + 9)) - 1);
  182. }
  183. static inline uint32_t custer_to_sector(uint32_t n) {
  184. return (n - 2) * (1 << SDClass::sector2cluster)
  185. + SDClass::data_begin_lba;
  186. }
  187. static inline bool is_new_cluster(uint32_t lba) {
  188. return (lba & ((1 << SDClass::sector2cluster) - 1)) == 0;
  189. }
  190. };
  191. class SDCache
  192. {
  193. private:
  194. // SDCache objects should be created with local scope.
  195. // read(), get(), alloc() acquire temporary locks on
  196. // cache buffers, which are automatically released
  197. // by the destructor when the object goes out of scope.
  198. // Pointers returned by those functions must NEVER be
  199. // used after the SDCache object which returned them
  200. // no longer exists.
  201. SDCache(void) { item = NULL; }
  202. ~SDCache(void) { release(); }
  203. typedef struct cache_struct {
  204. SDClass::sector_t data;
  205. uint32_t lba;
  206. cache_struct * next;
  207. uint8_t usagecount;
  208. uint8_t flags;
  209. } cache_t;
  210. SDClass::sector_t * read(uint32_t lba, bool is_fat=false);
  211. bool read(uint32_t lba, void *buffer);
  212. cache_t * get(uint32_t lba, bool allocate=true);
  213. void dirty(void);
  214. void flush(void);
  215. void release(void);
  216. cache_t * item;
  217. static cache_t *cache_list;
  218. static cache_t cache[SD_CACHE_SIZE];
  219. static void init(void);
  220. static void print_cache(void);
  221. friend class SDClass;
  222. friend class File;
  223. };
  224. extern SDClass SD;
  225. #endif