SdFatConfig.h 15 KB

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