No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

348 líneas
10KB

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