PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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