My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

persistent_store_flash.cpp 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #if defined(ARDUINO_ARCH_STM32) && !defined(STM32GENERIC)
  24. #include "../../inc/MarlinConfig.h"
  25. #if BOTH(EEPROM_SETTINGS, FLASH_EEPROM_EMULATION)
  26. #include "../shared/persistent_store_api.h"
  27. // Only STM32F4 can support wear leveling at this time
  28. #ifndef STM32F4xx
  29. #undef FLASH_EEPROM_LEVELING
  30. #endif
  31. /**
  32. * The STM32 HAL supports chips that deal with "pages" and some with "sectors" and some that
  33. * even have multiple "banks" of flash.
  34. *
  35. * This code is a bit of a mashup of
  36. * framework-arduinoststm32/cores/arduino/stm32/stm32_eeprom.c
  37. * hal/hal_lpc1768/persistent_store_flash.cpp
  38. *
  39. * This has only be written against those that use a single "sector" design.
  40. *
  41. * Those that deal with "pages" could be made to work. Looking at the STM32F07 for example, there are
  42. * 128 "pages", each 2kB in size. If we continued with our EEPROM being 4Kb, we'd always need to operate
  43. * on 2 of these pages. Each write, we'd use 2 different pages from a pool of pages until we are done.
  44. */
  45. #if ENABLED(FLASH_EEPROM_LEVELING)
  46. #include "stm32_def.h"
  47. #define DEBUG_OUT ENABLED(EEPROM_CHITCHAT)
  48. #include "src/core/debug_out.h"
  49. #ifndef EEPROM_SIZE
  50. #define EEPROM_SIZE 0x1000 // 4kB
  51. #endif
  52. #ifndef FLASH_SECTOR
  53. #define FLASH_SECTOR (FLASH_SECTOR_TOTAL - 1)
  54. #endif
  55. #ifndef FLASH_UNIT_SIZE
  56. #define FLASH_UNIT_SIZE 0x20000 // 128kB
  57. #endif
  58. #define FLASH_ADDRESS_START (FLASH_END - ((FLASH_SECTOR_TOTAL - FLASH_SECTOR) * FLASH_UNIT_SIZE) + 1)
  59. #define FLASH_ADDRESS_END (FLASH_ADDRESS_START + FLASH_UNIT_SIZE - 1)
  60. #define EEPROM_SLOTS (FLASH_UNIT_SIZE/EEPROM_SIZE)
  61. #define SLOT_ADDRESS(slot) (FLASH_ADDRESS_START + (slot * EEPROM_SIZE))
  62. #define UNLOCK_FLASH() if (!flash_unlocked) { \
  63. HAL_FLASH_Unlock(); \
  64. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | \
  65. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); \
  66. flash_unlocked = true; \
  67. }
  68. #define LOCK_FLASH() if (flash_unlocked) { HAL_FLASH_Lock(); flash_unlocked = false; }
  69. #define EMPTY_UINT32 ((uint32_t)-1)
  70. #define EMPTY_UINT8 ((uint8_t)-1)
  71. static uint8_t ram_eeprom[EEPROM_SIZE] __attribute__((aligned(4))) = {0};
  72. static int current_slot = -1;
  73. static_assert(0 == EEPROM_SIZE % 4, "EEPROM_SIZE must be a multiple of 4"); // Ensure copying as uint32_t is safe
  74. static_assert(0 == FLASH_UNIT_SIZE % EEPROM_SIZE, "EEPROM_SIZE must divide evenly into your FLASH_UNIT_SIZE");
  75. static_assert(FLASH_UNIT_SIZE >= EEPROM_SIZE, "FLASH_UNIT_SIZE must be greater than or equal to your EEPROM_SIZE");
  76. static_assert(IS_FLASH_SECTOR(FLASH_SECTOR), "FLASH_SECTOR is invalid");
  77. static_assert(IS_POWER_OF_2(FLASH_UNIT_SIZE), "FLASH_UNIT_SIZE should be a power of 2, please check your chip's spec sheet");
  78. #endif
  79. static bool eeprom_data_written = false;
  80. bool PersistentStore::access_start() {
  81. #if ENABLED(FLASH_EEPROM_LEVELING)
  82. if (current_slot == -1 || eeprom_data_written) {
  83. // This must be the first time since power on that we have accessed the storage, or someone
  84. // loaded and called write_data and never called access_finish.
  85. // Lets go looking for the slot that holds our configuration.
  86. if (eeprom_data_written) DEBUG_ECHOLN("Dangling EEPROM write_data");
  87. uint32_t address = FLASH_ADDRESS_START;
  88. while (address <= FLASH_ADDRESS_END) {
  89. uint32_t address_value = (*(__IO uint32_t*)address);
  90. if (address_value != EMPTY_UINT32) {
  91. current_slot = (address - FLASH_ADDRESS_START) / EEPROM_SIZE;
  92. break;
  93. }
  94. address += sizeof(uint32_t);
  95. }
  96. if (current_slot == -1) {
  97. // We didn't find anything, so we'll just intialize to empty
  98. for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = EMPTY_UINT8;
  99. current_slot = EEPROM_SLOTS;
  100. }
  101. else {
  102. // load current settings
  103. uint8_t *eeprom_data = (uint8_t *)SLOT_ADDRESS(current_slot);
  104. for (int i = 0; i < EEPROM_SIZE; i++) ram_eeprom[i] = eeprom_data[i];
  105. DEBUG_ECHOLNPAIR("EEPROM loaded from slot ", current_slot, ".");
  106. }
  107. eeprom_data_written = false;
  108. }
  109. #else
  110. eeprom_buffer_fill();
  111. #endif
  112. return true;
  113. }
  114. bool PersistentStore::access_finish() {
  115. if (eeprom_data_written) {
  116. #if ENABLED(FLASH_EEPROM_LEVELING)
  117. HAL_StatusTypeDef status = HAL_ERROR;
  118. bool flash_unlocked = false;
  119. if (--current_slot < 0) {
  120. // all slots have been used, erase everything and start again
  121. FLASH_EraseInitTypeDef EraseInitStruct;
  122. uint32_t SectorError = 0;
  123. EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  124. EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  125. EraseInitStruct.Sector = FLASH_SECTOR;
  126. EraseInitStruct.NbSectors = 1;
  127. current_slot = EEPROM_SLOTS - 1;
  128. UNLOCK_FLASH();
  129. status = HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);
  130. if (status != HAL_OK) {
  131. DEBUG_ECHOLNPAIR("HAL_FLASHEx_Erase=", status);
  132. DEBUG_ECHOLNPAIR("GetError=", HAL_FLASH_GetError());
  133. DEBUG_ECHOLNPAIR("SectorError=", SectorError);
  134. LOCK_FLASH();
  135. return false;
  136. }
  137. }
  138. UNLOCK_FLASH();
  139. uint32_t offset = 0;
  140. uint32_t address = SLOT_ADDRESS(current_slot);
  141. uint32_t address_end = address + EEPROM_SIZE;
  142. uint32_t data = 0;
  143. bool success = true;
  144. while (address < address_end) {
  145. memcpy(&data, ram_eeprom + offset, sizeof(uint32_t));
  146. status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data);
  147. if (status == HAL_OK) {
  148. address += sizeof(uint32_t);
  149. offset += sizeof(uint32_t);
  150. }
  151. else {
  152. DEBUG_ECHOLNPAIR("HAL_FLASH_Program=", status);
  153. DEBUG_ECHOLNPAIR("GetError=", HAL_FLASH_GetError());
  154. DEBUG_ECHOLNPAIR("address=", address);
  155. success = false;
  156. break;
  157. }
  158. }
  159. LOCK_FLASH();
  160. if (success) {
  161. eeprom_data_written = false;
  162. DEBUG_ECHOLNPAIR("EEPROM saved to slot ", current_slot, ".");
  163. }
  164. return success;
  165. #else
  166. eeprom_buffer_flush();
  167. eeprom_data_written = false;
  168. #endif
  169. }
  170. return true;
  171. }
  172. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  173. while (size--) {
  174. uint8_t v = *value;
  175. #if ENABLED(FLASH_EEPROM_LEVELING)
  176. if (v != ram_eeprom[pos]) {
  177. ram_eeprom[pos] = v;
  178. eeprom_data_written = true;
  179. }
  180. #else
  181. if (v != eeprom_buffered_read_byte(pos)) {
  182. eeprom_buffered_write_byte(pos, v);
  183. eeprom_data_written = true;
  184. }
  185. #endif
  186. crc16(crc, &v, 1);
  187. pos++;
  188. value++;
  189. }
  190. return false;
  191. }
  192. bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
  193. do {
  194. const uint8_t c = (
  195. #if ENABLED(FLASH_EEPROM_LEVELING)
  196. ram_eeprom[pos]
  197. #else
  198. eeprom_buffered_read_byte(pos)
  199. #endif
  200. );
  201. if (writing) *value = c;
  202. crc16(crc, &c, 1);
  203. pos++;
  204. value++;
  205. } while (--size);
  206. return false;
  207. }
  208. size_t PersistentStore::capacity() {
  209. return (
  210. #if ENABLED(FLASH_EEPROM_LEVELING)
  211. EEPROM_SIZE
  212. #else
  213. E2END + 1
  214. #endif
  215. );
  216. }
  217. #endif // EEPROM_SETTINGS && FLASH_EEPROM_EMULATION
  218. #endif // ARDUINO_ARCH_STM32 && !STM32GENERIC