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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 <https://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. #ifndef MARLIN_EEPROM_SIZE
  35. #define MARLIN_EEPROM_SIZE size_t(0x1000) // 4KiB of Emulated EEPROM
  36. #endif
  37. size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
  38. bool PersistentStore::access_start() {
  39. const char eeprom_erase_value = 0xFF;
  40. MSC_Aquire_Lock();
  41. if (f_mount(&fat_fs, "", 1)) {
  42. MSC_Release_Lock();
  43. return false;
  44. }
  45. FRESULT res = f_open(&eeprom_file, "eeprom.dat", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
  46. if (res) MSC_Release_Lock();
  47. if (res == FR_OK) {
  48. UINT bytes_written;
  49. FSIZE_t file_size = f_size(&eeprom_file);
  50. f_lseek(&eeprom_file, file_size);
  51. while (file_size < capacity() && res == FR_OK) {
  52. res = f_write(&eeprom_file, &eeprom_erase_value, 1, &bytes_written);
  53. file_size++;
  54. }
  55. }
  56. if (res == FR_OK) {
  57. f_lseek(&eeprom_file, 0);
  58. f_sync(&eeprom_file);
  59. eeprom_file_open = true;
  60. }
  61. return res == FR_OK;
  62. }
  63. bool PersistentStore::access_finish() {
  64. f_close(&eeprom_file);
  65. f_unmount("");
  66. MSC_Release_Lock();
  67. eeprom_file_open = false;
  68. return true;
  69. }
  70. // This extra chit-chat goes away soon, but is helpful for now
  71. // to see errors that are happening in read_data / write_data
  72. 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) {
  73. PGM_P const rw_str = write ? PSTR("write") : PSTR("read");
  74. SERIAL_CHAR(' ');
  75. SERIAL_ECHOPGM_P(rw_str);
  76. SERIAL_ECHOLNPGM("_data(", pos, ",", value, ",", size, ", ...)");
  77. if (total) {
  78. SERIAL_ECHOPGM(" f_");
  79. SERIAL_ECHOPGM_P(rw_str);
  80. SERIAL_ECHOPGM("()=", s, "\n size=", size, "\n bytes_");
  81. SERIAL_ECHOLNPGM_P(write ? PSTR("written=") : PSTR("read="), total);
  82. }
  83. else
  84. SERIAL_ECHOLNPGM(" f_lseek()=", s);
  85. }
  86. // File function return codes for type FRESULT. This goes away soon, but
  87. // is helpful right now to see any errors in read_data and write_data.
  88. //
  89. // typedef enum {
  90. // FR_OK = 0, /* (0) Succeeded */
  91. // FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
  92. // FR_INT_ERR, /* (2) Assertion failed */
  93. // FR_NOT_READY, /* (3) The physical drive cannot work */
  94. // FR_NO_FILE, /* (4) Could not find the file */
  95. // FR_NO_PATH, /* (5) Could not find the path */
  96. // FR_INVALID_NAME, /* (6) The path name format is invalid */
  97. // FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
  98. // FR_EXIST, /* (8) Access denied due to prohibited access */
  99. // FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
  100. // FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
  101. // FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
  102. // FR_NOT_ENABLED, /* (12) The volume has no work area */
  103. // FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
  104. // FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
  105. // FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
  106. // FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
  107. // FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
  108. // FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
  109. // FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
  110. // } FRESULT;
  111. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  112. if (!eeprom_file_open) return true;
  113. FRESULT s;
  114. UINT bytes_written = 0;
  115. s = f_lseek(&eeprom_file, pos);
  116. if (s) {
  117. debug_rw(true, pos, value, size, s);
  118. return s;
  119. }
  120. s = f_write(&eeprom_file, (void*)value, size, &bytes_written);
  121. if (s) {
  122. debug_rw(true, pos, value, size, s, bytes_written);
  123. return s;
  124. }
  125. crc16(crc, value, size);
  126. pos += size;
  127. return bytes_written != size; // return true for any error
  128. }
  129. bool PersistentStore::read_data(int &pos, uint8_t *value, const size_t size, uint16_t *crc, const bool writing/*=true*/) {
  130. if (!eeprom_file_open) return true;
  131. UINT bytes_read = 0;
  132. FRESULT s;
  133. s = f_lseek(&eeprom_file, pos);
  134. if (s) {
  135. debug_rw(false, pos, value, size, s);
  136. return true;
  137. }
  138. if (writing) {
  139. s = f_read(&eeprom_file, (void*)value, size, &bytes_read);
  140. crc16(crc, value, size);
  141. }
  142. else {
  143. uint8_t temp[size];
  144. s = f_read(&eeprom_file, (void*)temp, size, &bytes_read);
  145. crc16(crc, temp, size);
  146. }
  147. if (s) {
  148. debug_rw(false, pos, value, size, s, bytes_read);
  149. return true;
  150. }
  151. pos += size;
  152. return bytes_read != size; // return true for any error
  153. }
  154. #endif // SDCARD_EEPROM_EMULATION
  155. #endif // TARGET_LPC1768