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.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 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 <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. #ifdef __PLAT_LINUX__
  23. #include "../../inc/MarlinConfig.h"
  24. #if ENABLED(EEPROM_SETTINGS)
  25. #include "../shared/eeprom_api.h"
  26. #include <stdio.h>
  27. #ifndef MARLIN_EEPROM_SIZE
  28. #define MARLIN_EEPROM_SIZE 0x1000 // 4KB of Emulated EEPROM
  29. #endif
  30. uint8_t buffer[MARLIN_EEPROM_SIZE];
  31. char filename[] = "eeprom.dat";
  32. size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
  33. bool PersistentStore::access_start() {
  34. const char eeprom_erase_value = 0xFF;
  35. FILE * eeprom_file = fopen(filename, "rb");
  36. if (!eeprom_file) return false;
  37. fseek(eeprom_file, 0L, SEEK_END);
  38. std::size_t file_size = ftell(eeprom_file);
  39. if (file_size < MARLIN_EEPROM_SIZE) {
  40. memset(buffer + file_size, eeprom_erase_value, MARLIN_EEPROM_SIZE - file_size);
  41. }
  42. else {
  43. fseek(eeprom_file, 0L, SEEK_SET);
  44. fread(buffer, sizeof(uint8_t), sizeof(buffer), eeprom_file);
  45. }
  46. fclose(eeprom_file);
  47. return true;
  48. }
  49. bool PersistentStore::access_finish() {
  50. FILE * eeprom_file = fopen(filename, "wb");
  51. if (!eeprom_file) return false;
  52. fwrite(buffer, sizeof(uint8_t), sizeof(buffer), eeprom_file);
  53. fclose(eeprom_file);
  54. return true;
  55. }
  56. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  57. std::size_t bytes_written = 0;
  58. for (std::size_t i = 0; i < size; i++) {
  59. buffer[pos + i] = value[i];
  60. bytes_written++;
  61. }
  62. crc16(crc, value, size);
  63. pos += size;
  64. return (bytes_written != size); // return true for any error
  65. }
  66. bool PersistentStore::read_data(int &pos, uint8_t *value, const size_t size, uint16_t *crc, const bool writing/*=true*/) {
  67. std::size_t bytes_read = 0;
  68. if (writing) {
  69. for (std::size_t i = 0; i < size; i++) {
  70. value[i] = buffer[pos + i];
  71. bytes_read++;
  72. }
  73. crc16(crc, value, size);
  74. }
  75. else {
  76. uint8_t temp[size];
  77. for (std::size_t i = 0; i < size; i++) {
  78. temp[i] = buffer[pos + i];
  79. bytes_read++;
  80. }
  81. crc16(crc, temp, size);
  82. }
  83. pos += size;
  84. return bytes_read != size; // return true for any error
  85. }
  86. #endif // EEPROM_SETTINGS
  87. #endif // __PLAT_LINUX__