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.

EmulatedEeprom.cpp 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /**
  21. * Description: Functions for a Flash emulated EEPROM
  22. * Not platform dependent.
  23. */
  24. // Include configs and pins to get all EEPROM flags
  25. #include "../../inc/MarlinConfig.h"
  26. #if ENABLED(FLASH_EEPROM_EMULATION)
  27. // ------------------------
  28. // Includes
  29. // ------------------------
  30. #include "HAL.h"
  31. #include "eeprom_emul.h"
  32. // ------------------------
  33. // Local defines
  34. // ------------------------
  35. // FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
  36. // FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F4/7
  37. #ifdef STM32F7
  38. #define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
  39. #else
  40. //#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
  41. #endif
  42. // ------------------------
  43. // Private Variables
  44. // ------------------------
  45. static bool eeprom_initialized = false;
  46. // ------------------------
  47. // Public functions
  48. // ------------------------
  49. void eeprom_init() {
  50. if (!eeprom_initialized) {
  51. HAL_FLASH_Unlock();
  52. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  53. /* EEPROM Init */
  54. if (EE_Initialize() != EE_OK)
  55. for (;;) HAL_Delay(1); // Spin forever until watchdog reset
  56. HAL_FLASH_Lock();
  57. eeprom_initialized = true;
  58. }
  59. }
  60. void eeprom_write_byte(uint8_t *pos, unsigned char value) {
  61. eeprom_init();
  62. HAL_FLASH_Unlock();
  63. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  64. uint16_t eeprom_address = unsigned(pos);
  65. if (EE_WriteVariable(eeprom_address, uint16_t(value)) != EE_OK)
  66. for (;;) HAL_Delay(1); // Spin forever until watchdog reset
  67. HAL_FLASH_Lock();
  68. }
  69. uint8_t eeprom_read_byte(uint8_t *pos) {
  70. eeprom_init();
  71. uint16_t data = 0xFF;
  72. uint16_t eeprom_address = unsigned(pos);
  73. (void)EE_ReadVariable(eeprom_address, &data); // Data unchanged on error
  74. return uint8_t(data);
  75. }
  76. void eeprom_read_block(void *__dst, const void *__src, size_t __n) {
  77. eeprom_init();
  78. uint16_t data = 0xFF;
  79. uint16_t eeprom_address = unsigned(__src);
  80. for (uint8_t c = 0; c < __n; c++) {
  81. EE_ReadVariable(eeprom_address+c, &data);
  82. *((uint8_t*)__dst + c) = data;
  83. }
  84. }
  85. void eeprom_update_block(const void *__src, void *__dst, size_t __n) {
  86. }
  87. #endif // FLASH_EEPROM_EMULATION
  88. #endif // STM32GENERIC && (STM32F4 || STM32F7)