Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

361 lines
11KB

  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. #ifdef ARDUINO
  27. #include "SdSpiCard/SdSpiCard.h"
  28. #include "FatLib/FatLib.h"
  29. #else // ARDUINO
  30. #include "SdSpiCard.h"
  31. #include "FatLib.h"
  32. #endif // ARDUINO
  33. //------------------------------------------------------------------------------
  34. /** SdFat version YYYYMMDD */
  35. #define SD_FAT_VERSION 20160212
  36. //==============================================================================
  37. /**
  38. * \class SdBaseFile
  39. * \brief Class for backward compatibility.
  40. */
  41. class SdBaseFile : public FatFile {
  42. public:
  43. SdBaseFile() {}
  44. /** Create a file object and open it in the current working directory.
  45. *
  46. * \param[in] path A path for a file to be opened.
  47. *
  48. * \param[in] oflag Values for \a oflag are constructed by a
  49. * bitwise-inclusive OR of open flags. see
  50. * FatFile::open(FatFile*, const char*, uint8_t).
  51. */
  52. SdBaseFile(const char* path, uint8_t oflag) : FatFile(path, oflag) {}
  53. };
  54. #if ENABLE_ARDUINO_FEATURES
  55. /**
  56. * \class SdFile
  57. * \brief Class for backward compatibility.
  58. */
  59. class SdFile : public PrintFile {
  60. public:
  61. SdFile() {}
  62. /** Create a file object and open it in the current working directory.
  63. *
  64. * \param[in] path A path for a file to be opened.
  65. *
  66. * \param[in] oflag Values for \a oflag are constructed by a
  67. * bitwise-inclusive OR of open flags. see
  68. * FatFile::open(FatFile*, const char*, uint8_t).
  69. */
  70. SdFile(const char* path, uint8_t oflag) : PrintFile(path, oflag) {}
  71. };
  72. #endif // #if ENABLE_ARDUINO_FEATURES
  73. /**
  74. * \class SdFatBase
  75. * \brief Virtual base class for %SdFat library.
  76. */
  77. class SdFatBase : public FatFileSystem {
  78. public:
  79. /** Initialize SD card and file system.
  80. * \param[in] spi SPI object for the card.
  81. * \param[in] csPin SD card chip select pin.
  82. * \param[in] divisor SPI divisor.
  83. * \return true for success else false.
  84. */
  85. bool begin(SdSpiCard::m_spi_t* spi, uint8_t csPin = SS, uint8_t divisor = 2) {
  86. return m_sdCard.begin(spi, csPin, divisor) &&
  87. FatFileSystem::begin();
  88. }
  89. /** \return Pointer to SD card object */
  90. SdSpiCard *card() {
  91. return &m_sdCard;
  92. }
  93. /** %Print any SD error code to Serial and halt. */
  94. void errorHalt() {
  95. errorHalt(&Serial);
  96. }
  97. /** %Print any SD error code and halt.
  98. *
  99. * \param[in] pr Print destination.
  100. */
  101. void errorHalt(Print* pr);
  102. /** %Print msg, any SD error code and halt.
  103. *
  104. * \param[in] msg Message to print.
  105. */
  106. void errorHalt(char const* msg) {
  107. errorHalt(&Serial, msg);
  108. }
  109. /** %Print msg, any SD error code, and halt.
  110. *
  111. * \param[in] pr Print destination.
  112. * \param[in] msg Message to print.
  113. */
  114. void errorHalt(Print* pr, char const* msg);
  115. /** %Print any SD error code to Serial */
  116. void errorPrint() {
  117. errorPrint(&Serial);
  118. }
  119. /** %Print any SD error code.
  120. * \param[in] pr Print device.
  121. */
  122. void errorPrint(Print* pr);
  123. /** %Print msg, any SD error code.
  124. *
  125. * \param[in] msg Message to print.
  126. */
  127. void errorPrint(const char* msg) {
  128. errorPrint(&Serial, msg);
  129. }
  130. /** %Print msg, any SD error code.
  131. *
  132. * \param[in] pr Print destination.
  133. * \param[in] msg Message to print.
  134. */
  135. void errorPrint(Print* pr, char const* msg);
  136. /** Diagnostic call to initialize FatFileSystem - use for
  137. * diagnostic purposes only.
  138. * \return true for success else false.
  139. */
  140. bool fsBegin() {
  141. return FatFileSystem::begin();
  142. }
  143. /** %Print any SD error code and halt. */
  144. void initErrorHalt() {
  145. initErrorHalt(&Serial);
  146. }
  147. /** %Print error details and halt after begin fails.
  148. *
  149. * \param[in] pr Print destination.
  150. */
  151. void initErrorHalt(Print* pr);
  152. /**Print message, error details, and halt after SdFat::init() fails.
  153. *
  154. * \param[in] msg Message to print.
  155. */
  156. void initErrorHalt(char const *msg) {
  157. initErrorHalt(&Serial, msg);
  158. }
  159. /**Print message, error details, and halt after SdFatBase::init() fails.
  160. * \param[in] pr Print device.
  161. * \param[in] msg Message to print.
  162. */
  163. void initErrorHalt(Print* pr, char const *msg);
  164. /** Print error details after SdFat::init() fails. */
  165. void initErrorPrint() {
  166. initErrorPrint(&Serial);
  167. }
  168. /** Print error details after SdFatBase::init() fails.
  169. *
  170. * \param[in] pr Print destination.
  171. */
  172. void initErrorPrint(Print* pr);
  173. /**Print message and error details and halt after SdFat::init() fails.
  174. *
  175. * \param[in] msg Message to print.
  176. */
  177. void initErrorPrint(char const *msg) {
  178. initErrorPrint(&Serial, msg);
  179. }
  180. /**Print message and error details and halt after SdFatBase::init() fails.
  181. *
  182. * \param[in] pr Print destination.
  183. * \param[in] msg Message to print.
  184. */
  185. void initErrorPrint(Print* pr, char const *msg);
  186. #if defined(ARDUINO) || defined(DOXYGEN)
  187. /** %Print msg, any SD error code, and halt.
  188. *
  189. * \param[in] msg Message to print.
  190. */
  191. void errorHalt(const __FlashStringHelper* msg) {
  192. errorHalt(&Serial, msg);
  193. }
  194. /** %Print msg, any SD error code, and halt.
  195. *
  196. * \param[in] pr Print destination.
  197. * \param[in] msg Message to print.
  198. */
  199. void errorHalt(Print* pr, const __FlashStringHelper* msg);
  200. /** %Print msg, any SD error code.
  201. *
  202. * \param[in] msg Message to print.
  203. */
  204. void errorPrint(const __FlashStringHelper* msg) {
  205. errorPrint(&Serial, msg);
  206. }
  207. /** %Print msg, any SD error code.
  208. *
  209. * \param[in] pr Print destination.
  210. * \param[in] msg Message to print.
  211. */
  212. void errorPrint(Print* pr, const __FlashStringHelper* msg);
  213. /**Print message, error details, and halt after SdFat::init() fails.
  214. *
  215. * \param[in] msg Message to print.
  216. */
  217. void initErrorHalt(const __FlashStringHelper* msg) {
  218. initErrorHalt(&Serial, msg);
  219. }
  220. /**Print message, error details, and halt after SdFatBase::init() fails.
  221. * \param[in] pr Print device for message.
  222. * \param[in] msg Message to print.
  223. */
  224. void initErrorHalt(Print* pr, const __FlashStringHelper* msg);
  225. /**Print message and error details and halt after SdFat::init() fails.
  226. *
  227. * \param[in] msg Message to print.
  228. */
  229. void initErrorPrint(const __FlashStringHelper* msg) {
  230. initErrorPrint(&Serial, msg);
  231. }
  232. /**Print message and error details and halt after SdFatBase::init() fails.
  233. *
  234. * \param[in] pr Print destination.
  235. * \param[in] msg Message to print.
  236. */
  237. void initErrorPrint(Print* pr, const __FlashStringHelper* msg);
  238. #endif // defined(ARDUINO) || defined(DOXYGEN)
  239. private:
  240. uint8_t cardErrorCode() {
  241. return m_sdCard.errorCode();
  242. }
  243. uint8_t cardErrorData() {
  244. return m_sdCard.errorData();
  245. }
  246. bool readBlock(uint32_t block, uint8_t* dst) {
  247. return m_sdCard.readBlock(block, dst);
  248. }
  249. bool writeBlock(uint32_t block, const uint8_t* src) {
  250. return m_sdCard.writeBlock(block, src);
  251. }
  252. bool readBlocks(uint32_t block, uint8_t* dst, size_t n) {
  253. return m_sdCard.readBlocks(block, dst, n);
  254. }
  255. bool writeBlocks(uint32_t block, const uint8_t* src, size_t n) {
  256. return m_sdCard.writeBlocks(block, src, n);
  257. }
  258. SdSpiCard m_sdCard;
  259. };
  260. //==============================================================================
  261. /**
  262. * \class SdFat
  263. * \brief Main file system class for %SdFat library.
  264. */
  265. class SdFat : public SdFatBase {
  266. public:
  267. #if IMPLEMENT_SPI_INTERFACE_SELECTION
  268. SdFat() {
  269. m_spi.setSpiIf(0);
  270. }
  271. explicit SdFat(uint8_t spiIf) {
  272. m_spi.setSpiIf(spiIf < SPI_INTERFACE_COUNT ? spiIf : 0);
  273. }
  274. #endif // IMPLEMENT_SPI_INTERFACE_SELECTION
  275. /** Initialize SD card and file system.
  276. *
  277. * \param[in] csPin SD card chip select pin.
  278. * \param[in] divisor SPI divisor.
  279. * \return true for success else false.
  280. */
  281. bool begin(uint8_t csPin = SS, uint8_t divisor = 2) {
  282. return SdFatBase::begin(&m_spi, csPin, divisor);
  283. }
  284. /** Diagnostic call to initialize SD card - use for diagnostic purposes only.
  285. * \param[in] csPin SD card chip select pin.
  286. * \param[in] divisor SPI divisor.
  287. * \return true for success else false.
  288. */
  289. bool cardBegin(uint8_t csPin = SS, uint8_t divisor = 2) {
  290. return card()->begin(&m_spi, csPin, divisor);
  291. }
  292. private:
  293. SpiDefault_t m_spi;
  294. };
  295. //==============================================================================
  296. #if SD_SPI_CONFIGURATION >= 3 || defined(DOXYGEN)
  297. /**
  298. * \class SdFatLibSpi
  299. * \brief SdFat class using the standard Arduino SPI library.
  300. */
  301. class SdFatLibSpi: public SdFatBase {
  302. public:
  303. /** Initialize SD card and file system.
  304. *
  305. * \param[in] csPin SD card chip select pin.
  306. * \param[in] divisor SPI divisor.
  307. * \return true for success else false.
  308. */
  309. bool begin(uint8_t csPin = SS, uint8_t divisor = 2) {
  310. return SdFatBase::begin(&m_spi, csPin, divisor);
  311. }
  312. /** Diagnostic call to initialize SD card - use for diagnostic purposes only.
  313. * \param[in] csPin SD card chip select pin.
  314. * \param[in] divisor SPI divisor.
  315. * \return true for success else false.
  316. */
  317. bool cardBegin(uint8_t csPin = SS, uint8_t divisor = 2) {
  318. return card()->begin(&m_spi, csPin, divisor);
  319. }
  320. private:
  321. SdSpiLib m_spi;
  322. };
  323. //==============================================================================
  324. /**
  325. * \class SdFatSoftSpi
  326. * \brief SdFat class using software SPI.
  327. */
  328. template<uint8_t MisoPin, uint8_t MosiPin, uint8_t SckPin>
  329. class SdFatSoftSpi : public SdFatBase {
  330. public:
  331. /** Initialize SD card and file system.
  332. *
  333. * \param[in] csPin SD card chip select pin.
  334. * \param[in] divisor SPI divisor.
  335. * \return true for success else false.
  336. */
  337. bool begin(uint8_t csPin = SS, uint8_t divisor = 2) {
  338. return SdFatBase::begin(&m_spi, csPin, divisor);
  339. }
  340. /** Diagnostic call to initialize SD card - use for diagnostic purposes only.
  341. * \param[in] csPin SD card chip select pin.
  342. * \param[in] divisor SPI divisor.
  343. * \return true for success else false.
  344. */
  345. bool cardBegin(uint8_t csPin = SS, uint8_t divisor = 2) {
  346. return card()->begin(&m_spi, csPin, divisor);
  347. }
  348. private:
  349. SdSpiSoft<MisoPin, MosiPin, SckPin> m_spi;
  350. };
  351. #endif /// SD_SPI_CONFIGURATION >= 3 || defined(DOXYGEN)
  352. #endif // SdFat_h