Keyboard firmwares for Atmel AVR and Cortex-M
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

FATFileSystem.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2012 ARM Limited
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "mbed.h"
  23. #include "ffconf.h"
  24. #include "mbed_debug.h"
  25. #include "FATFileSystem.h"
  26. #include "FATFileHandle.h"
  27. #include "FATDirHandle.h"
  28. DWORD get_fattime(void) {
  29. time_t rawtime;
  30. time(&rawtime);
  31. struct tm *ptm = localtime(&rawtime);
  32. return (DWORD)(ptm->tm_year - 80) << 25
  33. | (DWORD)(ptm->tm_mon + 1 ) << 21
  34. | (DWORD)(ptm->tm_mday ) << 16
  35. | (DWORD)(ptm->tm_hour ) << 11
  36. | (DWORD)(ptm->tm_min ) << 5
  37. | (DWORD)(ptm->tm_sec/2 );
  38. }
  39. FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
  40. FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
  41. debug_if(FFS_DBG, "FATFileSystem(%s)\n", n);
  42. for(int i=0; i<_VOLUMES; i++) {
  43. if(_ffs[i] == 0) {
  44. _ffs[i] = this;
  45. _fsid = i;
  46. debug_if(FFS_DBG, "Mounting [%s] on ffs drive [%d]\n", _name, _fsid);
  47. f_mount(i, &_fs);
  48. return;
  49. }
  50. }
  51. error("Couldn't create %s in FATFileSystem::FATFileSystem\n", n);
  52. }
  53. FATFileSystem::~FATFileSystem() {
  54. for (int i=0; i<_VOLUMES; i++) {
  55. if (_ffs[i] == this) {
  56. _ffs[i] = 0;
  57. f_mount(i, NULL);
  58. }
  59. }
  60. }
  61. FileHandle *FATFileSystem::open(const char* name, int flags) {
  62. debug_if(FFS_DBG, "open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid);
  63. char n[64];
  64. sprintf(n, "%d:/%s", _fsid, name);
  65. /* POSIX flags -> FatFS open mode */
  66. BYTE openmode;
  67. if (flags & O_RDWR) {
  68. openmode = FA_READ|FA_WRITE;
  69. } else if(flags & O_WRONLY) {
  70. openmode = FA_WRITE;
  71. } else {
  72. openmode = FA_READ;
  73. }
  74. if(flags & O_CREAT) {
  75. if(flags & O_TRUNC) {
  76. openmode |= FA_CREATE_ALWAYS;
  77. } else {
  78. openmode |= FA_OPEN_ALWAYS;
  79. }
  80. }
  81. FIL fh;
  82. FRESULT res = f_open(&fh, n, openmode);
  83. if (res) {
  84. debug_if(FFS_DBG, "f_open('w') failed: %d\n", res);
  85. return NULL;
  86. }
  87. if (flags & O_APPEND) {
  88. f_lseek(&fh, fh.fsize);
  89. }
  90. return new FATFileHandle(fh);
  91. }
  92. int FATFileSystem::remove(const char *filename) {
  93. FRESULT res = f_unlink(filename);
  94. if (res) {
  95. debug_if(FFS_DBG, "f_unlink() failed: %d\n", res);
  96. return -1;
  97. }
  98. return 0;
  99. }
  100. int FATFileSystem::rename(const char *oldname, const char *newname) {
  101. FRESULT res = f_rename(oldname, newname);
  102. if (res) {
  103. debug_if(FFS_DBG, "f_rename() failed: %d\n", res);
  104. return -1;
  105. }
  106. return 0;
  107. }
  108. int FATFileSystem::format() {
  109. FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
  110. if (res) {
  111. debug_if(FFS_DBG, "f_mkfs() failed: %d\n", res);
  112. return -1;
  113. }
  114. return 0;
  115. }
  116. DirHandle *FATFileSystem::opendir(const char *name) {
  117. FATFS_DIR dir;
  118. FRESULT res = f_opendir(&dir, name);
  119. if (res != 0) {
  120. return NULL;
  121. }
  122. return new FATDirHandle(dir);
  123. }
  124. int FATFileSystem::mkdir(const char *name, mode_t mode) {
  125. FRESULT res = f_mkdir(name);
  126. return res == 0 ? 0 : -1;
  127. }
  128. int FATFileSystem::mount() {
  129. FRESULT res = f_mount(_fsid, &_fs);
  130. return res == 0 ? 0 : -1;
  131. }
  132. int FATFileSystem::unmount() {
  133. if (disk_sync())
  134. return -1;
  135. FRESULT res = f_mount(_fsid, NULL);
  136. return res == 0 ? 0 : -1;
  137. }