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_impl.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016, 2017 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef TARGET_LPC1768
  23. #include "../../inc/MarlinConfig.h"
  24. #if ENABLED(EEPROM_SETTINGS)
  25. #include "../persistent_store_api.h"
  26. #include "chanfs/diskio.h"
  27. #include "chanfs/ff.h"
  28. extern uint32_t MSC_Aquire_Lock();
  29. extern uint32_t MSC_Release_Lock();
  30. namespace HAL {
  31. namespace PersistentStore {
  32. FATFS fat_fs;
  33. FIL eeprom_file;
  34. bool 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. uint16_t bytes_written, file_size = f_size(&eeprom_file);
  45. f_lseek(&eeprom_file, file_size);
  46. while (file_size <= E2END && res == FR_OK) {
  47. res = f_write(&eeprom_file, &eeprom_erase_value, 1, &bytes_written);
  48. file_size++;
  49. }
  50. }
  51. if (res == FR_OK) {
  52. f_lseek(&eeprom_file, 0);
  53. f_sync(&eeprom_file);
  54. }
  55. return res == FR_OK;
  56. }
  57. bool access_finish() {
  58. f_close(&eeprom_file);
  59. f_unmount("");
  60. MSC_Release_Lock();
  61. return true;
  62. }
  63. // File function return codes for type FRESULT This goes away soon. But it is helpful right now to see
  64. // the different errors the read_data() and write_data() functions are seeing.
  65. //
  66. // typedef enum {
  67. // FR_OK = 0, /* (0) Succeeded */
  68. // FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
  69. // FR_INT_ERR, /* (2) Assertion failed */
  70. // FR_NOT_READY, /* (3) The physical drive cannot work */
  71. // FR_NO_FILE, /* (4) Could not find the file */
  72. // FR_NO_PATH, /* (5) Could not find the path */
  73. // FR_INVALID_NAME, /* (6) The path name format is invalid */
  74. // FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
  75. // FR_EXIST, /* (8) Access denied due to prohibited access */
  76. // FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
  77. // FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
  78. // FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
  79. // FR_NOT_ENABLED, /* (12) The volume has no work area */
  80. // FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
  81. // FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
  82. // FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
  83. // FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
  84. // FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
  85. // FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
  86. // FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
  87. // } FRESULT;
  88. bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
  89. FRESULT s;
  90. uint16_t bytes_written = 0;
  91. s = f_lseek(&eeprom_file, pos);
  92. if (s) {
  93. SERIAL_PROTOCOLPAIR(" write_data(", pos); // This extra chit-chat goes away soon. But it is helpful
  94. SERIAL_PROTOCOLPAIR(",", (int)value); // right now to see errors that are happening in the
  95. SERIAL_PROTOCOLPAIR(",", (int)size); // read_data() and write_data() functions
  96. SERIAL_PROTOCOL("...)\n");
  97. SERIAL_PROTOCOLLNPAIR(" f_lseek()=", (int)s);
  98. return s;
  99. }
  100. s = f_write(&eeprom_file, (void *)value, size, &bytes_written);
  101. if (s) {
  102. SERIAL_PROTOCOLPAIR(" write_data(", pos); // This extra chit-chat goes away soon. But it is helpful
  103. SERIAL_PROTOCOLPAIR(",", (int)value); // right now to see errors that are happening in the
  104. SERIAL_PROTOCOLPAIR(",", size); // read_data() and write_data() functions
  105. SERIAL_PROTOCOLLN("...)");
  106. SERIAL_PROTOCOLLNPAIR(" f_write()=", (int)s);
  107. SERIAL_PROTOCOLPAIR(" size=", size);
  108. SERIAL_PROTOCOLLNPAIR("\n bytes_written=", bytes_written);
  109. return s;
  110. }
  111. crc16(crc, value, size);
  112. pos = pos + size;
  113. return (bytes_written != size); // return true for any error
  114. }
  115. bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc) {
  116. uint16_t bytes_read = 0;
  117. FRESULT s;
  118. s = f_lseek(&eeprom_file, pos);
  119. if ( s ) {
  120. SERIAL_PROTOCOLPAIR(" read_data(", pos); // This extra chit-chat goes away soon. But it is helpful
  121. SERIAL_PROTOCOLPAIR(",", (int)value); // right now to see errors that are happening in the
  122. SERIAL_PROTOCOLPAIR(",", size); // read_data() and write_data() functions
  123. SERIAL_PROTOCOLLN("...)");
  124. SERIAL_PROTOCOLLNPAIR(" f_lseek()=", (int)s);
  125. return true;
  126. }
  127. s = f_read(&eeprom_file, (void *)value, size, &bytes_read);
  128. if (s) {
  129. SERIAL_PROTOCOLPAIR(" read_data(", pos); // This extra chit-chat goes away soon. But it is helpful
  130. SERIAL_PROTOCOLPAIR(",", (int)value); // right now to see errors that are happening in the
  131. SERIAL_PROTOCOLPAIR(",", size); // read_data() and write_data() functions
  132. SERIAL_PROTOCOLLN("...)");
  133. SERIAL_PROTOCOLLNPAIR(" f_write()=", (int)s);
  134. SERIAL_PROTOCOLPAIR(" size=", size);
  135. SERIAL_PROTOCOLLNPAIR("\n bytes_read=", bytes_read);
  136. return true;
  137. }
  138. crc16(crc, value, size);
  139. pos = pos + size;
  140. return bytes_read != size; // return true for any error
  141. }
  142. } // PersistentStore
  143. } // HAL
  144. #endif // EEPROM_SETTINGS
  145. #endif // TARGET_LPC1768