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

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