My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

eeprom_flash.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #if defined(STM32GENERIC) && (defined(STM32F4) || defined(STM32F7))
  20. #include "../../inc/MarlinConfig.h"
  21. #if ENABLED(FLASH_EEPROM_EMULATION)
  22. #include "../shared/eeprom_api.h"
  23. #include "eeprom_emul.h"
  24. // FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
  25. // FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F4/7
  26. #ifdef STM32F7
  27. #define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
  28. #else
  29. //#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
  30. #endif
  31. void ee_write_byte(uint8_t *pos, unsigned char value) {
  32. HAL_FLASH_Unlock();
  33. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  34. const unsigned eeprom_address = (unsigned)pos;
  35. if (EE_WriteVariable(eeprom_address, uint16_t(value)) != EE_OK)
  36. for (;;) HAL_Delay(1); // Spin forever until watchdog reset
  37. HAL_FLASH_Lock();
  38. }
  39. uint8_t ee_read_byte(uint8_t *pos) {
  40. uint16_t data = 0xFF;
  41. const unsigned eeprom_address = (unsigned)pos;
  42. (void)EE_ReadVariable(eeprom_address, &data); // Data unchanged on error
  43. return uint8_t(data);
  44. }
  45. #ifndef MARLIN_EEPROM_SIZE
  46. #error "MARLIN_EEPROM_SIZE is required for Flash-based EEPROM."
  47. #endif
  48. size_t PersistentStore::capacity() { return MARLIN_EEPROM_SIZE; }
  49. bool PersistentStore::access_finish() { return true; }
  50. bool PersistentStore::access_start() {
  51. static bool ee_initialized = false;
  52. if (!ee_initialized) {
  53. HAL_FLASH_Unlock();
  54. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  55. /* EEPROM Init */
  56. if (EE_Initialize() != EE_OK)
  57. for (;;) HAL_Delay(1); // Spin forever until watchdog reset
  58. HAL_FLASH_Lock();
  59. ee_initialized = true;
  60. }
  61. return true;
  62. }
  63. bool PersistentStore::write_data(int &pos, const uint8_t *value, size_t size, uint16_t *crc) {
  64. while (size--) {
  65. uint8_t * const p = (uint8_t * const)pos;
  66. uint8_t v = *value;
  67. // EEPROM has only ~100,000 write cycles,
  68. // so only write bytes that have changed!
  69. if (v != ee_read_byte(p)) {
  70. ee_write_byte(p, v);
  71. if (ee_read_byte(p) != v) {
  72. SERIAL_ECHO_MSG(STR_ERR_EEPROM_WRITE);
  73. return true;
  74. }
  75. }
  76. crc16(crc, &v, 1);
  77. pos++;
  78. value++;
  79. };
  80. return false;
  81. }
  82. bool PersistentStore::read_data(int &pos, uint8_t* value, size_t size, uint16_t *crc, const bool writing/*=true*/) {
  83. do {
  84. uint8_t c = ee_read_byte((uint8_t*)pos);
  85. if (writing) *value = c;
  86. crc16(crc, &c, 1);
  87. pos++;
  88. value++;
  89. } while (--size);
  90. return false;
  91. }
  92. #endif // FLASH_EEPROM_EMULATION
  93. #endif // STM32GENERIC && (STM32F4 || STM32F7)