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.

eeprom_flash.cpp 9.4KB

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