SdFatConfig.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /**
  2. * Copyright (c) 2011-2021 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. /**
  26. * \file
  27. * \brief configuration definitions
  28. */
  29. #ifndef SdFatConfig_h
  30. #define SdFatConfig_h
  31. #include <stdint.h>
  32. #ifdef __AVR__
  33. #include <avr/io.h>
  34. #endif // __AVR__
  35. //
  36. // To try UTF-8 encoded filenames.
  37. // #define USE_UTF8_LONG_NAMES 1
  38. //
  39. // For minimum flash size use these settings:
  40. // #define USE_FAT_FILE_FLAG_CONTIGUOUS 0
  41. // #define ENABLE_DEDICATED_SPI 0
  42. // #define USE_LONG_FILE_NAMES 0
  43. // #define SDFAT_FILE_TYPE 1
  44. //
  45. // Options can be set in a makefile or an IDE like platformIO
  46. // if they are in a #ifndef/#endif block below.
  47. //------------------------------------------------------------------------------
  48. /** For Debug - must be one */
  49. #define ENABLE_ARDUINO_FEATURES 0
  50. /** For Debug - must be one */
  51. #define ENABLE_ARDUINO_SERIAL 0
  52. /** For Debug - must be one */
  53. #define ENABLE_ARDUINO_STRING 0
  54. //------------------------------------------------------------------------------
  55. #if ENABLE_ARDUINO_FEATURES
  56. #include "Arduino.h"
  57. #ifdef PLATFORM_ID
  58. // Only defined if a Particle device.
  59. #include "application.h"
  60. #endif // PLATFORM_ID
  61. #else
  62. #define SS 0
  63. extern "C" unsigned long millis();
  64. #endif // ENABLE_ARDUINO_FEATURES
  65. //------------------------------------------------------------------------------
  66. /**
  67. * File types for SdFat, File, SdFile, SdBaseFile, fstream,
  68. * ifstream, and ofstream.
  69. *
  70. * Set SDFAT_FILE_TYPE to:
  71. *
  72. * 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
  73. */
  74. #ifndef SDFAT_FILE_TYPE
  75. #if defined(__AVR__) && FLASHEND < 0X8000
  76. // 32K AVR boards.
  77. #define SDFAT_FILE_TYPE 1
  78. #else // defined(__AVR__) && FLASHEND < 0X8000
  79. // All other boards.
  80. #define SDFAT_FILE_TYPE 3
  81. #endif // defined(__AVR__) && FLASHEND < 0X8000
  82. #endif // SDFAT_FILE_TYPE
  83. //------------------------------------------------------------------------------
  84. /**
  85. * Set USE_FAT_FILE_FLAG_CONTIGUOUS nonzero to optimize access to
  86. * contiguous files. A small amount of flash is flash is used.
  87. */
  88. #ifndef USE_FAT_FILE_FLAG_CONTIGUOUS
  89. #define USE_FAT_FILE_FLAG_CONTIGUOUS 1
  90. #endif // USE_FAT_FILE_FLAG_CONTIGUOUS
  91. //------------------------------------------------------------------------------
  92. /**
  93. * Set ENABLE_DEDICATED_SPI non-zero to enable dedicated use of the SPI bus.
  94. * Selecting dedicated SPI in SdSpiConfig() will produce better
  95. * performance by using very large multi-block transfers to and
  96. * from the SD card.
  97. *
  98. * Enabling dedicated SPI will cost extra flash and RAM.
  99. */
  100. #ifndef ENABLE_DEDICATED_SPI
  101. #if defined(__AVR__) && FLASHEND < 0X8000
  102. // 32K AVR boards.
  103. #define ENABLE_DEDICATED_SPI 1
  104. #else // defined(__AVR__) && FLASHEND < 0X8000
  105. // All other boards.
  106. #define ENABLE_DEDICATED_SPI 1
  107. #endif // defined(__AVR__) && FLASHEND < 0X8000
  108. #endif // ENABLE_DEDICATED_SPI
  109. //------------------------------------------------------------------------------
  110. // Driver options
  111. /**
  112. * If the symbol SPI_DRIVER_SELECT is:
  113. *
  114. * 0 - An optimized custom SPI driver is used if it exists
  115. * else the standard library driver is used.
  116. *
  117. * 1 - The standard library driver is always used.
  118. *
  119. * 2 - An external SPI driver of SoftSpiDriver template class is always used.
  120. *
  121. * 3 - An external SPI driver derived from SdSpiBaseClass is always used.
  122. */
  123. #ifndef SPI_DRIVER_SELECT
  124. #define SPI_DRIVER_SELECT 0
  125. #endif // SPI_DRIVER_SELECT
  126. /**
  127. * If USE_SPI_ARRAY_TRANSFER is non-zero and the standard SPI library is
  128. * use, the array transfer function, transfer(buf, size), will be used.
  129. * This option will allocate up to a 512 byte temporary buffer for send.
  130. * This may be faster for some boards. Do not use this with AVR boards.
  131. */
  132. #ifndef USE_SPI_ARRAY_TRANSFER
  133. #define USE_SPI_ARRAY_TRANSFER 0
  134. #endif // USE_SPI_ARRAY_TRANSFER
  135. /**
  136. * SD maximum initialization clock rate.
  137. */
  138. #ifndef SD_MAX_INIT_RATE_KHZ
  139. #define SD_MAX_INIT_RATE_KHZ 400
  140. #endif // SD_MAX_INIT_RATE_KHZ
  141. /**
  142. * Set USE_BLOCK_DEVICE_INTERFACE nonzero to use a generic block device.
  143. * This allow use of an external FsBlockDevice driver that is derived from
  144. * the FsBlockDeviceInterface like this:
  145. *
  146. * class UsbMscDriver : public FsBlockDeviceInterface {
  147. * ... code for USB mass storage class driver.
  148. * };
  149. *
  150. * UsbMscDriver usbMsc;
  151. * FsVolume key;
  152. * ...
  153. *
  154. * // Init USB MSC driver.
  155. * if (!usbMsc.begin()) {
  156. * ... handle driver init failure.
  157. * }
  158. * // Init FAT/exFAT volume.
  159. * if (!key.begin(&usbMsc)) {
  160. * ... handle FAT/exFAT failure.
  161. * }
  162. */
  163. #ifndef USE_BLOCK_DEVICE_INTERFACE
  164. #define USE_BLOCK_DEVICE_INTERFACE 0
  165. #endif // USE_BLOCK_DEVICE_INTERFACE
  166. /**
  167. * SD_CHIP_SELECT_MODE defines how the functions
  168. * void sdCsInit(SdCsPin_t pin) {pinMode(pin, OUTPUT);}
  169. * and
  170. * void sdCsWrite(SdCsPin_t pin, bool level) {digitalWrite(pin, level);}
  171. * are defined.
  172. *
  173. * 0 - Internal definition is a strong symbol and can't be replaced.
  174. *
  175. * 1 - Internal definition is a weak symbol and can be replaced.
  176. *
  177. * 2 - No internal definition and must be defined in the application.
  178. */
  179. #ifndef SD_CHIP_SELECT_MODE
  180. #define SD_CHIP_SELECT_MODE 0
  181. #endif // SD_CHIP_SELECT_MODE
  182. /** Type for card chip select pin. */
  183. typedef uint8_t SdCsPin_t;
  184. //------------------------------------------------------------------------------
  185. /**
  186. * Set USE_LONG_FILE_NAMES nonzero to use long file names (LFN) in FAT16/FAT32.
  187. * exFAT always uses long file names.
  188. *
  189. * Long File Name are limited to a maximum length of 255 characters.
  190. *
  191. * This implementation allows 7-bit characters in the range
  192. * 0X20 to 0X7E except the following characters are not allowed:
  193. *
  194. * < (less than)
  195. * > (greater than)
  196. * : (colon)
  197. * " (double quote)
  198. * / (forward slash)
  199. * \ (backslash)
  200. * | (vertical bar or pipe)
  201. * ? (question mark)
  202. * * (asterisk)
  203. *
  204. */
  205. #ifndef USE_LONG_FILE_NAMES
  206. #define USE_LONG_FILE_NAMES 1
  207. #endif // USE_LONG_FILE_NAMES
  208. /**
  209. * Set USE_UTF8_LONG_NAMES nonzero to use UTF-8 file names. Use of UTF-8 names
  210. * will require significantly more flash memory and a small amount of extra
  211. * RAM.
  212. *
  213. * UTF-8 filenames allow encoding of 1,112,064 code points in Unicode using
  214. * one to four one-byte (8-bit) code units.
  215. *
  216. * As of Version 13.0, the Unicode Standard defines 143,859 characters.
  217. *
  218. * getName() will return UTF-8 strings and printName() will write UTF-8 strings.
  219. */
  220. #ifndef USE_UTF8_LONG_NAMES
  221. #define USE_UTF8_LONG_NAMES 0
  222. #endif // USE_UTF8_LONG_NAMES
  223. #if USE_UTF8_LONG_NAMES && !USE_LONG_FILE_NAMES
  224. #error "USE_UTF8_LONG_NAMES requires USE_LONG_FILE_NAMES to be non-zero."
  225. #endif // USE_UTF8_LONG_NAMES && !USE_LONG_FILE_NAMES
  226. //------------------------------------------------------------------------------
  227. /**
  228. * Set MAINTAIN_FREE_CLUSTER_COUNT nonzero to keep the count of free clusters
  229. * updated. This will increase the speed of the freeClusterCount() call
  230. * after the first call. Extra flash will be required.
  231. */
  232. #ifndef MAINTAIN_FREE_CLUSTER_COUNT
  233. #define MAINTAIN_FREE_CLUSTER_COUNT 0
  234. #endif // MAINTAIN_FREE_CLUSTER_COUNT
  235. //------------------------------------------------------------------------------
  236. /**
  237. * Set the default file time stamp when a RTC callback is not used.
  238. * A valid date and time is required by the FAT/exFAT standard.
  239. *
  240. * The default below is YYYY-01-01 00:00:00 midnight where YYYY is
  241. * the compile year from the __DATE__ macro. This is easy to recognize
  242. * as a placeholder for a correct date/time.
  243. *
  244. * The full compile date is:
  245. * FS_DATE(compileYear(), compileMonth(), compileDay())
  246. *
  247. * The full compile time is:
  248. * FS_TIME(compileHour(), compileMinute(), compileSecond())
  249. */
  250. #define FS_DEFAULT_DATE FS_DATE(compileYear(), 1, 1)
  251. /** 00:00:00 midnight */
  252. #define FS_DEFAULT_TIME FS_TIME(0, 0, 0)
  253. //------------------------------------------------------------------------------
  254. /**
  255. * If CHECK_FLASH_PROGRAMMING is zero, overlap of single sector flash
  256. * programming and other operations will be allowed for faster write
  257. * performance.
  258. *
  259. * Some cards will not sleep in low power mode unless CHECK_FLASH_PROGRAMMING
  260. * is non-zero.
  261. */
  262. #ifndef CHECK_FLASH_PROGRAMMING
  263. #define CHECK_FLASH_PROGRAMMING 0
  264. #endif // CHECK_FLASH_PROGRAMMING
  265. //------------------------------------------------------------------------------
  266. /**
  267. * To enable SD card CRC checking for SPI, set USE_SD_CRC nonzero.
  268. *
  269. * Set USE_SD_CRC to 1 to use a smaller CRC-CCITT function. This function
  270. * is slower for AVR but may be fast for ARM and other processors.
  271. *
  272. * Set USE_SD_CRC to 2 to used a larger table driven CRC-CCITT function. This
  273. * function is faster for AVR but may be slower for ARM and other processors.
  274. */
  275. #ifndef USE_SD_CRC
  276. #define USE_SD_CRC 0
  277. #endif // USE_SD_CRC
  278. //------------------------------------------------------------------------------
  279. /** If the symbol USE_FCNTL_H is nonzero, open flags for access modes O_RDONLY,
  280. * O_WRONLY, O_RDWR and the open modifiers O_APPEND, O_CREAT, O_EXCL, O_SYNC
  281. * will be defined by including the system file fcntl.h.
  282. */
  283. #ifndef USE_FCNTL_H
  284. #if defined(__AVR__)
  285. // AVR fcntl.h does not define open flags.
  286. #define USE_FCNTL_H 0
  287. #elif defined(PLATFORM_ID)
  288. // Particle boards - use fcntl.h.
  289. #define USE_FCNTL_H 1
  290. #elif defined(__arm__)
  291. // ARM gcc defines open flags.
  292. #define USE_FCNTL_H 1
  293. #elif defined(ESP32)
  294. #define USE_FCNTL_H 1
  295. #else // defined(__AVR__)
  296. #define USE_FCNTL_H 0
  297. #endif // defined(__AVR__)
  298. #endif // USE_FCNTL_H
  299. //------------------------------------------------------------------------------
  300. /**
  301. * Set INCLUDE_SDIOS nonzero to include sdios.h in SdFat.h.
  302. * sdios.h provides C++ style IO Streams.
  303. */
  304. #ifndef INCLUDE_SDIOS
  305. #define INCLUDE_SDIOS 0
  306. #endif // INCLUDE_SDIOS
  307. //------------------------------------------------------------------------------
  308. /**
  309. * Set FAT12_SUPPORT nonzero to enable use if FAT12 volumes.
  310. * FAT12 has not been well tested and requires additional flash.
  311. */
  312. #ifndef FAT12_SUPPORT
  313. #define FAT12_SUPPORT 0
  314. #endif // FAT12_SUPPORT
  315. //------------------------------------------------------------------------------
  316. /**
  317. * Set DESTRUCTOR_CLOSES_FILE nonzero to close a file in its destructor.
  318. *
  319. * Causes use of lots of heap in ARM.
  320. */
  321. #ifndef DESTRUCTOR_CLOSES_FILE
  322. #define DESTRUCTOR_CLOSES_FILE 0
  323. #endif // DESTRUCTOR_CLOSES_FILE
  324. //------------------------------------------------------------------------------
  325. /**
  326. * Call flush for endl if ENDL_CALLS_FLUSH is nonzero
  327. *
  328. * The standard for iostreams is to call flush. This is very costly for
  329. * SdFat. Each call to flush causes 2048 bytes of I/O to the SD.
  330. *
  331. * SdFat has a single 512 byte buffer for SD I/O so it must write the current
  332. * data sector to the SD, read the directory sector from the SD, update the
  333. * directory entry, write the directory sector to the SD and read the data
  334. * sector back into the buffer.
  335. *
  336. * The SD flash memory controller is not designed for this many rewrites
  337. * so performance may be reduced by more than a factor of 100.
  338. *
  339. * If ENDL_CALLS_FLUSH is zero, you must call flush and/or close to force
  340. * all data to be written to the SD.
  341. */
  342. #ifndef ENDL_CALLS_FLUSH
  343. #define ENDL_CALLS_FLUSH 0
  344. #endif // ENDL_CALLS_FLUSH
  345. //------------------------------------------------------------------------------
  346. /**
  347. * Set USE_SIMPLE_LITTLE_ENDIAN nonzero for little endian processors
  348. * with no memory alignment restrictions.
  349. */
  350. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__\
  351. && (defined(__AVR__) || defined(__ARM_FEATURE_UNALIGNED))
  352. #define USE_SIMPLE_LITTLE_ENDIAN 1
  353. #else // __BYTE_ORDER_
  354. #define USE_SIMPLE_LITTLE_ENDIAN 0
  355. #endif // __BYTE_ORDER_
  356. //------------------------------------------------------------------------------
  357. /**
  358. * Set USE_SEPARATE_FAT_CACHE nonzero to use a second 512 byte cache
  359. * for FAT16/FAT32 table entries. This improves performance for large
  360. * writes that are not a multiple of 512 bytes.
  361. */
  362. #ifdef __arm__
  363. #define USE_SEPARATE_FAT_CACHE 1
  364. #else // __arm__
  365. #define USE_SEPARATE_FAT_CACHE 0
  366. #endif // __arm__
  367. //------------------------------------------------------------------------------
  368. /**
  369. * Set USE_EXFAT_BITMAP_CACHE nonzero to use a second 512 byte cache
  370. * for exFAT bitmap entries. This improves performance for large
  371. * writes that are not a multiple of 512 bytes.
  372. */
  373. #ifdef __arm__
  374. #define USE_EXFAT_BITMAP_CACHE 1
  375. #else // __arm__
  376. #define USE_EXFAT_BITMAP_CACHE 0
  377. #endif // __arm__
  378. //------------------------------------------------------------------------------
  379. /**
  380. * Set USE_MULTI_SECTOR_IO nonzero to use multi-sector SD read/write.
  381. *
  382. * Don't use mult-sector read/write on small AVR boards.
  383. */
  384. #if defined(RAMEND) && RAMEND < 3000
  385. #define USE_MULTI_SECTOR_IO 0
  386. #else // RAMEND
  387. #define USE_MULTI_SECTOR_IO 1
  388. #endif // RAMEND
  389. //------------------------------------------------------------------------------
  390. /** Enable SDIO driver if available. */
  391. #if defined(__MK64FX512__) || defined(__MK66FX1M0__)
  392. // Pseudo pin select for SDIO.
  393. #ifndef BUILTIN_SDCARD
  394. #define BUILTIN_SDCARD 254
  395. #endif // BUILTIN_SDCARD
  396. // SPI for built-in card.
  397. #ifndef SDCARD_SPI
  398. #define SDCARD_SPI SPI1
  399. #define SDCARD_MISO_PIN 59
  400. #define SDCARD_MOSI_PIN 61
  401. #define SDCARD_SCK_PIN 60
  402. #define SDCARD_SS_PIN 62
  403. #endif // SDCARD_SPI
  404. #define HAS_SDIO_CLASS 1
  405. #endif // defined(__MK64FX512__) || defined(__MK66FX1M0__)
  406. #if defined(__IMXRT1062__)
  407. #define HAS_SDIO_CLASS 1
  408. #endif // defined(__IMXRT1062__)
  409. //------------------------------------------------------------------------------
  410. /**
  411. * Determine the default SPI configuration.
  412. */
  413. #if defined(ARDUINO_ARCH_APOLLO3)\
  414. || (defined(__AVR__) && defined(SPDR) && defined(SPSR) && defined(SPIF))\
  415. || (defined(__AVR__) && defined(SPI0) && defined(SPI_RXCIF_bm))\
  416. || defined(ESP8266) || defined(ESP32)\
  417. || defined(PLATFORM_ID)\
  418. || defined(ARDUINO_SAM_DUE)\
  419. || defined(STM32_CORE_VERSION)\
  420. || defined(__STM32F1__) || defined(__STM32F4__)\
  421. || (defined(CORE_TEENSY) && defined(__arm__))
  422. #define SD_HAS_CUSTOM_SPI 1
  423. #else // SD_HAS_CUSTOM_SPI
  424. // Use standard SPI library.
  425. #define SD_HAS_CUSTOM_SPI 0
  426. #endif // SD_HAS_CUSTOM_SPI
  427. //------------------------------------------------------------------------------
  428. #ifndef HAS_SDIO_CLASS
  429. /** Default is no SDIO. */
  430. #define HAS_SDIO_CLASS 0
  431. #endif // HAS_SDIO_CLASS
  432. #endif // SdFatConfig_h