You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SdFat.h 10KB

10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
10 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /* Arduino SdFat Library
  2. * Copyright (C) 2012 by William Greiman
  3. *
  4. * This file is part of the Arduino SdFat Library
  5. *
  6. * This Library is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This Library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with the Arduino SdFat Library. If not, see
  18. * <http://www.gnu.org/licenses/>.
  19. */
  20. #ifndef SdFat_h
  21. #define SdFat_h
  22. /**
  23. * \file
  24. * \brief SdFat class
  25. */
  26. #include "SdSpiCard.h"
  27. #include "utility/FatLib.h"
  28. //------------------------------------------------------------------------------
  29. /** SdFat version YYYYMMDD */
  30. #define SD_FAT_VERSION 20150201
  31. //==============================================================================
  32. /**
  33. * \class SdBaseFile
  34. * \brief Class for backward compatibility.
  35. */
  36. class SdBaseFile : public FatFile {
  37. public:
  38. SdBaseFile() {}
  39. /** Create a file object and open it in the current working directory.
  40. *
  41. * \param[in] path A path for a file to be opened.
  42. *
  43. * \param[in] oflag Values for \a oflag are constructed by a
  44. * bitwise-inclusive OR of open flags. see
  45. * FatFile::open(FatFile*, const char*, uint8_t).
  46. */
  47. SdBaseFile(const char* path, uint8_t oflag) : FatFile(path, oflag) {}
  48. };
  49. #if ENABLE_ARDUINO_FEATURES
  50. /**
  51. * \class SdFile
  52. * \brief Class for backward compatibility.
  53. */
  54. class SdFile : public PrintFile {
  55. public:
  56. SdFile() {}
  57. /** Create a file object and open it in the current working directory.
  58. *
  59. * \param[in] path A path for a file to be opened.
  60. *
  61. * \param[in] oflag Values for \a oflag are constructed by a
  62. * bitwise-inclusive OR of open flags. see
  63. * FatFile::open(FatFile*, const char*, uint8_t).
  64. */
  65. SdFile(const char* path, uint8_t oflag) : PrintFile(path, oflag) {}
  66. };
  67. #endif // #if ENABLE_ARDUINO_FEATURES
  68. /**
  69. * \class SdFatBase
  70. * \brief Virtual base class for %SdFat library.
  71. */
  72. class SdFatBase : public FatFileSystem {
  73. public:
  74. /** Initialize SD card and file system.
  75. * \param[in] spi SPI object for the card.
  76. * \param[in] csPin SD card chip select pin.
  77. * \param[in] divisor SPI divisor.
  78. * \return true for success else false.
  79. */
  80. bool begin(SdSpiCard::m_spi_t* spi, uint8_t csPin = SS, uint8_t divisor = 2) {
  81. return m_sdCard.begin(spi, csPin, divisor) &&
  82. FatFileSystem::begin();
  83. }
  84. /** \return Pointer to SD card object */
  85. SdSpiCard *card() {
  86. return &m_sdCard;
  87. }
  88. /** %Print any SD error code to Serial and halt. */
  89. void errorHalt() {
  90. errorHalt(&Serial);
  91. }
  92. /** %Print any SD error code and halt.
  93. *
  94. * \param[in] pr Print destination.
  95. */
  96. void errorHalt(Print* pr);
  97. /** %Print msg, any SD error code and halt.
  98. *
  99. * \param[in] msg Message to print.
  100. */
  101. void errorHalt(char const* msg) {
  102. errorHalt(&Serial, msg);
  103. }
  104. /** %Print msg, any SD error code, and halt.
  105. *
  106. * \param[in] pr Print destination.
  107. * \param[in] msg Message to print.
  108. */
  109. void errorHalt(Print* pr, char const* msg);
  110. /** %Print msg, any SD error code, and halt.
  111. *
  112. * \param[in] msg Message to print.
  113. */
  114. void errorHalt(const __FlashStringHelper* msg) {
  115. errorHalt(&Serial, msg);
  116. }
  117. /** %Print msg, any SD error code, and halt.
  118. *
  119. * \param[in] pr Print destination.
  120. * \param[in] msg Message to print.
  121. */
  122. void errorHalt(Print* pr, const __FlashStringHelper* msg);
  123. /** %Print any SD error code to Serial */
  124. void errorPrint() {
  125. errorPrint(&Serial);
  126. }
  127. /** %Print any SD error code.
  128. * \param[in] pr Print device.
  129. */
  130. void errorPrint(Print* pr);
  131. /** %Print msg, any SD error code.
  132. *
  133. * \param[in] msg Message to print.
  134. */
  135. void errorPrint(const char* msg) {
  136. errorPrint(&Serial, msg);
  137. }
  138. /** %Print msg, any SD error code.
  139. *
  140. * \param[in] pr Print destination.
  141. * \param[in] msg Message to print.
  142. */
  143. void errorPrint(Print* pr, char const* msg);
  144. /** %Print msg, any SD error code.
  145. *
  146. * \param[in] msg Message to print.
  147. */
  148. void errorPrint(const __FlashStringHelper* msg) {
  149. errorPrint(&Serial, msg);
  150. }
  151. /** %Print msg, any SD error code.
  152. *
  153. * \param[in] pr Print destination.
  154. * \param[in] msg Message to print.
  155. */
  156. void errorPrint(Print* pr, const __FlashStringHelper* msg);
  157. /** Diagnostic call to initialize FatFileSystem - use for
  158. * diagnostic purposes only.
  159. * \return true for success else false.
  160. */
  161. bool fsBegin() {
  162. return FatFileSystem::begin();
  163. }
  164. /** %Print any SD error code and halt. */
  165. void initErrorHalt() {
  166. initErrorHalt(&Serial);
  167. }
  168. /** %Print error details and halt after begin fails.
  169. *
  170. * \param[in] pr Print destination.
  171. */
  172. void initErrorHalt(Print* pr);
  173. /**Print message, error details, and halt after SdFat::init() fails.
  174. *
  175. * \param[in] msg Message to print.
  176. */
  177. void initErrorHalt(char const *msg) {
  178. initErrorHalt(&Serial, msg);
  179. }
  180. /**Print message, error details, and halt after SdFatBase::init() fails.
  181. * \param[in] pr Print device.
  182. * \param[in] msg Message to print.
  183. */
  184. void initErrorHalt(Print* pr, char const *msg);
  185. /**Print message, error details, and halt after SdFat::init() fails.
  186. *
  187. * \param[in] msg Message to print.
  188. */
  189. void initErrorHalt(const __FlashStringHelper* msg) {
  190. initErrorHalt(&Serial, msg);
  191. }
  192. /**Print message, error details, and halt after SdFatBase::init() fails.
  193. * \param[in] pr Print device for message.
  194. * \param[in] msg Message to print.
  195. */
  196. void initErrorHalt(Print* pr, const __FlashStringHelper* msg);
  197. /** Print error details after SdFat::init() fails. */
  198. void initErrorPrint() {
  199. initErrorPrint(&Serial);
  200. }
  201. /** Print error details after SdFatBase::init() fails.
  202. *
  203. * \param[in] pr Print destination.
  204. */
  205. void initErrorPrint(Print* pr);
  206. /**Print message and error details and halt after SdFat::init() fails.
  207. *
  208. * \param[in] msg Message to print.
  209. */
  210. void initErrorPrint(char const *msg) {
  211. initErrorPrint(&Serial, msg);
  212. }
  213. /**Print message and error details and halt after SdFatBase::init() fails.
  214. *
  215. * \param[in] pr Print destination.
  216. * \param[in] msg Message to print.
  217. */
  218. void initErrorPrint(Print* pr, char const *msg);
  219. /**Print message and error details and halt after SdFat::init() fails.
  220. *
  221. * \param[in] msg Message to print.
  222. */
  223. void initErrorPrint(const __FlashStringHelper* msg) {
  224. initErrorPrint(&Serial, msg);
  225. }
  226. /**Print message and error details and halt after SdFatBase::init() fails.
  227. *
  228. * \param[in] pr Print destination.
  229. * \param[in] msg Message to print.
  230. */
  231. void initErrorPrint(Print* pr, const __FlashStringHelper* msg);
  232. private:
  233. uint8_t cardErrorCode() {
  234. return m_sdCard.errorCode();
  235. }
  236. uint8_t cardErrorData() {
  237. return m_sdCard.errorData();
  238. }
  239. bool readBlock(uint32_t block, uint8_t* dst) {
  240. return m_sdCard.readBlock(block, dst);
  241. }
  242. bool writeBlock(uint32_t block, const uint8_t* src) {
  243. return m_sdCard.writeBlock(block, src);
  244. }
  245. bool readBlocks(uint32_t block, uint8_t* dst, size_t n) {
  246. return m_sdCard.readBlocks(block, dst, n);
  247. }
  248. bool writeBlocks(uint32_t block, const uint8_t* src, size_t n) {
  249. return m_sdCard.writeBlocks(block, src, n);
  250. }
  251. SdSpiCard m_sdCard;
  252. };
  253. //==============================================================================
  254. /**
  255. * \class SdFat
  256. * \brief Main file system class for %SdFat library.
  257. */
  258. class SdFat : public SdFatBase {
  259. public:
  260. /** Initialize SD card and file system.
  261. *
  262. * \param[in] csPin SD card chip select pin.
  263. * \param[in] divisor SPI divisor.
  264. * \return true for success else false.
  265. */
  266. bool begin(uint8_t csPin = SS, uint8_t divisor = 2) {
  267. return SdFatBase::begin(&m_spi, csPin, divisor);
  268. }
  269. /** Diagnostic call to initialize SD card - use for diagnostic purposes only.
  270. * \param[in] csPin SD card chip select pin.
  271. * \param[in] divisor SPI divisor.
  272. * \return true for success else false.
  273. */
  274. bool cardBegin(uint8_t csPin = SS, uint8_t divisor = 2) {
  275. return card()->begin(&m_spi, csPin, divisor);
  276. }
  277. private:
  278. SpiDefault_t m_spi;
  279. };
  280. //==============================================================================
  281. #if SD_SPI_CONFIGURATION >= 3 || defined(DOXYGEN)
  282. /**
  283. * \class SdFatLibSpi
  284. * \brief SdFat class using the standard Arduino SPI library.
  285. */
  286. class SdFatLibSpi: public SdFatBase {
  287. public:
  288. /** Initialize SD card and file system.
  289. *
  290. * \param[in] csPin SD card chip select pin.
  291. * \param[in] divisor SPI divisor.
  292. * \return true for success else false.
  293. */
  294. bool begin(uint8_t csPin = SS, uint8_t divisor = 2) {
  295. return SdFatBase::begin(&m_spi, csPin, divisor);
  296. }
  297. /** Diagnostic call to initialize SD card - use for diagnostic purposes only.
  298. * \param[in] csPin SD card chip select pin.
  299. * \param[in] divisor SPI divisor.
  300. * \return true for success else false.
  301. */
  302. bool cardBegin(uint8_t csPin = SS, uint8_t divisor = 2) {
  303. return card()->begin(&m_spi, csPin, divisor);
  304. }
  305. private:
  306. SdSpiLib m_spi;
  307. };
  308. //==============================================================================
  309. /**
  310. * \class SdFatSoftSpi
  311. * \brief SdFat class using software SPI.
  312. */
  313. template<uint8_t MisoPin, uint8_t MosiPin, uint8_t SckPin>
  314. class SdFatSoftSpi : public SdFatBase {
  315. public:
  316. /** Initialize SD card and file system.
  317. *
  318. * \param[in] csPin SD card chip select pin.
  319. * \param[in] divisor SPI divisor.
  320. * \return true for success else false.
  321. */
  322. bool begin(uint8_t csPin = SS, uint8_t divisor = 2) {
  323. return SdFatBase::begin(&m_spi, csPin, divisor);
  324. }
  325. /** Diagnostic call to initialize SD card - use for diagnostic purposes only.
  326. * \param[in] csPin SD card chip select pin.
  327. * \param[in] divisor SPI divisor.
  328. * \return true for success else false.
  329. */
  330. bool cardBegin(uint8_t csPin = SS, uint8_t divisor = 2) {
  331. return card()->begin(&m_spi, csPin, divisor);
  332. }
  333. private:
  334. SdSpiSoft<MisoPin, MosiPin, SckPin> m_spi;
  335. };
  336. #endif /// SD_SPI_CONFIGURATION >= 3 || defined(DOXYGEN)
  337. #endif // SdFat_h