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 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * HAL for stm32duino.com based on Libmaple and compatible (STM32F1)
  24. * Implementation of EEPROM settings in SD Card
  25. */
  26. #ifdef __STM32F1__
  27. #include "../../inc/MarlinConfig.h"
  28. #if ENABLED(SDCARD_EEPROM_EMULATION)
  29. #include "../shared/eeprom_api.h"
  30. #include "../../sd/cardreader.h"
  31. #ifndef E2END
  32. #define E2END 0xFFF // 4KB
  33. #endif
  34. #define HAL_EEPROM_SIZE (E2END + 1)
  35. #define _ALIGN(x) __attribute__ ((aligned(x))) // SDIO uint32_t* compat.
  36. static char _ALIGN(4) HAL_eeprom_data[HAL_EEPROM_SIZE];
  37. #define EEPROM_FILENAME "eeprom.dat"
  38. bool PersistentStore::access_start() {
  39. if (!card.isMounted()) return false;
  40. SdFile file, root = card.getroot();
  41. if (!file.open(&root, EEPROM_FILENAME, O_RDONLY))
  42. return true; // false aborts the save
  43. int bytes_read = file.read(HAL_eeprom_data, HAL_EEPROM_SIZE);
  44. if (bytes_read < 0) return false;
  45. for (; bytes_read < HAL_EEPROM_SIZE; bytes_read++)
  46. HAL_eeprom_data[bytes_read] = 0xFF;
  47. file.close();
  48. return true;
  49. }
  50. bool PersistentStore::access_finish() {
  51. if (!card.isMounted()) return false;
  52. SdFile file, root = card.getroot();
  53. int bytes_written = 0;
  54. if (file.open(&root, EEPROM_FILENAME, O_CREAT | O_WRITE | O_TRUNC)) {
  55. bytes_written = file.write(HAL_eeprom_data, HAL_EEPROM_SIZE);
  56. file.close();
  57. }
  58. return (bytes_written == HAL_EEPROM_SIZE);
  59. }
  60. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  61. for (size_t i = 0; i < size; i++)
  62. HAL_eeprom_data[pos + i] = value[i];
  63. crc16(crc, value, size);
  64. pos += size;
  65. return false;
  66. }
  67. bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uint16_t *crc, const bool writing/*=true*/) {
  68. for (size_t i = 0; i < size; i++) {
  69. uint8_t c = HAL_eeprom_data[pos + i];
  70. if (writing) value[i] = c;
  71. crc16(crc, &c, 1);
  72. }
  73. pos += size;
  74. return false;
  75. }
  76. size_t PersistentStore::capacity() { return HAL_EEPROM_SIZE; }
  77. #endif // SDCARD_EEPROM_EMULATION
  78. #endif // __STM32F1__