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.6KB

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