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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2019 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))
  20. /**
  21. * Description: functions for I2C connected external EEPROM.
  22. * Not platform dependent.
  23. */
  24. #include "../../inc/MarlinConfig.h"
  25. #if ENABLED(EEPROM_SETTINGS) && DISABLED(I2C_EEPROM, SPI_EEPROM)
  26. // --------------------------------------------------------------------------
  27. // Includes
  28. // --------------------------------------------------------------------------
  29. #include "HAL.h"
  30. #include "EEPROM_Emul/eeprom_emul.h"
  31. // --------------------------------------------------------------------------
  32. // Externals
  33. // --------------------------------------------------------------------------
  34. // --------------------------------------------------------------------------
  35. // Local defines
  36. // --------------------------------------------------------------------------
  37. // --------------------------------------------------------------------------
  38. // Types
  39. // --------------------------------------------------------------------------
  40. // --------------------------------------------------------------------------
  41. // Variables
  42. // --------------------------------------------------------------------------
  43. // --------------------------------------------------------------------------
  44. // Public Variables
  45. // --------------------------------------------------------------------------
  46. // --------------------------------------------------------------------------
  47. // Private Variables
  48. // --------------------------------------------------------------------------
  49. static bool eeprom_initialized = false;
  50. // --------------------------------------------------------------------------
  51. // Function prototypes
  52. // --------------------------------------------------------------------------
  53. // --------------------------------------------------------------------------
  54. // Private functions
  55. // --------------------------------------------------------------------------
  56. // --------------------------------------------------------------------------
  57. // Public functions
  58. // --------------------------------------------------------------------------
  59. // FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
  60. // FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F4
  61. // #define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
  62. // --------------------------------------------------------------------------
  63. // EEPROM
  64. // --------------------------------------------------------------------------
  65. void eeprom_init() {
  66. if (!eeprom_initialized) {
  67. HAL_FLASH_Unlock();
  68. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  69. /* EEPROM Init */
  70. if (EE_Initialize() != EE_OK)
  71. for (;;) HAL_Delay(1); // Spin forever until watchdog reset
  72. HAL_FLASH_Lock();
  73. eeprom_initialized = true;
  74. }
  75. }
  76. void eeprom_write_byte(uint8_t *pos, unsigned char value) {
  77. uint16_t eeprom_address = (unsigned) pos;
  78. eeprom_init();
  79. HAL_FLASH_Unlock();
  80. __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
  81. if (EE_WriteVariable(eeprom_address, (uint16_t) value) != EE_OK)
  82. for (;;) HAL_Delay(1); // Spin forever until watchdog reset
  83. HAL_FLASH_Lock();
  84. }
  85. uint8_t eeprom_read_byte(uint8_t *pos) {
  86. uint16_t data = 0xFF;
  87. uint16_t eeprom_address = (unsigned)pos;
  88. eeprom_init();
  89. if (EE_ReadVariable(eeprom_address, &data) != EE_OK) {
  90. return (unsigned char)data;
  91. }
  92. return (unsigned char)data;
  93. }
  94. void eeprom_read_block(void *__dst, const void *__src, size_t __n) {
  95. uint16_t data = 0xFF;
  96. uint16_t eeprom_address = (unsigned) __src;
  97. eeprom_init();
  98. for (uint8_t c = 0; c < __n; c++) {
  99. EE_ReadVariable(eeprom_address+c, &data);
  100. *((uint8_t*)__dst + c) = data;
  101. }
  102. }
  103. void eeprom_update_block(const void *__src, void *__dst, size_t __n) {
  104. }
  105. #endif // EEPROM_SETTINGS && (!I2C_EEPROM && !SPI_EEPROM)
  106. #endif // STM32GENERIC && STM32F4