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_flash.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /**
  24. * persistent_store_flash.cpp
  25. * HAL for stm32duino and compatible (STM32F1)
  26. * Implementation of EEPROM settings in SDCard
  27. */
  28. #ifdef __STM32F1__
  29. #include "../../inc/MarlinConfig.h"
  30. #if ENABLED(FLASH_EEPROM_EMULATION)
  31. #include "../shared/eeprom_api.h"
  32. #include <flash_stm32.h>
  33. #include <EEPROM.h>
  34. // Store settings in the last two pages
  35. #ifndef MARLIN_EEPROM_SIZE
  36. #define MARLIN_EEPROM_SIZE ((EEPROM_PAGE_SIZE) * 2)
  37. #endif
  38. size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
  39. static uint8_t ram_eeprom[MARLIN_EEPROM_SIZE] __attribute__((aligned(4))) = {0};
  40. static bool eeprom_dirty = false;
  41. bool PersistentStore::access_start() {
  42. const uint32_t* source = reinterpret_cast<const uint32_t*>(EEPROM_PAGE0_BASE);
  43. uint32_t* destination = reinterpret_cast<uint32_t*>(ram_eeprom);
  44. static_assert(0 == (MARLIN_EEPROM_SIZE) % 4, "MARLIN_EEPROM_SIZE is corrupted. (Must be a multiple of 4.)"); // Ensure copying as uint32_t is safe
  45. constexpr size_t eeprom_size_u32 = (MARLIN_EEPROM_SIZE) / 4;
  46. for (size_t i = 0; i < eeprom_size_u32; ++i, ++destination, ++source)
  47. *destination = *source;
  48. eeprom_dirty = false;
  49. return true;
  50. }
  51. bool PersistentStore::access_finish() {
  52. if (eeprom_dirty) {
  53. FLASH_Status status;
  54. // Instead of erasing all (both) pages, maybe in the loop we check what page we are in, and if the
  55. // data has changed in that page. We then erase the first time we "detect" a change. In theory, if
  56. // nothing changed in a page, we wouldn't need to erase/write it.
  57. // Or, instead of checking at this point, turn eeprom_dirty into an array of bool the size of number
  58. // of pages. Inside write_data, we set the flag to true at that time if something in that
  59. // page changes...either way, something to look at later.
  60. FLASH_Unlock();
  61. #define ACCESS_FINISHED(TF) { FLASH_Lock(); eeprom_dirty = false; return TF; }
  62. status = FLASH_ErasePage(EEPROM_PAGE0_BASE);
  63. if (status != FLASH_COMPLETE) ACCESS_FINISHED(true);
  64. status = FLASH_ErasePage(EEPROM_PAGE1_BASE);
  65. if (status != FLASH_COMPLETE) ACCESS_FINISHED(true);
  66. const uint16_t *source = reinterpret_cast<const uint16_t*>(ram_eeprom);
  67. for (size_t i = 0; i < MARLIN_EEPROM_SIZE; i += 2, ++source) {
  68. if (FLASH_ProgramHalfWord(EEPROM_PAGE0_BASE + i, *source) != FLASH_COMPLETE)
  69. ACCESS_FINISHED(false);
  70. }
  71. ACCESS_FINISHED(true);
  72. }
  73. return true;
  74. }
  75. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  76. for (size_t i = 0; i < size; ++i) ram_eeprom[pos + i] = value[i];
  77. eeprom_dirty = true;
  78. crc16(crc, value, size);
  79. pos += size;
  80. return false; // return true for any error
  81. }
  82. bool PersistentStore::read_data(int &pos, uint8_t* value, const size_t size, uint16_t *crc, const bool writing/*=true*/) {
  83. const uint8_t * const buff = writing ? &value[0] : &ram_eeprom[pos];
  84. if (writing) for (size_t i = 0; i < size; i++) value[i] = ram_eeprom[pos + i];
  85. crc16(crc, buff, size);
  86. pos += size;
  87. return false; // return true for any error
  88. }
  89. #endif // FLASH_EEPROM_EMULATION
  90. #endif // __STM32F1__