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

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