My Marlin configs for Fabrikator Mini and CTC i3 Pro B
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.

persistent_store_sdcard.cpp 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /**
  2. * Marlin 3D Printer Firmware
  3. *
  4. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  5. * Copyright (c) 2016 Bob Cousins bobcousins42@googlemail.com
  6. * Copyright (c) 2015-2016 Nico Tonnhofer wurstnase.reprap@gmail.com
  7. * Copyright (c) 2016 Victor Perez victor_pv@hotmail.com
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. #ifdef TARGET_LPC1768
  24. #include "../../inc/MarlinConfig.h"
  25. #if ENABLED(SDCARD_EEPROM_EMULATION)
  26. #include "persistent_store_api.h"
  27. #include <chanfs/diskio.h>
  28. #include <chanfs/ff.h>
  29. extern uint32_t MSC_Aquire_Lock();
  30. extern uint32_t MSC_Release_Lock();
  31. FATFS fat_fs;
  32. FIL eeprom_file;
  33. bool eeprom_file_open = false;
  34. bool PersistentStore::access_start() {
  35. const char eeprom_erase_value = 0xFF;
  36. MSC_Aquire_Lock();
  37. if (f_mount(&fat_fs, "", 1)) {
  38. MSC_Release_Lock();
  39. return false;
  40. }
  41. FRESULT res = f_open(&eeprom_file, "eeprom.dat", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
  42. if (res) MSC_Release_Lock();
  43. if (res == FR_OK) {
  44. UINT bytes_written;
  45. FSIZE_t file_size = f_size(&eeprom_file);
  46. f_lseek(&eeprom_file, file_size);
  47. while (file_size < capacity() && res == FR_OK) {
  48. res = f_write(&eeprom_file, &eeprom_erase_value, 1, &bytes_written);
  49. file_size++;
  50. }
  51. }
  52. if (res == FR_OK) {
  53. f_lseek(&eeprom_file, 0);
  54. f_sync(&eeprom_file);
  55. eeprom_file_open = true;
  56. }
  57. return res == FR_OK;
  58. }
  59. bool PersistentStore::access_finish() {
  60. f_close(&eeprom_file);
  61. f_unmount("");
  62. MSC_Release_Lock();
  63. eeprom_file_open = false;
  64. return true;
  65. }
  66. // This extra chit-chat goes away soon, but is helpful for now
  67. // to see errors that are happening in read_data / write_data
  68. static void debug_rw(const bool write, int &pos, const uint8_t *value, const size_t size, const FRESULT s, const size_t total=0) {
  69. PGM_P const rw_str = write ? PSTR("write") : PSTR("read");
  70. SERIAL_CHAR(' ');
  71. serialprintPGM(rw_str);
  72. SERIAL_ECHOPAIR("_data(", pos);
  73. SERIAL_ECHOPAIR(",", (int)value);
  74. SERIAL_ECHOPAIR(",", (int)size);
  75. SERIAL_ECHOLNPGM(", ...)");
  76. if (total) {
  77. SERIAL_ECHOPGM(" f_");
  78. serialprintPGM(rw_str);
  79. SERIAL_ECHOPAIR("()=", (int)s);
  80. SERIAL_ECHOPAIR("\n size=", size);
  81. SERIAL_ECHOPGM("\n bytes_");
  82. serialprintPGM(write ? PSTR("written=") : PSTR("read="));
  83. SERIAL_ECHOLN(total);
  84. }
  85. else
  86. SERIAL_ECHOLNPAIR(" f_lseek()=", (int)s);
  87. }
  88. // File function return codes for type FRESULT. This goes away soon, but
  89. // is helpful right now to see any errors in read_data and write_data.
  90. //
  91. // typedef enum {
  92. // FR_OK = 0, /* (0) Succeeded */
  93. // FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
  94. // FR_INT_ERR, /* (2) Assertion failed */
  95. // FR_NOT_READY, /* (3) The physical drive cannot work */
  96. // FR_NO_FILE, /* (4) Could not find the file */
  97. // FR_NO_PATH, /* (5) Could not find the path */
  98. // FR_INVALID_NAME, /* (6) The path name format is invalid */
  99. // FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
  100. // FR_EXIST, /* (8) Access denied due to prohibited access */
  101. // FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
  102. // FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
  103. // FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
  104. // FR_NOT_ENABLED, /* (12) The volume has no work area */
  105. // FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
  106. // FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
  107. // FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
  108. // FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
  109. // FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
  110. // FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
  111. // FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
  112. // } FRESULT;
  113. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  114. if (!eeprom_file_open) return true;
  115. FRESULT s;
  116. UINT bytes_written = 0;
  117. s = f_lseek(&eeprom_file, pos);
  118. if (s) {
  119. debug_rw(true, pos, value, size, s);
  120. return s;
  121. }
  122. s = f_write(&eeprom_file, (void*)value, size, &bytes_written);
  123. if (s) {
  124. debug_rw(true, pos, value, size, s, bytes_written);
  125. return s;
  126. }
  127. crc16(crc, value, size);
  128. pos += size;
  129. return bytes_written != size; // return true for any error
  130. }
  131. bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uint16_t *crc, const bool writing/*=true*/) {
  132. if (!eeprom_file_open) return true;
  133. UINT bytes_read = 0;
  134. FRESULT s;
  135. s = f_lseek(&eeprom_file, pos);
  136. if (s) {
  137. debug_rw(false, pos, value, size, s);
  138. return true;
  139. }
  140. if (writing) {
  141. s = f_read(&eeprom_file, (void*)value, size, &bytes_read);
  142. crc16(crc, value, size);
  143. }
  144. else {
  145. uint8_t temp[size];
  146. s = f_read(&eeprom_file, (void*)temp, size, &bytes_read);
  147. crc16(crc, temp, size);
  148. }
  149. if (s) {
  150. debug_rw(false, pos, value, size, s, bytes_read);
  151. return true;
  152. }
  153. pos += size;
  154. return bytes_read != size; // return true for any error
  155. }
  156. size_t PersistentStore::capacity() { return 4096; } // 4KiB of Emulated EEPROM
  157. #endif // SDCARD_EEPROM_EMULATION
  158. #endif // TARGET_LPC1768