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.

162 lines
3.9KB

  1. // Copyright 2011 Olivier Gillet.
  2. //
  3. // Author: Olivier Gillet (ol.gillet@gmail.com)
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // -----------------------------------------------------------------------------
  17. //
  18. // FatFS wrappers.
  19. #ifndef AVRLIBX_FILESYSTEM_FILE_SYSTEM_H_
  20. #define AVRLIBX_FILESYSTEM_FILE_SYSTEM_H_
  21. #include <string.h>
  22. #include "avrlibx/avrlibx.h"
  23. #include "avrlibx/third_party/ff/ff.h"
  24. #include "avrlibx/third_party/ff/mmc.h"
  25. namespace avrlibx {
  26. enum FilesystemStatus {
  27. FS_OK = 0,
  28. FS_DISK_ERROR,
  29. FS_EXCEPTION,
  30. FS_DRIVE_NOT_READY,
  31. FS_FILE_NOT_FOUND,
  32. FS_PATH_NOT_FOUND,
  33. FS_INVALID_NAME,
  34. FS_ACCESS_DENIED,
  35. FS_FILE_EXISTS,
  36. FS_INVALID_OBJECT,
  37. FS_WRITE_PROTECTED,
  38. FS_INVALID_DRIVE,
  39. FS_VOLUME_NOT_INITIALIZED,
  40. FS_NO_FAT_VOLUME,
  41. FS_FORMAT_FAILED,
  42. FS_TIMEOUT,
  43. FS_LOCKED,
  44. FS_NOT_ENOUGH_MEMORY,
  45. FS_TOO_MANY_FILES,
  46. FS_INVALID_PARAMETER,
  47. FS_NOT_OPENED,
  48. FS_BAD_FILE_FORMAT,
  49. FS_COPY_ERROR
  50. };
  51. enum FileAttribute {
  52. FS_ATTRIBUTE_READ_ONLY = 1,
  53. FS_ATTRIBUTE_HIDDEN = 2,
  54. FS_ATTRIBUTE_SYSTEM = 4,
  55. FS_ATTRIBUTE_VOLUME = 8,
  56. FS_ATTRIBUTE_LFN = 15,
  57. FS_ATTRIBUTE_DIRECTORY = 16,
  58. FS_ATTRIBUTE_ARCHIVE = 32,
  59. FS_ATTRIBUTE_ATTRIBUTES = 0x3f,
  60. };
  61. struct FileInfo {
  62. inline uint32_t size() const {
  63. return file_info.fsize;
  64. }
  65. inline uint16_t modification_date() const {
  66. return file_info.fdate;
  67. }
  68. inline uint16_t modification_time() const {
  69. return file_info.ftime;
  70. }
  71. inline uint8_t attributes() const {
  72. return file_info.fattrib;
  73. }
  74. inline uint8_t is_read_only() const {
  75. return file_info.fattrib & FS_ATTRIBUTE_READ_ONLY;
  76. }
  77. inline uint8_t is_hidden() const {
  78. return file_info.fattrib & FS_ATTRIBUTE_HIDDEN;
  79. }
  80. inline uint8_t is_system() const {
  81. return file_info.fattrib & FS_ATTRIBUTE_SYSTEM;
  82. }
  83. inline uint8_t is_volume() const {
  84. return file_info.fattrib & FS_ATTRIBUTE_VOLUME;
  85. }
  86. inline uint8_t is_directory() const {
  87. return file_info.fattrib & FS_ATTRIBUTE_DIRECTORY;
  88. }
  89. inline uint8_t is_archive() const {
  90. return file_info.fattrib & FS_ATTRIBUTE_ARCHIVE;
  91. }
  92. inline const char* name() const {
  93. return file_info.fname;
  94. }
  95. FILINFO file_info;
  96. };
  97. class Filesystem {
  98. public:
  99. Filesystem() { }
  100. static FilesystemStatus Init();
  101. static FilesystemStatus Init(uint16_t timeout_ms);
  102. static FilesystemStatus Unlink(const char* file_name);
  103. static FilesystemStatus Mkdir(const char* dir_name);
  104. static FilesystemStatus Mkdirs(char* path);
  105. static FilesystemStatus Chmod(
  106. const char* file_name,
  107. uint8_t value,
  108. uint8_t mask);
  109. static FilesystemStatus Rename(const char* old_name, const char* new_name);
  110. static FilesystemStatus FileStatus(const char* file_name, FileInfo* info);
  111. static FilesystemStatus Utime(
  112. const char* file_name,
  113. uint16_t date,
  114. uint16_t time);
  115. static FilesystemStatus Mkfs();
  116. static uint32_t GetFreeSpace();
  117. static uint16_t GetType() {
  118. uint8_t card_type;
  119. disk_ioctl(0, MMC_GET_TYPE, &card_type);
  120. return fs_.fs_type | (card_type << 8);
  121. }
  122. static inline void Tick() {
  123. disk_timerproc();
  124. }
  125. static uint8_t* buffer() { return fs_.win; }
  126. private:
  127. static FATFS fs_;
  128. DISALLOW_COPY_AND_ASSIGN(Filesystem);
  129. };
  130. } // namespace avrlibx
  131. #endif // AVRLIBX_FILESYSTEM_FILE_SYSTEM_H_