My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

persistent_store_impl.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. UINT bytes_written;
  45. FSIZE_t file_size = f_size(&eeprom_file);
  46. f_lseek(&eeprom_file, file_size);
  47. while (file_size <= E2END && 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. }
  56. return res == FR_OK;
  57. }
  58. bool access_finish() {
  59. f_close(&eeprom_file);
  60. f_unmount("");
  61. MSC_Release_Lock();
  62. return true;
  63. }
  64. // File function return codes for type FRESULT This goes away soon. But it is helpful right now to see
  65. // the different errors the read_data() and write_data() functions are seeing.
  66. //
  67. // typedef enum {
  68. // FR_OK = 0, /* (0) Succeeded */
  69. // FR_DISK_ERR, /* (1) A hard error occurred in the low level disk I/O layer */
  70. // FR_INT_ERR, /* (2) Assertion failed */
  71. // FR_NOT_READY, /* (3) The physical drive cannot work */
  72. // FR_NO_FILE, /* (4) Could not find the file */
  73. // FR_NO_PATH, /* (5) Could not find the path */
  74. // FR_INVALID_NAME, /* (6) The path name format is invalid */
  75. // FR_DENIED, /* (7) Access denied due to prohibited access or directory full */
  76. // FR_EXIST, /* (8) Access denied due to prohibited access */
  77. // FR_INVALID_OBJECT, /* (9) The file/directory object is invalid */
  78. // FR_WRITE_PROTECTED, /* (10) The physical drive is write protected */
  79. // FR_INVALID_DRIVE, /* (11) The logical drive number is invalid */
  80. // FR_NOT_ENABLED, /* (12) The volume has no work area */
  81. // FR_NO_FILESYSTEM, /* (13) There is no valid FAT volume */
  82. // FR_MKFS_ABORTED, /* (14) The f_mkfs() aborted due to any problem */
  83. // FR_TIMEOUT, /* (15) Could not get a grant to access the volume within defined period */
  84. // FR_LOCKED, /* (16) The operation is rejected according to the file sharing policy */
  85. // FR_NOT_ENOUGH_CORE, /* (17) LFN working buffer could not be allocated */
  86. // FR_TOO_MANY_OPEN_FILES, /* (18) Number of open files > FF_FS_LOCK */
  87. // FR_INVALID_PARAMETER /* (19) Given parameter is invalid */
  88. // } FRESULT;
  89. bool write_data(int &pos, const uint8_t *value, uint16_t size, uint16_t *crc) {
  90. FRESULT s;
  91. UINT bytes_written = 0;
  92. s = f_lseek(&eeprom_file, pos);
  93. if (s) {
  94. SERIAL_PROTOCOLPAIR(" write_data(", pos); // This extra chit-chat goes away soon. But it is helpful
  95. SERIAL_PROTOCOLPAIR(",", (int)value); // right now to see errors that are happening in the
  96. SERIAL_PROTOCOLPAIR(",", (int)size); // read_data() and write_data() functions
  97. SERIAL_PROTOCOL("...)\n");
  98. SERIAL_PROTOCOLLNPAIR(" f_lseek()=", (int)s);
  99. return s;
  100. }
  101. s = f_write(&eeprom_file, (void *)value, size, &bytes_written);
  102. if (s) {
  103. SERIAL_PROTOCOLPAIR(" write_data(", pos); // This extra chit-chat goes away soon. But it is helpful
  104. SERIAL_PROTOCOLPAIR(",", (int)value); // right now to see errors that are happening in the
  105. SERIAL_PROTOCOLPAIR(",", size); // read_data() and write_data() functions
  106. SERIAL_PROTOCOLLN("...)");
  107. SERIAL_PROTOCOLLNPAIR(" f_write()=", (int)s);
  108. SERIAL_PROTOCOLPAIR(" size=", size);
  109. SERIAL_PROTOCOLLNPAIR("\n bytes_written=", bytes_written);
  110. return s;
  111. }
  112. crc16(crc, value, size);
  113. pos = pos + size;
  114. return (bytes_written != size); // return true for any error
  115. }
  116. bool read_data(int &pos, uint8_t* value, uint16_t size, uint16_t *crc) {
  117. UINT bytes_read = 0;
  118. FRESULT s;
  119. s = f_lseek(&eeprom_file, pos);
  120. if ( s ) {
  121. SERIAL_PROTOCOLPAIR(" read_data(", pos); // This extra chit-chat goes away soon. But it is helpful
  122. SERIAL_PROTOCOLPAIR(",", (int)value); // right now to see errors that are happening in the
  123. SERIAL_PROTOCOLPAIR(",", size); // read_data() and write_data() functions
  124. SERIAL_PROTOCOLLN("...)");
  125. SERIAL_PROTOCOLLNPAIR(" f_lseek()=", (int)s);
  126. return true;
  127. }
  128. s = f_read(&eeprom_file, (void *)value, size, &bytes_read);
  129. if (s) {
  130. SERIAL_PROTOCOLPAIR(" read_data(", pos); // This extra chit-chat goes away soon. But it is helpful
  131. SERIAL_PROTOCOLPAIR(",", (int)value); // right now to see errors that are happening in the
  132. SERIAL_PROTOCOLPAIR(",", size); // read_data() and write_data() functions
  133. SERIAL_PROTOCOLLN("...)");
  134. SERIAL_PROTOCOLLNPAIR(" f_write()=", (int)s);
  135. SERIAL_PROTOCOLPAIR(" size=", size);
  136. SERIAL_PROTOCOLLNPAIR("\n bytes_read=", bytes_read);
  137. return true;
  138. }
  139. crc16(crc, value, size);
  140. pos = pos + size;
  141. return bytes_read != size; // return true for any error
  142. }
  143. } // PersistentStore
  144. } // HAL
  145. #endif // EEPROM_SETTINGS
  146. #endif // TARGET_LPC1768