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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. #include "../platforms.h"
  24. #ifdef HAL_STM32
  25. #include "../../inc/MarlinConfig.h"
  26. #if ENABLED(FLASH_EEPROM_EMULATION)
  27. #include "../shared/eeprom_api.h"
  28. // Better: "utility/stm32_eeprom.h", but only after updating stm32duino to 2.0.0
  29. // Use EEPROM.h for compatibility, for now.
  30. #include <EEPROM.h>
  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 "../../core/debug_out.h"
  49. #ifndef MARLIN_EEPROM_SIZE
  50. #define MARLIN_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. #ifndef FLASH_ADDRESS_START
  59. #define FLASH_ADDRESS_START (FLASH_END - ((FLASH_SECTOR_TOTAL - (FLASH_SECTOR)) * (FLASH_UNIT_SIZE)) + 1)
  60. #endif
  61. #define FLASH_ADDRESS_END (FLASH_ADDRESS_START + FLASH_UNIT_SIZE - 1)
  62. #define EEPROM_SLOTS ((FLASH_UNIT_SIZE) / (MARLIN_EEPROM_SIZE))
  63. #define SLOT_ADDRESS(slot) (FLASH_ADDRESS_START + (slot * (MARLIN_EEPROM_SIZE)))
  64. #define UNLOCK_FLASH() if (!flash_unlocked) { \
  65. HAL_FLASH_Unlock(); \
  66. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | \
  67. FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR); \
  68. flash_unlocked = true; \
  69. }
  70. #define LOCK_FLASH() if (flash_unlocked) { HAL_FLASH_Lock(); flash_unlocked = false; }
  71. #define EMPTY_UINT32 ((uint32_t)-1)
  72. #define EMPTY_UINT8 ((uint8_t)-1)
  73. static uint8_t ram_eeprom[MARLIN_EEPROM_SIZE] __attribute__((aligned(4))) = {0};
  74. static int current_slot = -1;
  75. static_assert(0 == MARLIN_EEPROM_SIZE % 4, "MARLIN_EEPROM_SIZE must be a multiple of 4"); // Ensure copying as uint32_t is safe
  76. static_assert(0 == FLASH_UNIT_SIZE % MARLIN_EEPROM_SIZE, "MARLIN_EEPROM_SIZE must divide evenly into your FLASH_UNIT_SIZE");
  77. static_assert(FLASH_UNIT_SIZE >= MARLIN_EEPROM_SIZE, "FLASH_UNIT_SIZE must be greater than or equal to your MARLIN_EEPROM_SIZE");
  78. static_assert(IS_FLASH_SECTOR(FLASH_SECTOR), "FLASH_SECTOR is invalid");
  79. 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");
  80. #endif // FLASH_EEPROM_LEVELING
  81. static bool eeprom_data_written = false;
  82. #ifndef MARLIN_EEPROM_SIZE
  83. #define MARLIN_EEPROM_SIZE size_t(E2END + 1)
  84. #endif
  85. size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
  86. bool PersistentStore::access_start() {
  87. EEPROM.begin(); // Avoid STM32 EEPROM.h warning (do nothing)
  88. #if ENABLED(FLASH_EEPROM_LEVELING)
  89. if (current_slot == -1 || eeprom_data_written) {
  90. // This must be the first time since power on that we have accessed the storage, or someone
  91. // loaded and called write_data and never called access_finish.
  92. // Lets go looking for the slot that holds our configuration.
  93. if (eeprom_data_written) DEBUG_ECHOLNPGM("Dangling EEPROM write_data");
  94. uint32_t address = FLASH_ADDRESS_START;
  95. while (address <= FLASH_ADDRESS_END) {
  96. uint32_t address_value = (*(__IO uint32_t*)address);
  97. if (address_value != EMPTY_UINT32) {
  98. current_slot = (address - (FLASH_ADDRESS_START)) / (MARLIN_EEPROM_SIZE);
  99. break;
  100. }
  101. address += sizeof(uint32_t);
  102. }
  103. if (current_slot == -1) {
  104. // We didn't find anything, so we'll just initialize to empty
  105. for (int i = 0; i < MARLIN_EEPROM_SIZE; i++) ram_eeprom[i] = EMPTY_UINT8;
  106. current_slot = EEPROM_SLOTS;
  107. }
  108. else {
  109. // load current settings
  110. uint8_t *eeprom_data = (uint8_t *)SLOT_ADDRESS(current_slot);
  111. for (int i = 0; i < MARLIN_EEPROM_SIZE; i++) ram_eeprom[i] = eeprom_data[i];
  112. DEBUG_ECHOLNPGM("EEPROM loaded from slot ", current_slot, ".");
  113. }
  114. eeprom_data_written = false;
  115. }
  116. #else
  117. eeprom_buffer_fill();
  118. #endif
  119. return true;
  120. }
  121. bool PersistentStore::access_finish() {
  122. if (eeprom_data_written) {
  123. #ifdef STM32F4xx
  124. // MCU may come up with flash error bits which prevent some flash operations.
  125. // Clear flags prior to flash operations to prevent errors.
  126. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  127. #endif
  128. #if ENABLED(FLASH_EEPROM_LEVELING)
  129. HAL_StatusTypeDef status = HAL_ERROR;
  130. bool flash_unlocked = false;
  131. if (--current_slot < 0) {
  132. // all slots have been used, erase everything and start again
  133. FLASH_EraseInitTypeDef EraseInitStruct;
  134. uint32_t SectorError = 0;
  135. EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
  136. EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
  137. EraseInitStruct.Sector = FLASH_SECTOR;
  138. EraseInitStruct.NbSectors = 1;
  139. current_slot = EEPROM_SLOTS - 1;
  140. UNLOCK_FLASH();
  141. TERN_(HAS_PAUSE_SERVO_OUTPUT, PAUSE_SERVO_OUTPUT());
  142. hal.isr_off();
  143. status = HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);
  144. hal.isr_on();
  145. TERN_(HAS_PAUSE_SERVO_OUTPUT, RESUME_SERVO_OUTPUT());
  146. if (status != HAL_OK) {
  147. DEBUG_ECHOLNPGM("HAL_FLASHEx_Erase=", status);
  148. DEBUG_ECHOLNPGM("GetError=", HAL_FLASH_GetError());
  149. DEBUG_ECHOLNPGM("SectorError=", SectorError);
  150. LOCK_FLASH();
  151. return false;
  152. }
  153. }
  154. UNLOCK_FLASH();
  155. uint32_t offset = 0,
  156. address = SLOT_ADDRESS(current_slot),
  157. address_end = address + MARLIN_EEPROM_SIZE,
  158. data = 0;
  159. bool success = true;
  160. while (address < address_end) {
  161. memcpy(&data, ram_eeprom + offset, sizeof(data));
  162. status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data);
  163. if (status == HAL_OK) {
  164. address += sizeof(uint32_t);
  165. offset += sizeof(uint32_t);
  166. }
  167. else {
  168. DEBUG_ECHOLNPGM("HAL_FLASH_Program=", status);
  169. DEBUG_ECHOLNPGM("GetError=", HAL_FLASH_GetError());
  170. DEBUG_ECHOLNPGM("address=", address);
  171. success = false;
  172. break;
  173. }
  174. }
  175. LOCK_FLASH();
  176. if (success) {
  177. eeprom_data_written = false;
  178. DEBUG_ECHOLNPGM("EEPROM saved to slot ", current_slot, ".");
  179. }
  180. return success;
  181. #else // !FLASH_EEPROM_LEVELING
  182. // The following was written for the STM32F4 but may work with other MCUs as well.
  183. // Most STM32F4 flash does not allow reading from flash during erase operations.
  184. // This takes about a second on a STM32F407 with a 128kB sector used as EEPROM.
  185. // Interrupts during this time can have unpredictable results, such as killing Servo
  186. // output. Servo output still glitches with interrupts disabled, but recovers after the
  187. // erase.
  188. TERN_(HAS_PAUSE_SERVO_OUTPUT, PAUSE_SERVO_OUTPUT());
  189. hal.isr_off();
  190. eeprom_buffer_flush();
  191. hal.isr_on();
  192. TERN_(HAS_PAUSE_SERVO_OUTPUT, RESUME_SERVO_OUTPUT());
  193. eeprom_data_written = false;
  194. #endif // !FLASH_EEPROM_LEVELING
  195. }
  196. return true;
  197. }
  198. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  199. while (size--) {
  200. uint8_t v = *value;
  201. #if ENABLED(FLASH_EEPROM_LEVELING)
  202. if (v != ram_eeprom[pos]) {
  203. ram_eeprom[pos] = v;
  204. eeprom_data_written = true;
  205. }
  206. #else
  207. if (v != eeprom_buffered_read_byte(pos)) {
  208. eeprom_buffered_write_byte(pos, v);
  209. eeprom_data_written = true;
  210. }
  211. #endif
  212. crc16(crc, &v, 1);
  213. pos++;
  214. value++;
  215. }
  216. return false;
  217. }
  218. bool PersistentStore::read_data(int &pos, uint8_t *value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
  219. do {
  220. const uint8_t c = TERN(FLASH_EEPROM_LEVELING, ram_eeprom[pos], eeprom_buffered_read_byte(pos));
  221. if (writing) *value = c;
  222. crc16(crc, &c, 1);
  223. pos++;
  224. value++;
  225. } while (--size);
  226. return false;
  227. }
  228. #endif // FLASH_EEPROM_EMULATION
  229. #endif // HAL_STM32