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.

eeprom_sdcard.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "../shared/eeprom_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_ECHOLNPAIR("_data(", pos, ",", int(value), ",", int(size), ", ...)");
  73. if (total) {
  74. SERIAL_ECHOPGM(" f_");
  75. serialprintPGM(rw_str);
  76. SERIAL_ECHOPAIR("()=", int(s), "\n size=", int(size), "\n bytes_");
  77. serialprintPGM(write ? PSTR("written=") : PSTR("read="));
  78. SERIAL_ECHOLN(total);
  79. }
  80. else
  81. SERIAL_ECHOLNPAIR(" f_lseek()=", int(s));
  82. }
  83. // File function return codes for type FRESULT. This goes away soon, but
  84. // is helpful right now to see any errors in read_data and write_data.
  85. //
  86. // typedef enum {
  87. // FR_OK = 0, /* (0) Succeeded */
  88. // FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
  89. // FR_INT_ERR, /* (2) Assertion failed */
  90. // FR_NOT_READY, /* (3) The physical drive cannot work */
  91. // FR_NO_FILE, /* (4) Could not find the file */
  92. // FR_NO_PATH, /* (5) Could not find the path */
  93. // FR_INVALID_NAME, /* (6) The path name format is invalid */
  94. // FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
  95. // FR_EXIST, /* (8) Access denied due to prohibited access */
  96. // FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
  97. // FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
  98. // FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
  99. // FR_NOT_ENABLED, /* (12) The volume has no work area */
  100. // FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
  101. // FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
  102. // FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
  103. // FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
  104. // FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
  105. // FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
  106. // FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
  107. // } FRESULT;
  108. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  109. if (!eeprom_file_open) return true;
  110. FRESULT s;
  111. UINT bytes_written = 0;
  112. s = f_lseek(&eeprom_file, pos);
  113. if (s) {
  114. debug_rw(true, pos, value, size, s);
  115. return s;
  116. }
  117. s = f_write(&eeprom_file, (void*)value, size, &bytes_written);
  118. if (s) {
  119. debug_rw(true, pos, value, size, s, bytes_written);
  120. return s;
  121. }
  122. crc16(crc, value, size);
  123. pos += size;
  124. return bytes_written != size; // return true for any error
  125. }
  126. bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uint16_t *crc, const bool writing/*=true*/) {
  127. if (!eeprom_file_open) return true;
  128. UINT bytes_read = 0;
  129. FRESULT s;
  130. s = f_lseek(&eeprom_file, pos);
  131. if (s) {
  132. debug_rw(false, pos, value, size, s);
  133. return true;
  134. }
  135. if (writing) {
  136. s = f_read(&eeprom_file, (void*)value, size, &bytes_read);
  137. crc16(crc, value, size);
  138. }
  139. else {
  140. uint8_t temp[size];
  141. s = f_read(&eeprom_file, (void*)temp, size, &bytes_read);
  142. crc16(crc, temp, size);
  143. }
  144. if (s) {
  145. debug_rw(false, pos, value, size, s, bytes_read);
  146. return true;
  147. }
  148. pos += size;
  149. return bytes_read != size; // return true for any error
  150. }
  151. size_t PersistentStore::capacity() { return 4096; } // 4KiB of Emulated EEPROM
  152. #endif // SDCARD_EEPROM_EMULATION
  153. #endif // TARGET_LPC1768